| // Copyright 2020 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef PDF_PDF_VIEW_WEB_PLUGIN_H_ | |
| #define PDF_PDF_VIEW_WEB_PLUGIN_H_ | |
| #include <stdint.h> | |
| #include <memory> | |
| #include <optional> | |
| #include <string> | |
| #include <string_view> | |
| #include <vector> | |
| #include "base/containers/flat_set.h" | |
| #include "base/containers/queue.h" | |
| #include "base/containers/span.h" | |
| #include "base/functional/callback_forward.h" | |
| #include "base/i18n/rtl.h" | |
| #include "base/memory/raw_ptr.h" | |
| #include "base/memory/weak_ptr.h" | |
| #include "base/values.h" | |
| #include "cc/paint/paint_image.h" | |
| #include "mojo/public/cpp/bindings/associated_remote.h" | |
| #include "mojo/public/cpp/bindings/receiver.h" | |
| #include "pdf/accessibility_structs.h" | |
| #include "pdf/buildflags.h" | |
| #include "pdf/loader/result_codes.h" | |
| #include "pdf/loader/url_loader.h" | |
| #include "pdf/mojom/pdf.mojom.h" | |
| #include "pdf/paint_manager.h" | |
| #include "pdf/pdf_accessibility_action_handler.h" | |
| #include "pdf/pdfium/pdfium_engine_client.h" | |
| #include "pdf/pdfium/pdfium_form_filler.h" | |
| #include "pdf/post_message_receiver.h" | |
| #include "pdf/preview_mode_client.h" | |
| #include "pdf/v8_value_converter.h" | |
| #include "services/screen_ai/buildflags/buildflags.h" | |
| #include "third_party/blink/public/mojom/annotation/annotation.mojom.h" | |
| #include "third_party/blink/public/platform/cross_variant_mojo_util.h" | |
| #include "third_party/blink/public/platform/web_string.h" | |
| #include "third_party/blink/public/platform/web_text_input_type.h" | |
| #include "third_party/blink/public/web/web_plugin.h" | |
| #include "third_party/blink/public/web/web_plugin_container.h" | |
| #include "third_party/blink/public/web/web_plugin_params.h" | |
| #include "third_party/blink/public/web/web_print_params.h" | |
| #include "third_party/blink/public/web/web_view_observer.h" | |
| #include "third_party/skia/include/core/SkBitmap.h" | |
| #include "third_party/skia/include/core/SkColor.h" | |
| #include "ui/base/cursor/cursor.h" | |
| #include "ui/base/cursor/mojom/cursor_type.mojom-shared.h" | |
| #include "ui/gfx/geometry/rect.h" | |
| #include "ui/gfx/geometry/size.h" | |
| #include "ui/gfx/geometry/vector2d_f.h" | |
| #include "v8/include/v8.h" | |
| #if BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| #include "mojo/public/cpp/bindings/unique_receiver_set.h" | |
| #include "pdf/save_data_buffer_handler_for_drive.h" | |
| #endif // BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE) | |
| #include "services/screen_ai/public/mojom/screen_ai_service.mojom-forward.h" | |
| #endif | |
| namespace blink { | |
| class WebAssociatedURLLoader; | |
| class WebInputEvent; | |
| class WebURL; | |
| class WebURLRequest; | |
| struct WebAssociatedURLLoaderOptions; | |
| struct WebPrintPresetOptions; | |
| } // namespace blink | |
| namespace gfx { | |
| class PointF; | |
| class Range; | |
| } // namespace gfx | |
| namespace net { | |
| class SiteForCookies; | |
| } // namespace net | |
| namespace printing { | |
| class MetafileSkia; | |
| } // namespace printing | |
| namespace chrome_pdf { | |
| class MetricsHandler; | |
| class PDFiumEngine; | |
| class PdfAccessibilityDataHandler; | |
| class PdfAnnotationAgent; | |
| class Thumbnail; | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| class PdfInkModule; | |
| class PdfInkModuleClient; | |
| #endif | |
| class PdfViewWebPlugin final : public PDFiumEngineClient, | |
| public blink::WebPlugin, | |
| public blink::WebViewObserver, | |
| public pdf::mojom::PdfListener, | |
| public UrlLoader::Client, | |
| public PostMessageReceiver::Client, | |
| public PaintManager::Client, | |
| public PdfAccessibilityActionHandler, | |
| public PreviewModeClient::Client, | |
| public blink::mojom::AnnotationAgentContainer { | |
| public: | |
| // Do not save files larger than 100 MB. This cap should be kept in sync with | |
| // and is also enforced in chrome/browser/resources/pdf/pdf_viewer.ts. | |
| static constexpr size_t kMaximumSavedFileSize = 100 * 1000 * 1000; | |
| enum class DocumentLoadState { | |
| kLoading = 0, | |
| kComplete, | |
| kFailed, | |
| }; | |
| // Provides services from the plugin's container. | |
| class Client : public V8ValueConverter { | |
| public: | |
| virtual ~Client() = default; | |
| virtual base::WeakPtr<Client> GetWeakPtr() = 0; | |
| // Creates a new `PDFiumEngine`. | |
| virtual std::unique_ptr<PDFiumEngine> CreateEngine( | |
| PDFiumEngineClient* client, | |
| PDFiumFormFiller::ScriptOption script_option); | |
| // Passes the plugin container to the client. This is first called in | |
| // `Initialize()`, and cleared to null in `Destroy()`. The container may | |
| // also be null for testing. | |
| virtual void SetPluginContainer(blink::WebPluginContainer* container) = 0; | |
| // Returns the plugin container set by `SetPluginContainer()`. | |
| virtual blink::WebPluginContainer* PluginContainer() = 0; | |
| // Returns the current V8 isolate, if any. | |
| virtual v8::Isolate* GetIsolate() = 0; | |
| // Returns the document's site for cookies. | |
| virtual net::SiteForCookies SiteForCookies() const = 0; | |
| // Resolves `partial_url` relative to the document's base URL. | |
| virtual blink::WebURL CompleteURL( | |
| const blink::WebString& partial_url) const = 0; | |
| // Enqueues a "message" event carrying `message` to the embedder. | |
| // Messages are guaranteed to be received in the order that they are sent. | |
| // This method is non-blocking. | |
| virtual void PostMessage(base::DictValue message) {} | |
| // Invalidates the entire web plugin container and schedules a paint of the | |
| // page in it. | |
| virtual void Invalidate() = 0; | |
| // Notifies the container about which touch events the plugin accepts. | |
| virtual void RequestTouchEventType( | |
| blink::WebPluginContainer::TouchEventRequestType request_type) = 0; | |
| // Notify the web plugin container about the total matches of a find | |
| // request. | |
| virtual void ReportFindInPageMatchCount(int identifier, | |
| int total, | |
| bool final_update) = 0; | |
| // Notify the web plugin container about the selected find result in plugin. | |
| virtual void ReportFindInPageSelection(int identifier, | |
| int index, | |
| bool final_update) = 0; | |
| // Notify the web plugin container about find result tickmarks. | |
| virtual void ReportFindInPageTickmarks( | |
| const std::vector<gfx::Rect>& tickmarks) = 0; | |
| // Returns the device scale factor. | |
| virtual float DeviceScaleFactor() = 0; | |
| // Gets the scroll position. | |
| virtual gfx::PointF GetScrollPosition() = 0; | |
| // Tells the embedder to allow the plugin to handle find requests. | |
| virtual void UsePluginAsFindHandler() = 0; | |
| // Calls underlying WebLocalFrame::SetReferrerForRequest(). | |
| virtual void SetReferrerForRequest(blink::WebURLRequest& request, | |
| const blink::WebURL& referrer_url) = 0; | |
| // Calls underlying WebLocalFrame::Alert(). | |
| virtual void Alert(const blink::WebString& message) = 0; | |
| // Calls underlying WebLocalFrame::Confirm(). | |
| virtual bool Confirm(const blink::WebString& message) = 0; | |
| // Calls underlying WebLocalFrame::Prompt(). | |
| virtual blink::WebString Prompt(const blink::WebString& message, | |
| const blink::WebString& default_value) = 0; | |
| // Calls underlying WebLocalFrame::TextSelectionChanged(). | |
| virtual void TextSelectionChanged(const blink::WebString& selection_text, | |
| uint32_t offset, | |
| const gfx::Range& range) = 0; | |
| // Calls underlying WebLocalFrame::CreateAssociatedURLLoader(). | |
| virtual std::unique_ptr<blink::WebAssociatedURLLoader> | |
| CreateAssociatedURLLoader( | |
| const blink::WebAssociatedURLLoaderOptions& options) = 0; | |
| // Notifies the frame widget about the text input type change. | |
| virtual void UpdateTextInputState() = 0; | |
| // Notifies the frame widget about the selection bound change. | |
| virtual void UpdateSelectionBounds() = 0; | |
| // Gets the embedder's origin as a serialized string. | |
| virtual std::string GetEmbedderOriginString() = 0; | |
| // Returns whether the plugin container's frame exists. | |
| virtual bool HasFrame() const = 0; | |
| // Notifies the frame's client that the plugin started loading. | |
| virtual void DidStartLoading() = 0; | |
| // Notifies the frame's client that the plugin stopped loading. | |
| virtual void DidStopLoading() = 0; | |
| // Prints the plugin element. | |
| virtual void Print() {} | |
| // Sends over a string to be recorded by user metrics as a computed action. | |
| // When you use this, you need to also update the rules for extracting known | |
| // actions in tools/metrics/actions/extract_actions.py. | |
| virtual void RecordComputedAction(const std::string& action) {} | |
| // Creates an implementation of `PdfAccessibilityDataHandler` catered to the | |
| // client. The return value must be non-null. | |
| virtual std::unique_ptr<PdfAccessibilityDataHandler> | |
| CreateAccessibilityDataHandler( | |
| PdfAccessibilityActionHandler* action_handler, | |
| blink::WebPluginContainer* plugin_container) = 0; | |
| #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE) | |
| // Returns the maximum expected dimension of the images to be OCRed. If | |
| // images are larger than the returned value, they will be downsampled | |
| // before processing. | |
| virtual void GetOcrMaxImageDimension( | |
| base::OnceCallback<void(uint32_t)> callback) = 0; | |
| // Performs OCR on `image` and sends the recognized text to `callback`. | |
| // In case OCR service gets disconnected before or during running this | |
| // request, `callback` will not be called. | |
| virtual void PerformOcr( | |
| const SkBitmap& image, | |
| base::OnceCallback<void(screen_ai::mojom::VisualAnnotationPtr)> | |
| callback) = 0; | |
| // Sets a `callback` that is notified if OCR service gets disconnected. | |
| virtual void SetOcrDisconnectedCallback( | |
| base::RepeatingClosure callback) = 0; | |
| #endif | |
| }; | |
| struct SaveDataBlock { | |
| SaveDataBlock(); | |
| SaveDataBlock(SaveDataBlock&& other) noexcept; | |
| SaveDataBlock& operator=(SaveDataBlock&&) noexcept; | |
| ~SaveDataBlock(); | |
| std::vector<uint8_t> block; | |
| uint32_t total_file_size = 0; | |
| }; | |
| PdfViewWebPlugin(std::unique_ptr<Client> client, | |
| mojo::AssociatedRemote<pdf::mojom::PdfHost> pdf_host, | |
| blink::WebPluginParams params); | |
| PdfViewWebPlugin(const PdfViewWebPlugin& other) = delete; | |
| PdfViewWebPlugin& operator=(const PdfViewWebPlugin& other) = delete; | |
| // blink::WebPlugin: | |
| bool Initialize(blink::WebPluginContainer* container) override; | |
| void Destroy() override; | |
| blink::WebPluginContainer* Container() const override; | |
| v8::Local<v8::Object> V8ScriptableObject(v8::Isolate* isolate) override; | |
| bool SupportsKeyboardFocus() const override; | |
| void UpdateAllLifecyclePhases(blink::DocumentUpdateReason reason) override; | |
| void Paint(cc::PaintCanvas* canvas, const gfx::Rect& rect) override; | |
| void UpdateGeometry(const gfx::Rect& window_rect, | |
| const gfx::Rect& clip_rect, | |
| const gfx::Rect& unobscured_rect, | |
| bool is_visible) override; | |
| void UpdateFocus(bool focused, blink::mojom::FocusType focus_type) override; | |
| void UpdateVisibility(bool visibility) override; | |
| blink::WebInputEventResult HandleInputEvent( | |
| const blink::WebCoalescedInputEvent& event, | |
| ui::Cursor* cursor) override; | |
| void DidReceiveResponse(const blink::WebURLResponse& response) override; | |
| void DidReceiveData(base::span<const char> data) override; | |
| void DidFinishLoading() override; | |
| void DidFailLoading(const blink::WebURLError& error) override; | |
| bool SupportsPaginatedPrint() override; | |
| bool GetPrintPresetOptionsFromDocument( | |
| blink::WebPrintPresetOptions* print_preset_options) override; | |
| int PrintBegin(const blink::WebPrintParams& print_params) override; | |
| void PrintPage(int page_index, cc::PaintCanvas* canvas) override; | |
| void PrintEnd() override; | |
| bool HasSelection() const override; | |
| blink::WebString SelectionAsText() const override; | |
| blink::WebString SelectionAsMarkup() const override; | |
| bool CanEditText() const override; | |
| bool HasEditableText() const override; | |
| bool CanUndo() const override; | |
| bool CanRedo() const override; | |
| bool CanCopy() const override; | |
| std::optional<base::i18n::TextDirection> GetFocusedFormTextDirection() | |
| const override; | |
| bool ExecuteEditCommand(const blink::WebString& name, | |
| const blink::WebString& value) override; | |
| blink::WebURL LinkAtPosition(const gfx::Point& /*position*/) const override; | |
| bool StartFind(const blink::WebString& search_text, | |
| bool case_sensitive, | |
| int identifier) override; | |
| void SelectFindResult(bool forward, int identifier) override; | |
| void StopFind() override; | |
| bool CanRotateView() override; | |
| void RotateView(blink::WebPlugin::RotationType type) override; | |
| bool ShouldDispatchImeEventsToPlugin() override; | |
| blink::WebTextInputType GetPluginTextInputType() override; | |
| gfx::Rect GetPluginCaretBounds() override; | |
| gfx::Rect GetPluginSelectionBounds() override; | |
| void ImeSetCompositionForPlugin( | |
| const blink::WebString& text, | |
| const std::vector<ui::ImeTextSpan>& ime_text_spans, | |
| const gfx::Range& replacement_range, | |
| int selection_start, | |
| int selection_end) override; | |
| void ImeCommitTextForPlugin( | |
| const blink::WebString& text, | |
| const std::vector<ui::ImeTextSpan>& ime_text_spans, | |
| const gfx::Range& replacement_range, | |
| int relative_cursor_pos) override; | |
| void ImeFinishComposingTextForPlugin(bool keep_selection) override; | |
| bool SupportsAnnotation() const override; | |
| void BindAnnotationAgentContainer( | |
| blink::CrossVariantMojoReceiver< | |
| blink::mojom::AnnotationAgentContainerInterfaceBase> pending_receiver) | |
| override; | |
| // blink::WebViewObserver: | |
| void OnDestruct() override; | |
| void OnRendererPreferencesUpdated( | |
| const blink::RendererPreferences& preferences) override; | |
| // PDFiumEngineClient: | |
| void ProposeDocumentLayout(const DocumentLayout& layout) override; | |
| bool UseSkiaPremultipliedAlpha() override; | |
| void Invalidate(const gfx::Rect& rect) override; | |
| void DidScroll(const gfx::Vector2d& offset) override; | |
| void ScrollToX(int x_screen_coords, bool force_smooth_scroll) override; | |
| void ScrollToY(int y_screen_coords, bool force_smooth_scroll) override; | |
| void ScrollBy(const gfx::Vector2d& delta) override; | |
| void ScrollToPage(int page) override; | |
| void NavigateTo(const std::string& url, | |
| WindowOpenDisposition disposition) override; | |
| void NavigateToDestination(int page, | |
| const float* x, | |
| const float* y, | |
| const float* zoom) override; | |
| void UpdateCursor(ui::mojom::CursorType new_cursor_type) override; | |
| void UpdateTickMarks(const std::vector<gfx::Rect>& tickmarks) override; | |
| void NotifyNumberOfFindResultsChanged(int total, bool final_result) override; | |
| void NotifySelectedFindResultChanged(int current_find_index, | |
| bool final_result) override; | |
| void NotifyTouchSelectionOccurred() override; | |
| void GetDocumentPassword( | |
| base::OnceCallback<void(const std::string&)> callback) override; | |
| void Beep() override; | |
| void Alert(const std::string& message) override; | |
| bool Confirm(const std::string& message) override; | |
| std::string Prompt(const std::string& question, | |
| const std::string& default_answer) override; | |
| std::string GetURL() override; | |
| void Email(const std::string& to, | |
| const std::string& cc, | |
| const std::string& bcc, | |
| const std::string& subject, | |
| const std::string& body) override; | |
| void Print() override; | |
| void SubmitForm(const std::string& url, | |
| base::span<const uint8_t> data) override; | |
| std::unique_ptr<UrlLoader> CreateUrlLoader() override; | |
| v8::Isolate* GetIsolate() override; | |
| std::vector<SearchStringResult> SearchString(const std::u16string& needle, | |
| const std::u16string& haystack, | |
| bool case_sensitive) override; | |
| void DocumentLoadComplete() override; | |
| void OnFirstContentPainted() override; | |
| void DocumentLoadFailed() override; | |
| void DocumentHasUnsupportedFeature(const std::string& feature) override; | |
| void DocumentLoadProgress(uint32_t available, uint32_t doc_size) override; | |
| void FormFieldFocusChange(PDFiumEngineClient::FocusFieldType type) override; | |
| bool IsPrintPreview() const override; | |
| SkColor GetBackgroundColor() const override; | |
| void SelectionChanged(const gfx::Rect& left, | |
| const gfx::Rect& right, | |
| const gfx::Rect& bounding_box) override; | |
| void CaretChanged(const gfx::Rect& caret_rect) override; | |
| void EnteredEditMode() override; | |
| void DocumentFocusChanged(bool document_has_focus) override; | |
| void SetSelectedText(const std::string& selected_text) override; | |
| void SetLinkUnderCursor(const std::string& link_under_cursor) override; | |
| bool IsValidLink(const std::string& url) override; | |
| void OnNewTextFragmentsSearchStarted() override; | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| bool IsInAnnotationMode() const override; | |
| #endif // BUILDFLAG(ENABLE_PDF_INK2) | |
| #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE) | |
| void OnSearchifyStateChange(bool busy) override; | |
| void OnHasSearchifyText() override; | |
| void MaybeShowSearchifyInProgress() override; | |
| #endif | |
| // pdf::mojom::PdfListener: | |
| void SetCaretPosition(const gfx::PointF& position) override; | |
| void MoveRangeSelectionExtent(const gfx::PointF& extent) override; | |
| void SetSelectionBase(const gfx::PointF& base) override; | |
| void GetPdfBytes(uint32_t size_limit, GetPdfBytesCallback callback) override; | |
| void GetPageText(int32_t page_index, GetPageTextCallback callback) override; | |
| void GetMostVisiblePageIndex( | |
| GetMostVisiblePageIndexCallback callback) override; | |
| void HasMeaningfulText(HasMeaningfulTextCallback callback) override; | |
| void HasJavaScript(HasJavaScriptCallback callback) override; | |
| void IsPasswordProtected(IsPasswordProtectedCallback callback) override; | |
| #if BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| void GetSaveDataBufferHandlerForDrive( | |
| pdf::mojom::SaveRequestType request_type, | |
| GetSaveDataBufferHandlerForDriveCallback callback) override; | |
| #endif // BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| // UrlLoader::Client: | |
| bool IsValid() const override; | |
| blink::WebURL CompleteURL(const blink::WebString& partial_url) const override; | |
| net::SiteForCookies SiteForCookies() const override; | |
| void SetReferrerForRequest(blink::WebURLRequest& request, | |
| const blink::WebURL& referrer_url) override; | |
| std::unique_ptr<blink::WebAssociatedURLLoader> CreateAssociatedURLLoader( | |
| const blink::WebAssociatedURLLoaderOptions& options) override; | |
| // PostMessageReceiver::Client: | |
| void OnMessage(const base::DictValue& message) override; | |
| // PaintManager::Client: | |
| void InvalidatePluginContainer() override; | |
| void OnPaint(const std::vector<gfx::Rect>& paint_rects, | |
| std::vector<PaintReadyRect>& ready, | |
| std::vector<gfx::Rect>& pending) override; | |
| SkBitmap* InstallBuffer(SkImageInfo image_info, | |
| base::span<uint8_t> data) override; | |
| void UpdateSnapshot(sk_sp<SkImage> snapshot) override; | |
| void UpdateScale(float scale) override; | |
| void UpdateLayerTransform(float scale, | |
| const gfx::Vector2dF& translate) override; | |
| // PdfAccessibilityActionHandler: | |
| void EnableAccessibility() override; | |
| void HandleAccessibilityAction( | |
| const AccessibilityActionData& action_data) override; | |
| void LoadOrReloadAccessibility() override; | |
| // PreviewModeClient::Client: | |
| void PreviewDocumentLoadComplete() override; | |
| void PreviewDocumentLoadFailed() override; | |
| // `blink::mojom::AnnotationAgentContainer`: | |
| void CreateAgent( | |
| mojo::PendingRemote<blink::mojom::AnnotationAgentHost> host_remote, | |
| mojo::PendingReceiver<blink::mojom::AnnotationAgent> agent_receiver, | |
| blink::mojom::AnnotationType type, | |
| blink::mojom::SelectorPtr selector, | |
| std::optional<int> search_range_start_node_id) override; | |
| void CreateAgentFromSelection( | |
| blink::mojom::AnnotationType type, | |
| CreateAgentFromSelectionCallback callback) override; | |
| void RemoveAgentsOfType(blink::mojom::AnnotationType type) override; | |
| // Initializes the plugin for testing, bypassing certain consistency checks. | |
| bool InitializeForTesting(); | |
| const gfx::Rect& GetPluginRectForTesting() const { return plugin_rect_; } | |
| float GetDeviceScaleForTesting() const { return device_scale_; } | |
| void SendThumbnailForTesting(base::DictValue reply, | |
| int page_index, | |
| Thumbnail thumbnail); | |
| DocumentLoadState document_load_state_for_testing() const { | |
| return document_load_state_; | |
| } | |
| void set_cursor_type_for_testing(ui::mojom::CursorType cursor_type) { | |
| cursor_ = cursor_type; | |
| } | |
| const ui::Cursor& cursor_for_testing() const { return cursor_; } | |
| int GetContentRestrictionsForTesting() const { | |
| return GetContentRestrictions(); | |
| } | |
| std::unique_ptr<AccessibilityDocInfo> GetAccessibilityDocInfoForTesting() | |
| const { | |
| return GetAccessibilityDocInfo(); | |
| } | |
| int32_t next_accessibility_page_index_for_testing() const { | |
| return next_accessibility_page_index_; | |
| } | |
| void set_next_accessibility_page_index_for_testing(int32_t index) { | |
| next_accessibility_page_index_ = index; | |
| } | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| PdfInkModuleClient* ink_module_client_for_testing() { | |
| return ink_module_client_.get(); | |
| } | |
| void set_in_paint_for_testing(bool in_paint) { in_paint_ = in_paint; } | |
| const std::vector<gfx::Rect>& deferred_invalidates_for_testing() const { | |
| return deferred_invalidates_; | |
| } | |
| bool HasInkInputsSnapshotForTesting() const { | |
| return snapshot_ink_inputs_.has_value(); | |
| } | |
| #endif // BUILDFLAG(ENABLE_PDF_INK2) | |
| void SetMaxSaveBufferSizeForTesting(uint32_t max_save_buffer_size) { | |
| max_save_buffer_size_ = max_save_buffer_size; | |
| } | |
| bool IsSaveDataBufferEmptyForTesting() const { | |
| return save_data_buffer_.empty(); | |
| } | |
| #if BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| size_t GetSaveToDriveBufferHandlerReceiverSizeForTesting() const { | |
| return save_data_buffer_handler_receivers_.size(); | |
| } | |
| #endif // BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| // Returns the size of the original PDF file. It does not handle files larger | |
| // than INT_MAX. If the file is larger than that, it returns 0. | |
| uint32_t GetOriginalFileSize(); | |
| // Returns a block of data from the original PDF file. | |
| std::vector<uint8_t> GetOriginalFileData(uint32_t offset, | |
| uint32_t block_size); | |
| // Populates `buffer` with the modified PDF file data. | |
| // The `buffer` is released if the file is larger than `INT_MAX`. | |
| void PopulateBufferWithModifiedFileData(std::vector<uint8_t>& buffer); | |
| // Gets a block of modified PDF data from `buffer`. | |
| // `PopulateBufferWithModifiedFileData()` must be called first to populate | |
| // `buffer`. | |
| std::vector<uint8_t> GetModifiedFileDataFromBuffer( | |
| base::span<const uint8_t> buffer, | |
| uint32_t offset, | |
| uint32_t block_size); | |
| private: | |
| // Callback that runs after `LoadUrl()`. The `loader` is the loader used to | |
| // load the URL, and `result` is the result code for the load. | |
| using LoadUrlCallback = | |
| base::OnceCallback<void(std::unique_ptr<UrlLoader> loader, | |
| Result result)>; | |
| enum class AccessibilityState { | |
| kOff = 0, // Off. | |
| kPending, // Enabled but waiting for doc to load. | |
| kLoaded, // Fully loaded. | |
| }; | |
| struct BackgroundPart { | |
| gfx::Rect location; | |
| uint32_t color; | |
| }; | |
| // Metadata about an available preview page. | |
| struct PreviewPageInfo { | |
| // Data source URL. | |
| std::string url; | |
| // Page index in destination document. | |
| int dest_page_index = -1; | |
| }; | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| class PdfInkModuleClientImpl; | |
| #endif // BUILDFLAG(ENABLE_PDF_INK2) | |
| // Call `Destroy()` instead. | |
| ~PdfViewWebPlugin() override; | |
| bool InitializeCommon(); | |
| // Sends whether to do smooth scrolling. | |
| void SendSetSmoothScrolling(); | |
| // Handles `LoadUrl()` result for the main document. | |
| void DidOpen(std::unique_ptr<UrlLoader> loader, Result result); | |
| // Updates the scroll position, which is in CSS pixels relative to the | |
| // top-left corner. | |
| void UpdateScroll(const gfx::PointF& scroll_position); | |
| // Loads `url`, invoking `callback` on receiving the initial response. | |
| void LoadUrl(std::string_view url, LoadUrlCallback callback); | |
| // Handles `Open()` result for `form_loader_`. | |
| void DidFormOpen(Result result); | |
| // Sends start/stop loading notifications to the plugin's render frame. | |
| void DidStartLoading(); | |
| void DidStopLoading(); | |
| // Gets the content restrictions based on the permissions which `engine_` has. | |
| int GetContentRestrictions() const; | |
| // Message handlers. | |
| void HandleDisplayAnnotationsMessage(const base::DictValue& message); | |
| void HandleGetNamedDestinationMessage(const base::DictValue& message); | |
| void HandleGetPageBoundingBoxMessage(const base::DictValue& message); | |
| void HandleGetPasswordCompleteMessage(const base::DictValue& message); | |
| void HandleGetSaveDataBlockMessage(const base::DictValue& message); | |
| void HandleGetSelectedTextMessage(const base::DictValue& message); | |
| void HandleGetSuggestedFileName(const base::DictValue& message); | |
| void HandleGetThumbnailMessage(const base::DictValue& message); | |
| void HandleHighlightSearchTermsMessage(const base::DictValue& message); | |
| void HandleHighlightTextFragmentsMessage(const base::DictValue& message); | |
| void HandlePrintMessage(const base::DictValue& /*message*/); | |
| void HandleReleaseSaveInBlockBuffers(const base::DictValue& /*message*/); | |
| void HandleRotateClockwiseMessage(const base::DictValue& /*message*/); | |
| void HandleRotateCounterclockwiseMessage(const base::DictValue& /*message*/); | |
| void HandleSaveAttachmentMessage(const base::DictValue& message); | |
| void HandleSaveMessage(const base::DictValue& message); | |
| void HandleSelectAllMessage(const base::DictValue& /*message*/); | |
| void HandleSetBackgroundColorMessage(const base::DictValue& message); | |
| void HandleSetPresentationModeMessage(const base::DictValue& message); | |
| void HandleSetTwoUpViewMessage(const base::DictValue& message); | |
| void HandleStopScrollingMessage(const base::DictValue& message); | |
| void HandleViewportMessage(const base::DictValue& message); | |
| void SaveToBuffer(pdf::mojom::SaveRequestType request_type, | |
| const std::string& token); | |
| void SaveToFile(const std::string& token); | |
| // For a call in `SaveBlockToBuffer()`, ensures `offset` and `block_size` have | |
| // expected values and returns the effective `block_size`. | |
| uint32_t VerifyParamsAndGetSaveBlockSize(uint32_t total_file_size, | |
| uint32_t offset, | |
| uint32_t block_size); | |
| // Returns `block_size` bytes to save the PDF with `request_type`, starting | |
| // from location `offset`. Since the caller may not know the exact file size, | |
| // the first request (when `offset` is 0) can be called with `block_size` 0 | |
| // and in that case, the entire file data, capped at 16MB limit is returned. | |
| // The function also returns the total file size. | |
| // Note that it only handles files less than INT_MAX size, and if the file is | |
| // larger than that, it returns 0 as file size and no data. | |
| SaveDataBlock SaveBlockToBuffer(pdf::mojom::SaveRequestType request_type, | |
| uint32_t offset, | |
| uint32_t block_size); | |
| // Sets whether the plugin can and should handle the save by using `pdf_host_` | |
| // to notify the browser. Prevents duplicate notifications to the browser if | |
| // the state has not changed. | |
| void SetPluginCanSave(bool can_save); | |
| // Converts a scroll offset (which is relative to a UI direction-dependent | |
| // scroll origin) to a scroll position (which is always relative to the | |
| // top-left corner). | |
| gfx::PointF GetScrollPositionFromOffset( | |
| const gfx::Vector2dF& scroll_offset) const; | |
| // Paints the given invalid area of the plugin to the given graphics device. | |
| // PaintManager::Client::OnPaint() should be its only caller. | |
| void DoPaint(const std::vector<gfx::Rect>& paint_rects, | |
| std::vector<PaintReadyRect>& ready, | |
| std::vector<gfx::Rect>& pending); | |
| // The preparation when painting on the image data buffer for the first | |
| // time. | |
| void PrepareForFirstPaint(std::vector<PaintReadyRect>& ready); | |
| // Updates the available area and the background parts, notifies the PDF | |
| // engine, and updates the accessibility information. | |
| void OnGeometryChanged(double old_zoom, float old_device_scale); | |
| // A helper of OnGeometryChanged() which updates the available area and | |
| // the background parts, and notifies the PDF engine of geometry changes. | |
| void RecalculateAreas(double old_zoom, float old_device_scale); | |
| // Figures out the location of any background rectangles (i.e. those that | |
| // aren't painted by the PDF engine). | |
| void CalculateBackgroundParts(); | |
| // Computes document width/height in device pixels, based on current zoom and | |
| // device scale | |
| int GetDocumentPixelWidth() const; | |
| int GetDocumentPixelHeight() const; | |
| // Schedules invalidation tasks after painting finishes. | |
| void InvalidateAfterPaintDone(); | |
| // Callback to clear deferred invalidates after painting finishes. | |
| void ClearDeferredInvalidates(); | |
| // Recalculates values that depend on scale factors. | |
| void UpdateScaledValues(); | |
| void OnViewportChanged(const gfx::Rect& new_plugin_rect_in_css_pixel, | |
| float new_device_scale); | |
| // Text editing methods. | |
| bool SelectAll(); | |
| bool Cut(); | |
| bool Paste(const blink::WebString& value); | |
| bool Undo(); | |
| bool Redo(); | |
| bool SetFocusedFormTextDirection(base::i18n::TextDirection direction); | |
| bool HandleWebInputEvent(const blink::WebInputEvent& event); | |
| // Helper method for converting IME text to input events. | |
| // TODO(crbug.com/40199248): Consider handling composition events. | |
| void HandleImeCommit(const blink::WebString& text); | |
| // Callback to print without re-entrancy issues. The callback prevents the | |
| // invocation of printing in the middle of an event handler, which is risky; | |
| // see crbug.com/66334. | |
| // TODO(crbug.com/40185029): Re-evaluate the need for a callback when parts of | |
| // the plugin are moved off the main thread. | |
| void OnInvokePrintDialog(); | |
| void ResetRecentlySentFindUpdate(); | |
| // Records metrics about the document metadata. | |
| void RecordDocumentMetrics(); | |
| // Sends the attachments data to the frontend. | |
| void SendAttachments(); | |
| // Sends the bookmarks data to the frontend. | |
| void SendBookmarks(); | |
| // Notifies the frontend that `edit_command` got executed. | |
| void SendExecutedEditCommand(std::string_view edit_command); | |
| // Notifies the frontend that find-in-page started. | |
| void SendStartedFindInPage(); | |
| // Sends document metadata data to the frontend. | |
| void SendMetadata(); | |
| // Sends the loading progress, where `percentage` represents the progress, or | |
| // -1 for loading error. | |
| void SendLoadingProgress(double percentage); | |
| // Handles message for resetting Print Preview. | |
| void HandleResetPrintPreviewModeMessage(const base::DictValue& message); | |
| // Handles message for loading a preview page. | |
| void HandleLoadPreviewPageMessage(const base::DictValue& message); | |
| // Starts loading the next available preview page into a blank page. | |
| void LoadAvailablePreviewPage(); | |
| // Handles `LoadUrl()` result for a preview page. | |
| void DidOpenPreview(std::unique_ptr<UrlLoader> loader, Result result); | |
| // Continues loading the next preview page. | |
| void LoadNextPreviewPage(); | |
| // Sends a notification that the print preview has loaded. | |
| void SendPrintPreviewLoadedNotification(); | |
| // Sends the thumbnail image data. | |
| void SendThumbnail(base::DictValue reply, | |
| int page_index, | |
| Thumbnail thumbnail); | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| static std::unique_ptr<PdfInkModuleClient> MaybeCreatePdfInkModuleClient( | |
| PdfViewWebPlugin& plugin); | |
| static std::unique_ptr<PdfInkModule> MaybeCreatePdfInkModule( | |
| PdfInkModuleClient* client); | |
| #endif | |
| // Converts `frame_coordinates` to PDF coordinates. | |
| gfx::Point FrameToPdfCoordinates(const gfx::PointF& frame_coordinates) const; | |
| // Gets the accessibility doc info based on the information from `engine_`. | |
| // The return value is never nullptr. | |
| std::unique_ptr<AccessibilityDocInfo> GetAccessibilityDocInfo() const; | |
| // Sets the accessibility information about the given `page_index` in the | |
| // renderer. | |
| void PrepareAndSetAccessibilityPageInfo(int32_t page_index); | |
| // Prepares the accessibility information about the current viewport. This is | |
| // done once when accessibility is first loaded and again when the geometry | |
| // changes. | |
| void PrepareAndSetAccessibilityViewportInfo(); | |
| // Starts loading accessibility information. | |
| void LoadAccessibility(); | |
| // Applies the initial renderer preferences and observes future updates. | |
| void ApplyAndObserveRendererPreferences(); | |
| #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE) | |
| // Triggered to show/hide Searchify progress indicator. | |
| void SetShowSearchifyInProgress(bool show); | |
| #endif | |
| bool initialized_ = false; | |
| bool plugin_can_save_ = false; | |
| blink::WebString selected_text_; | |
| std::unique_ptr<Client> const client_; | |
| // Used to access the services provided by the browser. | |
| mojo::AssociatedRemote<pdf::mojom::PdfHost> const pdf_host_; | |
| mojo::Receiver<pdf::mojom::PdfListener> listener_receiver_{this}; | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| // Both are non-null if `features::kPdfInk2` is enabled. | |
| // Both are null if `features::kPdfInk2` is disabled. | |
| // | |
| // `ink_module_client_` must outlive `ink_module_`. | |
| std::unique_ptr<PdfInkModuleClient> const ink_module_client_; | |
| std::unique_ptr<PdfInkModule> const ink_module_; | |
| #endif | |
| std::unique_ptr<PDFiumEngine> engine_; | |
| // The URL of the PDF document. | |
| std::string url_; | |
| // The callback for receiving the password from the page. | |
| base::OnceCallback<void(const std::string&)> password_callback_; | |
| // The current cursor type. | |
| ui::Cursor cursor_ = ui::mojom::CursorType::kPointer; | |
| blink::WebTextInputType text_input_type_ = | |
| blink::WebTextInputType::kWebTextInputTypeNone; | |
| gfx::Rect caret_rect_; | |
| // The selection bounding box in pixels, with a top-left origin relative to | |
| // the plugin. | |
| gfx::Rect selection_bounding_box_; | |
| blink::WebString composition_text_; | |
| blink::WebPluginParams initial_params_; | |
| v8::Persistent<v8::Object> scriptable_receiver_; | |
| PaintManager paint_manager_{this}; | |
| // Image data buffer for painting. | |
| SkBitmap image_data_; | |
| // The current image snapshot. | |
| cc::PaintImage snapshot_; | |
| #if BUILDFLAG(ENABLE_PDF_INK2) | |
| // The last saved image snapshot for rendering of Ink inputs. | |
| std::optional<cc::PaintImage> snapshot_ink_inputs_; | |
| // Tracks if `snapshot_` still needs to be updated to reflect a newly | |
| // added Ink stroke. | |
| bool snapshot_needs_update_for_ink_input_ = false; | |
| #endif | |
| // Translate from snapshot to device pixels. | |
| gfx::Vector2dF snapshot_translate_; | |
| // Scale from snapshot to device pixels. | |
| float snapshot_scale_ = 1.0f; | |
| // The viewport coordinates to DIP (device-independent pixel) ratio. | |
| float viewport_to_dip_scale_ = 1.0f; | |
| // Combined translate from snapshot to device to CSS pixels. | |
| gfx::Vector2dF total_translate_; | |
| // The plugin rect in CSS pixels. | |
| gfx::Rect css_plugin_rect_; | |
| // True if the plugin occupies the entire frame (not embedded). | |
| bool full_frame_ = false; | |
| // The background color of the PDF viewer. | |
| SkColor background_color_ = SK_ColorTRANSPARENT; | |
| // Size, in DIPs, of plugin rectangle. | |
| gfx::Size plugin_dip_size_; | |
| // The plugin rectangle in device pixels. | |
| gfx::Rect plugin_rect_; | |
| // Remaining area, in pixels, to render the PDF in after accounting for | |
| // horizontal centering. | |
| gfx::Rect available_area_; | |
| // Current zoom factor. | |
| double zoom_ = 1.0; | |
| // Current device scale factor. Multiply by `device_scale_` to convert from | |
| // viewport to screen coordinates. Divide by `device_scale_` to convert from | |
| // screen to viewport coordinates. | |
| float device_scale_ = 1.0f; | |
| // Use Skia when set to true, or AGG when set to false. | |
| bool use_skia_renderer_ = false; | |
| // True if we haven't painted the plugin viewport yet. | |
| bool first_paint_ = true; | |
| // Whether OnPaint() is in progress or not. | |
| bool in_paint_ = false; | |
| // True if last bitmap was smaller than the screen. | |
| bool last_bitmap_smaller_ = false; | |
| // True if we request a new bitmap rendering. | |
| bool needs_reraster_ = true; | |
| // The size of the entire document in pixels (i.e. if each page is 800 pixels | |
| // high and there are 10 pages, the height will be 8000). | |
| gfx::Size document_size_; | |
| std::vector<BackgroundPart> background_parts_; | |
| // Deferred invalidates while `in_paint_` is true. | |
| std::vector<gfx::Rect> deferred_invalidates_; | |
| // The UI direction. | |
| base::i18n::TextDirection ui_direction_ = base::i18n::UNKNOWN_DIRECTION; | |
| // The scroll offset for the last raster in CSS pixels, before any | |
| // transformations are applied. | |
| gfx::Vector2dF scroll_offset_at_last_raster_; | |
| // If this is true, then don't scroll the plugin in response to calls to | |
| // `UpdateScroll()`. This will be true when the extension page is in the | |
| // process of zooming the plugin so that flickering doesn't occur while | |
| // zooming. | |
| bool stop_scrolling_ = false; | |
| // Whether the plugin has received a viewport changed message. Nothing should | |
| // be painted until this is received. | |
| bool received_viewport_message_ = false; | |
| // If true, the render frame has been notified that we're starting a network | |
| // request so that it can start the throbber. It will be notified again once | |
| // the document finishes loading. | |
| bool did_call_start_loading_ = false; | |
| // The last document load progress value sent to the web page. | |
| double last_progress_sent_ = 0.0; | |
| // The current state of document load. | |
| DocumentLoadState document_load_state_ = DocumentLoadState::kLoading; | |
| // The current state of accessibility. | |
| AccessibilityState accessibility_state_ = AccessibilityState::kOff; | |
| // The next accessibility page index, used to track interprocess calls when | |
| // reconstructing the tree for new document layouts. | |
| int32_t next_accessibility_page_index_ = 0; | |
| // Used for submitting forms. | |
| std::unique_ptr<UrlLoader> form_loader_; | |
| // Handler for accessibility data updates. | |
| std::unique_ptr<PdfAccessibilityDataHandler> pdf_accessibility_data_handler_; | |
| // The URL currently under the cursor. | |
| std::string link_under_cursor_; | |
| // The ID of the current find operation, or -1 if no current operation is | |
| // present. | |
| int find_identifier_ = -1; | |
| // Whether an update to the number of find results found was sent less than | |
| // `kFindResultCooldown` TimeDelta ago. | |
| bool recently_sent_find_update_ = false; | |
| // Stores the tickmarks to be shown for the current find results. | |
| std::vector<gfx::Rect> tickmarks_; | |
| // Whether the document is in edit mode. | |
| bool edit_mode_ = false; | |
| // Only instantiated when not print previewing. | |
| std::unique_ptr<MetricsHandler> metrics_handler_; | |
| // Keeps track of which unsupported features have been reported to avoid | |
| // spamming the metrics if a feature shows up many times per document. | |
| base::flat_set<std::string> unsupported_features_reported_; | |
| // The metafile in which to save the printed output. Assigned a value only | |
| // between `PrintBegin()` and `PrintEnd()` calls. | |
| raw_ptr<printing::MetafileSkia> printing_metafile_ = nullptr; | |
| // The indices of pages to print. | |
| std::vector<int> pages_to_print_; | |
| // Assigned a value only between `PrintBegin()` and `PrintEnd()` calls. | |
| std::optional<blink::WebPrintParams> print_params_; | |
| // For identifying actual print operations to avoid double logging of UMA. | |
| bool print_pages_called_; | |
| // Whether the plugin is loaded in Print Preview. | |
| bool is_print_preview_ = false; | |
| // Number of pages in Print Preview (non-PDF). 0 if previewing a PDF, and -1 | |
| // if not in Print Preview. | |
| int print_preview_page_count_ = -1; | |
| // Number of pages loaded in Print Preview (non-PDF). Always less than or | |
| // equal to `print_preview_page_count_`. | |
| int print_preview_loaded_page_count_ = -1; | |
| // The PreviewModeClient used for print preview. Will be passed to | |
| // `preview_engine_`. | |
| std::unique_ptr<PreviewModeClient> preview_client_; | |
| // Engine used to render individual preview pages. This will use the | |
| // `PreviewModeClient` interface. | |
| std::unique_ptr<PDFiumEngine> preview_engine_; | |
| // Document load state for the Print Preview engine. | |
| DocumentLoadState preview_document_load_state_ = DocumentLoadState::kComplete; | |
| // Queue of available preview pages to load next. | |
| base::queue<PreviewPageInfo> preview_pages_info_; | |
| // Buffer for saving data by Web UI. | |
| // `SaveBlockToBuffer` allocates this variable when WebUI requests saving the | |
| // PDF by getting the content in blocks, and the content needs to be buffered | |
| // during the save process. It is released when saving is finished or | |
| // canceled. | |
| std::vector<uint8_t> save_data_buffer_; | |
| // Maximum size of save data in each block. | |
| uint32_t max_save_buffer_size_; | |
| #if BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| // Owns all the receivers for `pdf::mojom::SaveDataBufferHandler`. Each | |
| // receiver is associated with a Save to Drive handler and is used to read the | |
| // corresponding data request. Once a receiver is destroyed, the corresponding | |
| // buffer and handler are freed. | |
| mojo::UniqueReceiverSet<pdf::mojom::SaveDataBufferHandler> | |
| save_data_buffer_handler_receivers_; | |
| #endif // BUILDFLAG(ENABLE_PDF_SAVE_TO_DRIVE) | |
| // Used to allow the embedder to scroll-to and highlight a text fragment. | |
| mojo::Receiver<blink::mojom::AnnotationAgentContainer> | |
| annotation_agent_container_receiver_{this}; | |
| std::unique_ptr<PdfAnnotationAgent> annotation_agent_; | |
| #if BUILDFLAG(ENABLE_SCREEN_AI_SERVICE) | |
| enum class SearchifyState { | |
| kNotStarted, | |
| kStarted, | |
| kShowingInProgress, | |
| kStopped, | |
| }; | |
| SearchifyState searchify_state_ = SearchifyState::kNotStarted; | |
| #endif | |
| base::WeakPtrFactory<PdfViewWebPlugin> weak_factory_{this}; | |
| }; | |
| } // namespace chrome_pdf | |
| #endif // PDF_PDF_VIEW_WEB_PLUGIN_H_ |