OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 5 #ifndef COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
6 #define COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 6 #define COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "content/public/renderer/render_view_observer.h" | 18 #include "content/public/renderer/render_frame_observer.h" |
19 #include "content/public/renderer/render_view_observer_tracker.h" | 19 #include "content/public/renderer/render_frame_observer_tracker.h" |
20 #include "printing/features/features.h" | 20 #include "printing/features/features.h" |
21 #include "printing/pdf_metafile_skia.h" | 21 #include "printing/pdf_metafile_skia.h" |
22 #include "third_party/WebKit/public/platform/WebCanvas.h" | 22 #include "third_party/WebKit/public/platform/WebCanvas.h" |
23 #include "third_party/WebKit/public/web/WebNode.h" | 23 #include "third_party/WebKit/public/web/WebNode.h" |
24 #include "third_party/WebKit/public/web/WebPrintParams.h" | 24 #include "third_party/WebKit/public/web/WebPrintParams.h" |
25 #include "ui/gfx/geometry/size.h" | 25 #include "ui/gfx/geometry/size.h" |
26 | 26 |
27 struct PrintMsg_Print_Params; | 27 struct PrintMsg_Print_Params; |
28 struct PrintMsg_PrintPage_Params; | 28 struct PrintMsg_PrintPage_Params; |
29 struct PrintMsg_PrintPages_Params; | 29 struct PrintMsg_PrintPages_Params; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 | 71 |
72 private: | 72 private: |
73 blink::WebView* view_; | 73 blink::WebView* view_; |
74 blink::WebLocalFrame* frame_; | 74 blink::WebLocalFrame* frame_; |
75 }; | 75 }; |
76 | 76 |
77 // PrintWebViewHelper handles most of the printing grunt work for RenderView. | 77 // PrintWebViewHelper handles most of the printing grunt work for RenderView. |
78 // We plan on making print asynchronous and that will require copying the DOM | 78 // We plan on making print asynchronous and that will require copying the DOM |
79 // of the document and creating a new WebView with the contents. | 79 // of the document and creating a new WebView with the contents. |
80 class PrintWebViewHelper | 80 class PrintWebViewHelper |
81 : public content::RenderViewObserver, | 81 : public content::RenderFrameObserver, |
82 public content::RenderViewObserverTracker<PrintWebViewHelper> { | 82 public content::RenderFrameObserverTracker<PrintWebViewHelper> { |
83 public: | 83 public: |
84 class Delegate { | 84 class Delegate { |
85 public: | 85 public: |
86 virtual ~Delegate() {} | 86 virtual ~Delegate() {} |
87 | 87 |
88 // Cancels prerender if it's currently in progress and returns |true| if | 88 // Cancels prerender if it's currently in progress and returns true if the |
89 // the cancellation was done with success. | 89 // cancellation succeeded. |
90 virtual bool CancelPrerender(content::RenderView* render_view, | 90 virtual bool CancelPrerender(content::RenderFrame* render_frame) = 0; |
91 int routing_id) = 0; | |
92 | 91 |
93 // Returns the element to be printed. Returns a null WebElement if | 92 // Returns the element to be printed. Returns a null WebElement if |
94 // a pdf plugin element can't be extracted from the frame. | 93 // a pdf plugin element can't be extracted from the frame. |
95 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; | 94 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; |
96 | 95 |
97 virtual bool IsPrintPreviewEnabled() = 0; | 96 virtual bool IsPrintPreviewEnabled() = 0; |
98 | 97 |
99 // If true, the user can be asked to provide print settings. | 98 // If true, the user can be asked to provide print settings. |
100 // The default implementation returns |true|. | 99 // The default implementation returns |true|. |
101 virtual bool IsAskPrintSettingsEnabled(); | 100 virtual bool IsAskPrintSettingsEnabled(); |
102 | 101 |
103 // If false, window.print() won't do anything. | 102 // If false, window.print() won't do anything. |
104 // The default implementation returns |true|. | 103 // The default implementation returns |true|. |
105 virtual bool IsScriptedPrintEnabled(); | 104 virtual bool IsScriptedPrintEnabled(); |
106 | 105 |
107 // Returns true if printing is overridden and the default behavior should be | 106 // Returns true if printing is overridden and the default behavior should be |
108 // skipped for |frame|. | 107 // skipped for |frame|. |
109 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; | 108 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; |
110 }; | 109 }; |
111 | 110 |
112 PrintWebViewHelper(content::RenderView* render_view, | 111 PrintWebViewHelper(content::RenderFrame* render_frame, |
113 std::unique_ptr<Delegate> delegate); | 112 std::unique_ptr<Delegate> delegate); |
114 ~PrintWebViewHelper() override; | 113 ~PrintWebViewHelper() override; |
115 | 114 |
116 // Minimum valid value for scaling. Since scaling is originally an integer | 115 // Minimum valid value for scaling. Since scaling is originally an integer |
117 // representing a percentage, it should never be less than this if it is | 116 // representing a percentage, it should never be less than this if it is |
118 // valid. | 117 // valid. |
119 static constexpr double kEpsilon = 0.01f; | 118 static constexpr double kEpsilon = 0.01f; |
120 | 119 |
121 // Disable print preview and switch to system dialog printing even if full | 120 // Disable print preview and switch to system dialog printing even if full |
122 // printing is build-in. This method is used by CEF. | 121 // printing is build-in. This method is used by CEF. |
123 static void DisablePreview(); | 122 static void DisablePreview(); |
124 | 123 |
125 bool IsPrintingEnabled(); | 124 bool IsPrintingEnabled() const; |
126 | 125 |
127 void PrintNode(const blink::WebNode& node); | 126 void PrintNode(const blink::WebNode& node); |
128 | 127 |
129 private: | 128 private: |
130 friend class PrintWebViewHelperTestBase; | 129 friend class PrintWebViewHelperTestBase; |
131 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, | 130 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, |
132 BlockScriptInitiatedPrinting); | 131 BlockScriptInitiatedPrinting); |
133 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); | 132 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); |
134 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, | 133 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, |
135 BlockScriptInitiatedPrinting); | 134 BlockScriptInitiatedPrinting); |
(...skipping 25 matching lines...) Expand all Loading... |
161 PREVIEW_ERROR_LAST_ENUM // Always last. | 160 PREVIEW_ERROR_LAST_ENUM // Always last. |
162 }; | 161 }; |
163 | 162 |
164 enum PrintPreviewRequestType { | 163 enum PrintPreviewRequestType { |
165 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, | 164 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, |
166 PRINT_PREVIEW_USER_INITIATED_SELECTION, | 165 PRINT_PREVIEW_USER_INITIATED_SELECTION, |
167 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, | 166 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, |
168 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). | 167 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). |
169 }; | 168 }; |
170 | 169 |
171 // RenderViewObserver implementation. | 170 // RenderFrameObserver implementation. |
| 171 void OnDestruct() override; |
| 172 void DidStartProvisionalLoad() override; |
| 173 void DidFailProvisionalLoad(const blink::WebURLError& error) override; |
| 174 void DidFinishLoad() override; |
| 175 void ScriptedPrint(bool user_initiated) override; |
172 bool OnMessageReceived(const IPC::Message& message) override; | 176 bool OnMessageReceived(const IPC::Message& message) override; |
173 void PrintPage(blink::WebLocalFrame* frame, bool user_initiated) override; | |
174 void DidStartLoading() override; | |
175 void DidStopLoading() override; | |
176 void OnDestruct() override; | |
177 | 177 |
178 // Message handlers --------------------------------------------------------- | 178 // Message handlers --------------------------------------------------------- |
179 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 179 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
180 void OnPrintPages(); | 180 void OnPrintPages(); |
181 void OnPrintForSystemDialog(); | 181 void OnPrintForSystemDialog(); |
182 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); | 182 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); |
183 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 183 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
184 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 184 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
185 void OnInitiatePrintPreview(bool selection_only); | 185 void OnInitiatePrintPreview(bool has_selection); |
186 void OnPrintPreview(const base::DictionaryValue& settings); | 186 void OnPrintPreview(const base::DictionaryValue& settings); |
187 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 187 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
188 void OnPrintingDone(bool success); | 188 void OnPrintingDone(bool success); |
189 | 189 |
190 // Get |page_size| and |content_area| information from | 190 // Get |page_size| and |content_area| information from |
191 // |page_layout_in_points|. | 191 // |page_layout_in_points|. |
192 void GetPageSizeAndContentAreaFromPageLayout( | 192 void GetPageSizeAndContentAreaFromPageLayout( |
193 const PageSizeMargins& page_layout_in_points, | 193 const PageSizeMargins& page_layout_in_points, |
194 gfx::Size* page_size, | 194 gfx::Size* page_size, |
195 gfx::Rect* content_area); | 195 gfx::Rect* content_area); |
(...skipping 13 matching lines...) Expand all Loading... |
209 | 209 |
210 // Renders a print preview page. |page_number| is 0-based. | 210 // Renders a print preview page. |page_number| is 0-based. |
211 // Returns true if print preview should continue, false on failure. | 211 // Returns true if print preview should continue, false on failure. |
212 bool RenderPreviewPage(int page_number, | 212 bool RenderPreviewPage(int page_number, |
213 const PrintMsg_Print_Params& print_params); | 213 const PrintMsg_Print_Params& print_params); |
214 | 214 |
215 // Finalize the print ready preview document. | 215 // Finalize the print ready preview document. |
216 bool FinalizePrintReadyDocument(); | 216 bool FinalizePrintReadyDocument(); |
217 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 217 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
218 | 218 |
219 // Enable/Disable window.print calls. If |blocked| is true window.print | 219 // Enable/Disable printing. |
220 // calls will silently fail. Call with |blocked| set to false to reenable. | 220 void OnSetPrintingEnabled(bool enabled); |
221 void SetScriptedPrintBlocked(bool blocked); | |
222 | 221 |
223 // Main printing code ------------------------------------------------------- | 222 // Main printing code ------------------------------------------------------- |
224 | 223 |
225 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 224 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
226 // |is_scripted| should be true when the call is coming from window.print() | 225 // |is_scripted| should be true when the call is coming from window.print() |
227 void Print(blink::WebLocalFrame* frame, | 226 void Print(blink::WebLocalFrame* frame, |
228 const blink::WebNode& node, | 227 const blink::WebNode& node, |
229 bool is_scripted); | 228 bool is_scripted); |
230 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 229 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
231 | 230 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 // and footers using strings from |header_footer_info| on to the canvas. | 333 // and footers using strings from |header_footer_info| on to the canvas. |
335 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, | 334 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, |
336 int page_number, | 335 int page_number, |
337 int total_pages, | 336 int total_pages, |
338 const blink::WebFrame& source_frame, | 337 const blink::WebFrame& source_frame, |
339 float webkit_scale_factor, | 338 float webkit_scale_factor, |
340 const PageSizeMargins& page_layout_in_points, | 339 const PageSizeMargins& page_layout_in_points, |
341 const PrintMsg_Print_Params& params); | 340 const PrintMsg_Print_Params& params); |
342 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 341 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
343 | 342 |
344 bool GetPrintFrame(blink::WebLocalFrame** frame); | |
345 | |
346 // Script Initiated Printing ------------------------------------------------ | 343 // Script Initiated Printing ------------------------------------------------ |
347 | 344 |
348 // Return true if script initiated printing is currently | 345 // Return true if script initiated printing is currently |
349 // allowed. |user_initiated| should be true when a user event triggered the | 346 // allowed. |user_initiated| should be true when a user event triggered the |
350 // script, most likely by pressing a print button on the page. | 347 // script, most likely by pressing a print button on the page. |
351 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, | 348 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, |
352 bool user_initiated); | 349 bool user_initiated); |
353 | 350 |
354 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 351 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
355 // Shows scripted print preview when options from plugin are available. | 352 // Shows scripted print preview when options from plugin are available. |
(...skipping 16 matching lines...) Expand all Loading... |
372 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); | 369 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); |
373 | 370 |
374 // WebView used only to print the selection. | 371 // WebView used only to print the selection. |
375 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | 372 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; |
376 bool reset_prep_frame_view_; | 373 bool reset_prep_frame_view_; |
377 | 374 |
378 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | 375 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; |
379 bool is_print_ready_metafile_sent_; | 376 bool is_print_ready_metafile_sent_; |
380 bool ignore_css_margins_; | 377 bool ignore_css_margins_; |
381 | 378 |
382 // Used for scripted initiated printing blocking. | 379 bool is_printing_enabled_; |
383 bool is_scripted_printing_blocked_; | |
384 | 380 |
385 // Let the browser process know of a printing failure. Only set to false when | 381 // Let the browser process know of a printing failure. Only set to false when |
386 // the failure came from the browser in the first place. | 382 // the failure came from the browser in the first place. |
387 bool notify_browser_of_print_failure_; | 383 bool notify_browser_of_print_failure_; |
388 | 384 |
389 // True, when printing from print preview. | 385 // True, when printing from print preview. |
390 bool print_for_preview_; | 386 bool print_for_preview_; |
391 | 387 |
392 // Used to check the prerendering status. | 388 // Used to check the prerendering status. |
393 const std::unique_ptr<Delegate> delegate_; | 389 const std::unique_ptr<Delegate> delegate_; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 base::Closure on_stop_loading_closure_; | 528 base::Closure on_stop_loading_closure_; |
533 | 529 |
534 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; | 530 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; |
535 | 531 |
536 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 532 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
537 }; | 533 }; |
538 | 534 |
539 } // namespace printing | 535 } // namespace printing |
540 | 536 |
541 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 537 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
OLD | NEW |