File: pdf_document_helper.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (164 lines) | stat: -rw-r--r-- 6,683 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PDF_BROWSER_PDF_DOCUMENT_HELPER_H_
#define COMPONENTS_PDF_BROWSER_PDF_DOCUMENT_HELPER_H_

#include <memory>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "content/public/browser/document_user_data.h"
#include "content/public/browser/render_frame_host_receiver_set.h"
#include "content/public/browser/render_widget_host_observer.h"
#include "content/public/browser/touch_selection_controller_client_manager.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "pdf/mojom/pdf.mojom.h"
#include "services/screen_ai/buildflags/buildflags.h"
#include "ui/touch_selection/selection_event_type.h"
#include "ui/touch_selection/touch_selection_controller.h"
#include "ui/touch_selection/touch_selection_menu_runner.h"

namespace content {
class RenderWidgetHost;
class WebContents;
}  // namespace content

namespace pdf {

class PDFDocumentHelperClient;

// Per-Document class to handle PDF messages.
class PDFDocumentHelper
    : public content::DocumentUserData<PDFDocumentHelper>,
      public content::RenderWidgetHostObserver,
      public mojom::PdfHost,
      public ui::TouchSelectionControllerClient,
      public ui::TouchSelectionMenuClient,
      public content::TouchSelectionControllerClientManager::Observer {
 public:
  PDFDocumentHelper(const PDFDocumentHelper&) = delete;
  PDFDocumentHelper& operator=(const PDFDocumentHelper&) = delete;

  ~PDFDocumentHelper() override;

  static void BindPdfHost(
      mojo::PendingAssociatedReceiver<mojom::PdfHost> pdf_host,
      content::RenderFrameHost* rfh,
      std::unique_ptr<PDFDocumentHelperClient> client);

  static PDFDocumentHelper* MaybeGetForWebContents(
      content::WebContents* contents);

  // content::RenderWidgetHostObserver:
  void RenderWidgetHostDestroyed(
      content::RenderWidgetHost* widget_host) override;

  // ui::TouchSelectionControllerClient:
  bool SupportsAnimation() const override;
  void SetNeedsAnimate() override {}
  void MoveCaret(const gfx::PointF& position) override;
  void MoveRangeSelectionExtent(const gfx::PointF& extent) override;
  void SelectBetweenCoordinates(const gfx::PointF& base,
                                const gfx::PointF& extent) override;
  void OnSelectionEvent(ui::SelectionEventType event) override;
  void OnDragUpdate(const ui::TouchSelectionDraggable::Type type,
                    const gfx::PointF& position) override;
  std::unique_ptr<ui::TouchHandleDrawable> CreateDrawable() override;
  void DidScroll() override;

  // ui::TouchSelectionMenuClient:
  bool IsCommandIdEnabled(int command_id) const override;
  void ExecuteCommand(int command_id, int event_flags) override;
  void RunContextMenu() override;
  bool ShouldShowQuickMenu() override;
  std::u16string GetSelectedText() override;

  // content::TouchSelectionControllerClientManager::Observer:
  void OnManagerWillDestroy(
      content::TouchSelectionControllerClientManager* manager) override;

  // pdf::mojom::PdfHost:
  void SetListener(mojo::PendingRemote<mojom::PdfListener> listener) override;
  void OnDocumentLoadComplete() override;
  void SaveUrlAs(const GURL& url,
                 network::mojom::ReferrerPolicy policy) override;
  void UpdateContentRestrictions(int32_t content_restrictions) override;
  void SelectionChanged(const gfx::PointF& left,
                        int32_t left_height,
                        const gfx::PointF& right,
                        int32_t right_height) override;
  void SetPluginCanSave(bool can_save) override;
#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
  void OnSearchifyStarted() override;
#endif

  // Returns whether document is loaded, at which point, the other calls to
  // document metadata such as `GetPdfBytes`, `GetPageText` can return data.
  bool IsDocumentLoadComplete() const { return is_document_load_complete_; }
  // Get PDF bytes, if they do not exceed the size limit. If called before
  // document is loaded, the callback will be invoked with an empty vector.
  void GetPdfBytes(uint32_t size_limit,
                   pdf::mojom::PdfListener::GetPdfBytesCallback callback);
  // Returns text of the given page. If called before document is loaded, the
  // callback will be invoked with an empty string.
  void GetPageText(int32_t page_index,
                   pdf::mojom::PdfListener::GetPageTextCallback callback);
  void GetMostVisiblePageIndex(
      pdf::mojom::PdfListener::GetMostVisiblePageIndexCallback callback);

  // Registers `callback` to be run when document load completes successfully.
  // When the PDF is already loaded, `callback` is invoked immediately. Will not
  // be invoked when the load fails. This is useful to wait for document
  // metadata to be loaded, before calls to `GetPdfBytes`, and `GetPageText`
  // should be made.
  void RegisterForDocumentLoadComplete(base::OnceClosure callback);

 private:
  friend class content::DocumentUserData<PDFDocumentHelper>;

  PDFDocumentHelper(content::RenderFrameHost* rfh,
                    std::unique_ptr<PDFDocumentHelperClient> client);

  content::WebContents& GetWebContents();
  void InitTouchSelectionClientManager();
  gfx::PointF ConvertFromRoot(const gfx::PointF& point_f);
  gfx::PointF ConvertToRoot(const gfx::PointF& point_f);
  gfx::PointF ConvertHelper(const gfx::PointF& point_f, float scale);

  content::RenderFrameHostReceiverSet<mojom::PdfHost> pdf_host_receivers_;
  std::unique_ptr<PDFDocumentHelperClient> const client_;
  raw_ptr<content::TouchSelectionControllerClientManager>
      touch_selection_controller_client_manager_ = nullptr;

  // The `RenderWidgetHost` associated to the frame containing the PDF plugin.
  // This should be null until the plugin is known to have been created; the
  // signal comes from `SetListener()`.
  raw_ptr<content::RenderWidgetHost> pdf_rwh_ = nullptr;

  // Latest selection bounds received from PDFium.
  gfx::PointF selection_left_;
  int32_t selection_left_height_ = 0;
  gfx::PointF selection_right_;
  int32_t selection_right_height_ = 0;
  bool has_selection_ = false;

  bool is_document_load_complete_ = false;

  // Callbacks to invoke when document load is completed.
  std::vector<base::OnceClosure> document_load_complete_callbacks_;

#if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE)
  bool searchify_started_ = false;
#endif
  mojo::Remote<mojom::PdfListener> remote_pdf_client_;

  DOCUMENT_USER_DATA_KEY_DECL();
};

}  // namespace pdf

#endif  // COMPONENTS_PDF_BROWSER_PDF_DOCUMENT_HELPER_H_