| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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> |
| 10 |
| 9 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 11 #include "base/time.h" | 13 #include "base/time.h" |
| 12 #include "content/renderer/render_view_observer.h" | 14 #include "content/renderer/render_view_observer.h" |
| 13 #include "content/renderer/render_view_observer_tracker.h" | 15 #include "content/renderer/render_view_observer_tracker.h" |
| 14 #include "printing/metafile.h" | 16 #include "printing/metafile.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebViewClient.h" |
| 17 #include "ui/gfx/size.h" | 19 #include "ui/gfx/size.h" |
| 18 | 20 |
| 19 struct PrintMsg_Print_Params; | 21 struct PrintMsg_Print_Params; |
| 20 struct PrintMsg_PrintPage_Params; | 22 struct PrintMsg_PrintPage_Params; |
| 21 struct PrintMsg_PrintPages_Params; | 23 struct PrintMsg_PrintPages_Params; |
| 22 | 24 |
| 23 namespace base { | 25 namespace base { |
| 24 class DictionaryValue; | 26 class DictionaryValue; |
| 25 } | 27 } |
| 26 | 28 |
| 27 // Class that calls the Begin and End print functions on the frame and changes | 29 // Class that calls the Begin and End print functions on the frame and changes |
| 28 // the size of the view temporarily to support full page printing.. | 30 // the size of the view temporarily to support full page printing.. |
| 29 // Do not serve any events in the time between construction and destruction of | 31 // Do not serve any events in the time between construction and destruction of |
| 30 // this class because it will cause flicker. | 32 // this class because it will cause flicker. |
| 31 class PrepareFrameAndViewForPrint { | 33 class PrepareFrameAndViewForPrint { |
| 32 public: | 34 public: |
| 33 // Prints |frame|. If |node| is not NULL, then only that node will be | 35 // Prints |frame|. If |node| is not NULL, then only that node will be |
| 34 // printed. | 36 // printed. |
| 35 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& print_params, | 37 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& print_params, |
| 36 WebKit::WebFrame* frame, | 38 WebKit::WebFrame* frame, |
| 37 WebKit::WebNode* node, | 39 WebKit::WebNode* node); |
| 38 WebKit::WebView* web_view); | |
| 39 ~PrepareFrameAndViewForPrint(); | 40 ~PrepareFrameAndViewForPrint(); |
| 40 | 41 |
| 41 int GetExpectedPageCount() const { | 42 int GetExpectedPageCount() const { |
| 42 return expected_pages_count_; | 43 return expected_pages_count_; |
| 43 } | 44 } |
| 44 | 45 |
| 45 bool ShouldUseBrowserOverlays() const { | 46 bool ShouldUseBrowserOverlays() const { |
| 46 return use_browser_overlays_; | 47 return use_browser_overlays_; |
| 47 } | 48 } |
| 48 | 49 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 virtual void PrintPage(WebKit::WebFrame* frame); | 102 virtual void PrintPage(WebKit::WebFrame* frame); |
| 102 | 103 |
| 103 // Message handlers --------------------------------------------------------- | 104 // Message handlers --------------------------------------------------------- |
| 104 | 105 |
| 105 // Print the document. | 106 // Print the document. |
| 106 void OnPrintPages(); | 107 void OnPrintPages(); |
| 107 | 108 |
| 108 // Initiate print preview. | 109 // Initiate print preview. |
| 109 void OnInitiatePrintPreview(); | 110 void OnInitiatePrintPreview(); |
| 110 | 111 |
| 111 // Generate a print preview using |settings|. | 112 // Start the process of generating a print preview using |settings|. |
| 112 void OnPrintPreview(const base::DictionaryValue& settings); | 113 void OnPrintPreview(const base::DictionaryValue& settings); |
| 114 // Initialize the print preview document. |
| 115 bool CreatePreviewDocument(); |
| 116 |
| 117 // Continue generating the print preview. |
| 118 void OnContinuePreview(); |
| 119 // Renders a print preview page. |page_number| is 0-based. |
| 120 void RenderPreviewPage(int page_number); |
| 121 // Finalize the print preview document. |
| 122 bool FinalizePreviewDocument(); |
| 123 |
| 124 // Abort the preview to put |print_preview_context_| into the 'UNINITIALIZED' |
| 125 // state. |
| 126 void OnAbortPreview(); |
| 113 | 127 |
| 114 // Print / preview the node under the context menu. | 128 // Print / preview the node under the context menu. |
| 115 void OnPrintNodeUnderContextMenu(); | 129 void OnPrintNodeUnderContextMenu(); |
| 116 | 130 |
| 117 // Print the pages for print preview. Do not display the native print dialog | 131 // Print the pages for print preview. Do not display the native print dialog |
| 118 // for user settings. |job_settings| has new print job settings values. | 132 // for user settings. |job_settings| has new print job settings values. |
| 119 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); | 133 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); |
| 120 | 134 |
| 121 void OnPrintingDone(bool success); | 135 void OnPrintingDone(bool success); |
| 122 | 136 |
| 123 // Main printing code ------------------------------------------------------- | 137 // Main printing code ------------------------------------------------------- |
| 124 | 138 |
| 125 void Print(WebKit::WebFrame* frame, WebKit::WebNode* node); | 139 void Print(WebKit::WebFrame* frame, WebKit::WebNode* node); |
| 126 | 140 |
| 127 void PrintPreview(WebKit::WebFrame* frame, | |
| 128 WebKit::WebNode* node, | |
| 129 const base::DictionaryValue& settings); | |
| 130 | |
| 131 enum PrintingResult { | 141 enum PrintingResult { |
| 132 OK, | 142 OK, |
| 133 FAIL_PRINT, | 143 FAIL_PRINT, |
| 134 FAIL_PREVIEW, | 144 FAIL_PREVIEW, |
| 145 ABORT_PREVIEW, |
| 135 }; | 146 }; |
| 136 | 147 |
| 137 // Notification when printing is done - signal tear-down/free resources. | 148 // Notification when printing is done - signal tear-down/free resources. |
| 138 void DidFinishPrinting(PrintingResult result); | 149 void DidFinishPrinting(PrintingResult result); |
| 139 | 150 |
| 140 // Print Settings ----------------------------------------------------------- | 151 // Print Settings ----------------------------------------------------------- |
| 141 | 152 |
| 142 // Initialize print page settings with default settings. | 153 // Initialize print page settings with default settings. |
| 143 bool InitPrintSettings(WebKit::WebFrame* frame, | 154 bool InitPrintSettings(WebKit::WebFrame* frame, |
| 144 WebKit::WebNode* node); | 155 WebKit::WebNode* node); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 printing::Metafile* metafile); | 191 printing::Metafile* metafile); |
| 181 #else | 192 #else |
| 182 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, | 193 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, |
| 183 const gfx::Size& canvas_size, | 194 const gfx::Size& canvas_size, |
| 184 WebKit::WebFrame* frame); | 195 WebKit::WebFrame* frame); |
| 185 #endif | 196 #endif |
| 186 | 197 |
| 187 // Render the frame for printing. | 198 // Render the frame for printing. |
| 188 bool RenderPagesForPrint(WebKit::WebFrame* frame, WebKit::WebNode* node); | 199 bool RenderPagesForPrint(WebKit::WebFrame* frame, WebKit::WebNode* node); |
| 189 | 200 |
| 190 // Render the frame for preview. | |
| 191 bool RenderPagesForPreview(WebKit::WebFrame* frame, WebKit::WebNode* node); | |
| 192 | |
| 193 // Renders all the pages listed in |params| for preview. | |
| 194 // On success, Send PrintHostMsg_PagesReadyForPreview message with a | |
| 195 // valid metafile data handle. | |
| 196 bool CreatePreviewDocument(const PrintMsg_PrintPages_Params& params, | |
| 197 WebKit::WebFrame* frame, | |
| 198 WebKit::WebNode* node); | |
| 199 | |
| 200 // Platform specific helper function for rendering page(s) to |metafile|. | 201 // Platform specific helper function for rendering page(s) to |metafile|. |
| 201 #if defined(OS_WIN) | 202 #if defined(OS_WIN) |
| 202 void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor, | 203 void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor, |
| 203 int page_number, bool is_preview, WebKit::WebFrame* frame, | 204 int page_number, bool is_preview, WebKit::WebFrame* frame, |
| 204 scoped_ptr<printing::Metafile>* metafile); | 205 scoped_ptr<printing::Metafile>* metafile); |
| 205 #elif defined(OS_MACOSX) | 206 #elif defined(OS_MACOSX) |
| 206 void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area, | 207 void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area, |
| 207 const float& scale_factor, int page_number, | 208 const float& scale_factor, int page_number, |
| 208 WebKit::WebFrame* frame, printing::Metafile* metafile); | 209 WebKit::WebFrame* frame, printing::Metafile* metafile); |
| 209 #elif defined(OS_POSIX) | 210 #elif defined(OS_POSIX) |
| 210 bool RenderPages(const PrintMsg_PrintPages_Params& params, | 211 bool RenderPages(const PrintMsg_PrintPages_Params& params, |
| 211 WebKit::WebFrame* frame, | 212 WebKit::WebFrame* frame, WebKit::WebNode* node, |
| 212 WebKit::WebNode* node, | 213 int* page_count, printing::Metafile* metafile); |
| 213 int* page_count, | |
| 214 printing::Metafile* metafile, | |
| 215 bool is_preview); | |
| 216 #endif // defined(OS_WIN) | 214 #endif // defined(OS_WIN) |
| 217 | 215 |
| 218 // Helper methods ----------------------------------------------------------- | 216 // Helper methods ----------------------------------------------------------- |
| 219 | 217 |
| 220 bool CopyAndPrint(WebKit::WebFrame* web_frame); | 218 bool CopyAndPrint(WebKit::WebFrame* web_frame); |
| 221 | 219 |
| 222 bool CopyMetafileDataToSharedMem(printing::Metafile* metafile, | 220 bool CopyMetafileDataToSharedMem(printing::Metafile* metafile, |
| 223 base::SharedMemoryHandle* shared_mem_handle); | 221 base::SharedMemoryHandle* shared_mem_handle); |
| 224 | 222 |
| 225 void GetPageSizeAndMarginsInPoints( | 223 void GetPageSizeAndMarginsInPoints( |
| 226 WebKit::WebFrame* frame, | 224 WebKit::WebFrame* frame, |
| 227 int page_index, | 225 int page_index, |
| 228 const PrintMsg_Print_Params& default_params, | 226 const PrintMsg_Print_Params& default_params, |
| 229 double* content_width_in_points, | 227 double* content_width_in_points, |
| 230 double* content_height_in_points, | 228 double* content_height_in_points, |
| 231 double* margin_top_in_points, | 229 double* margin_top_in_points, |
| 232 double* margin_right_in_points, | 230 double* margin_right_in_points, |
| 233 double* margin_bottom_in_points, | 231 double* margin_bottom_in_points, |
| 234 double* margin_left_in_points); | 232 double* margin_left_in_points); |
| 235 | 233 |
| 236 bool IsModifiable(WebKit::WebFrame* frame, WebKit::WebNode* node); | |
| 237 | |
| 238 void UpdatePrintableSizeInPrintParameters(WebKit::WebFrame* frame, | 234 void UpdatePrintableSizeInPrintParameters(WebKit::WebFrame* frame, |
| 239 WebKit::WebNode* node, | 235 WebKit::WebNode* node, |
| 240 PrintMsg_Print_Params* params); | 236 PrintMsg_Print_Params* params); |
| 241 | 237 |
| 242 bool GetPrintFrame(WebKit::WebFrame** frame); | 238 bool GetPrintFrame(WebKit::WebFrame** frame); |
| 243 | 239 |
| 244 // This reports the current time - |start_time| as the time to render | 240 // This reports the current time - |start_time| as the time to render a page. |
| 245 // a page and returns the current time. | 241 void ReportPreviewPageRenderTime(base::TimeTicks start_time); |
| 246 base::TimeTicks ReportPreviewPageRenderTime(base::TimeTicks start_time); | |
| 247 void ReportTotalPreviewGenerationTime(int selected_pages_length, | |
| 248 int total_pages, | |
| 249 base::TimeDelta render_time, | |
| 250 base::TimeDelta total_time); | |
| 251 | 242 |
| 252 // Script Initiated Printing ------------------------------------------------ | 243 // Script Initiated Printing ------------------------------------------------ |
| 253 | 244 |
| 254 // Returns true if script initiated printing occurs too often. | 245 // Returns true if script initiated printing occurs too often. |
| 255 bool IsScriptInitiatedPrintTooFrequent(WebKit::WebFrame* frame); | 246 bool IsScriptInitiatedPrintTooFrequent(WebKit::WebFrame* frame); |
| 256 | 247 |
| 257 // Reset the counter for script initiated printing. | 248 // Reset the counter for script initiated printing. |
| 258 // Scripted printing will be allowed to continue. | 249 // Scripted printing will be allowed to continue. |
| 259 void ResetScriptedPrintCount(); | 250 void ResetScriptedPrintCount(); |
| 260 | 251 |
| 261 // Increment the counter for script initiated printing. | 252 // Increment the counter for script initiated printing. |
| 262 // Scripted printing will be blocked for a limited amount of time. | 253 // Scripted printing will be blocked for a limited amount of time. |
| 263 void IncrementScriptedPrintCount(); | 254 void IncrementScriptedPrintCount(); |
| 264 | 255 |
| 265 // Displays the print job error message to the user. | 256 // Displays the print job error message to the user. |
| 266 void DisplayPrintJobError(); | 257 void DisplayPrintJobError(); |
| 267 | 258 |
| 268 void RequestPrintPreview(); | 259 void RequestPrintPreview(); |
| 269 | 260 |
| 270 // Called every time print preview renders a page. Notify the browser of the | 261 // Notify the browser a print preview page has been rendered. |
| 271 // event and check if print preview should be cancelled. Returns false if | 262 // |page_number| is 0-based. |
| 272 // print preview has been cancelled. |page_number| is 0-based, or negative to | 263 void PreviewPageRendered(int page_number); |
| 273 // indicate its a cancel check only. | |
| 274 bool PreviewPageRendered(int page_number); | |
| 275 | 264 |
| 276 WebKit::WebView* print_web_view_; | 265 WebKit::WebView* print_web_view_; |
| 277 | 266 |
| 278 // The frame to print for script initiated print preview. | |
| 279 WebKit::WebFrame* script_initiated_preview_frame_; | |
| 280 | |
| 281 // The node under the context menu to print preview. | |
| 282 scoped_ptr<WebKit::WebNode> context_menu_preview_node_; | |
| 283 | |
| 284 scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_; | 267 scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_; |
| 285 bool is_preview_; | 268 bool is_preview_; |
| 286 | 269 |
| 287 // Used for scripted initiated printing blocking. | 270 // Used for scripted initiated printing blocking. |
| 288 base::Time last_cancelled_script_print_; | 271 base::Time last_cancelled_script_print_; |
| 289 int user_cancelled_scripted_print_count_; | 272 int user_cancelled_scripted_print_count_; |
| 290 | 273 |
| 291 // Let the browser process know of a printing failure. Only set to false when | 274 // Let the browser process know of a printing failure. Only set to false when |
| 292 // the failure came from the browser in the first place. | 275 // the failure came from the browser in the first place. |
| 293 bool notify_browser_of_print_failure_; | 276 bool notify_browser_of_print_failure_; |
| 294 | 277 |
| 295 int preview_page_count_; | |
| 296 scoped_ptr<PrintMsg_PrintPages_Params> old_print_pages_params_; | 278 scoped_ptr<PrintMsg_PrintPages_Params> old_print_pages_params_; |
| 297 | 279 |
| 280 // Keeps track of the state of print preview between messages. |
| 281 class PrintPreviewContext { |
| 282 public: |
| 283 PrintPreviewContext(); |
| 284 ~PrintPreviewContext(); |
| 285 |
| 286 // Initializes the print preview context. Need to be called to set |
| 287 // the |web_frame| / |web_node| to generate the print preview for. |
| 288 void InitWithFrame(WebKit::WebFrame* web_frame); |
| 289 void InitWithNode(const WebKit::WebNode& web_node); |
| 290 |
| 291 // Does bookkeeping at the beginning of print preview. |
| 292 void OnPrintPreview(); |
| 293 |
| 294 // Create the print preview document. |pages| is empty to print all pages. |
| 295 bool CreatePreviewDocument(const PrintMsg_Print_Params& params, |
| 296 const std::vector<int>& pages); |
| 297 |
| 298 // Called after a page gets rendered. |page_time| is how long the |
| 299 // rendering took. |
| 300 void RenderedPreviewPage(const base::TimeDelta& page_time); |
| 301 |
| 302 // Finalizes the print preview document. |
| 303 void FinalizePreviewDocument(); |
| 304 |
| 305 // Cleanup after print preview finishes. |
| 306 void Finished(); |
| 307 |
| 308 // Abort the print preview. |
| 309 void Abort(); |
| 310 |
| 311 // Helper functions |
| 312 int GetNextPageNumber(); |
| 313 bool IsReadyToRender() const; |
| 314 bool IsBusy() const; |
| 315 bool IsModifiable() const; |
| 316 |
| 317 // Getters |
| 318 WebKit::WebFrame* frame() const; |
| 319 WebKit::WebNode* node() const; |
| 320 int total_page_count() const; |
| 321 printing::Metafile* metafile() const; |
| 322 const PrintMsg_Print_Params& print_params() const; |
| 323 const gfx::Size& GetPrintCanvasSize() const; |
| 324 |
| 325 private: |
| 326 enum State { |
| 327 UNINITIALIZED, // Not ready to render. |
| 328 INITIALIZED, // Ready to render. |
| 329 RENDERING, // Rendering. |
| 330 DONE // Finished rendering. |
| 331 }; |
| 332 |
| 333 // Reset some of the internal rendering context. |
| 334 void ClearContext(); |
| 335 |
| 336 // Specifies what to render for print preview. |
| 337 WebKit::WebFrame* frame_; |
| 338 scoped_ptr<WebKit::WebNode> node_; |
| 339 |
| 340 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_; |
| 341 scoped_ptr<printing::Metafile> metafile_; |
| 342 scoped_ptr<PrintMsg_Print_Params> print_params_; |
| 343 |
| 344 // Total page count in the renderer. |
| 345 int total_page_count_; |
| 346 |
| 347 // Number of pages to render. |
| 348 int actual_page_count_; |
| 349 |
| 350 // The current page to render. |
| 351 int current_page_number_; |
| 352 |
| 353 // Array to keep track of which pages have been printed. |
| 354 std::vector<bool> rendered_pages_; |
| 355 |
| 356 base::TimeDelta document_render_time_; |
| 357 base::TimeTicks begin_time_; |
| 358 |
| 359 State state_; |
| 360 }; |
| 361 |
| 362 PrintPreviewContext print_preview_context_; |
| 363 |
| 298 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); | 364 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); |
| 299 }; | 365 }; |
| 300 | 366 |
| 301 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ | 367 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ |
| OLD | NEW |