Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(702)

Side by Side Diff: chrome/renderer/print_web_view_helper.h

Issue 7365003: Print Preview: Make preview generation event driven to eliminate synchronous messages. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Speculative fix for DidFinishPrint DCHECK Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 // Cancel the preview to put |print_preview_context_| into the 'DONE' state.
125 void OnCancelPreview();
126
127 // Abort the preview to put |print_preview_context_| into the 'UNINITIALIZED'
128 // state.
129 void OnAbortPreview();
113 130
114 // Print / preview the node under the context menu. 131 // Print / preview the node under the context menu.
115 void OnPrintNodeUnderContextMenu(); 132 void OnPrintNodeUnderContextMenu();
116 133
117 // Print the pages for print preview. Do not display the native print dialog 134 // 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. 135 // for user settings. |job_settings| has new print job settings values.
119 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings); 136 void OnPrintForPrintPreview(const base::DictionaryValue& job_settings);
120 137
121 void OnPrintingDone(bool success); 138 void OnPrintingDone(bool success);
122 139
123 // Main printing code ------------------------------------------------------- 140 // Main printing code -------------------------------------------------------
124 141
125 void Print(WebKit::WebFrame* frame, WebKit::WebNode* node); 142 void Print(WebKit::WebFrame* frame, WebKit::WebNode* node);
126 143
127 void PrintPreview(WebKit::WebFrame* frame,
128 WebKit::WebNode* node,
129 const base::DictionaryValue& settings);
130
131 enum PrintingResult { 144 enum PrintingResult {
132 OK, 145 OK,
133 FAIL_PRINT, 146 FAIL_PRINT,
134 FAIL_PREVIEW, 147 FAIL_PREVIEW,
148 CANCEL_PREVIEW,
149 ABORT_PREVIEW,
135 }; 150 };
136 151
137 // Notification when printing is done - signal tear-down/free resources. 152 // Notification when printing is done - signal tear-down/free resources.
138 void DidFinishPrinting(PrintingResult result); 153 void DidFinishPrinting(PrintingResult result);
139 154
140 // Print Settings ----------------------------------------------------------- 155 // Print Settings -----------------------------------------------------------
141 156
142 // Initialize print page settings with default settings. 157 // Initialize print page settings with default settings.
143 bool InitPrintSettings(WebKit::WebFrame* frame, 158 bool InitPrintSettings(WebKit::WebFrame* frame,
144 WebKit::WebNode* node); 159 WebKit::WebNode* node);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 printing::Metafile* metafile); 195 printing::Metafile* metafile);
181 #else 196 #else
182 void PrintPageInternal(const PrintMsg_PrintPage_Params& params, 197 void PrintPageInternal(const PrintMsg_PrintPage_Params& params,
183 const gfx::Size& canvas_size, 198 const gfx::Size& canvas_size,
184 WebKit::WebFrame* frame); 199 WebKit::WebFrame* frame);
185 #endif 200 #endif
186 201
187 // Render the frame for printing. 202 // Render the frame for printing.
188 bool RenderPagesForPrint(WebKit::WebFrame* frame, WebKit::WebNode* node); 203 bool RenderPagesForPrint(WebKit::WebFrame* frame, WebKit::WebNode* node);
189 204
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|. 205 // Platform specific helper function for rendering page(s) to |metafile|.
201 #if defined(OS_WIN) 206 #if defined(OS_WIN)
202 void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor, 207 void RenderPage(const PrintMsg_Print_Params& params, float* scale_factor,
203 int page_number, bool is_preview, WebKit::WebFrame* frame, 208 int page_number, bool is_preview, WebKit::WebFrame* frame,
204 scoped_ptr<printing::Metafile>* metafile); 209 scoped_ptr<printing::Metafile>* metafile);
205 #elif defined(OS_MACOSX) 210 #elif defined(OS_MACOSX)
206 void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area, 211 void RenderPage(const gfx::Size& page_size, const gfx::Rect& content_area,
207 const float& scale_factor, int page_number, 212 const float& scale_factor, int page_number,
208 WebKit::WebFrame* frame, printing::Metafile* metafile); 213 WebKit::WebFrame* frame, printing::Metafile* metafile);
209 #elif defined(OS_POSIX) 214 #elif defined(OS_POSIX)
210 bool RenderPages(const PrintMsg_PrintPages_Params& params, 215 bool RenderPages(const PrintMsg_PrintPages_Params& params,
211 WebKit::WebFrame* frame, 216 WebKit::WebFrame* frame, WebKit::WebNode* node,
212 WebKit::WebNode* node, 217 int* page_count, printing::Metafile* metafile);
213 int* page_count,
214 printing::Metafile* metafile,
215 bool is_preview);
216 #endif // defined(OS_WIN) 218 #endif // defined(OS_WIN)
217 219
218 // Helper methods ----------------------------------------------------------- 220 // Helper methods -----------------------------------------------------------
219 221
220 bool CopyAndPrint(WebKit::WebFrame* web_frame); 222 bool CopyAndPrint(WebKit::WebFrame* web_frame);
221 223
222 bool CopyMetafileDataToSharedMem(printing::Metafile* metafile, 224 bool CopyMetafileDataToSharedMem(printing::Metafile* metafile,
223 base::SharedMemoryHandle* shared_mem_handle); 225 base::SharedMemoryHandle* shared_mem_handle);
224 226
225 void GetPageSizeAndMarginsInPoints( 227 void GetPageSizeAndMarginsInPoints(
226 WebKit::WebFrame* frame, 228 WebKit::WebFrame* frame,
227 int page_index, 229 int page_index,
228 const PrintMsg_Print_Params& default_params, 230 const PrintMsg_Print_Params& default_params,
229 double* content_width_in_points, 231 double* content_width_in_points,
230 double* content_height_in_points, 232 double* content_height_in_points,
231 double* margin_top_in_points, 233 double* margin_top_in_points,
232 double* margin_right_in_points, 234 double* margin_right_in_points,
233 double* margin_bottom_in_points, 235 double* margin_bottom_in_points,
234 double* margin_left_in_points); 236 double* margin_left_in_points);
235 237
236 bool IsModifiable(WebKit::WebFrame* frame, WebKit::WebNode* node);
237
238 void UpdatePrintableSizeInPrintParameters(WebKit::WebFrame* frame, 238 void UpdatePrintableSizeInPrintParameters(WebKit::WebFrame* frame,
239 WebKit::WebNode* node, 239 WebKit::WebNode* node,
240 PrintMsg_Print_Params* params); 240 PrintMsg_Print_Params* params);
241 241
242 bool GetPrintFrame(WebKit::WebFrame** frame); 242 bool GetPrintFrame(WebKit::WebFrame** frame);
243 243
244 // This reports the current time - |start_time| as the time to render 244 // This reports the current time - |start_time| as the time to render a page.
245 // a page and returns the current time. 245 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 246
252 // Script Initiated Printing ------------------------------------------------ 247 // Script Initiated Printing ------------------------------------------------
253 248
254 // Returns true if script initiated printing occurs too often. 249 // Returns true if script initiated printing occurs too often.
255 bool IsScriptInitiatedPrintTooFrequent(WebKit::WebFrame* frame); 250 bool IsScriptInitiatedPrintTooFrequent(WebKit::WebFrame* frame);
256 251
257 // Reset the counter for script initiated printing. 252 // Reset the counter for script initiated printing.
258 // Scripted printing will be allowed to continue. 253 // Scripted printing will be allowed to continue.
259 void ResetScriptedPrintCount(); 254 void ResetScriptedPrintCount();
260 255
261 // Increment the counter for script initiated printing. 256 // Increment the counter for script initiated printing.
262 // Scripted printing will be blocked for a limited amount of time. 257 // Scripted printing will be blocked for a limited amount of time.
263 void IncrementScriptedPrintCount(); 258 void IncrementScriptedPrintCount();
264 259
265 // Displays the print job error message to the user. 260 // Displays the print job error message to the user.
266 void DisplayPrintJobError(); 261 void DisplayPrintJobError();
267 262
268 void RequestPrintPreview(); 263 void RequestPrintPreview();
269 264
270 // Called every time print preview renders a page. Notify the browser of the 265 // Notify the browser a print preview page has been rendered.
271 // event and check if print preview should be cancelled. Returns false if 266 // |page_number| is 0-based.
272 // print preview has been cancelled. |page_number| is 0-based, or negative to 267 void PreviewPageRendered(int page_number);
273 // indicate its a cancel check only.
274 bool PreviewPageRendered(int page_number);
275 268
276 WebKit::WebView* print_web_view_; 269 WebKit::WebView* print_web_view_;
277 270
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_; 271 scoped_ptr<PrintMsg_PrintPages_Params> print_pages_params_;
285 bool is_preview_; 272 bool is_preview_;
286 273
287 // Used for scripted initiated printing blocking. 274 // Used for scripted initiated printing blocking.
288 base::Time last_cancelled_script_print_; 275 base::Time last_cancelled_script_print_;
289 int user_cancelled_scripted_print_count_; 276 int user_cancelled_scripted_print_count_;
290 277
291 // Let the browser process know of a printing failure. Only set to false when 278 // 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. 279 // the failure came from the browser in the first place.
293 bool notify_browser_of_print_failure_; 280 bool notify_browser_of_print_failure_;
294 281
295 int preview_page_count_;
296 scoped_ptr<PrintMsg_PrintPages_Params> old_print_pages_params_; 282 scoped_ptr<PrintMsg_PrintPages_Params> old_print_pages_params_;
297 283
284 // Keeps track of the state of print preview between messages.
285 class PrintPreviewContext {
286 public:
287 PrintPreviewContext();
288
289 // Initializes the print preview context. Need to be called to set
290 // the |web_frame| / |web_node| to generate the print preview for.
291 void InitWithFrame(WebKit::WebFrame* web_frame);
292 void InitWithNode(const WebKit::WebNode& web_node);
293
294 // Does bookkeeping at the beginning of print preview.
295 void OnPrintPreview();
296
297 // Create the print preview document. |pages| is empty to print all pages.
298 bool CreatePreviewDocument(const PrintMsg_Print_Params& params,
299 const std::vector<int>& pages);
300
301 // Called after a page gets rendered. |page_time| is how long the
302 // rendering took.
303 void RenderedPreviewPage(const base::TimeDelta& page_time);
304
305 // Finalizes the print preview document.
306 void FinalizePreviewDocument();
307
308 // Cleanup after print preview finishes.
309 void Finished();
310
311 // Abort the print preview.
312 void Abort();
313
314 // Cancel the print preview.
315 void Cancel();
316
317 // Helper functions
318 int GetNextPageNumber();
319 bool IsReadyToRender() const;
320 bool IsBusy() const;
321 bool IsModifiable() const;
322
323 // Getters
324 WebKit::WebFrame* frame() const;
325 WebKit::WebNode* node() const;
326 int total_page_count() const;
327 printing::Metafile* metafile() const;
328 const PrintMsg_Print_Params& print_params() const;
329 const gfx::Size& GetPrintCanvasSize() const;
330
331 private:
332 enum State {
333 UNINITIALIZED, // Not ready to render.
334 INITIALIZED, // Ready to render.
335 RENDERING, // Rendering.
336 DONE // Finished rendering.
337 };
338
339 // Reset the internal rendering state.
340 void ResetRenderingState();
341
342 // Specifies what to render for print preview.
343 WebKit::WebFrame* frame_;
344 scoped_ptr<WebKit::WebNode> node_;
345
346 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
347 scoped_ptr<printing::Metafile> metafile_;
348 scoped_ptr<PrintMsg_Print_Params> print_params_;
349
350 // Total page count in the renderer.
351 int total_page_count_;
352
353 // Number of pages to print.
kmadhusu 2011/07/15 19:25:47 // Actual number of pages that needs to be rendere
Lei Zhang 2011/07/15 20:23:35 Done.
354 int actual_page_count_;
355
356 // The current page to print.
kmadhusu 2011/07/15 19:25:47 s/print/render
Lei Zhang 2011/07/15 20:23:35 Done.
357 int current_page_number_;
358
359 // Array to keep track of which pages have been printed.
360 std::vector<bool> rendered_pages_;
361
362 base::TimeDelta document_render_time_;
363 base::TimeTicks begin_time_;
364
365 State state_;
366 };
367
368 PrintPreviewContext print_preview_context_;
369
298 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); 370 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
299 }; 371 };
300 372
301 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ 373 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698