Send "PDF Searchify started" message only once to the browser process.
PdfSearchify feature promo is triggered every time PDF Searchify starts,
but it is shown only once.
To reduce the unnecessary trips through different layers, send the
message to the browser process only once.
Bug: 360803943
Change-Id: Ic34a9b01c85cbb802092a78d06f1aa5099230da0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6261061
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Mike West <mkwst@chromium.org>
Auto-Submit: Ramin Halavati <rhalavati@chromium.org>
Reviewed-by: Mike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1421248}
diff --git a/components/pdf/browser/pdf_document_helper.cc b/components/pdf/browser/pdf_document_helper.cc
index 9327356..17ba2fb1 100644
--- a/components/pdf/browser/pdf_document_helper.cc
+++ b/components/pdf/browser/pdf_document_helper.cc
@@ -154,7 +154,10 @@
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
void PDFDocumentHelper::OnSearchifyStarted() {
- client_->OnSearchifyStarted(&GetWebContents());
+ if (!searchify_started_) {
+ searchify_started_ = true;
+ client_->OnSearchifyStarted(&GetWebContents());
+ }
}
#endif
diff --git a/components/pdf/browser/pdf_document_helper.h b/components/pdf/browser/pdf_document_helper.h
index 2604554..f28650ee 100644
--- a/components/pdf/browser/pdf_document_helper.h
+++ b/components/pdf/browser/pdf_document_helper.h
@@ -150,6 +150,9 @@
bool is_document_load_complete_ = false;
+#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
+ bool searchify_started_ = false;
+#endif
mojo::Remote<mojom::PdfListener> remote_pdf_client_;
base::ObserverList<Observer> observers_;
diff --git a/pdf/mojom/pdf.mojom b/pdf/mojom/pdf.mojom
index ce2f20b..13024a8 100644
--- a/pdf/mojom/pdf.mojom
+++ b/pdf/mojom/pdf.mojom
@@ -68,7 +68,8 @@
// Notifies the embedder know the plugin can handle save commands internally.
SetPluginCanSave(bool can_save);
- // Notifies that PDF searchifier has started processing pages.
+ // Notifies that PDF searchifier has started processing pages. This should be
+ // called at most once.
[EnableIf=enable_screen_ai_service]
OnSearchifyStarted();
};
diff --git a/pdf/pdf_view_web_plugin.cc b/pdf/pdf_view_web_plugin.cc
index 9531734..4894455 100644
--- a/pdf/pdf_view_web_plugin.cc
+++ b/pdf/pdf_view_web_plugin.cc
@@ -1459,7 +1459,10 @@
return;
}
- pdf_host_->OnSearchifyStarted();
+ if (!searchify_started_) {
+ searchify_started_ = true;
+ pdf_host_->OnSearchifyStarted();
+ }
if (!show_searchify_in_progress_) {
// The UI is asked to show the progress indicator with 1s delay, so that if
diff --git a/pdf/pdf_view_web_plugin.h b/pdf/pdf_view_web_plugin.h
index df33c47..886d8d0 100644
--- a/pdf/pdf_view_web_plugin.h
+++ b/pdf/pdf_view_web_plugin.h
@@ -935,6 +935,9 @@
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
bool show_searchify_in_progress_ = false;
+
+ // Tells if searchify ever started.
+ bool searchify_started_ = false;
#endif
base::WeakPtrFactory<PdfViewWebPlugin> weak_factory_{this};
diff --git a/pdf/pdf_view_web_plugin_unittest.cc b/pdf/pdf_view_web_plugin_unittest.cc
index d731213..5f79f86b 100644
--- a/pdf/pdf_view_web_plugin_unittest.cc
+++ b/pdf/pdf_view_web_plugin_unittest.cc
@@ -1805,6 +1805,8 @@
plugin_->OnSearchifyStateChange(true);
+ EXPECT_CALL(pdf_host_, OnSearchifyStarted);
+
// Wait for the state to be propagated.
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
@@ -1856,6 +1858,16 @@
plugin_->OnSearchifyStateChange(false);
}
+TEST_F(PdfViewWebPluginTest, OnSearchifyStartedMoreThanOnce) {
+ plugin_->OnSearchifyStateChange(true);
+ plugin_->OnSearchifyStateChange(false);
+ plugin_->OnSearchifyStateChange(true);
+
+ EXPECT_CALL(pdf_host_, OnSearchifyStarted);
+
+ pdf_receiver_.FlushForTesting();
+}
+
TEST_F(PdfViewWebPluginTest, OnHasSearchifyText) {
base::Value::Dict message;
message.Set("type", "setHasSearchifyText");