| 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_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | 5 #ifndef CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ |
| 6 #define CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | 6 #define CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 } | 30 } |
| 31 | 31 |
| 32 namespace WebKit { | 32 namespace WebKit { |
| 33 class WebFrame; | 33 class WebFrame; |
| 34 class WebView; | 34 class WebView; |
| 35 } | 35 } |
| 36 | 36 |
| 37 namespace printing { | 37 namespace printing { |
| 38 | 38 |
| 39 struct PageSizeMargins; | 39 struct PageSizeMargins; |
| 40 | 40 class PrepareFrameAndViewForPrint; |
| 41 // Class that calls the Begin and End print functions on the frame and changes | |
| 42 // the size of the view temporarily to support full page printing.. | |
| 43 // Do not serve any events in the time between construction and destruction of | |
| 44 // this class because it will cause flicker. | |
| 45 class PrepareFrameAndViewForPrint { | |
| 46 public: | |
| 47 // Prints |frame|. If |node| is not NULL, then only that node will be | |
| 48 // printed. | |
| 49 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& print_params, | |
| 50 WebKit::WebFrame* frame, | |
| 51 const WebKit::WebNode& node); | |
| 52 ~PrepareFrameAndViewForPrint(); | |
| 53 | |
| 54 void UpdatePrintParams(const PrintMsg_Print_Params& print_params); | |
| 55 | |
| 56 int GetExpectedPageCount() const { | |
| 57 return expected_pages_count_; | |
| 58 } | |
| 59 | |
| 60 bool ShouldUseBrowserOverlays() const { | |
| 61 return use_browser_overlays_; | |
| 62 } | |
| 63 | |
| 64 gfx::Size GetPrintCanvasSize() const; | |
| 65 | |
| 66 void FinishPrinting(); | |
| 67 | |
| 68 private: | |
| 69 void StartPrinting(const WebKit::WebPrintParams& webkit_print_params); | |
| 70 | |
| 71 WebKit::WebFrame* frame_; | |
| 72 WebKit::WebNode node_to_print_; | |
| 73 WebKit::WebView* web_view_; | |
| 74 WebKit::WebPrintParams web_print_params_; | |
| 75 gfx::Size prev_view_size_; | |
| 76 gfx::Size prev_scroll_offset_; | |
| 77 int expected_pages_count_; | |
| 78 bool use_browser_overlays_; | |
| 79 bool finished_; | |
| 80 bool should_print_backgrounds_; | |
| 81 | |
| 82 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | |
| 83 }; | |
| 84 | 41 |
| 85 // PrintWebViewHelper handles most of the printing grunt work for RenderView. | 42 // PrintWebViewHelper handles most of the printing grunt work for RenderView. |
| 86 // We plan on making print asynchronous and that will require copying the DOM | 43 // We plan on making print asynchronous and that will require copying the DOM |
| 87 // of the document and creating a new WebView with the contents. | 44 // of the document and creating a new WebView with the contents. |
| 88 class PrintWebViewHelper | 45 class PrintWebViewHelper |
| 89 : public content::RenderViewObserver, | 46 : public content::RenderViewObserver, |
| 90 public content::RenderViewObserverTracker<PrintWebViewHelper>, | 47 public content::RenderViewObserverTracker<PrintWebViewHelper>, |
| 91 public WebKit::WebViewClient, | 48 public WebKit::WebViewClient, |
| 92 public WebKit::WebFrameClient { | 49 public WebKit::WebFrameClient { |
| 93 public: | 50 public: |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 | 176 |
| 220 // Notification when printing is done - signal tear-down/free resources. | 177 // Notification when printing is done - signal tear-down/free resources. |
| 221 void DidFinishPrinting(PrintingResult result); | 178 void DidFinishPrinting(PrintingResult result); |
| 222 | 179 |
| 223 // Print Settings ----------------------------------------------------------- | 180 // Print Settings ----------------------------------------------------------- |
| 224 | 181 |
| 225 // Initialize print page settings with default settings. | 182 // Initialize print page settings with default settings. |
| 226 // Used only for native printing workflow. | 183 // Used only for native printing workflow. |
| 227 bool InitPrintSettings(bool fit_to_paper_size); | 184 bool InitPrintSettings(bool fit_to_paper_size); |
| 228 | 185 |
| 229 // Initialize print page settings with default settings and prepare the frame | 186 // Calculate number of pages in source document. |
| 230 // for print. A new PrepareFrameAndViewForPrint is created to fulfill the | 187 bool CalculateNumberOfPages(WebKit::WebFrame* frame, |
| 231 // request and is filled into the |prepare| argument. | 188 const WebKit::WebNode& node, |
| 232 // Used only for native printing workflow. | 189 int* number_of_pages); |
| 233 bool InitPrintSettingsAndPrepareFrame( | |
| 234 WebKit::WebFrame* frame, | |
| 235 const WebKit::WebNode& node, | |
| 236 scoped_ptr<PrepareFrameAndViewForPrint>* prepare); | |
| 237 | 190 |
| 238 // Update the current print settings with new |passed_job_settings|. | 191 // Update the current print settings with new |passed_job_settings|. |
| 239 // |passed_job_settings| dictionary contains print job details such as printer | 192 // |passed_job_settings| dictionary contains print job details such as printer |
| 240 // name, number of copies, page range, etc. | 193 // name, number of copies, page range, etc. |
| 241 bool UpdatePrintSettings(WebKit::WebFrame* frame, | 194 bool UpdatePrintSettings(WebKit::WebFrame* frame, |
| 242 const WebKit::WebNode& node, | 195 const WebKit::WebNode& node, |
| 243 const base::DictionaryValue& passed_job_settings); | 196 const base::DictionaryValue& passed_job_settings); |
| 244 | 197 |
| 245 // Get final print settings from the user. | 198 // Get final print settings from the user. |
| 246 // Return false if the user cancels or on error. | 199 // Return false if the user cancels or on error. |
| 247 bool GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 200 bool GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
| 248 const WebKit::WebNode& node, | 201 const WebKit::WebNode& node, |
| 249 int expected_pages_count, | 202 int expected_pages_count); |
| 250 bool use_browser_overlays); | |
| 251 | 203 |
| 252 // Page Printing / Rendering ------------------------------------------------ | 204 // Page Printing / Rendering ------------------------------------------------ |
| 253 | 205 |
| 254 // Prints all the pages listed in |print_pages_params_|. | 206 // Prints all the pages listed in |print_pages_params_|. |
| 255 // It will implicitly revert the document to display CSS media type. | 207 // It will implicitly revert the document to display CSS media type. |
| 256 bool PrintPages(WebKit::WebFrame* frame, const WebKit::WebNode& node); | 208 bool PrintPages(WebKit::WebFrame* frame, const WebKit::WebNode& node); |
| 209 bool PrintPagesNative(WebKit::WebFrame* frame, |
| 210 const WebKit::WebNode& node, |
| 211 int page_count, |
| 212 const gfx::Size& canvas_size); |
| 257 | 213 |
| 258 // Prints the page listed in |params|. | 214 // Prints the page listed in |params|. |
| 259 #if defined(USE_X11) | 215 #if defined(USE_X11) |
| 260 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | 216 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, |
| 261 const gfx::Size& canvas_size, | 217 const gfx::Size& canvas_size, |
| 262 WebKit::WebFrame* frame, | 218 WebKit::WebFrame* frame, |
| 263 Metafile* metafile); | 219 Metafile* metafile); |
| 264 #else | 220 #else |
| 265 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | 221 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, |
| 266 const gfx::Size& canvas_size, | 222 const gfx::Size& canvas_size, |
| 267 WebKit::WebFrame* frame); | 223 WebKit::WebFrame* frame); |
| 268 #endif | 224 #endif |
| 269 | 225 |
| 270 // Render the frame for printing. | 226 // Render the frame for printing. |
| 271 bool RenderPagesForPrint(WebKit::WebFrame* frame, | 227 bool RenderPagesForPrint(WebKit::WebFrame* frame, |
| 272 const WebKit::WebNode& node); | 228 const WebKit::WebNode& node); |
| 273 | 229 |
| 274 // Platform specific helper function for rendering page(s) to |metafile|. | 230 // Platform specific helper function for rendering page(s) to |metafile|. |
| 275 #if defined(OS_WIN) | 231 #if defined(OS_WIN) |
| 276 void RenderPage(const PrintMsg_Print_Params& params, | 232 void RenderPage(const PrintMsg_Print_Params& params, |
| 277 int page_number, | 233 int page_number, |
| 278 WebKit::WebFrame* frame, | 234 WebKit::WebFrame* frame, |
| 279 bool is_preview, | 235 bool is_preview, |
| 280 Metafile* metafile, | 236 Metafile* metafile, |
| 281 double* scale_factor, | 237 double* scale_factor, |
| 282 gfx::Size* page_size_in_dpi, | 238 gfx::Size* page_size_in_dpi, |
| 283 gfx::Rect* content_area_in_dpi); | 239 gfx::Rect* content_area_in_dpi); |
| 284 #elif defined(OS_MACOSX) | 240 #elif defined(OS_MACOSX) |
| 285 void RenderPage(const PrintMsg_Print_Params& params, int page_number, | 241 void RenderPage(const PrintMsg_Print_Params& params, |
| 286 WebKit::WebFrame* frame, bool is_preview, | 242 int page_number, |
| 287 Metafile* metafile, gfx::Size* page_size, | 243 WebKit::WebFrame* frame, |
| 244 bool is_preview, |
| 245 Metafile* metafile, |
| 246 gfx::Size* page_size, |
| 288 gfx::Rect* content_rect); | 247 gfx::Rect* content_rect); |
| 289 #elif defined(OS_POSIX) | |
| 290 bool RenderPages(const PrintMsg_PrintPages_Params& params, | |
| 291 WebKit::WebFrame* frame, const WebKit::WebNode& node, | |
| 292 std::vector<int>* printed_pages, | |
| 293 Metafile* metafile); | |
| 294 #endif // defined(OS_WIN) | 248 #endif // defined(OS_WIN) |
| 295 | 249 |
| 296 // Renders page contents from |frame| to |content_area| of |canvas|. | 250 // Renders page contents from |frame| to |content_area| of |canvas|. |
| 297 // |page_number| is zero-based. | 251 // |page_number| is zero-based. |
| 298 // When method is called, canvas should be setup to draw to |canvas_area| | 252 // When method is called, canvas should be setup to draw to |canvas_area| |
| 299 // with |scale_factor|. | 253 // with |scale_factor|. |
| 300 static float RenderPageContent(WebKit::WebFrame* frame, | 254 static float RenderPageContent(WebKit::WebFrame* frame, |
| 301 int page_number, | 255 int page_number, |
| 302 const gfx::Rect& canvas_area, | 256 const gfx::Rect& canvas_area, |
| 303 const gfx::Rect& content_area, | 257 const gfx::Rect& content_area, |
| 304 double scale_factor, | 258 double scale_factor, |
| 305 WebKit::WebCanvas* canvas); | 259 WebKit::WebCanvas* canvas); |
| 306 | 260 |
| 307 // Helper methods ----------------------------------------------------------- | 261 // Helper methods ----------------------------------------------------------- |
| 308 | 262 |
| 309 bool CopyAndPrint(WebKit::WebFrame* web_frame); | 263 bool CopyAndPrint(WebKit::WebFrame* web_frame); |
| 310 | 264 |
| 311 bool CopyMetafileDataToSharedMem(Metafile* metafile, | 265 bool CopyMetafileDataToSharedMem(Metafile* metafile, |
| 312 base::SharedMemoryHandle* shared_mem_handle); | 266 base::SharedMemoryHandle* shared_mem_handle); |
| 313 | 267 |
| 314 // Helper method to get page layout in points and fit to page if needed. | 268 // Helper method to get page layout in points and fit to page if needed. |
| 315 static void ComputePageLayoutInPointsForCss( | 269 static void ComputePageLayoutInPointsForCss( |
| 316 WebKit::WebFrame* frame, | 270 WebKit::WebFrame* frame, |
| 317 int page_index, | 271 int page_index, |
| 318 const PrintMsg_Print_Params& default_params, | 272 const PrintMsg_Print_Params& default_params, |
| 319 bool ignore_css_margins, | 273 bool ignore_css_margins, |
| 320 double* scale_factor, | 274 double* scale_factor, |
| 321 PageSizeMargins* page_layout_in_points); | 275 PageSizeMargins* page_layout_in_points); |
| 322 | 276 |
| 323 // Prepare the frame and view for print and then call this function to honor | |
| 324 // the CSS page layout information. | |
| 325 static void UpdateFrameAndViewFromCssPageLayout( | |
| 326 WebKit::WebFrame* frame, | |
| 327 const WebKit::WebNode& node, | |
| 328 PrepareFrameAndViewForPrint* prepare, | |
| 329 const PrintMsg_Print_Params& params, | |
| 330 bool ignore_css_margins); | |
| 331 | |
| 332 // Given the |device| and |canvas| to draw on, prints the appropriate headers | 277 // Given the |device| and |canvas| to draw on, prints the appropriate headers |
| 333 // and footers using strings from |header_footer_info| on to the canvas. | 278 // and footers using strings from |header_footer_info| on to the canvas. |
| 334 static void PrintHeaderAndFooter( | 279 static void PrintHeaderAndFooter( |
| 335 WebKit::WebCanvas* canvas, | 280 WebKit::WebCanvas* canvas, |
| 336 int page_number, | 281 int page_number, |
| 337 int total_pages, | 282 int total_pages, |
| 338 float webkit_scale_factor, | 283 float webkit_scale_factor, |
| 339 const PageSizeMargins& page_layout_in_points, | 284 const PageSizeMargins& page_layout_in_points, |
| 340 const base::DictionaryValue& header_footer_info, | 285 const base::DictionaryValue& header_footer_info, |
| 341 const PrintMsg_Print_Params& params); | 286 const PrintMsg_Print_Params& params); |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 }; | 446 }; |
| 502 | 447 |
| 503 bool print_node_in_progress_; | 448 bool print_node_in_progress_; |
| 504 PrintPreviewContext print_preview_context_; | 449 PrintPreviewContext print_preview_context_; |
| 505 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 450 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
| 506 }; | 451 }; |
| 507 | 452 |
| 508 } // namespace printing | 453 } // namespace printing |
| 509 | 454 |
| 510 #endif // CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ | 455 #endif // CHROME_RENDERER_PRINTING_PRINT_WEB_VIEW_HELPER_H_ |
| OLD | NEW |