File: pdf_ocr_controller.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (295 lines) | stat: -rw-r--r-- 10,981 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/accessibility/pdf_ocr_controller.h"

#include <vector>

#include "base/check_is_test.h"
#include "base/check_op.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/metrics_hashes.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "chrome/browser/pdf/pdf_viewer_stream_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/screen_ai/screen_ai_install_state.h"
#include "chrome/browser/screen_ai/screen_ai_service_router.h"
#include "chrome/browser/screen_ai/screen_ai_service_router_factory.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/grit/generated_resources.h"
#include "components/language/core/browser/pref_names.h"
#include "components/language/core/common/language_util.h"
#include "components/pdf/common/pdf_util.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/scoped_accessibility_mode.h"
#include "content/public/browser/web_contents.h"
#include "pdf/pdf_features.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/platform/ax_platform.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#endif

namespace {

constexpr uint32_t kMaxInitializationRetry = 3;
constexpr base::TimeDelta kRetryDelay = base::Minutes(5);

// Returns all WebContents with PDF content associated with a given Profile.
// When a PDF is opened in GuestView PDF Viewer, the following structure is
// expected:
// -----------------------------------------
// WebContents A:
//  Primary main frame
//   WebContents B (inner PDF WebContents):
//    PDF extension frame
//     PDF content frame (renderer)
// -----------------------------------------
// On the other hand, OOPIF PDF Viewer doesn't create an inner WebContents.
// When a PDF is opened in OOPIF PDF Viewer, the following structure is
// expected:
// -----------------------------------------
// WebContents A:
//  Primary main frame
//   PDF extension frame
//    PDF content frame (renderer)
// -----------------------------------------
std::vector<content::WebContents*> GetAllPdfWebContents(Profile* profile) {
  // Code borrowed from `content::WebContentsImpl::GetAllWebContents()`.
  std::vector<content::WebContents*> result;

  std::unique_ptr<content::RenderWidgetHostIterator> widgets(
      content::RenderWidgetHost::GetRenderWidgetHosts());
  // Iterate over all RWHs and their RVHs and store a WebContents if the
  // WebContents is associated with PDF Viewer Mimehandler and belongs to the
  // given Profile.
  while (content::RenderWidgetHost* rwh = widgets->GetNextHost()) {
    content::RenderViewHost* rvh = content::RenderViewHost::From(rwh);
    if (!rvh) {
      continue;
    }
    content::WebContents* web_contents =
        content::WebContents::FromRenderViewHost(rvh);
    if (!web_contents) {
      continue;
    }
    if (profile !=
        Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
      continue;
    }

    if (chrome_pdf::features::IsOopifPdfEnabled()) {
      // If `web_contents` has a `pdf::PdfViewerStreamManager`, then there must
      // be a PDF in the WebContents in OOPIF PDF Viewer.
      if (pdf::PdfViewerStreamManager::FromWebContents(web_contents)) {
        result.push_back(web_contents);
      }
    } else if (IsPdfExtensionOrigin(web_contents->GetPrimaryMainFrame()
                                        ->GetLastCommittedOrigin())) {
      // GuestView PDF Viewer case. If the WebContents has a PDF, GuestView PDF
      // Viewer has one inner PDF WebContents, and its primary main frame has
      // the PDF extension origin. It will iterate on this innter PDF
      // WebContents, so check its primary main frame.
      result.push_back(web_contents);
    }
  }
  return result;
}

// Returns true if a screen reader is present, if the screen reader AXMode is
// enabled on any PDF web contents, or (on Chrome OS only) if select-to-speak is
// enabled.
bool IsAccessibilityEnabled(Profile* profile) {
  // Active if a screen reader is present.
  if (ui::AXPlatform::GetInstance().IsScreenReaderActive()) {
    return true;
  }

#if BUILDFLAG(IS_CHROMEOS)
  // Conditionally active if select-to-speak is enabled.
  if (features::IsAccessibilityPdfOcrForSelectToSpeakEnabled() &&
      ash::AccessibilityManager::Get()->IsSelectToSpeakEnabled()) {
    return true;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Check all web contentses. `ReadAnythingUntrustedPageHandler` sets the
  // extended properties mode when starting to observe a PDF WebContents via
  // `SetUpPdfObserver()`. So if any of them have that mode enabled,
  // return true.
  for (auto* contents : GetAllPdfWebContents(profile)) {
    if (contents->GetAccessibilityMode().has_mode(
            ui::AXMode::kExtendedProperties)) {
      return true;
    }
  }

  return false;
}

void RecordAcceptLanguages(const std::string& accept_languages) {
  for (const std::string& language :
       base::SplitString(accept_languages, ",", base::TRIM_WHITESPACE,
                         base::SPLIT_WANT_NONEMPTY)) {
    // Convert to a Chrome language code synonym. This language synonym is then
    // converted into a `LocaleCodeBCP47` enum value for a UMA histogram. See
    // tools/metrics/histograms/enums.xml enum LocaleCodeBCP47. The enum there
    // doesn't always have locales where the base lang and the locale are the
    // same (e.g. they don't have id-id, but do have id). So if the base lang
    // and the locale are the same, just use the base lang.
    std::string language_to_log = language;
    std::vector<std::string> lang_split =
        base::SplitString(base::ToLowerASCII(language_to_log), "-",
                          base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
    if (lang_split.size() == 2 && lang_split[0] == lang_split[1]) {
      language_to_log = lang_split[0];
    }
    language::ToChromeLanguageSynonym(&language_to_log);
    // TODO(crbug.com/40267312): Add a browser test to validate this UMA metric.
    base::UmaHistogramSparse("Accessibility.PdfOcr.UserAcceptLanguage",
                             base::HashMetricName(language_to_log));
  }
}

}  // namespace

namespace screen_ai {

PdfOcrController::PdfOcrController(Profile* profile)
    : profile_(profile), initialization_retry_wait_(kRetryDelay) {
  DCHECK(profile_);

  // Register for changes to screenreader/spoken feedback/select to speak.
#if BUILDFLAG(IS_CHROMEOS)
  if (auto* const accessibility_manager = ash::AccessibilityManager::Get();
      accessibility_manager) {
    // Unretained is safe because `this` owns the subscription.
    accessibility_status_subscription_ =
        accessibility_manager->RegisterCallback(
            base::BindRepeating(&PdfOcrController::OnAccessibilityStatusEvent,
                                base::Unretained(this)));
  }
#else   // BUILDFLAG(IS_CHROMEOS)
  ax_mode_observation_.Observe(&ui::AXPlatform::GetInstance());
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Trigger if a screen reader or Select-to-Speak on ChromeOS is enabled.
  OnActivationChanged();
}

PdfOcrController::~PdfOcrController() = default;

// static
std::vector<content::WebContents*>
PdfOcrController::GetAllPdfWebContentsForTesting(Profile* profile) {
  return GetAllPdfWebContents(profile);
}

bool PdfOcrController::IsEnabled() const {
  return scoped_accessibility_mode_ != nullptr;
}

#if BUILDFLAG(IS_CHROMEOS)
void PdfOcrController::OnAccessibilityStatusEvent(
    const ash::AccessibilityStatusEventDetails& details) {
  if (details.notification_type ==
          ash::AccessibilityNotificationType::kToggleSpokenFeedback ||
      details.notification_type ==
          ash::AccessibilityNotificationType::kToggleSelectToSpeak) {
    OnActivationChanged();
  }
}
#endif  // BUIDLFLAG(IS_CHROMEOS)

void PdfOcrController::OnActivationChanged() {
  // PDF Searchify feature performs OCR on all inaccessible PDFs regardless of
  // accessibility settings. Therefore if it is enabled, we don't need to enable
  // OCR in PDF viewer.
  // TODO(crbug.com/360803943): Remove this class when PDF Searchify is
  // launched.
  bool enable =
      (!base::FeatureList::IsEnabled(chrome_pdf::features::kPdfSearchify) &&
       IsAccessibilityEnabled(profile_));

  if (enable == IsEnabled()) {
    return;  // No change in activation.
  }

  if (enable) {
    RecordAcceptLanguages(
        profile_->GetPrefs()->GetString(language::prefs::kAcceptLanguages));

    if (!ocr_service_ready_) {
      InitializeService();
      return;
    }

    // This will send the `kPDFOcr` flag to all WebContents. Strictly speaking,
    // it need only be sent to those associated with PDF Viewer Mimehandlers,
    // but we have no filtering mechanism today. The others should simply ignore
    // it.
    scoped_accessibility_mode_ =
        content::BrowserAccessibilityState::GetInstance()
            ->CreateScopedModeForBrowserContext(profile_, ui::AXMode::kPDFOcr);
  } else {
    scoped_accessibility_mode_.reset();
  }
}

void PdfOcrController::InitializeService() {
  // Avoid repeated requests.
  if (waiting_for_ocr_service_initialization_) {
    return;
  }
  waiting_for_ocr_service_initialization_ = true;

  screen_ai::ScreenAIServiceRouterFactory::GetForBrowserContext(profile_)
      ->GetServiceStateAsync(
          ScreenAIServiceRouter::Service::kOCR,
          base::BindOnce(&PdfOcrController::OCRServiceInitializationCallback,
                         weak_ptr_factory_.GetWeakPtr()));
}

void PdfOcrController::OCRServiceInitializationCallback(bool successful) {
  waiting_for_ocr_service_initialization_ = false;
  ocr_service_ready_ = successful;
  if (successful) {
    OnActivationChanged();
    base::UmaHistogramCounts100(
        "Accessibility.ScreenAI.Component.InstallRetries",
        initialization_retries_);
  } else {
    // Schedule a retry.
    initialization_retries_++;
    if (initialization_retries_ < kMaxInitializationRetry) {
      base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
          FROM_HERE,
          base::BindOnce(&PdfOcrController::InitializeService,
                         weak_ptr_factory_.GetWeakPtr()),
          initialization_retry_wait_);
    }
  }
}

void PdfOcrController::Activate() {
  OnActivationChanged();
}

#if !BUILDFLAG(IS_CHROMEOS)
void PdfOcrController::OnAXModeAdded(ui::AXMode mode) {
  OnActivationChanged();
}

void PdfOcrController::OnAssistiveTechChanged(
    ui::AssistiveTech assistive_tech) {
  OnActivationChanged();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace screen_ai