| 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 CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 5 #ifndef CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| 6 #define CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 6 #define CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "content/public/renderer/render_view_observer.h" | 14 #include "content/public/renderer/render_view_observer.h" |
| 15 #include "content/public/renderer/render_view_observer_tracker.h" | 15 #include "content/public/renderer/render_view_observer_tracker.h" |
| 16 #include "printing/metafile.h" | 16 #include "printing/metafile.h" |
| 17 #include "printing/metafile_impl.h" | 17 #include "printing/metafile_impl.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCanvas.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPrintParams.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" |
| 22 #include "ui/gfx/size.h" | 23 #include "ui/gfx/size.h" |
| 23 | 24 |
| 24 struct PrintMsg_Print_Params; | 25 struct PrintMsg_Print_Params; |
| 25 struct PrintMsg_PrintPage_Params; | 26 struct PrintMsg_PrintPage_Params; |
| 26 struct PrintMsg_PrintPages_Params; | 27 struct PrintMsg_PrintPages_Params; |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class DictionaryValue; | 30 class DictionaryValue; |
| 30 } | 31 } |
| 32 |
| 31 namespace printing { | 33 namespace printing { |
| 32 struct PageSizeMargins; | 34 struct PageSizeMargins; |
| 33 } | 35 } |
| 34 | 36 |
| 37 namespace webkit { |
| 38 struct WebPrintScalingOption; |
| 39 struct WebRect; |
| 40 struct WebSize; |
| 41 } |
| 35 // Class that calls the Begin and End print functions on the frame and changes | 42 // Class that calls the Begin and End print functions on the frame and changes |
| 36 // the size of the view temporarily to support full page printing.. | 43 // the size of the view temporarily to support full page printing.. |
| 37 // Do not serve any events in the time between construction and destruction of | 44 // Do not serve any events in the time between construction and destruction of |
| 38 // this class because it will cause flicker. | 45 // this class because it will cause flicker. |
| 39 class PrepareFrameAndViewForPrint { | 46 class PrepareFrameAndViewForPrint { |
| 40 public: | 47 public: |
| 41 // Prints |frame|. If |node| is not NULL, then only that node will be | 48 // Prints |frame|. If |node| is not NULL, then only that node will be |
| 42 // printed. | 49 // printed. |
| 43 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& print_params, | 50 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& print_params, |
| 44 WebKit::WebFrame* frame, | 51 WebKit::WebFrame* frame, |
| 45 const WebKit::WebNode& node); | 52 const WebKit::WebNode& node); |
| 46 ~PrepareFrameAndViewForPrint(); | 53 ~PrepareFrameAndViewForPrint(); |
| 47 | 54 |
| 48 void UpdatePrintParams(const PrintMsg_Print_Params& print_params); | 55 void UpdatePrintParams(const PrintMsg_Print_Params& print_params); |
| 49 | 56 |
| 50 int GetExpectedPageCount() const { | 57 int GetExpectedPageCount() const { |
| 51 return expected_pages_count_; | 58 return expected_pages_count_; |
| 52 } | 59 } |
| 53 | 60 |
| 54 bool ShouldUseBrowserOverlays() const { | 61 bool ShouldUseBrowserOverlays() const { |
| 55 return use_browser_overlays_; | 62 return use_browser_overlays_; |
| 56 } | 63 } |
| 57 | 64 |
| 58 const gfx::Size& GetPrintCanvasSize() const { | 65 gfx::Size GetPrintCanvasSize() const { |
| 59 return print_canvas_size_; | 66 return gfx::Size(web_print_params_.printContentArea.width, |
| 67 web_print_params_.printContentArea.height); |
| 60 } | 68 } |
| 61 | 69 |
| 62 void FinishPrinting(); | 70 void FinishPrinting(); |
| 63 | 71 |
| 64 private: | 72 private: |
| 65 void StartPrinting(const gfx::Size& print_params); | 73 void StartPrinting(const WebKit::WebRect& canvas_area, |
| 74 const WebKit::WebRect& printable_area, |
| 75 const WebKit::WebSize& paper_size, |
| 76 int dpi, |
| 77 const WebKit::WebPrintScalingOption& print_scaling_option); |
| 66 | 78 |
| 67 WebKit::WebFrame* frame_; | 79 WebKit::WebFrame* frame_; |
| 68 WebKit::WebNode node_to_print_; | 80 WebKit::WebNode node_to_print_; |
| 69 WebKit::WebView* web_view_; | 81 WebKit::WebView* web_view_; |
| 70 gfx::Size print_canvas_size_; | 82 WebKit::WebPrintParams web_print_params_; |
| 71 gfx::Size prev_view_size_; | 83 gfx::Size prev_view_size_; |
| 72 gfx::Size prev_scroll_offset_; | 84 gfx::Size prev_scroll_offset_; |
| 73 int dpi_; | |
| 74 int expected_pages_count_; | 85 int expected_pages_count_; |
| 75 bool use_browser_overlays_; | 86 bool use_browser_overlays_; |
| 76 bool finished_; | 87 bool finished_; |
| 77 | 88 |
| 78 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 89 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
| 79 }; | 90 }; |
| 80 | 91 |
| 81 // PrintWebViewHelper handles most of the printing grunt work for RenderView. | 92 // PrintWebViewHelper handles most of the printing grunt work for RenderView. |
| 82 // We plan on making print asynchronous and that will require copying the DOM | 93 // We plan on making print asynchronous and that will require copying the DOM |
| 83 // of the document and creating a new WebView with the contents. | 94 // of the document and creating a new WebView with the contents. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 const printing::PageSizeMargins& page_layout_in_points, | 164 const printing::PageSizeMargins& page_layout_in_points, |
| 154 gfx::Size* page_size, | 165 gfx::Size* page_size, |
| 155 gfx::Rect* content_area); | 166 gfx::Rect* content_area); |
| 156 | 167 |
| 157 // Update |ignore_css_margins_| based on settings. | 168 // Update |ignore_css_margins_| based on settings. |
| 158 void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); | 169 void UpdateFrameMarginsCssInfo(const base::DictionaryValue& settings); |
| 159 | 170 |
| 160 // Returns true if the current destination printer is PRINT_TO_PDF. | 171 // Returns true if the current destination printer is PRINT_TO_PDF. |
| 161 bool IsPrintToPdfRequested(const base::DictionaryValue& settings); | 172 bool IsPrintToPdfRequested(const base::DictionaryValue& settings); |
| 162 | 173 |
| 174 // Returns the print scaling option to retain/scale/crop the source page size |
| 175 // to fit the printable area of the paper. |
| 176 // |
| 177 // We retain the source page size when the current destination printer is |
| 178 // SAVE_AS_PDF. |
| 179 // |
| 180 // We crop the source page size to fit the printable area or we print only the |
| 181 // left top page contents when |
| 182 // (1) Source is PDF and the user has requested not to fit to printable area |
| 183 // via |job_settings|. |
| 184 // (2) Source is PDF. This is the first preview request and print scaling |
| 185 // option is disabled for initiator renderer plugin. |
| 186 // |
| 187 // In all other cases, we scale the source page to fit the printable area. |
| 188 WebKit::WebPrintScalingOption GetPrintScalingOption( |
| 189 bool source_is_html, |
| 190 const base::DictionaryValue& settings, |
| 191 const PrintMsg_Print_Params& params); |
| 192 |
| 163 // Initiate print preview. | 193 // Initiate print preview. |
| 164 void OnInitiatePrintPreview(); | 194 void OnInitiatePrintPreview(); |
| 165 | 195 |
| 166 // Start the process of generating a print preview using |settings|. | 196 // Start the process of generating a print preview using |settings|. |
| 167 void OnPrintPreview(const base::DictionaryValue& settings); | 197 void OnPrintPreview(const base::DictionaryValue& settings); |
| 168 | 198 |
| 169 // Initialize the print preview document. | 199 // Initialize the print preview document. |
| 170 bool CreatePreviewDocument(); | 200 bool CreatePreviewDocument(); |
| 171 | 201 |
| 172 // Renders a print preview page. |page_number| is 0-based. | 202 // Renders a print preview page. |page_number| is 0-based. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 193 | 223 |
| 194 void Print(WebKit::WebFrame* frame, const WebKit::WebNode& node); | 224 void Print(WebKit::WebFrame* frame, const WebKit::WebNode& node); |
| 195 | 225 |
| 196 // Notification when printing is done - signal tear-down/free resources. | 226 // Notification when printing is done - signal tear-down/free resources. |
| 197 void DidFinishPrinting(PrintingResult result); | 227 void DidFinishPrinting(PrintingResult result); |
| 198 | 228 |
| 199 // Print Settings ----------------------------------------------------------- | 229 // Print Settings ----------------------------------------------------------- |
| 200 | 230 |
| 201 // Initialize print page settings with default settings. | 231 // Initialize print page settings with default settings. |
| 202 // Used only for native printing workflow. | 232 // Used only for native printing workflow. |
| 203 bool InitPrintSettings(); | 233 bool InitPrintSettings(bool fit_to_paper_size); |
| 204 | 234 |
| 205 // Initialize print page settings with default settings and prepare the frame | 235 // Initialize print page settings with default settings and prepare the frame |
| 206 // for print. A new PrepareFrameAndViewForPrint is created to fulfill the | 236 // for print. A new PrepareFrameAndViewForPrint is created to fulfill the |
| 207 // request and is filled into the |prepare| argument. | 237 // request and is filled into the |prepare| argument. |
| 208 // Used only for native printing workflow. | 238 // Used only for native printing workflow. |
| 209 bool InitPrintSettingsAndPrepareFrame( | 239 bool InitPrintSettingsAndPrepareFrame( |
| 210 WebKit::WebFrame* frame, | 240 WebKit::WebFrame* frame, |
| 211 const WebKit::WebNode& node, | 241 const WebKit::WebNode& node, |
| 212 scoped_ptr<PrepareFrameAndViewForPrint>* prepare); | 242 scoped_ptr<PrepareFrameAndViewForPrint>* prepare); |
| 213 | 243 |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 void set_generate_draft_pages(bool generate_draft_pages); | 447 void set_generate_draft_pages(bool generate_draft_pages); |
| 418 void set_error(enum PrintPreviewErrorBuckets error); | 448 void set_error(enum PrintPreviewErrorBuckets error); |
| 419 | 449 |
| 420 // Getters | 450 // Getters |
| 421 WebKit::WebFrame* frame(); | 451 WebKit::WebFrame* frame(); |
| 422 const WebKit::WebNode& node() const; | 452 const WebKit::WebNode& node() const; |
| 423 int total_page_count() const; | 453 int total_page_count() const; |
| 424 bool generate_draft_pages() const; | 454 bool generate_draft_pages() const; |
| 425 printing::PreviewMetafile* metafile(); | 455 printing::PreviewMetafile* metafile(); |
| 426 const PrintMsg_Print_Params& print_params() const; | 456 const PrintMsg_Print_Params& print_params() const; |
| 427 const gfx::Size& GetPrintCanvasSize() const; | 457 gfx::Size GetPrintCanvasSize() const; |
| 428 int last_error() const; | 458 int last_error() const; |
| 429 | 459 |
| 430 private: | 460 private: |
| 431 enum State { | 461 enum State { |
| 432 UNINITIALIZED, // Not ready to render. | 462 UNINITIALIZED, // Not ready to render. |
| 433 INITIALIZED, // Ready to render. | 463 INITIALIZED, // Ready to render. |
| 434 RENDERING, // Rendering. | 464 RENDERING, // Rendering. |
| 435 DONE // Finished rendering. | 465 DONE // Finished rendering. |
| 436 }; | 466 }; |
| 437 | 467 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 467 enum PrintPreviewErrorBuckets error_; | 497 enum PrintPreviewErrorBuckets error_; |
| 468 | 498 |
| 469 State state_; | 499 State state_; |
| 470 }; | 500 }; |
| 471 | 501 |
| 472 PrintPreviewContext print_preview_context_; | 502 PrintPreviewContext print_preview_context_; |
| 473 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 503 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
| 474 }; | 504 }; |
| 475 | 505 |
| 476 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 506 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| OLD | NEW |