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_frame_observer.h" | 18 #include "content/public/renderer/render_view_observer.h" |
19 #include "content/public/renderer/render_frame_observer_tracker.h" | 19 #include "content/public/renderer/render_view_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::RenderFrameObserver, | 81 : public content::RenderViewObserver, |
82 public content::RenderFrameObserverTracker<PrintWebViewHelper> { | 82 public content::RenderViewObserverTracker<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 the | 88 // Cancels prerender if it's currently in progress and returns |true| if |
89 // cancellation succeeded. | 89 // the cancellation was done with success. |
90 virtual bool CancelPrerender(content::RenderFrame* render_frame) = 0; | 90 virtual bool CancelPrerender(content::RenderView* render_view, |
| 91 int routing_id) = 0; |
91 | 92 |
92 // Returns the element to be printed. Returns a null WebElement if | 93 // Returns the element to be printed. Returns a null WebElement if |
93 // a pdf plugin element can't be extracted from the frame. | 94 // a pdf plugin element can't be extracted from the frame. |
94 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; | 95 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; |
95 | 96 |
96 virtual bool IsPrintPreviewEnabled() = 0; | 97 virtual bool IsPrintPreviewEnabled() = 0; |
97 | 98 |
98 // If true, the user can be asked to provide print settings. | 99 // If true, the user can be asked to provide print settings. |
99 // The default implementation returns |true|. | 100 // The default implementation returns |true|. |
100 virtual bool IsAskPrintSettingsEnabled(); | 101 virtual bool IsAskPrintSettingsEnabled(); |
101 | 102 |
102 // If false, window.print() won't do anything. | 103 // If false, window.print() won't do anything. |
103 // The default implementation returns |true|. | 104 // The default implementation returns |true|. |
104 virtual bool IsScriptedPrintEnabled(); | 105 virtual bool IsScriptedPrintEnabled(); |
105 | 106 |
106 // Returns true if printing is overridden and the default behavior should be | 107 // Returns true if printing is overridden and the default behavior should be |
107 // skipped for |frame|. | 108 // skipped for |frame|. |
108 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; | 109 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; |
109 }; | 110 }; |
110 | 111 |
111 PrintWebViewHelper(content::RenderFrame* render_frame, | 112 PrintWebViewHelper(content::RenderView* render_view, |
112 std::unique_ptr<Delegate> delegate); | 113 std::unique_ptr<Delegate> delegate); |
113 ~PrintWebViewHelper() override; | 114 ~PrintWebViewHelper() override; |
114 | 115 |
115 // Minimum valid value for scaling. Since scaling is originally an integer | 116 // Minimum valid value for scaling. Since scaling is originally an integer |
116 // representing a percentage, it should never be less than this if it is | 117 // representing a percentage, it should never be less than this if it is |
117 // valid. | 118 // valid. |
118 static constexpr double kEpsilon = 0.01f; | 119 static constexpr double kEpsilon = 0.01f; |
119 | 120 |
120 // Disable print preview and switch to system dialog printing even if full | 121 // Disable print preview and switch to system dialog printing even if full |
121 // printing is build-in. This method is used by CEF. | 122 // printing is build-in. This method is used by CEF. |
122 static void DisablePreview(); | 123 static void DisablePreview(); |
123 | 124 |
124 bool IsPrintingEnabled() const; | 125 bool IsPrintingEnabled(); |
125 | 126 |
126 void PrintNode(const blink::WebNode& node); | 127 void PrintNode(const blink::WebNode& node); |
127 | 128 |
128 private: | 129 private: |
129 friend class PrintWebViewHelperTestBase; | 130 friend class PrintWebViewHelperTestBase; |
130 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, | 131 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, |
131 BlockScriptInitiatedPrinting); | 132 BlockScriptInitiatedPrinting); |
132 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); | 133 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); |
133 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, | 134 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, |
134 BlockScriptInitiatedPrinting); | 135 BlockScriptInitiatedPrinting); |
(...skipping 25 matching lines...) Expand all Loading... |
160 PREVIEW_ERROR_LAST_ENUM // Always last. | 161 PREVIEW_ERROR_LAST_ENUM // Always last. |
161 }; | 162 }; |
162 | 163 |
163 enum PrintPreviewRequestType { | 164 enum PrintPreviewRequestType { |
164 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, | 165 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, |
165 PRINT_PREVIEW_USER_INITIATED_SELECTION, | 166 PRINT_PREVIEW_USER_INITIATED_SELECTION, |
166 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, | 167 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, |
167 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). | 168 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). |
168 }; | 169 }; |
169 | 170 |
170 // RenderFrameObserver implementation. | 171 // RenderViewObserver implementation. |
| 172 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; |
171 void OnDestruct() override; | 176 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; | |
176 bool OnMessageReceived(const IPC::Message& message) 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 has_selection); | 185 void OnInitiatePrintPreview(bool selection_only); |
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 printing. | 219 // Enable/Disable window.print calls. If |blocked| is true window.print |
220 void OnSetPrintingEnabled(bool enabled); | 220 // calls will silently fail. Call with |blocked| set to false to reenable. |
| 221 void SetScriptedPrintBlocked(bool blocked); |
221 | 222 |
222 // Main printing code ------------------------------------------------------- | 223 // Main printing code ------------------------------------------------------- |
223 | 224 |
224 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 225 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
225 // |is_scripted| should be true when the call is coming from window.print() | 226 // |is_scripted| should be true when the call is coming from window.print() |
226 void Print(blink::WebLocalFrame* frame, | 227 void Print(blink::WebLocalFrame* frame, |
227 const blink::WebNode& node, | 228 const blink::WebNode& node, |
228 bool is_scripted); | 229 bool is_scripted); |
229 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 230 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
230 | 231 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 // and footers using strings from |header_footer_info| on to the canvas. | 334 // and footers using strings from |header_footer_info| on to the canvas. |
334 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, | 335 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, |
335 int page_number, | 336 int page_number, |
336 int total_pages, | 337 int total_pages, |
337 const blink::WebFrame& source_frame, | 338 const blink::WebFrame& source_frame, |
338 float webkit_scale_factor, | 339 float webkit_scale_factor, |
339 const PageSizeMargins& page_layout_in_points, | 340 const PageSizeMargins& page_layout_in_points, |
340 const PrintMsg_Print_Params& params); | 341 const PrintMsg_Print_Params& params); |
341 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) | 342 #endif // BUILDFLAG(ENABLE_PRINT_PREVIEW) |
342 | 343 |
| 344 bool GetPrintFrame(blink::WebLocalFrame** frame); |
| 345 |
343 // Script Initiated Printing ------------------------------------------------ | 346 // Script Initiated Printing ------------------------------------------------ |
344 | 347 |
345 // Return true if script initiated printing is currently | 348 // Return true if script initiated printing is currently |
346 // allowed. |user_initiated| should be true when a user event triggered the | 349 // allowed. |user_initiated| should be true when a user event triggered the |
347 // script, most likely by pressing a print button on the page. | 350 // script, most likely by pressing a print button on the page. |
348 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, | 351 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, |
349 bool user_initiated); | 352 bool user_initiated); |
350 | 353 |
351 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) | 354 #if BUILDFLAG(ENABLE_PRINT_PREVIEW) |
352 // Shows scripted print preview when options from plugin are available. | 355 // Shows scripted print preview when options from plugin are available. |
(...skipping 16 matching lines...) Expand all Loading... |
369 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); | 372 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); |
370 | 373 |
371 // WebView used only to print the selection. | 374 // WebView used only to print the selection. |
372 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | 375 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; |
373 bool reset_prep_frame_view_; | 376 bool reset_prep_frame_view_; |
374 | 377 |
375 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | 378 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; |
376 bool is_print_ready_metafile_sent_; | 379 bool is_print_ready_metafile_sent_; |
377 bool ignore_css_margins_; | 380 bool ignore_css_margins_; |
378 | 381 |
379 bool is_printing_enabled_; | 382 // Used for scripted initiated printing blocking. |
| 383 bool is_scripted_printing_blocked_; |
380 | 384 |
381 // Let the browser process know of a printing failure. Only set to false when | 385 // Let the browser process know of a printing failure. Only set to false when |
382 // the failure came from the browser in the first place. | 386 // the failure came from the browser in the first place. |
383 bool notify_browser_of_print_failure_; | 387 bool notify_browser_of_print_failure_; |
384 | 388 |
385 // True, when printing from print preview. | 389 // True, when printing from print preview. |
386 bool print_for_preview_; | 390 bool print_for_preview_; |
387 | 391 |
388 // Used to check the prerendering status. | 392 // Used to check the prerendering status. |
389 const std::unique_ptr<Delegate> delegate_; | 393 const std::unique_ptr<Delegate> delegate_; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
528 base::Closure on_stop_loading_closure_; | 532 base::Closure on_stop_loading_closure_; |
529 | 533 |
530 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; | 534 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; |
531 | 535 |
532 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 536 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
533 }; | 537 }; |
534 | 538 |
535 } // namespace printing | 539 } // namespace printing |
536 | 540 |
537 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 541 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
OLD | NEW |