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/pdf_metafile_skia.h" | 20 #include "printing/pdf_metafile_skia.h" |
21 #include "third_party/WebKit/public/platform/WebCanvas.h" | 21 #include "third_party/WebKit/public/platform/WebCanvas.h" |
22 #include "third_party/WebKit/public/web/WebNode.h" | 22 #include "third_party/WebKit/public/web/WebNode.h" |
23 #include "third_party/WebKit/public/web/WebPrintParams.h" | 23 #include "third_party/WebKit/public/web/WebPrintParams.h" |
24 #include "ui/gfx/geometry/size.h" | 24 #include "ui/gfx/geometry/size.h" |
25 | 25 |
26 struct PrintMsg_Print_Params; | 26 struct PrintMsg_Print_Params; |
27 struct PrintMsg_PrintPage_Params; | 27 struct PrintMsg_PrintPage_Params; |
28 struct PrintMsg_PrintPages_Params; | 28 struct PrintMsg_PrintPages_Params; |
29 struct PrintHostMsg_SetOptionsFromDocument_Params; | 29 struct PrintHostMsg_SetOptionsFromDocument_Params; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 private: | 71 private: |
72 blink::WebView* view_; | 72 blink::WebView* view_; |
73 blink::WebLocalFrame* frame_; | 73 blink::WebLocalFrame* frame_; |
74 }; | 74 }; |
75 | 75 |
76 // PrintWebViewHelper handles most of the printing grunt work for RenderView. | 76 // PrintWebViewHelper handles most of the printing grunt work for RenderView. |
77 // We plan on making print asynchronous and that will require copying the DOM | 77 // We plan on making print asynchronous and that will require copying the DOM |
78 // of the document and creating a new WebView with the contents. | 78 // of the document and creating a new WebView with the contents. |
79 class PrintWebViewHelper | 79 class PrintWebViewHelper |
80 : public content::RenderViewObserver, | 80 : public content::RenderFrameObserver, |
81 public content::RenderViewObserverTracker<PrintWebViewHelper> { | 81 public content::RenderFrameObserverTracker<PrintWebViewHelper> { |
82 public: | 82 public: |
83 class Delegate { | 83 class Delegate { |
84 public: | 84 public: |
85 virtual ~Delegate() {} | 85 virtual ~Delegate() {} |
86 | 86 |
87 // Cancels prerender if it's currently in progress and returns |true| if | 87 // Cancels prerender if it's currently in progress and returns |true| if |
88 // the cancellation was done with success. | 88 // the cancellation was done with success. |
89 virtual bool CancelPrerender(content::RenderView* render_view, | 89 virtual bool CancelPrerender(content::RenderFrame* render_frame, |
90 int routing_id) = 0; | 90 int routing_id) = 0; |
91 | 91 |
92 // Returns the element to be printed. Returns a null WebElement if | 92 // Returns the element to be printed. Returns a null WebElement if |
93 // a pdf plugin element can't be extracted from the frame. | 93 // a pdf plugin element can't be extracted from the frame. |
94 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; | 94 virtual blink::WebElement GetPdfElement(blink::WebLocalFrame* frame) = 0; |
95 | 95 |
96 virtual bool IsPrintPreviewEnabled() = 0; | 96 virtual bool IsPrintPreviewEnabled() = 0; |
97 | 97 |
98 // If true, the user can be asked to provide print settings. | 98 // If true, the user can be asked to provide print settings. |
99 // The default implementation returns |true|. | 99 // The default implementation returns |true|. |
100 virtual bool IsAskPrintSettingsEnabled(); | 100 virtual bool IsAskPrintSettingsEnabled(); |
101 | 101 |
102 // If false, window.print() won't do anything. | 102 // If false, window.print() won't do anything. |
103 // The default implementation returns |true|. | 103 // The default implementation returns |true|. |
104 virtual bool IsScriptedPrintEnabled(); | 104 virtual bool IsScriptedPrintEnabled(); |
105 | 105 |
106 // 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 |
107 // skipped for |frame|. | 107 // skipped for |frame|. |
108 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; | 108 virtual bool OverridePrint(blink::WebLocalFrame* frame) = 0; |
109 }; | 109 }; |
110 | 110 |
111 PrintWebViewHelper(content::RenderView* render_view, | 111 PrintWebViewHelper(content::RenderFrame* render_frame, |
112 std::unique_ptr<Delegate> delegate); | 112 std::unique_ptr<Delegate> delegate); |
113 ~PrintWebViewHelper() override; | 113 ~PrintWebViewHelper() override; |
114 | 114 |
115 // Disable print preview and switch to system dialog printing even if full | 115 // Disable print preview and switch to system dialog printing even if full |
116 // printing is build-in. This method is used by CEF. | 116 // printing is build-in. This method is used by CEF. |
117 static void DisablePreview(); | 117 static void DisablePreview(); |
118 | 118 |
119 bool IsPrintingEnabled(); | 119 bool IsPrintingEnabled() const; |
120 | 120 |
121 void PrintNode(const blink::WebNode& node); | 121 void PrintNode(const blink::WebNode& node); |
122 | 122 |
123 private: | 123 private: |
124 friend class PrintWebViewHelperTestBase; | 124 friend class PrintWebViewHelperTestBase; |
125 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, | 125 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperPreviewTest, |
126 BlockScriptInitiatedPrinting); | 126 BlockScriptInitiatedPrinting); |
127 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); | 127 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, OnPrintPages); |
128 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, | 128 FRIEND_TEST_ALL_PREFIXES(MAYBE_PrintWebViewHelperTest, |
129 BlockScriptInitiatedPrinting); | 129 BlockScriptInitiatedPrinting); |
(...skipping 25 matching lines...) Expand all Loading... |
155 PREVIEW_ERROR_LAST_ENUM // Always last. | 155 PREVIEW_ERROR_LAST_ENUM // Always last. |
156 }; | 156 }; |
157 | 157 |
158 enum PrintPreviewRequestType { | 158 enum PrintPreviewRequestType { |
159 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, | 159 PRINT_PREVIEW_USER_INITIATED_ENTIRE_FRAME, |
160 PRINT_PREVIEW_USER_INITIATED_SELECTION, | 160 PRINT_PREVIEW_USER_INITIATED_SELECTION, |
161 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, | 161 PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE, |
162 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). | 162 PRINT_PREVIEW_SCRIPTED // triggered by window.print(). |
163 }; | 163 }; |
164 | 164 |
165 // RenderViewObserver implementation. | 165 // RenderFrameObserver implementation. |
166 bool OnMessageReceived(const IPC::Message& message) override; | 166 bool OnMessageReceived(const IPC::Message& message) override; |
167 void PrintPage(blink::WebLocalFrame* frame, bool user_initiated) override; | 167 void ScriptedPrint(bool user_initiated) override; |
168 void DidStartLoading() override; | 168 void DidStartLoading() override; |
169 void DidStopLoading() override; | 169 void DidStopLoading() override; |
170 void OnDestruct() override; | 170 void OnDestruct() override; |
171 | 171 |
172 // Message handlers --------------------------------------------------------- | 172 // Message handlers --------------------------------------------------------- |
173 #if defined(ENABLE_BASIC_PRINTING) | 173 #if defined(ENABLE_BASIC_PRINTING) |
174 void OnPrintPages(); | 174 void OnPrintPages(); |
175 void OnPrintForSystemDialog(); | 175 void OnPrintForSystemDialog(); |
176 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); | 176 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); |
177 #endif // defined(ENABLE_BASIC_PRINTING) | 177 #endif // defined(ENABLE_BASIC_PRINTING) |
178 #if defined(ENABLE_PRINT_PREVIEW) | 178 #if defined(ENABLE_PRINT_PREVIEW) |
179 void OnInitiatePrintPreview(bool selection_only); | 179 void OnInitiatePrintPreview(bool has_selection); |
180 void OnPrintPreview(const base::DictionaryValue& settings); | 180 void OnPrintPreview(const base::DictionaryValue& settings); |
181 #endif // defined(ENABLE_PRINT_PREVIEW) | 181 #endif // defined(ENABLE_PRINT_PREVIEW) |
182 void OnPrintingDone(bool success); | 182 void OnPrintingDone(bool success); |
183 | 183 |
184 // Get |page_size| and |content_area| information from | 184 // Get |page_size| and |content_area| information from |
185 // |page_layout_in_points|. | 185 // |page_layout_in_points|. |
186 void GetPageSizeAndContentAreaFromPageLayout( | 186 void GetPageSizeAndContentAreaFromPageLayout( |
187 const PageSizeMargins& page_layout_in_points, | 187 const PageSizeMargins& page_layout_in_points, |
188 gfx::Size* page_size, | 188 gfx::Size* page_size, |
189 gfx::Rect* content_area); | 189 gfx::Rect* content_area); |
(...skipping 13 matching lines...) Expand all Loading... |
203 | 203 |
204 // Renders a print preview page. |page_number| is 0-based. | 204 // Renders a print preview page. |page_number| is 0-based. |
205 // Returns true if print preview should continue, false on failure. | 205 // Returns true if print preview should continue, false on failure. |
206 bool RenderPreviewPage(int page_number, | 206 bool RenderPreviewPage(int page_number, |
207 const PrintMsg_Print_Params& print_params); | 207 const PrintMsg_Print_Params& print_params); |
208 | 208 |
209 // Finalize the print ready preview document. | 209 // Finalize the print ready preview document. |
210 bool FinalizePrintReadyDocument(); | 210 bool FinalizePrintReadyDocument(); |
211 #endif // defined(ENABLE_PRINT_PREVIEW) | 211 #endif // defined(ENABLE_PRINT_PREVIEW) |
212 | 212 |
213 // Enable/Disable window.print calls. If |blocked| is true window.print | 213 // Enable/Disable printing. |
214 // calls will silently fail. Call with |blocked| set to false to reenable. | 214 void SetPrintingEnabled(bool enabled); |
215 void SetScriptedPrintBlocked(bool blocked); | |
216 | 215 |
217 // Main printing code ------------------------------------------------------- | 216 // Main printing code ------------------------------------------------------- |
218 | 217 |
219 #if defined(ENABLE_BASIC_PRINTING) | 218 #if defined(ENABLE_BASIC_PRINTING) |
220 // |is_scripted| should be true when the call is coming from window.print() | 219 // |is_scripted| should be true when the call is coming from window.print() |
221 void Print(blink::WebLocalFrame* frame, | 220 void Print(blink::WebLocalFrame* frame, |
222 const blink::WebNode& node, | 221 const blink::WebNode& node, |
223 bool is_scripted); | 222 bool is_scripted); |
224 #endif // defined(ENABLE_BASIC_PRINTING) | 223 #endif // defined(ENABLE_BASIC_PRINTING) |
225 | 224 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // and footers using strings from |header_footer_info| on to the canvas. | 327 // and footers using strings from |header_footer_info| on to the canvas. |
329 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, | 328 static void PrintHeaderAndFooter(blink::WebCanvas* canvas, |
330 int page_number, | 329 int page_number, |
331 int total_pages, | 330 int total_pages, |
332 const blink::WebFrame& source_frame, | 331 const blink::WebFrame& source_frame, |
333 float webkit_scale_factor, | 332 float webkit_scale_factor, |
334 const PageSizeMargins& page_layout_in_points, | 333 const PageSizeMargins& page_layout_in_points, |
335 const PrintMsg_Print_Params& params); | 334 const PrintMsg_Print_Params& params); |
336 #endif // defined(ENABLE_PRINT_PREVIEW) | 335 #endif // defined(ENABLE_PRINT_PREVIEW) |
337 | 336 |
338 bool GetPrintFrame(blink::WebLocalFrame** frame); | |
339 | |
340 // Script Initiated Printing ------------------------------------------------ | 337 // Script Initiated Printing ------------------------------------------------ |
341 | 338 |
342 // Return true if script initiated printing is currently | 339 // Return true if script initiated printing is currently |
343 // allowed. |user_initiated| should be true when a user event triggered the | 340 // allowed. |user_initiated| should be true when a user event triggered the |
344 // script, most likely by pressing a print button on the page. | 341 // script, most likely by pressing a print button on the page. |
345 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, | 342 bool IsScriptInitiatedPrintAllowed(blink::WebFrame* frame, |
346 bool user_initiated); | 343 bool user_initiated); |
347 | 344 |
348 #if defined(ENABLE_PRINT_PREVIEW) | 345 #if defined(ENABLE_PRINT_PREVIEW) |
349 // Shows scripted print preview when options from plugin are available. | 346 // Shows scripted print preview when options from plugin are available. |
(...skipping 16 matching lines...) Expand all Loading... |
366 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); | 363 void SetPrintPagesParams(const PrintMsg_PrintPages_Params& settings); |
367 | 364 |
368 // WebView used only to print the selection. | 365 // WebView used only to print the selection. |
369 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; | 366 std::unique_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; |
370 bool reset_prep_frame_view_; | 367 bool reset_prep_frame_view_; |
371 | 368 |
372 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | 369 std::unique_ptr<PrintMsg_PrintPages_Params> print_pages_params_; |
373 bool is_print_ready_metafile_sent_; | 370 bool is_print_ready_metafile_sent_; |
374 bool ignore_css_margins_; | 371 bool ignore_css_margins_; |
375 | 372 |
376 // Used for scripted initiated printing blocking. | 373 bool is_printing_enabled_; |
377 bool is_scripted_printing_blocked_; | |
378 | 374 |
379 // Let the browser process know of a printing failure. Only set to false when | 375 // Let the browser process know of a printing failure. Only set to false when |
380 // the failure came from the browser in the first place. | 376 // the failure came from the browser in the first place. |
381 bool notify_browser_of_print_failure_; | 377 bool notify_browser_of_print_failure_; |
382 | 378 |
383 // True, when printing from print preview. | 379 // True, when printing from print preview. |
384 bool print_for_preview_; | 380 bool print_for_preview_; |
385 | 381 |
386 // Used to check the prerendering status. | 382 // Used to check the prerendering status. |
387 const std::unique_ptr<Delegate> delegate_; | 383 const std::unique_ptr<Delegate> delegate_; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 base::Closure on_stop_loading_closure_; | 522 base::Closure on_stop_loading_closure_; |
527 | 523 |
528 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; | 524 base::WeakPtrFactory<PrintWebViewHelper> weak_ptr_factory_; |
529 | 525 |
530 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 526 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
531 }; | 527 }; |
532 | 528 |
533 } // namespace printing | 529 } // namespace printing |
534 | 530 |
535 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 531 #endif // COMPONENTS_PRINTING_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
OLD | NEW |