From 3a745b822c0c34b0ae934b3b0722b5a68be1fbea Mon Sep 17 00:00:00 2001
From: Lei Zhang <thestig@chromium.org>
Date: Fri, 28 Mar 2025 11:25:19 -0700
Subject: [PATCH] [PDF Searchify] Do not Searchify Print Preview

The PDF displayed in Print Preview should reflect what is being sent to
the printer and should not be modified by Searchify. The minimal PDF
Viewer that displays the preview is also not capable of displaying
Searchify's UI. Thus it raises JS errors when Searchify tries to ask it
to.

Add a PDFiumOnDemandSearchifierTest case to show this CL stops Searchify
in Print Preview. To do so, enhance SearchifierTestClient to be able to
tell PDFiumEngine that it is in Print Preview mode.

Along the way, encapsulate SearchifierTestClient.

Bug: 405433817, 406530484
Change-Id: I535ece9beb7429a4844c3310add52bc5fd6f1952
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6405712
Reviewed-by: Ramin Halavati <rhalavati@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1439561}
---
 pdf/pdfium/pdfium_engine.cc                   |  2 +-
 .../pdfium_on_demand_searchifier_unittest.cc  | 30 +++++++++++++++++--
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/pdf/pdfium/pdfium_engine.cc b/pdf/pdfium/pdfium_engine.cc
index 59064e8b9d4c9..3de3000a9f375 100644
--- a/pdf/pdfium/pdfium_engine.cc
+++ b/pdf/pdfium/pdfium_engine.cc
@@ -4410,7 +4410,7 @@ void PDFiumEngine::ScheduleSearchifyIfNeeded(PDFiumPage* page) {
   CHECK(page);
   CHECK(page->available());
 
-  if (page->IsPageSearchified()) {
+  if (client_->IsPrintPreview() || page->IsPageSearchified()) {
     return;
   }
 
diff --git a/pdf/pdfium/pdfium_on_demand_searchifier_unittest.cc b/pdf/pdfium/pdfium_on_demand_searchifier_unittest.cc
index f03a85fc3f4de..c58a596f6d275 100644
--- a/pdf/pdfium/pdfium_on_demand_searchifier_unittest.cc
+++ b/pdf/pdfium/pdfium_on_demand_searchifier_unittest.cc
@@ -41,6 +41,8 @@ class SearchifierTestClient : public TestClient {
   SearchifierTestClient& operator=(const SearchifierTestClient&) = delete;
   ~SearchifierTestClient() override = default;
 
+  bool IsPrintPreview() const override { return is_print_preview_; }
+
   void OnSearchifyStateChange(bool busy) override {
     if (busy) {
       busy_state_changed_count_++;
@@ -49,6 +51,13 @@ class SearchifierTestClient : public TestClient {
     }
   }
 
+  void set_for_print_preview() { is_print_preview_ = true; }
+
+  int busy_state_changed_count() const { return busy_state_changed_count_; }
+  int idle_state_changed_count() const { return idle_state_changed_count_; }
+
+ private:
+  bool is_print_preview_ = false;
   int busy_state_changed_count_ = 0;
   int idle_state_changed_count_ = 0;
 };
@@ -116,6 +125,11 @@ class PDFiumOnDemandSearchifierTest : public PDFiumTestBase {
     ASSERT_TRUE(engine_) << test_filename;
   }
 
+  void CreatePreviewEngine(const base::FilePath::CharType* test_filename) {
+    client_.set_for_print_preview();
+    CreateEngine(test_filename);
+  }
+
   void TearDown() override {
     // PDFium gets uninitialized via `FPDF_DestroyLibrary`. If `engine_` is not
     // destroyed here, its destruction results in a crash later.
@@ -152,10 +166,10 @@ class PDFiumOnDemandSearchifierTest : public PDFiumTestBase {
   int performed_ocrs() const { return performed_ocrs_; }
   PDFiumEngine* engine() { return engine_.get(); }
   int busy_state_changed_count() const {
-    return client_.busy_state_changed_count_;
+    return client_.busy_state_changed_count();
   }
   int idle_state_changed_count() const {
-    return client_.idle_state_changed_count_;
+    return client_.idle_state_changed_count();
   }
 
  private:
@@ -400,6 +414,18 @@ TEST_P(PDFiumOnDemandSearchifierTest, MultiplePagesWithUnload) {
   EXPECT_TRUE(page3_info.value().is_searchified);
 }
 
+TEST_P(PDFiumOnDemandSearchifierTest, OnePageWithImagesInPrintPreview) {
+  CreatePreviewEngine(FILE_PATH_LITERAL("image_alt_text.pdf"));
+
+  PDFiumPage& page = GetPDFiumPageForTest(*engine(), 0);
+
+  // Load the page to trigger Searchify, but it should not do anything for Print
+  // Preview.
+  page.GetPage();
+  ASSERT_FALSE(engine()->PageNeedsSearchify(0));
+  ASSERT_FALSE(engine()->GetSearchifierForTesting());
+}
+
 TEST_P(PDFiumOnDemandSearchifierTest, OcrCancellation) {
   constexpr int kPageCount = 4;
   CreateEngine(FILE_PATH_LITERAL("multi_page_no_text.pdf"));