pdf_view_plugin_base.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PDF_PDF_VIEW_PLUGIN_BASE_H_
  5. #define PDF_PDF_VIEW_PLUGIN_BASE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/i18n/rtl.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/strings/string_piece.h"
  15. #include "base/values.h"
  16. #include "pdf/accessibility_structs.h"
  17. #include "pdf/paint_manager.h"
  18. #include "pdf/pdf_engine.h"
  19. #include "pdf/pdfium/pdfium_form_filler.h"
  20. #include "third_party/abseil-cpp/absl/types/optional.h"
  21. #include "third_party/blink/public/web/web_print_params.h"
  22. #include "third_party/skia/include/core/SkBitmap.h"
  23. #include "ui/gfx/geometry/rect.h"
  24. namespace gfx {
  25. class PointF;
  26. class Vector2d;
  27. class Vector2dF;
  28. } // namespace gfx
  29. namespace chrome_pdf {
  30. class PDFiumEngine;
  31. class PaintReadyRect;
  32. struct AccessibilityCharInfo;
  33. struct AccessibilityDocInfo;
  34. struct AccessibilityPageInfo;
  35. struct AccessibilityPageObjects;
  36. struct AccessibilityTextRunInfo;
  37. struct AccessibilityViewportInfo;
  38. // TODO(crbug.com/1302059): Merge with PdfViewWebPlugin.
  39. class PdfViewPluginBase : public PDFEngine::Client,
  40. public PaintManager::Client {
  41. public:
  42. // Do not save files with over 100 MB. This cap should be kept in sync with
  43. // and is also enforced in chrome/browser/resources/pdf/pdf_viewer.js.
  44. static constexpr size_t kMaximumSavedFileSize = 100 * 1000 * 1000;
  45. enum class AccessibilityState {
  46. kOff = 0, // Off.
  47. kPending, // Enabled but waiting for doc to load.
  48. kLoaded, // Fully loaded.
  49. };
  50. enum class DocumentLoadState {
  51. kLoading = 0,
  52. kComplete,
  53. kFailed,
  54. };
  55. PdfViewPluginBase(const PdfViewPluginBase& other) = delete;
  56. PdfViewPluginBase& operator=(const PdfViewPluginBase& other) = delete;
  57. // PDFEngine::Client:
  58. void Invalidate(const gfx::Rect& rect) override;
  59. void DidScroll(const gfx::Vector2d& offset) override;
  60. void ScrollToX(int x_screen_coords) override;
  61. void ScrollToY(int y_screen_coords) override;
  62. void ScrollBy(const gfx::Vector2d& delta) override;
  63. void ScrollToPage(int page) override;
  64. void NavigateTo(const std::string& url,
  65. WindowOpenDisposition disposition) override;
  66. void NavigateToDestination(int page,
  67. const float* x,
  68. const float* y,
  69. const float* zoom) override;
  70. void NotifyTouchSelectionOccurred() override;
  71. void Email(const std::string& to,
  72. const std::string& cc,
  73. const std::string& bcc,
  74. const std::string& subject,
  75. const std::string& body) override;
  76. void DocumentLoadComplete() override;
  77. void DocumentLoadFailed() override;
  78. void DocumentLoadProgress(uint32_t available, uint32_t doc_size) override;
  79. void FormFieldFocusChange(PDFEngine::FocusFieldType type) override;
  80. void SetIsSelecting(bool is_selecting) override;
  81. void SelectionChanged(const gfx::Rect& left, const gfx::Rect& right) override;
  82. void DocumentFocusChanged(bool document_has_focus) override;
  83. // PaintManager::Client:
  84. void OnPaint(const std::vector<gfx::Rect>& paint_rects,
  85. std::vector<PaintReadyRect>& ready,
  86. std::vector<gfx::Rect>& pending) override;
  87. // Gets the content restrictions based on the permissions which `engine_` has.
  88. int GetContentRestrictions() const;
  89. // Gets the accessibility doc info based on the information from `engine_`.
  90. AccessibilityDocInfo GetAccessibilityDocInfo() const;
  91. DocumentLoadState document_load_state_for_testing() const {
  92. return document_load_state_;
  93. }
  94. protected:
  95. struct BackgroundPart {
  96. gfx::Rect location;
  97. uint32_t color;
  98. };
  99. PdfViewPluginBase();
  100. ~PdfViewPluginBase() override;
  101. // Creates a new `PDFiumEngine`.
  102. virtual std::unique_ptr<PDFiumEngine> CreateEngine(
  103. PDFEngine::Client* client,
  104. PDFiumFormFiller::ScriptOption script_option) = 0;
  105. virtual const PDFiumEngine* engine() const = 0;
  106. virtual PDFiumEngine* engine() = 0;
  107. // Gets a weak pointer with a lifetime matching the derived class.
  108. virtual base::WeakPtr<PdfViewPluginBase> GetWeakPtr() = 0;
  109. // Runs when document load completes in Print Preview, before
  110. // `OnDocumentLoadComplete()`.
  111. virtual void OnPrintPreviewLoaded() = 0;
  112. // Runs when document load completes.
  113. virtual void OnDocumentLoadComplete() = 0;
  114. // Enqueues a "message" event carrying `message` to the embedder. Messages are
  115. // guaranteed to be received in the order that they are sent. This method is
  116. // non-blocking.
  117. virtual void SendMessage(base::Value::Dict message) = 0;
  118. // Sends the loading progress, where `percentage` represents the progress, or
  119. // -1 for loading error.
  120. void SendLoadingProgress(double percentage);
  121. // Schedules invalidation tasks after painting finishes.
  122. void InvalidateAfterPaintDone();
  123. // Updates the available area and the background parts, notifies the PDF
  124. // engine, and updates the accessibility information.
  125. void OnGeometryChanged(double old_zoom, float old_device_scale);
  126. // A helper of OnGeometryChanged() which updates the available area and
  127. // the background parts, and notifies the PDF engine of geometry changes.
  128. void RecalculateAreas(double old_zoom, float old_device_scale);
  129. // Figures out the location of any background rectangles (i.e. those that
  130. // aren't painted by the PDF engine).
  131. void CalculateBackgroundParts();
  132. // Computes document width/height in device pixels, based on current zoom and
  133. // device scale
  134. int GetDocumentPixelWidth() const;
  135. int GetDocumentPixelHeight() const;
  136. // Sets the text input type for this plugin based on `in_focus`.
  137. virtual void SetFormTextFieldInFocus(bool in_focus) = 0;
  138. // Sets the accessibility information about the PDF document in the renderer.
  139. virtual void SetAccessibilityDocInfo(AccessibilityDocInfo doc_info) = 0;
  140. // Sets the accessibility information about the given `page_index` in the
  141. // renderer.
  142. void PrepareAndSetAccessibilityPageInfo(int32_t page_index);
  143. // Sets the accessibility information about the page in the renderer.
  144. virtual void SetAccessibilityPageInfo(
  145. AccessibilityPageInfo page_info,
  146. std::vector<AccessibilityTextRunInfo> text_runs,
  147. std::vector<AccessibilityCharInfo> chars,
  148. AccessibilityPageObjects page_objects) = 0;
  149. // Prepares the accessibility information about the current viewport. Calls
  150. // SetAccessibilityViewportInfo() internally to set this information in the
  151. // renderer. This is done once when accessibility is first loaded and again
  152. // when the geometry changes.
  153. void PrepareAndSetAccessibilityViewportInfo();
  154. // Sets the accessibility information about the current viewport in the
  155. // renderer.
  156. virtual void SetAccessibilityViewportInfo(
  157. AccessibilityViewportInfo viewport_info) = 0;
  158. // Prints the pages specified by `page_numbers` using the parameters passed to
  159. // `PrintBegin()` Returns a vector of bytes containing the printed output. An
  160. // empty returned value indicates failure.
  161. std::vector<uint8_t> PrintPages(const std::vector<int>& page_numbers);
  162. // Disables browser commands because of restrictions on how the data is to be
  163. // used (i.e. can't copy/print). `content_restrictions` should have its bits
  164. // set by `chrome_pdf::ContentRestriction` enum values.
  165. virtual void SetContentRestrictions(int content_restrictions) = 0;
  166. // Sends start/stop loading notifications to the plugin's render frame.
  167. virtual void DidStartLoading() = 0;
  168. virtual void DidStopLoading() = 0;
  169. // Notifies the embedder of the top-left and bottom-right coordinates of the
  170. // current selection.
  171. virtual void NotifySelectionChanged(const gfx::PointF& left,
  172. int left_height,
  173. const gfx::PointF& right,
  174. int right_height) = 0;
  175. // Records user actions.
  176. virtual void UserMetricsRecordAction(const std::string& action) = 0;
  177. PaintManager& paint_manager() { return paint_manager_; }
  178. SkBitmap& image_data() { return image_data_; }
  179. virtual bool full_frame() const = 0;
  180. const gfx::Rect& available_area() const { return available_area_; }
  181. const gfx::Size& document_size() const { return document_size_; }
  182. void set_document_size(const gfx::Size& size) { document_size_ = size; }
  183. virtual const gfx::Size& plugin_dip_size() const = 0;
  184. // TODO(crbug.com/1288847): Don't provide direct access to the origin of
  185. // `plugin_rect_`, as this exposes the unintuitive "paint offset."
  186. virtual const gfx::Rect& plugin_rect() const = 0;
  187. // Sets the new zoom scale.
  188. void SetZoom(double scale);
  189. double zoom() const { return zoom_; }
  190. virtual float device_scale() const = 0;
  191. virtual bool needs_reraster() const = 0;
  192. virtual base::i18n::TextDirection ui_direction() const = 0;
  193. virtual bool received_viewport_message() const = 0;
  194. double last_progress_sent() const { return last_progress_sent_; }
  195. void set_last_progress_sent(double progress) {
  196. last_progress_sent_ = progress;
  197. }
  198. DocumentLoadState document_load_state() const { return document_load_state_; }
  199. void set_document_load_state(DocumentLoadState state) {
  200. document_load_state_ = state;
  201. }
  202. AccessibilityState accessibility_state() const {
  203. return accessibility_state_;
  204. }
  205. void set_accessibility_state(AccessibilityState state) {
  206. accessibility_state_ = state;
  207. }
  208. static constexpr bool IsSaveDataSizeValid(size_t size) {
  209. return size > 0 && size <= kMaximumSavedFileSize;
  210. }
  211. // Converts a scroll offset (which is relative to a UI direction-dependent
  212. // scroll origin) to a scroll position (which is always relative to the
  213. // top-left corner).
  214. gfx::PointF GetScrollPositionFromOffset(
  215. const gfx::Vector2dF& scroll_offset) const;
  216. // Paints the given invalid area of the plugin to the given graphics device.
  217. // PaintManager::Client::OnPaint() should be its only caller.
  218. void DoPaint(const std::vector<gfx::Rect>& paint_rects,
  219. std::vector<PaintReadyRect>& ready,
  220. std::vector<gfx::Rect>& pending);
  221. // The preparation when painting on the image data buffer for the first
  222. // time.
  223. virtual void PrepareForFirstPaint(std::vector<PaintReadyRect>& ready) = 0;
  224. // Callback to clear deferred invalidates after painting finishes.
  225. void ClearDeferredInvalidates();
  226. // Starts loading accessibility information.
  227. void LoadAccessibility();
  228. // Converts `frame_coordinates` to PDF coordinates.
  229. gfx::Point FrameToPdfCoordinates(const gfx::PointF& frame_coordinates) const;
  230. private:
  231. // TODO(crbug.com/1302059): `PdfViewPluginBase` is being merged into
  232. // `PdfViewWebPlugin`, so all methods should be protected or public.
  233. PaintManager paint_manager_{this};
  234. // Image data buffer for painting.
  235. SkBitmap image_data_;
  236. std::vector<BackgroundPart> background_parts_;
  237. // Deferred invalidates while `in_paint_` is true.
  238. std::vector<gfx::Rect> deferred_invalidates_;
  239. // Remaining area, in pixels, to render the pdf in after accounting for
  240. // horizontal centering.
  241. gfx::Rect available_area_;
  242. // The size of the entire document in pixels (i.e. if each page is 800 pixels
  243. // high and there are 10 pages, the height will be 8000).
  244. gfx::Size document_size_;
  245. // Current zoom factor.
  246. double zoom_ = 1.0;
  247. // Whether OnPaint() is in progress or not.
  248. bool in_paint_ = false;
  249. // The last document load progress value sent to the web page.
  250. double last_progress_sent_ = 0.0;
  251. // The current state of document load.
  252. DocumentLoadState document_load_state_ = DocumentLoadState::kLoading;
  253. // The current state of accessibility.
  254. AccessibilityState accessibility_state_ = AccessibilityState::kOff;
  255. // The next accessibility page index, used to track interprocess calls when
  256. // reconstructing the tree for new document layouts.
  257. int32_t next_accessibility_page_index_ = 0;
  258. };
  259. } // namespace chrome_pdf
  260. #endif // PDF_PDF_VIEW_PLUGIN_BASE_H_