pdf_view_web_plugin.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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_WEB_PLUGIN_H_
  5. #define PDF_PDF_VIEW_WEB_PLUGIN_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/containers/flat_set.h"
  12. #include "base/containers/queue.h"
  13. #include "base/i18n/rtl.h"
  14. #include "base/memory/raw_ptr.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/strings/string_piece_forward.h"
  17. #include "base/values.h"
  18. #include "cc/paint/paint_image.h"
  19. #include "mojo/public/cpp/bindings/associated_remote.h"
  20. #include "mojo/public/cpp/bindings/receiver.h"
  21. #include "pdf/loader/url_loader.h"
  22. #include "pdf/mojom/pdf.mojom.h"
  23. #include "pdf/pdf_accessibility_action_handler.h"
  24. #include "pdf/pdf_engine.h"
  25. #include "pdf/pdf_view_plugin_base.h"
  26. #include "pdf/pdfium/pdfium_form_filler.h"
  27. #include "pdf/post_message_receiver.h"
  28. #include "pdf/preview_mode_client.h"
  29. #include "pdf/v8_value_converter.h"
  30. #include "third_party/blink/public/platform/web_string.h"
  31. #include "third_party/blink/public/platform/web_text_input_type.h"
  32. #include "third_party/blink/public/web/web_plugin.h"
  33. #include "third_party/blink/public/web/web_plugin_container.h"
  34. #include "third_party/blink/public/web/web_plugin_params.h"
  35. #include "third_party/blink/public/web/web_print_params.h"
  36. #include "third_party/skia/include/core/SkColor.h"
  37. #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
  38. #include "ui/gfx/geometry/rect.h"
  39. #include "ui/gfx/geometry/size.h"
  40. #include "ui/gfx/geometry/vector2d_f.h"
  41. #include "v8/include/v8.h"
  42. namespace blink {
  43. class WebAssociatedURLLoader;
  44. class WebInputEvent;
  45. class WebURL;
  46. class WebURLRequest;
  47. struct WebAssociatedURLLoaderOptions;
  48. struct WebPrintPresetOptions;
  49. } // namespace blink
  50. namespace gfx {
  51. class PointF;
  52. class Range;
  53. } // namespace gfx
  54. namespace net {
  55. class SiteForCookies;
  56. } // namespace net
  57. namespace printing {
  58. class MetafileSkia;
  59. } // namespace printing
  60. namespace chrome_pdf {
  61. class MetricsHandler;
  62. class PDFiumEngine;
  63. class PdfAccessibilityDataHandler;
  64. class Thumbnail;
  65. class PdfViewWebPlugin final : public PdfViewPluginBase,
  66. public blink::WebPlugin,
  67. public pdf::mojom::PdfListener,
  68. public UrlLoader::Client,
  69. public PostMessageReceiver::Client,
  70. public PdfAccessibilityActionHandler,
  71. public PreviewModeClient::Client {
  72. public:
  73. // Must match `SaveRequestType` in chrome/browser/resources/pdf/constants.ts.
  74. enum class SaveRequestType {
  75. kAnnotation = 0,
  76. kOriginal = 1,
  77. kEdited = 2,
  78. };
  79. // Provides services from the plugin's container.
  80. class Client : public V8ValueConverter {
  81. public:
  82. virtual ~Client() = default;
  83. virtual base::WeakPtr<Client> GetWeakPtr() = 0;
  84. // Creates a new `PDFiumEngine`.
  85. virtual std::unique_ptr<PDFiumEngine> CreateEngine(
  86. PDFEngine::Client* client,
  87. PDFiumFormFiller::ScriptOption script_option);
  88. // Passes the plugin container to the client. This is first called in
  89. // `Initialize()`, and cleared to null in `Destroy()`. The container may
  90. // also be null for testing.
  91. virtual void SetPluginContainer(blink::WebPluginContainer* container) = 0;
  92. // Returns the plugin container set by `SetPluginContainer()`.
  93. virtual blink::WebPluginContainer* PluginContainer() = 0;
  94. // Returrns the document's site for cookies.
  95. virtual net::SiteForCookies SiteForCookies() const = 0;
  96. // Resolves `partial_url` relative to the document's base URL.
  97. virtual blink::WebURL CompleteURL(
  98. const blink::WebString& partial_url) const = 0;
  99. // Enqueues a "message" event carrying `message` to the plugin embedder.
  100. virtual void PostMessage(base::Value::Dict message) {}
  101. // Invalidates the entire web plugin container and schedules a paint of the
  102. // page in it.
  103. virtual void Invalidate() = 0;
  104. // Notifies the container about which touch events the plugin accepts.
  105. virtual void RequestTouchEventType(
  106. blink::WebPluginContainer::TouchEventRequestType request_type) = 0;
  107. // Notify the web plugin container about the total matches of a find
  108. // request.
  109. virtual void ReportFindInPageMatchCount(int identifier,
  110. int total,
  111. bool final_update) = 0;
  112. // Notify the web plugin container about the selected find result in plugin.
  113. virtual void ReportFindInPageSelection(int identifier,
  114. int index,
  115. bool final_update) = 0;
  116. // Notify the web plugin container about find result tickmarks.
  117. virtual void ReportFindInPageTickmarks(
  118. const std::vector<gfx::Rect>& tickmarks) = 0;
  119. // Returns the device scale factor.
  120. virtual float DeviceScaleFactor() = 0;
  121. // Gets the scroll position.
  122. virtual gfx::PointF GetScrollPosition() = 0;
  123. // Tells the embedder to allow the plugin to handle find requests.
  124. virtual void UsePluginAsFindHandler() = 0;
  125. // Calls underlying WebLocalFrame::SetReferrerForRequest().
  126. virtual void SetReferrerForRequest(blink::WebURLRequest& request,
  127. const blink::WebURL& referrer_url) = 0;
  128. // Calls underlying WebLocalFrame::Alert().
  129. virtual void Alert(const blink::WebString& message) = 0;
  130. // Calls underlying WebLocalFrame::Confirm().
  131. virtual bool Confirm(const blink::WebString& message) = 0;
  132. // Calls underlying WebLocalFrame::Prompt().
  133. virtual blink::WebString Prompt(const blink::WebString& message,
  134. const blink::WebString& default_value) = 0;
  135. // Calls underlying WebLocalFrame::TextSelectionChanged().
  136. virtual void TextSelectionChanged(const blink::WebString& selection_text,
  137. uint32_t offset,
  138. const gfx::Range& range) = 0;
  139. // Calls underlying WebLocalFrame::CreateAssociatedURLLoader().
  140. virtual std::unique_ptr<blink::WebAssociatedURLLoader>
  141. CreateAssociatedURLLoader(
  142. const blink::WebAssociatedURLLoaderOptions& options) = 0;
  143. // Notifies the frame widget about the text input type change.
  144. virtual void UpdateTextInputState() = 0;
  145. // Notifies the frame widget about the selection bound change.
  146. virtual void UpdateSelectionBounds() = 0;
  147. // Gets the embedder's origin as a serialized string.
  148. virtual std::string GetEmbedderOriginString() = 0;
  149. // Returns whether the plugin container's frame exists.
  150. virtual bool HasFrame() const = 0;
  151. // Notifies the frame's client that the plugin started loading.
  152. virtual void DidStartLoading() = 0;
  153. // Notifies the frame's client that the plugin stopped loading.
  154. virtual void DidStopLoading() = 0;
  155. // Prints the plugin element.
  156. virtual void Print() {}
  157. // Sends over a string to be recorded by user metrics as a computed action.
  158. // When you use this, you need to also update the rules for extracting known
  159. // actions in tools/metrics/actions/extract_actions.py.
  160. virtual void RecordComputedAction(const std::string& action) {}
  161. // Creates an implementation of `PdfAccessibilityDataHandler` catered to the
  162. // client.
  163. virtual std::unique_ptr<PdfAccessibilityDataHandler>
  164. CreateAccessibilityDataHandler(
  165. PdfAccessibilityActionHandler* action_handler);
  166. };
  167. PdfViewWebPlugin(std::unique_ptr<Client> client,
  168. mojo::AssociatedRemote<pdf::mojom::PdfService> pdf_service,
  169. const blink::WebPluginParams& params);
  170. PdfViewWebPlugin(const PdfViewWebPlugin& other) = delete;
  171. PdfViewWebPlugin& operator=(const PdfViewWebPlugin& other) = delete;
  172. // blink::WebPlugin:
  173. bool Initialize(blink::WebPluginContainer* container) override;
  174. void Destroy() override;
  175. blink::WebPluginContainer* Container() const override;
  176. v8::Local<v8::Object> V8ScriptableObject(v8::Isolate* isolate) override;
  177. bool SupportsKeyboardFocus() const override;
  178. void UpdateAllLifecyclePhases(blink::DocumentUpdateReason reason) override;
  179. void Paint(cc::PaintCanvas* canvas, const gfx::Rect& rect) override;
  180. void UpdateGeometry(const gfx::Rect& window_rect,
  181. const gfx::Rect& clip_rect,
  182. const gfx::Rect& unobscured_rect,
  183. bool is_visible) override;
  184. void UpdateFocus(bool focused, blink::mojom::FocusType focus_type) override;
  185. void UpdateVisibility(bool visibility) override;
  186. blink::WebInputEventResult HandleInputEvent(
  187. const blink::WebCoalescedInputEvent& event,
  188. ui::Cursor* cursor) override;
  189. void DidReceiveResponse(const blink::WebURLResponse& response) override;
  190. void DidReceiveData(const char* data, size_t data_length) override;
  191. void DidFinishLoading() override;
  192. void DidFailLoading(const blink::WebURLError& error) override;
  193. bool SupportsPaginatedPrint() override;
  194. bool GetPrintPresetOptionsFromDocument(
  195. blink::WebPrintPresetOptions* print_preset_options) override;
  196. int PrintBegin(const blink::WebPrintParams& print_params) override;
  197. void PrintPage(int page_number, cc::PaintCanvas* canvas) override;
  198. void PrintEnd() override;
  199. bool HasSelection() const override;
  200. blink::WebString SelectionAsText() const override;
  201. blink::WebString SelectionAsMarkup() const override;
  202. bool CanEditText() const override;
  203. bool HasEditableText() const override;
  204. bool CanUndo() const override;
  205. bool CanRedo() const override;
  206. bool CanCopy() const override;
  207. bool ExecuteEditCommand(const blink::WebString& name,
  208. const blink::WebString& value) override;
  209. blink::WebURL LinkAtPosition(const gfx::Point& /*position*/) const override;
  210. bool StartFind(const blink::WebString& search_text,
  211. bool case_sensitive,
  212. int identifier) override;
  213. void SelectFindResult(bool forward, int identifier) override;
  214. void StopFind() override;
  215. bool CanRotateView() override;
  216. void RotateView(blink::WebPlugin::RotationType type) override;
  217. bool ShouldDispatchImeEventsToPlugin() override;
  218. blink::WebTextInputType GetPluginTextInputType() override;
  219. gfx::Rect GetPluginCaretBounds() override;
  220. void ImeSetCompositionForPlugin(
  221. const blink::WebString& text,
  222. const std::vector<ui::ImeTextSpan>& ime_text_spans,
  223. const gfx::Range& replacement_range,
  224. int selection_start,
  225. int selection_end) override;
  226. void ImeCommitTextForPlugin(
  227. const blink::WebString& text,
  228. const std::vector<ui::ImeTextSpan>& ime_text_spans,
  229. const gfx::Range& replacement_range,
  230. int relative_cursor_pos) override;
  231. void ImeFinishComposingTextForPlugin(bool keep_selection) override;
  232. // PDFEngine::Client:
  233. void ProposeDocumentLayout(const DocumentLayout& layout) override;
  234. void UpdateCursor(ui::mojom::CursorType new_cursor_type) override;
  235. void UpdateTickMarks(const std::vector<gfx::Rect>& tickmarks) override;
  236. void NotifyNumberOfFindResultsChanged(int total, bool final_result) override;
  237. void NotifySelectedFindResultChanged(int current_find_index,
  238. bool final_result) override;
  239. void GetDocumentPassword(
  240. base::OnceCallback<void(const std::string&)> callback) override;
  241. void Beep() override;
  242. void Alert(const std::string& message) override;
  243. bool Confirm(const std::string& message) override;
  244. std::string Prompt(const std::string& question,
  245. const std::string& default_answer) override;
  246. std::string GetURL() override;
  247. void Print() override;
  248. void SubmitForm(const std::string& url,
  249. const void* data,
  250. int length) override;
  251. std::unique_ptr<UrlLoader> CreateUrlLoader() override;
  252. std::vector<SearchStringResult> SearchString(const char16_t* string,
  253. const char16_t* term,
  254. bool case_sensitive) override;
  255. void DocumentHasUnsupportedFeature(const std::string& feature) override;
  256. bool IsPrintPreview() const override;
  257. SkColor GetBackgroundColor() const override;
  258. void CaretChanged(const gfx::Rect& caret_rect) override;
  259. void EnteredEditMode() override;
  260. void SetSelectedText(const std::string& selected_text) override;
  261. void SetLinkUnderCursor(const std::string& link_under_cursor) override;
  262. bool IsValidLink(const std::string& url) override;
  263. // pdf::mojom::PdfListener:
  264. void SetCaretPosition(const gfx::PointF& position) override;
  265. void MoveRangeSelectionExtent(const gfx::PointF& extent) override;
  266. void SetSelectionBounds(const gfx::PointF& base,
  267. const gfx::PointF& extent) override;
  268. // UrlLoader::Client:
  269. bool IsValid() const override;
  270. blink::WebURL CompleteURL(const blink::WebString& partial_url) const override;
  271. net::SiteForCookies SiteForCookies() const override;
  272. void SetReferrerForRequest(blink::WebURLRequest& request,
  273. const blink::WebURL& referrer_url) override;
  274. std::unique_ptr<blink::WebAssociatedURLLoader> CreateAssociatedURLLoader(
  275. const blink::WebAssociatedURLLoaderOptions& options) override;
  276. // PostMessageReceiver::Client:
  277. void OnMessage(const base::Value::Dict& message) override;
  278. // PaintManager::Client:
  279. void InvalidatePluginContainer() override;
  280. void UpdateSnapshot(sk_sp<SkImage> snapshot) override;
  281. void UpdateScale(float scale) override;
  282. void UpdateLayerTransform(float scale,
  283. const gfx::Vector2dF& translate) override;
  284. // PdfAccessibilityActionHandler:
  285. void EnableAccessibility() override;
  286. void HandleAccessibilityAction(
  287. const AccessibilityActionData& action_data) override;
  288. // PreviewModeClient::Client:
  289. void PreviewDocumentLoadComplete() override;
  290. void PreviewDocumentLoadFailed() override;
  291. // Initializes the plugin for testing, bypassing certain consistency checks.
  292. bool InitializeForTesting();
  293. const gfx::Rect& GetPluginRectForTesting() const { return plugin_rect(); }
  294. float GetDeviceScaleForTesting() const { return device_scale(); }
  295. protected:
  296. // PdfViewPluginBase:
  297. std::unique_ptr<PDFiumEngine> CreateEngine(
  298. PDFEngine::Client* client,
  299. PDFiumFormFiller::ScriptOption script_option) override;
  300. const PDFiumEngine* engine() const override;
  301. PDFiumEngine* engine() override;
  302. base::WeakPtr<PdfViewPluginBase> GetWeakPtr() override;
  303. void OnPrintPreviewLoaded() override;
  304. void OnDocumentLoadComplete() override;
  305. void SendMessage(base::Value::Dict message) override;
  306. void SetFormTextFieldInFocus(bool in_focus) override;
  307. void SetAccessibilityDocInfo(AccessibilityDocInfo doc_info) override;
  308. void SetAccessibilityPageInfo(AccessibilityPageInfo page_info,
  309. std::vector<AccessibilityTextRunInfo> text_runs,
  310. std::vector<AccessibilityCharInfo> chars,
  311. AccessibilityPageObjects page_objects) override;
  312. void SetAccessibilityViewportInfo(
  313. AccessibilityViewportInfo viewport_info) override;
  314. void SetContentRestrictions(int content_restrictions) override;
  315. void DidStartLoading() override;
  316. void DidStopLoading() override;
  317. void NotifySelectionChanged(const gfx::PointF& left,
  318. int left_height,
  319. const gfx::PointF& right,
  320. int right_height) override;
  321. void UserMetricsRecordAction(const std::string& action) override;
  322. bool full_frame() const override;
  323. const gfx::Size& plugin_dip_size() const override;
  324. const gfx::Rect& plugin_rect() const override;
  325. float device_scale() const override;
  326. bool needs_reraster() const override;
  327. base::i18n::TextDirection ui_direction() const override;
  328. bool received_viewport_message() const override;
  329. void PrepareForFirstPaint(std::vector<PaintReadyRect>& ready) override;
  330. private:
  331. // Callback that runs after `LoadUrl()`. The `loader` is the loader used to
  332. // load the URL, and `result` is the result code for the load.
  333. using LoadUrlCallback =
  334. base::OnceCallback<void(std::unique_ptr<UrlLoader> loader,
  335. int32_t result)>;
  336. // Metadata about an available preview page.
  337. struct PreviewPageInfo {
  338. // Data source URL.
  339. std::string url;
  340. // Page index in destination document.
  341. int dest_page_index = -1;
  342. };
  343. // Call `Destroy()` instead.
  344. ~PdfViewWebPlugin() override;
  345. bool InitializeCommon();
  346. // Sends whether to do smooth scrolling.
  347. void SendSetSmoothScrolling();
  348. // Handles `LoadUrl()` result for the main document.
  349. void DidOpen(std::unique_ptr<UrlLoader> loader, int32_t result);
  350. // Updates the scroll position, which is in CSS pixels relative to the
  351. // top-left corner.
  352. void UpdateScroll(const gfx::PointF& scroll_position);
  353. // Loads `url`, invoking `callback` on receiving the initial response.
  354. void LoadUrl(base::StringPiece url, LoadUrlCallback callback);
  355. // Handles `Open()` result for `form_loader_`.
  356. void DidFormOpen(int32_t result);
  357. // Message handlers.
  358. void HandleDisplayAnnotationsMessage(const base::Value::Dict& message);
  359. void HandleGetNamedDestinationMessage(const base::Value::Dict& message);
  360. void HandleGetPasswordCompleteMessage(const base::Value::Dict& message);
  361. void HandleGetSelectedTextMessage(const base::Value::Dict& message);
  362. void HandleGetThumbnailMessage(const base::Value::Dict& message);
  363. void HandlePrintMessage(const base::Value::Dict& /*message*/);
  364. void HandleRotateClockwiseMessage(const base::Value::Dict& /*message*/);
  365. void HandleRotateCounterclockwiseMessage(
  366. const base::Value::Dict& /*message*/);
  367. void HandleSaveAttachmentMessage(const base::Value::Dict& message);
  368. void HandleSaveMessage(const base::Value::Dict& message);
  369. void HandleSelectAllMessage(const base::Value::Dict& /*message*/);
  370. void HandleSetBackgroundColorMessage(const base::Value::Dict& message);
  371. void HandleSetPresentationModeMessage(const base::Value::Dict& message);
  372. void HandleSetTwoUpViewMessage(const base::Value::Dict& message);
  373. void HandleStopScrollingMessage(const base::Value::Dict& message);
  374. void HandleViewportMessage(const base::Value::Dict& message);
  375. void SaveToBuffer(const std::string& token);
  376. void SaveToFile(const std::string& token);
  377. // Recalculates values that depend on scale factors.
  378. void UpdateScaledValues();
  379. void OnViewportChanged(const gfx::Rect& new_plugin_rect_in_css_pixel,
  380. float new_device_scale);
  381. // Text editing methods.
  382. bool SelectAll();
  383. bool Cut();
  384. bool Paste(const blink::WebString& value);
  385. bool Undo();
  386. bool Redo();
  387. bool HandleWebInputEvent(const blink::WebInputEvent& event);
  388. // Helper method for converting IME text to input events.
  389. // TODO(crbug.com/1253665): Consider handling composition events.
  390. void HandleImeCommit(const blink::WebString& text);
  391. // Callback to print without re-entrancy issues. The callback prevents the
  392. // invocation of printing in the middle of an event handler, which is risky;
  393. // see crbug.com/66334.
  394. // TODO(crbug.com/1217012): Re-evaluate the need for a callback when parts of
  395. // the plugin are moved off the main thread.
  396. void OnInvokePrintDialog();
  397. void ResetRecentlySentFindUpdate();
  398. // Records metrics about the document metadata.
  399. void RecordDocumentMetrics();
  400. // Sends the attachments data.
  401. void SendAttachments();
  402. // Sends the bookmarks data.
  403. void SendBookmarks();
  404. // Send document metadata data.
  405. void SendMetadata();
  406. // Handles message for resetting Print Preview.
  407. void HandleResetPrintPreviewModeMessage(const base::Value::Dict& message);
  408. // Handles message for loading a preview page.
  409. void HandleLoadPreviewPageMessage(const base::Value::Dict& message);
  410. // Starts loading the next available preview page into a blank page.
  411. void LoadAvailablePreviewPage();
  412. // Handles `LoadUrl()` result for a preview page.
  413. void DidOpenPreview(std::unique_ptr<UrlLoader> loader, int32_t result);
  414. // Continues loading the next preview page.
  415. void LoadNextPreviewPage();
  416. // Sends a notification that the print preview has loaded.
  417. void SendPrintPreviewLoadedNotification();
  418. // Sends the thumbnail image data.
  419. void SendThumbnail(base::Value::Dict reply, Thumbnail thumbnail);
  420. blink::WebString selected_text_;
  421. std::unique_ptr<Client> const client_;
  422. // Used to access the services provided by the browser.
  423. mojo::AssociatedRemote<pdf::mojom::PdfService> const pdf_service_;
  424. mojo::Receiver<pdf::mojom::PdfListener> listener_receiver_{this};
  425. std::unique_ptr<PDFiumEngine> engine_;
  426. // The URL of the PDF document.
  427. std::string url_;
  428. // The callback for receiving the password from the page.
  429. base::OnceCallback<void(const std::string&)> password_callback_;
  430. // The current cursor type.
  431. ui::mojom::CursorType cursor_type_ = ui::mojom::CursorType::kPointer;
  432. blink::WebTextInputType text_input_type_ =
  433. blink::WebTextInputType::kWebTextInputTypeNone;
  434. gfx::Rect caret_rect_;
  435. blink::WebString composition_text_;
  436. // Whether the plugin element currently has focus.
  437. bool has_focus_ = false;
  438. blink::WebPluginParams initial_params_;
  439. v8::Persistent<v8::Object> scriptable_receiver_;
  440. // The current image snapshot.
  441. cc::PaintImage snapshot_;
  442. // Translate from snapshot to device pixels.
  443. gfx::Vector2dF snapshot_translate_;
  444. // Scale from snapshot to device pixels.
  445. float snapshot_scale_ = 1.0f;
  446. // The viewport coordinates to DIP (device-independent pixel) ratio.
  447. float viewport_to_dip_scale_ = 1.0f;
  448. // The device pixel to CSS pixel ratio.
  449. float device_to_css_scale_ = 1.0f;
  450. // Combined translate from snapshot to device to CSS pixels.
  451. gfx::Vector2dF total_translate_;
  452. // The plugin rect in CSS pixels.
  453. gfx::Rect css_plugin_rect_;
  454. // True if the plugin occupies the entire frame (not embedded).
  455. bool full_frame_ = false;
  456. // The background color of the PDF viewer.
  457. SkColor background_color_ = SK_ColorTRANSPARENT;
  458. // Size, in DIPs, of plugin rectangle.
  459. gfx::Size plugin_dip_size_;
  460. // The plugin rectangle in device pixels.
  461. gfx::Rect plugin_rect_;
  462. // Current device scale factor. Multiply by `device_scale_` to convert from
  463. // viewport to screen coordinates. Divide by `device_scale_` to convert from
  464. // screen to viewport coordinates.
  465. float device_scale_ = 1.0f;
  466. // True if we haven't painted the plugin viewport yet.
  467. bool first_paint_ = true;
  468. // True if last bitmap was smaller than the screen.
  469. bool last_bitmap_smaller_ = false;
  470. // True if we request a new bitmap rendering.
  471. bool needs_reraster_ = true;
  472. // The UI direction.
  473. base::i18n::TextDirection ui_direction_ = base::i18n::UNKNOWN_DIRECTION;
  474. // The scroll offset for the last raster in CSS pixels, before any
  475. // transformations are applied.
  476. gfx::Vector2dF scroll_offset_at_last_raster_;
  477. // If this is true, then don't scroll the plugin in response to calls to
  478. // `UpdateScroll()`. This will be true when the extension page is in the
  479. // process of zooming the plugin so that flickering doesn't occur while
  480. // zooming.
  481. bool stop_scrolling_ = false;
  482. // Whether the plugin has received a viewport changed message. Nothing should
  483. // be painted until this is received.
  484. bool received_viewport_message_ = false;
  485. // If true, the render frame has been notified that we're starting a network
  486. // request so that it can start the throbber. It will be notified again once
  487. // the document finishes loading.
  488. bool did_call_start_loading_ = false;
  489. // Used for submitting forms.
  490. std::unique_ptr<UrlLoader> form_loader_;
  491. // Handler for accessibility data updates.
  492. std::unique_ptr<PdfAccessibilityDataHandler> const
  493. pdf_accessibility_data_handler_;
  494. // The URL currently under the cursor.
  495. std::string link_under_cursor_;
  496. // The id of the current find operation, or -1 if no current operation is
  497. // present.
  498. int find_identifier_ = -1;
  499. // Whether an update to the number of find results found was sent less than
  500. // `kFindResultCooldown` TimeDelta ago.
  501. bool recently_sent_find_update_ = false;
  502. // Stores the tickmarks to be shown for the current find results.
  503. std::vector<gfx::Rect> tickmarks_;
  504. // Whether the document is in edit mode.
  505. bool edit_mode_ = false;
  506. // Only instantiated when not print previewing.
  507. std::unique_ptr<MetricsHandler> metrics_handler_;
  508. // Keeps track of which unsupported features have been reported to avoid
  509. // spamming the metrics if a feature shows up many times per document.
  510. base::flat_set<std::string> unsupported_features_reported_;
  511. // Indicates whether the browser has been notified about an unsupported
  512. // feature once, which helps prevent the infobar from going up more than once.
  513. bool notified_browser_about_unsupported_feature_ = false;
  514. // The metafile in which to save the printed output. Assigned a value only
  515. // between `PrintBegin()` and `PrintEnd()` calls.
  516. raw_ptr<printing::MetafileSkia> printing_metafile_ = nullptr;
  517. // The indices of pages to print.
  518. std::vector<int> pages_to_print_;
  519. // Assigned a value only between `PrintBegin()` and `PrintEnd()` calls.
  520. absl::optional<blink::WebPrintParams> print_params_;
  521. // For identifying actual print operations to avoid double logging of UMA.
  522. bool print_pages_called_;
  523. // Whether the plugin is loaded in Print Preview.
  524. bool is_print_preview_ = false;
  525. // Number of pages in Print Preview (non-PDF). 0 if previewing a PDF, and -1
  526. // if not in Print Preview.
  527. int print_preview_page_count_ = -1;
  528. // Number of pages loaded in Print Preview (non-PDF). Always less than or
  529. // equal to `print_preview_page_count_`.
  530. int print_preview_loaded_page_count_ = -1;
  531. // The PreviewModeClient used for print preview. Will be passed to
  532. // `preview_engine_`.
  533. std::unique_ptr<PreviewModeClient> preview_client_;
  534. // Engine used to render individual preview pages. This will use the
  535. // `PreviewModeClient` interface.
  536. std::unique_ptr<PDFiumEngine> preview_engine_;
  537. // Document load state for the Print Preview engine.
  538. DocumentLoadState preview_document_load_state_ = DocumentLoadState::kComplete;
  539. // Queue of available preview pages to load next.
  540. base::queue<PreviewPageInfo> preview_pages_info_;
  541. base::WeakPtrFactory<PdfViewWebPlugin> weak_factory_{this};
  542. };
  543. } // namespace chrome_pdf
  544. #endif // PDF_PDF_VIEW_WEB_PLUGIN_H_