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

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: fix broken test, add more DCHECKs 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
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 virtual void PrintPage(WebKit::WebFrame* frame); 103 virtual void PrintPage(WebKit::WebFrame* frame);
102 104
103 // Message handlers --------------------------------------------------------- 105 // Message handlers ---------------------------------------------------------
104 106
105 // Print the document. 107 // Print the document.
106 void OnPrintPages(); 108 void OnPrintPages();
107 109
108 // Initiate print preview. 110 // Initiate print preview.
109 void OnInitiatePrintPreview(); 111 void OnInitiatePrintPreview();
110 112
111 // Generate a print preview using |settings|. 113 // Start the process of generating a print preview using |settings|.
112 void OnPrintPreview(const base::DictionaryValue& settings); 114 void OnPrintPreview(const base::DictionaryValue& settings);
115 // Initialize the print preview document.
kmadhusu 2011/07/15 01:22:46 nit: Please add blank line after lines 114, 119, 1
Lei Zhang 2011/07/15 01:48:02 This is a very subtle way of showing OnPrintPrevie
116 bool CreatePreviewDocument();
117
118 // Continue generating the print preview.
119 void OnContinuePreview();
120 // Renders a print preview page.
kmadhusu 2011/07/15 01:22:46 Document |page_number|.
Lei Zhang 2011/07/15 01:48:02 Done.
121 void RenderPreviewPage(int page_number);
122 // Finalize the print preview document.
123 bool FinalizePreviewDocument();
124
125 // Cancel the preview to put it into the 'DONE' state.
kmadhusu 2011/07/15 01:22:46 Define "it".
Lei Zhang 2011/07/15 01:48:02 Done.
126 void OnCancelPreview();
127
128 // Abort the preview to put it into the 'UNINITIALIZED' state.
kmadhusu 2011/07/15 01:22:46 Define "it".
Lei Zhang 2011/07/15 01:48:02 Done.
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 class PrintPreviewContext {
kmadhusu 2011/07/15 01:22:46 Document this class.
Lei Zhang 2011/07/15 01:48:02 Done.
285 public:
286 PrintPreviewContext();
kmadhusu 2011/07/15 01:22:46 destructor??
Lei Zhang 2011/07/15 01:48:02 Got nothing to destroy.
287
288 // Initializes the print preview context. Need to be called to set
289 // the |web_frame| / |web_node| to generate the print preview for.
290 void InitWithFrame(WebKit::WebFrame* web_frame);
291 void InitWithNode(const WebKit::WebNode& web_node);
292
293 // Does bookkeeping at the beginning of print preview.
294 void OnPrintPreview();
295
296 // Create the print preview document.
297 bool CreatePreviewDocument(const PrintMsg_Print_Params& params,
298 const std::vector<int>& pages);
kmadhusu 2011/07/15 01:22:46 document |pages|.
Lei Zhang 2011/07/15 01:48:02 Done.
299
300 // Called after a page gets rendered. |page_time| is how long the
301 // rendering took.
302 void RenderedPreviewPage(const base::TimeDelta& page_time);
303
304 // Finalizes the print preview document.
305 void FinalizePreviewDocument();
306
307 // Cleanup after print preview finishes.
308 void Finished();
309
310 // Abort the print preview.
311 void Abort();
312
313 // Cancel the print preview.
314 void Cancel();
315
316 // Helper functions
317 bool GetNextPage(int* page_number);
kmadhusu 2011/07/15 01:22:46 How about GetNextPageNumber? Rather than returning
Lei Zhang 2011/07/15 01:48:02 Done.
318 bool IsInitialized() const;
319 bool IsBusy() const;
320 bool IsModifiable() const;
321
322 // Getters
323 WebKit::WebFrame* frame() const;
324 WebKit::WebNode* node() const;
325 int page_count() const;
326 printing::Metafile* metafile() const;
327 const PrintMsg_Print_Params& print_params() const;
328 const gfx::Size& GetPrintCanvasSize() const;
329
330 private:
331 enum State {
332 UNINITIALIZED, // Not ready to print.
333 INITIALIZED, // Ready to print.
334 PRINTING, // Printing.
kmadhusu 2011/07/15 01:22:46 Since we are not actually printing, can you change
Lei Zhang 2011/07/15 01:48:02 Done.
335 DONE // Finished printing.
336 };
337
338 void Reset();
kmadhusu 2011/07/15 01:22:46 Document the purpose of this function.
Lei Zhang 2011/07/15 01:48:02 Done.
339
340 // Specifies what to print.
kmadhusu 2011/07/15 01:22:46 In PrintPreviewContext class: s/print/print previe
Lei Zhang 2011/07/15 01:48:02 Done.
341 WebKit::WebFrame* frame_;
342 scoped_ptr<WebKit::WebNode> node_;
343
344 scoped_ptr<PrepareFrameAndViewForPrint> prep_frame_view_;
345 scoped_ptr<printing::Metafile> metafile_;
346 scoped_ptr<PrintMsg_Print_Params> print_params_;
347
348 // Total page count in the renderer.
349 int page_count_;
kmadhusu 2011/07/15 01:22:46 To make it clear: page_count_ => total_page_count_
Lei Zhang 2011/07/15 01:48:02 Done.
350
351 // Number of pages to print.
352 int actual_page_count_;
353
354 // The current page to print.
355 int current_page_;
kmadhusu 2011/07/15 01:22:46 current_page => current_page_number ?
Lei Zhang 2011/07/15 01:48:02 Done.
356
357 // Array to keep track of which pages have been printed.
358 std::vector<bool> rendered_pages_;
359
360 base::TimeDelta document_render_time_;
361 base::TimeTicks begin_time_;
362
363 State state_;
364 };
365
366 PrintPreviewContext print_preview_context_;
367
298 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper); 368 DISALLOW_COPY_AND_ASSIGN(PrintWebViewHelper);
299 }; 369 };
300 370
301 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_ 371 #endif // CHROME_RENDERER_PRINT_WEB_VIEW_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698