File: pdf_ocr_controller.h

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 (126 lines) | stat: -rw-r--r-- 3,774 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
// 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.

#ifndef CHROME_BROWSER_ACCESSIBILITY_PDF_OCR_CONTROLLER_H_
#define CHROME_BROWSER_ACCESSIBILITY_PDF_OCR_CONTROLLER_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "base/callback_list.h"
#else
#include "base/scoped_observation.h"
#include "ui/accessibility/platform/ax_mode_observer.h"
#include "ui/accessibility/platform/ax_platform.h"
#endif

class Profile;

#if BUILDFLAG(IS_CHROMEOS)
namespace ash {
struct AccessibilityStatusEventDetails;
}
#endif

namespace content {
class ScopedAccessibilityMode;
class WebContents;
}

namespace screen_ai {

class PdfOcrControllerFactory;

// Manages the PDF OCR feature that extracts text from an inaccessible PDF.
// Observes changes in assitive technologies and updates the accessibility
// mode of WebContents when the feature is needed.
class PdfOcrController : public KeyedService
#if !BUILDFLAG(IS_CHROMEOS)
    ,
                         public ui::AXModeObserver
#endif
{
 public:
  explicit PdfOcrController(Profile* profile);
  PdfOcrController(const PdfOcrController&) = delete;
  PdfOcrController& operator=(const PdfOcrController&) = delete;
  ~PdfOcrController() override;

  // Return all PDF-related WebContentses associated with a given Profile.
  static std::vector<content::WebContents*> GetAllPdfWebContentsForTesting(
      Profile* profile);

  // Return true if PDF OCR is enabled for the profile.F
  bool IsEnabled() const;

  void set_ocr_ready_for_testing() { ocr_service_ready_ = true; }

  void set_initialization_retry_wait_for_testing(const base::TimeDelta& wait) {
    initialization_retry_wait_ = wait;
  }

  void Activate();

#if !BUILDFLAG(IS_CHROMEOS)
  // ui::AXModeObserver:
  void OnAXModeAdded(ui::AXMode mode) override;
  void OnAssistiveTechChanged(ui::AssistiveTech assistive_tech) override;
#endif

 private:
  friend class PdfOcrControllerFactory;

#if BUILDFLAG(IS_CHROMEOS)
  void OnAccessibilityStatusEvent(
      const ash::AccessibilityStatusEventDetails& details);
#endif

  // Receives the result of OCR service initialization.
  void OCRServiceInitializationCallback(bool successful);

  // Handles a change to the activation state.
  void OnActivationChanged();

  // Sends an initialization request to ScreenAIServiceRouter if one is not
  // pending.
  void InitializeService();

  // PdfOcrController will be created via PdfOcrControllerFactory on this
  // profile and then destroyed before the profile gets destroyed.
  raw_ptr<Profile> profile_;

#if BUILDFLAG(IS_CHROMEOS)
  // Observes spoken feedback and select to speak.
  base::CallbackListSubscription accessibility_status_subscription_;
#else
  // Observes the presence of a screen reader.
  base::ScopedObservation<ui::AXPlatform, ui::AXModeObserver>
      ax_mode_observation_{this};
#endif

  // Enables the kPDFOcr accessibility mode flag for all tabs associated
  // with the controller's profile.
  std::unique_ptr<content::ScopedAccessibilityMode> scoped_accessibility_mode_;

  // True when OCR service is initialized and ready to use.
  bool ocr_service_ready_ = false;

  // OCR initialization has started, but is not finished yet.
  bool waiting_for_ocr_service_initialization_ = false;

  // Number of times initialization is retried.
  uint32_t initialization_retries_ = 0;

  base::TimeDelta initialization_retry_wait_;

  base::WeakPtrFactory<PdfOcrController> weak_ptr_factory_{this};
};

}  // namespace screen_ai

#endif  // CHROME_BROWSER_ACCESSIBILITY_PDF_OCR_CONTROLLER_H_