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 #include "chrome/renderer/print_web_view_helper.h" | 5 #include "chrome/renderer/print_web_view_helper.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/print_messages.h" | 15 #include "chrome/common/print_messages.h" |
16 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
17 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
18 #include "chrome/renderer/prerender/prerender_helper.h" | 18 #include "chrome/renderer/prerender/prerender_helper.h" |
19 #include "content/renderer/render_view.h" | 19 #include "content/renderer/render_view.h" |
20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
21 #include "printing/metafile.h" | 21 #include "printing/metafile_impl.h" |
22 #include "printing/print_job_constants.h" | 22 #include "printing/print_job_constants.h" |
23 #include "printing/units.h" | 23 #include "printing/units.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 oldParams.pages.size() == newParams.pages.size() && | 90 oldParams.pages.size() == newParams.pages.size() && |
91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), | 91 std::equal(oldParams.pages.begin(), oldParams.pages.end(), |
92 newParams.pages.begin()); | 92 newParams.pages.begin()); |
93 } | 93 } |
94 | 94 |
95 } // namespace | 95 } // namespace |
96 | 96 |
97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 97 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
98 const PrintMsg_Print_Params& print_params, | 98 const PrintMsg_Print_Params& print_params, |
99 WebFrame* frame, | 99 WebFrame* frame, |
100 WebNode* node, | 100 WebNode* node) |
101 WebView* web_view) | |
102 : frame_(frame), | 101 : frame_(frame), |
103 web_view_(web_view), | 102 web_view_(frame->view()), |
104 expected_pages_count_(0), | 103 expected_pages_count_(0), |
105 use_browser_overlays_(true), | 104 use_browser_overlays_(true), |
106 finished_(false) { | 105 finished_(false) { |
107 int dpi = GetDPI(&print_params); | 106 int dpi = GetDPI(&print_params); |
108 print_canvas_size_.set_width( | 107 print_canvas_size_.set_width( |
109 ConvertUnit(print_params.printable_size.width(), dpi, | 108 ConvertUnit(print_params.printable_size.width(), dpi, |
110 print_params.desired_dpi)); | 109 print_params.desired_dpi)); |
111 | 110 |
112 print_canvas_size_.set_height( | 111 print_canvas_size_.set_height( |
113 ConvertUnit(print_params.printable_size.height(), dpi, | 112 ConvertUnit(print_params.printable_size.height(), dpi, |
114 print_params.desired_dpi)); | 113 print_params.desired_dpi)); |
115 | 114 |
116 // Layout page according to printer page size. Since WebKit shrinks the | 115 // Layout page according to printer page size. Since WebKit shrinks the |
117 // size of the page automatically (from 125% to 200%) we trick it to | 116 // size of the page automatically (from 125% to 200%) we trick it to |
118 // think the page is 125% larger so the size of the page is correct for | 117 // think the page is 125% larger so the size of the page is correct for |
119 // minimum (default) scaling. | 118 // minimum (default) scaling. |
120 // This is important for sites that try to fill the page. | 119 // This is important for sites that try to fill the page. |
121 gfx::Size print_layout_size(print_canvas_size_); | 120 gfx::Size print_layout_size(print_canvas_size_); |
122 print_layout_size.set_height(static_cast<int>( | 121 print_layout_size.set_height(static_cast<int>( |
123 static_cast<double>(print_layout_size.height()) * 1.25)); | 122 static_cast<double>(print_layout_size.height()) * 1.25)); |
124 | 123 |
125 if (WebFrame* web_frame = web_view_->mainFrame()) | 124 if (WebFrame* web_frame = web_view_->mainFrame()) |
126 prev_scroll_offset_ = web_frame->scrollOffset(); | 125 prev_scroll_offset_ = web_frame->scrollOffset(); |
127 prev_view_size_ = web_view->size(); | 126 prev_view_size_ = web_view_->size(); |
128 | 127 |
129 web_view->resize(print_layout_size); | 128 web_view_->resize(print_layout_size); |
130 | 129 |
131 WebNode node_to_print; | 130 WebNode node_to_print; |
132 if (node) | 131 if (node) |
133 node_to_print = *node; | 132 node_to_print = *node; |
134 expected_pages_count_ = frame->printBegin( | 133 expected_pages_count_ = frame->printBegin( |
135 print_canvas_size_, node_to_print, static_cast<int>(print_params.dpi), | 134 print_canvas_size_, node_to_print, static_cast<int>(print_params.dpi), |
136 &use_browser_overlays_); | 135 &use_browser_overlays_); |
137 } | 136 } |
138 | 137 |
139 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { | 138 PrepareFrameAndViewForPrint::~PrepareFrameAndViewForPrint() { |
140 FinishPrinting(); | 139 FinishPrinting(); |
141 } | 140 } |
142 | 141 |
143 void PrepareFrameAndViewForPrint::FinishPrinting() { | 142 void PrepareFrameAndViewForPrint::FinishPrinting() { |
144 if (!finished_) { | 143 if (!finished_) { |
145 finished_ = true; | 144 finished_ = true; |
146 frame_->printEnd(); | 145 frame_->printEnd(); |
147 web_view_->resize(prev_view_size_); | 146 web_view_->resize(prev_view_size_); |
148 if (WebFrame* web_frame = web_view_->mainFrame()) | 147 if (WebFrame* web_frame = web_view_->mainFrame()) |
149 web_frame->setScrollOffset(prev_scroll_offset_); | 148 web_frame->setScrollOffset(prev_scroll_offset_); |
150 } | 149 } |
151 } | 150 } |
152 | 151 |
153 PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) | 152 PrintWebViewHelper::PrintWebViewHelper(RenderView* render_view) |
154 : RenderViewObserver(render_view), | 153 : RenderViewObserver(render_view), |
155 RenderViewObserverTracker<PrintWebViewHelper>(render_view), | 154 RenderViewObserverTracker<PrintWebViewHelper>(render_view), |
156 print_web_view_(NULL), | 155 print_web_view_(NULL), |
157 script_initiated_preview_frame_(NULL), | |
158 context_menu_preview_node_(NULL), | |
159 user_cancelled_scripted_print_count_(0), | 156 user_cancelled_scripted_print_count_(0), |
160 notify_browser_of_print_failure_(true), | 157 notify_browser_of_print_failure_(true) { |
161 preview_page_count_(0) { | |
162 is_preview_ = switches::IsPrintPreviewEnabled(); | 158 is_preview_ = switches::IsPrintPreviewEnabled(); |
163 } | 159 } |
164 | 160 |
165 PrintWebViewHelper::~PrintWebViewHelper() {} | 161 PrintWebViewHelper::~PrintWebViewHelper() {} |
166 | 162 |
167 // Prints |frame| which called window.print(). | 163 // Prints |frame| which called window.print(). |
168 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { | 164 void PrintWebViewHelper::PrintPage(WebKit::WebFrame* frame) { |
169 DCHECK(frame); | 165 DCHECK(frame); |
170 | 166 |
171 // Allow Prerendering to cancel this print request if necessary. | 167 // Allow Prerendering to cancel this print request if necessary. |
172 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { | 168 if (prerender::PrerenderHelper::IsPrerendering(render_view())) { |
173 Send(new ViewHostMsg_CancelPrerenderForPrinting(routing_id())); | 169 Send(new ViewHostMsg_CancelPrerenderForPrinting(routing_id())); |
174 return; | 170 return; |
175 } | 171 } |
176 | 172 |
177 if (IsScriptInitiatedPrintTooFrequent(frame)) | 173 if (IsScriptInitiatedPrintTooFrequent(frame)) |
178 return; | 174 return; |
179 IncrementScriptedPrintCount(); | 175 IncrementScriptedPrintCount(); |
180 | 176 |
181 if (is_preview_) { | 177 if (is_preview_) { |
182 script_initiated_preview_frame_ = frame; | 178 print_preview_context_.InitWithFrame(frame); |
183 context_menu_preview_node_.reset(); | |
184 RequestPrintPreview(); | 179 RequestPrintPreview(); |
185 } else { | 180 } else { |
186 Print(frame, NULL); | 181 Print(frame, NULL); |
187 } | 182 } |
188 } | 183 } |
189 | 184 |
190 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 185 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
191 bool handled = true; | 186 bool handled = true; |
192 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 187 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
193 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 188 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
194 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, | 189 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
195 OnInitiatePrintPreview) | |
196 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 190 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
197 OnPrintNodeUnderContextMenu) | 191 OnPrintNodeUnderContextMenu) |
198 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 192 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
199 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | 193 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) |
200 OnPrintForPrintPreview) | |
201 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 194 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
202 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 195 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
203 ResetScriptedPrintCount) | 196 ResetScriptedPrintCount) |
| 197 IPC_MESSAGE_HANDLER(PrintMsg_ContinuePreview, OnContinuePreview) |
| 198 IPC_MESSAGE_HANDLER(PrintMsg_AbortPreview, OnAbortPreview) |
204 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, | 199 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, |
205 DisplayPrintJobError) | 200 DisplayPrintJobError) |
206 IPC_MESSAGE_UNHANDLED(handled = false) | 201 IPC_MESSAGE_UNHANDLED(handled = false) |
207 IPC_END_MESSAGE_MAP() | 202 IPC_END_MESSAGE_MAP() |
208 return handled; | 203 return handled; |
209 } | 204 } |
210 | 205 |
211 void PrintWebViewHelper::OnPrintForPrintPreview( | 206 void PrintWebViewHelper::OnPrintForPrintPreview( |
212 const DictionaryValue& job_settings) { | 207 const DictionaryValue& job_settings) { |
213 DCHECK(is_preview_); | 208 DCHECK(is_preview_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 256 } |
262 | 257 |
263 void PrintWebViewHelper::OnPrintPages() { | 258 void PrintWebViewHelper::OnPrintPages() { |
264 WebFrame* frame; | 259 WebFrame* frame; |
265 if (GetPrintFrame(&frame)) | 260 if (GetPrintFrame(&frame)) |
266 Print(frame, NULL); | 261 Print(frame, NULL); |
267 } | 262 } |
268 | 263 |
269 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 264 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
270 DCHECK(is_preview_); | 265 DCHECK(is_preview_); |
271 DCHECK(!context_menu_preview_node_.get() || !script_initiated_preview_frame_); | 266 print_preview_context_.OnPrintPreview(); |
272 | 267 |
273 if (script_initiated_preview_frame_) { | 268 if (!InitPrintSettings(print_preview_context_.frame(), |
274 // Script initiated print preview. | 269 print_preview_context_.node())) { |
275 PrintPreview(script_initiated_preview_frame_, NULL, settings); | 270 NOTREACHED(); |
276 } else if (context_menu_preview_node_.get()) { | 271 return; |
277 // User initiated - print node under context menu. | |
278 PrintPreview(context_menu_preview_node_->document().frame(), | |
279 context_menu_preview_node_.get(), settings); | |
280 } else { | |
281 // User initiated - normal print preview. | |
282 WebFrame* frame; | |
283 if (GetPrintFrame(&frame)) | |
284 PrintPreview(frame, NULL, settings); | |
285 } | 272 } |
| 273 |
| 274 if (!UpdatePrintSettings(settings)) { |
| 275 DidFinishPrinting(FAIL_PREVIEW); |
| 276 return; |
| 277 } |
| 278 |
| 279 if (print_pages_params_->params.preview_request_id != 0 && |
| 280 old_print_pages_params_.get() && |
| 281 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, |
| 282 *print_pages_params_)) { |
| 283 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 284 preview_params.reuse_existing_data = true; |
| 285 preview_params.data_size = 0; |
| 286 preview_params.document_cookie = |
| 287 print_pages_params_->params.document_cookie; |
| 288 preview_params.expected_pages_count = |
| 289 print_preview_context_.total_page_count(); |
| 290 preview_params.modifiable = print_preview_context_.IsModifiable(); |
| 291 preview_params.preview_request_id = |
| 292 print_pages_params_->params.preview_request_id; |
| 293 |
| 294 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 295 return; |
| 296 } |
| 297 |
| 298 // PDF printer device supports alpha blending. |
| 299 print_pages_params_->params.supports_alpha_blend = true; |
| 300 if (!CreatePreviewDocument()) |
| 301 DidFinishPrinting(FAIL_PREVIEW); |
| 302 } |
| 303 |
| 304 bool PrintWebViewHelper::CreatePreviewDocument() { |
| 305 PrintMsg_Print_Params printParams = print_pages_params_->params; |
| 306 UpdatePrintableSizeInPrintParameters(print_preview_context_.frame(), |
| 307 print_preview_context_.node(), |
| 308 &printParams); |
| 309 |
| 310 const std::vector<int>& pages = print_pages_params_->pages; |
| 311 if (!print_preview_context_.CreatePreviewDocument(printParams, pages)) |
| 312 return false; |
| 313 int page_count = print_preview_context_.total_page_count(); |
| 314 int document_cookie = print_pages_params_->params.document_cookie; |
| 315 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), document_cookie, |
| 316 page_count)); |
| 317 PreviewPageRendered(-1); |
| 318 return true; |
| 319 } |
| 320 |
| 321 void PrintWebViewHelper::OnContinuePreview() { |
| 322 // Spurious message. We already finished/cancelled/aborted the print preview. |
| 323 if (!print_preview_context_.IsBusy()) |
| 324 return; |
| 325 |
| 326 int page_number = print_preview_context_.GetNextPageNumber(); |
| 327 if (page_number >= 0) { |
| 328 // Continue generating the print preview. |
| 329 RenderPreviewPage(page_number); |
| 330 return; |
| 331 } |
| 332 |
| 333 // Finished generating preview. Finalize the document. |
| 334 if (FinalizePreviewDocument()) |
| 335 DidFinishPrinting(OK); |
| 336 else |
| 337 DidFinishPrinting(FAIL_PREVIEW); |
| 338 } |
| 339 |
| 340 void PrintWebViewHelper::OnAbortPreview() { |
| 341 DidFinishPrinting(ABORT_PREVIEW); |
| 342 return; |
| 343 } |
| 344 |
| 345 bool PrintWebViewHelper::FinalizePreviewDocument() { |
| 346 print_preview_context_.FinalizePreviewDocument(); |
| 347 |
| 348 // Get the size of the resulting metafile. |
| 349 printing::Metafile* metafile = print_preview_context_.metafile(); |
| 350 uint32 buf_size = metafile->GetDataSize(); |
| 351 DCHECK_GT(buf_size, 0u); |
| 352 |
| 353 PrintHostMsg_DidPreviewDocument_Params preview_params; |
| 354 preview_params.reuse_existing_data = false; |
| 355 preview_params.data_size = buf_size; |
| 356 preview_params.document_cookie = print_pages_params_->params.document_cookie; |
| 357 preview_params.expected_pages_count = |
| 358 print_preview_context_.total_page_count(); |
| 359 preview_params.modifiable = print_preview_context_.IsModifiable(); |
| 360 preview_params.preview_request_id = |
| 361 print_pages_params_->params.preview_request_id; |
| 362 |
| 363 // Ask the browser to create the shared memory for us. |
| 364 if (!CopyMetafileDataToSharedMem(metafile, |
| 365 &(preview_params.metafile_data_handle))) { |
| 366 return false; |
| 367 } |
| 368 #if defined(OS_WIN) |
| 369 Send(new PrintHostMsg_DuplicateSection(routing_id(), |
| 370 preview_params.metafile_data_handle, |
| 371 &preview_params.metafile_data_handle)); |
| 372 #endif |
| 373 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
| 374 return true; |
286 } | 375 } |
287 | 376 |
288 void PrintWebViewHelper::OnPrintingDone(bool success) { | 377 void PrintWebViewHelper::OnPrintingDone(bool success) { |
289 notify_browser_of_print_failure_ = false; | 378 notify_browser_of_print_failure_ = false; |
290 DidFinishPrinting(success ? OK : FAIL_PRINT); | 379 DidFinishPrinting(success ? OK : FAIL_PRINT); |
291 } | 380 } |
292 | 381 |
293 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { | 382 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { |
294 const WebNode& context_menu_node = render_view()->context_menu_node(); | 383 const WebNode& context_menu_node = render_view()->context_menu_node(); |
295 if (context_menu_node.isNull()) { | 384 if (context_menu_node.isNull()) { |
296 NOTREACHED(); | 385 NOTREACHED(); |
297 return; | 386 return; |
298 } | 387 } |
299 | 388 |
300 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets | 389 // Make a copy of the node, in case RenderView::OnContextMenuClosed resets |
301 // its |context_menu_node_|. | 390 // its |context_menu_node_|. |
302 if (is_preview_) { | 391 if (is_preview_) { |
303 context_menu_preview_node_.reset(new WebNode(context_menu_node)); | 392 print_preview_context_.InitWithNode(context_menu_node); |
304 script_initiated_preview_frame_ = NULL; | |
305 RequestPrintPreview(); | 393 RequestPrintPreview(); |
306 } else { | 394 } else { |
307 WebNode duplicate_node(context_menu_node); | 395 WebNode duplicate_node(context_menu_node); |
308 Print(duplicate_node.document().frame(), &duplicate_node); | 396 Print(duplicate_node.document().frame(), &duplicate_node); |
309 } | 397 } |
310 } | 398 } |
311 | 399 |
312 void PrintWebViewHelper::OnInitiatePrintPreview() { | 400 void PrintWebViewHelper::OnInitiatePrintPreview() { |
313 DCHECK(is_preview_); | 401 DCHECK(is_preview_); |
314 script_initiated_preview_frame_ = NULL; | 402 WebFrame* frame; |
315 context_menu_preview_node_.reset(); | 403 if (GetPrintFrame(&frame)) { |
316 RequestPrintPreview(); | 404 print_preview_context_.InitWithFrame(frame); |
| 405 RequestPrintPreview(); |
| 406 } |
317 } | 407 } |
318 | 408 |
319 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { | 409 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, WebKit::WebNode* node) { |
320 // If still not finished with earlier print request simply ignore. | 410 // If still not finished with earlier print request simply ignore. |
321 if (print_web_view_) | 411 if (print_web_view_) |
322 return; | 412 return; |
323 | 413 |
324 // Initialize print settings. | 414 // Initialize print settings. |
325 if (!InitPrintSettings(frame, node)) | 415 if (!InitPrintSettings(frame, node)) |
326 return; // Failed to init print page settings. | 416 return; // Failed to init print page settings. |
327 | 417 |
328 int expected_pages_count = 0; | 418 int expected_pages_count = 0; |
329 bool use_browser_overlays = true; | 419 bool use_browser_overlays = true; |
330 | 420 |
331 // Prepare once to calculate the estimated page count. This must be in | 421 // Prepare once to calculate the estimated page count. This must be in |
332 // a scope for itself (see comments on PrepareFrameAndViewForPrint). | 422 // a scope for itself (see comments on PrepareFrameAndViewForPrint). |
333 { | 423 { |
334 PrepareFrameAndViewForPrint prep_frame_view( | 424 PrepareFrameAndViewForPrint prep_frame_view( |
335 print_pages_params_->params, frame, node, frame->view()); | 425 print_pages_params_->params, frame, node); |
336 expected_pages_count = prep_frame_view.GetExpectedPageCount(); | 426 expected_pages_count = prep_frame_view.GetExpectedPageCount(); |
337 if (expected_pages_count) | 427 if (expected_pages_count) |
338 use_browser_overlays = prep_frame_view.ShouldUseBrowserOverlays(); | 428 use_browser_overlays = prep_frame_view.ShouldUseBrowserOverlays(); |
339 } | 429 } |
340 | 430 |
341 // Some full screen plugins can say they don't want to print. | 431 // Some full screen plugins can say they don't want to print. |
342 if (!expected_pages_count) { | 432 if (!expected_pages_count) { |
343 DidFinishPrinting(OK); // Release resources and fail silently. | 433 DidFinishPrinting(OK); // Release resources and fail silently. |
344 return; | 434 return; |
345 } | 435 } |
346 | 436 |
347 // Ask the browser to show UI to retrieve the final print settings. | 437 // Ask the browser to show UI to retrieve the final print settings. |
348 if (!GetPrintSettingsFromUser(frame, expected_pages_count, | 438 if (!GetPrintSettingsFromUser(frame, expected_pages_count, |
349 use_browser_overlays)) { | 439 use_browser_overlays)) { |
350 DidFinishPrinting(OK); // Release resources and fail silently. | 440 DidFinishPrinting(OK); // Release resources and fail silently. |
351 return; | 441 return; |
352 } | 442 } |
353 | 443 |
354 // Render Pages for printing. | 444 // Render Pages for printing. |
355 if (!RenderPagesForPrint(frame, node)) | 445 if (!RenderPagesForPrint(frame, node)) |
356 DidFinishPrinting(FAIL_PRINT); | 446 DidFinishPrinting(FAIL_PRINT); |
357 ResetScriptedPrintCount(); | 447 ResetScriptedPrintCount(); |
358 } | 448 } |
359 | 449 |
360 void PrintWebViewHelper::PrintPreview(WebKit::WebFrame* frame, | |
361 WebKit::WebNode* node, | |
362 const DictionaryValue& settings) { | |
363 DCHECK(is_preview_); | |
364 | |
365 if (!InitPrintSettings(frame, node)) { | |
366 NOTREACHED() << "Failed to initialize print page settings"; | |
367 return; | |
368 } | |
369 | |
370 if (!UpdatePrintSettings(settings)) { | |
371 DidFinishPrinting(FAIL_PREVIEW); | |
372 return; | |
373 } | |
374 | |
375 if (print_pages_params_->params.preview_request_id != 0 && | |
376 old_print_pages_params_.get() && | |
377 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, | |
378 *print_pages_params_)) { | |
379 PrintHostMsg_DidPreviewDocument_Params preview_params; | |
380 preview_params.reuse_existing_data = true; | |
381 preview_params.data_size = 0; | |
382 preview_params.document_cookie = | |
383 print_pages_params_->params.document_cookie; | |
384 preview_params.expected_pages_count = preview_page_count_; | |
385 preview_params.modifiable = IsModifiable(frame, node); | |
386 preview_params.preview_request_id = | |
387 print_pages_params_->params.preview_request_id; | |
388 | |
389 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | |
390 return; | |
391 } | |
392 | |
393 // Render Pages for preview. | |
394 if (!RenderPagesForPreview(frame, node)) | |
395 DidFinishPrinting(FAIL_PREVIEW); | |
396 } | |
397 | |
398 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { | 450 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { |
399 bool store_print_pages_params = true; | 451 bool store_print_pages_params = true; |
400 if (result == FAIL_PRINT) { | 452 if (result == FAIL_PRINT) { |
401 DisplayPrintJobError(); | 453 DisplayPrintJobError(); |
402 | 454 |
403 if (notify_browser_of_print_failure_) { | 455 if (notify_browser_of_print_failure_) { |
404 int cookie = print_pages_params_->params.document_cookie; | 456 int cookie = print_pages_params_->params.document_cookie; |
405 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 457 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
406 } | 458 } |
407 } else if (result == FAIL_PREVIEW) { | 459 } else if (result == FAIL_PREVIEW) { |
| 460 DCHECK(is_preview_); |
| 461 store_print_pages_params = false; |
408 int cookie = print_pages_params_->params.document_cookie; | 462 int cookie = print_pages_params_->params.document_cookie; |
| 463 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 464 print_preview_context_.Abort(); |
| 465 } else if (result == ABORT_PREVIEW) { |
| 466 DCHECK(is_preview_); |
409 store_print_pages_params = false; | 467 store_print_pages_params = false; |
410 if (notify_browser_of_print_failure_) { | 468 print_preview_context_.Abort(); |
411 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 469 } else if (result == OK && is_preview_) { |
412 } else { | 470 print_preview_context_.Finished(); |
413 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); | |
414 } | |
415 } | 471 } |
416 | 472 |
417 if (print_web_view_) { | 473 if (print_web_view_) { |
418 print_web_view_->close(); | 474 print_web_view_->close(); |
419 print_web_view_ = NULL; | 475 print_web_view_ = NULL; |
420 } | 476 } |
421 | 477 |
422 if (store_print_pages_params) { | 478 if (store_print_pages_params) { |
423 old_print_pages_params_.reset(print_pages_params_.release()); | 479 old_print_pages_params_.reset(print_pages_params_.release()); |
424 } else { | 480 } else { |
(...skipping 30 matching lines...) Expand all Loading... |
455 return true; | 511 return true; |
456 } | 512 } |
457 | 513 |
458 #if defined(OS_MACOSX) || defined(OS_WIN) | 514 #if defined(OS_MACOSX) || defined(OS_WIN) |
459 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, | 515 bool PrintWebViewHelper::PrintPages(const PrintMsg_PrintPages_Params& params, |
460 WebFrame* frame, | 516 WebFrame* frame, |
461 WebNode* node) { | 517 WebNode* node) { |
462 PrintMsg_Print_Params printParams = params.params; | 518 PrintMsg_Print_Params printParams = params.params; |
463 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); | 519 UpdatePrintableSizeInPrintParameters(frame, node, &printParams); |
464 | 520 |
465 PrepareFrameAndViewForPrint prep_frame_view(printParams, | 521 PrepareFrameAndViewForPrint prep_frame_view(printParams, frame, node); |
466 frame, | |
467 node, | |
468 frame->view()); | |
469 int page_count = prep_frame_view.GetExpectedPageCount(); | 522 int page_count = prep_frame_view.GetExpectedPageCount(); |
470 if (!page_count) | 523 if (!page_count) |
471 return false; | 524 return false; |
472 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), | 525 Send(new PrintHostMsg_DidGetPrintedPagesCount(routing_id(), |
473 printParams.document_cookie, | 526 printParams.document_cookie, |
474 page_count)); | 527 page_count)); |
475 | 528 |
476 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); | 529 const gfx::Size& canvas_size = prep_frame_view.GetPrintCanvasSize(); |
477 PrintMsg_PrintPage_Params page_params; | 530 PrintMsg_PrintPage_Params page_params; |
478 page_params.params = printParams; | 531 page_params.params = printParams; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
568 *margin_right_in_points = | 621 *margin_right_in_points = |
569 ConvertPixelsToPointDouble(margin_right_in_pixels); | 622 ConvertPixelsToPointDouble(margin_right_in_pixels); |
570 if (margin_bottom_in_points) | 623 if (margin_bottom_in_points) |
571 *margin_bottom_in_points = | 624 *margin_bottom_in_points = |
572 ConvertPixelsToPointDouble(margin_bottom_in_pixels); | 625 ConvertPixelsToPointDouble(margin_bottom_in_pixels); |
573 if (margin_left_in_points) | 626 if (margin_left_in_points) |
574 *margin_left_in_points = | 627 *margin_left_in_points = |
575 ConvertPixelsToPointDouble(margin_left_in_pixels); | 628 ConvertPixelsToPointDouble(margin_left_in_pixels); |
576 } | 629 } |
577 | 630 |
578 bool PrintWebViewHelper::IsModifiable(WebKit::WebFrame* frame, | |
579 WebKit::WebNode* node) { | |
580 if (node) | |
581 return false; | |
582 std::string mime(frame->dataSource()->response().mimeType().utf8()); | |
583 if (mime == "application/pdf") | |
584 return false; | |
585 return true; | |
586 } | |
587 | |
588 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( | 631 void PrintWebViewHelper::UpdatePrintableSizeInPrintParameters( |
589 WebFrame* frame, | 632 WebFrame* frame, |
590 WebNode* node, | 633 WebNode* node, |
591 PrintMsg_Print_Params* params) { | 634 PrintMsg_Print_Params* params) { |
592 double content_width_in_points; | 635 double content_width_in_points; |
593 double content_height_in_points; | 636 double content_height_in_points; |
594 double margin_top_in_points; | 637 double margin_top_in_points; |
595 double margin_right_in_points; | 638 double margin_right_in_points; |
596 double margin_bottom_in_points; | 639 double margin_bottom_in_points; |
597 double margin_left_in_points; | 640 double margin_left_in_points; |
598 PrepareFrameAndViewForPrint prepare(*params, frame, node, frame->view()); | 641 PrepareFrameAndViewForPrint prepare(*params, frame, node); |
599 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, | 642 PrintWebViewHelper::GetPageSizeAndMarginsInPoints(frame, 0, *params, |
600 &content_width_in_points, &content_height_in_points, | 643 &content_width_in_points, &content_height_in_points, |
601 &margin_top_in_points, &margin_right_in_points, | 644 &margin_top_in_points, &margin_right_in_points, |
602 &margin_bottom_in_points, &margin_left_in_points); | 645 &margin_bottom_in_points, &margin_left_in_points); |
603 int dpi = GetDPI(params); | 646 int dpi = GetDPI(params); |
604 params->printable_size = gfx::Size( | 647 params->printable_size = gfx::Size( |
605 static_cast<int>(ConvertUnitDouble(content_width_in_points, | 648 static_cast<int>(ConvertUnitDouble(content_width_in_points, |
606 printing::kPointsPerInch, dpi)), | 649 printing::kPointsPerInch, dpi)), |
607 static_cast<int>(ConvertUnitDouble(content_height_in_points, | 650 static_cast<int>(ConvertUnitDouble(content_height_in_points, |
608 printing::kPointsPerInch, dpi))); | 651 printing::kPointsPerInch, dpi))); |
(...skipping 10 matching lines...) Expand all Loading... |
619 page_height_in_points, printing::kPointsPerInch, dpi))); | 662 page_height_in_points, printing::kPointsPerInch, dpi))); |
620 | 663 |
621 params->margin_top = static_cast<int>(ConvertUnitDouble( | 664 params->margin_top = static_cast<int>(ConvertUnitDouble( |
622 margin_top_in_points, printing::kPointsPerInch, dpi)); | 665 margin_top_in_points, printing::kPointsPerInch, dpi)); |
623 params->margin_left = static_cast<int>(ConvertUnitDouble( | 666 params->margin_left = static_cast<int>(ConvertUnitDouble( |
624 margin_left_in_points, printing::kPointsPerInch, dpi)); | 667 margin_left_in_points, printing::kPointsPerInch, dpi)); |
625 } | 668 } |
626 | 669 |
627 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, | 670 bool PrintWebViewHelper::InitPrintSettings(WebKit::WebFrame* frame, |
628 WebKit::WebNode* node) { | 671 WebKit::WebNode* node) { |
| 672 DCHECK(frame); |
629 PrintMsg_PrintPages_Params settings; | 673 PrintMsg_PrintPages_Params settings; |
630 | 674 |
631 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our | 675 // TODO(abodenha@chromium.org) It doesn't make sense to do this if our |
632 // "default" is a cloud based printer. Split InitPrintSettings up | 676 // "default" is a cloud based printer. Split InitPrintSettings up |
633 // so that we can avoid the overhead of unneeded calls into the native | 677 // so that we can avoid the overhead of unneeded calls into the native |
634 // print system. | 678 // print system. |
635 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), | 679 Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), |
636 &settings.params)); | 680 &settings.params)); |
637 // Check if the printer returned any settings, if the settings is empty, we | 681 // Check if the printer returned any settings, if the settings is empty, we |
638 // can safely assume there are no printer drivers configured. So we safely | 682 // can safely assume there are no printer drivers configured. So we safely |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
744 WebKit::WebNode* node) { | 788 WebKit::WebNode* node) { |
745 PrintMsg_PrintPages_Params print_settings = *print_pages_params_; | 789 PrintMsg_PrintPages_Params print_settings = *print_pages_params_; |
746 if (print_settings.params.selection_only) { | 790 if (print_settings.params.selection_only) { |
747 return CopyAndPrint(frame); | 791 return CopyAndPrint(frame); |
748 } else { | 792 } else { |
749 // TODO: Always copy before printing. | 793 // TODO: Always copy before printing. |
750 return PrintPages(print_settings, frame, node); | 794 return PrintPages(print_settings, frame, node); |
751 } | 795 } |
752 } | 796 } |
753 | 797 |
754 bool PrintWebViewHelper::RenderPagesForPreview(WebKit::WebFrame* frame, | |
755 WebKit::WebNode* node) { | |
756 PrintMsg_PrintPages_Params print_settings = *print_pages_params_; | |
757 // PDF printer device supports alpha blending. | |
758 print_settings.params.supports_alpha_blend = true; | |
759 // TODO(kmadhusu): Handle print selection. | |
760 return CreatePreviewDocument(print_settings, frame, node); | |
761 } | |
762 | |
763 base::TimeTicks PrintWebViewHelper::ReportPreviewPageRenderTime( | |
764 base::TimeTicks start_time) { | |
765 base::TimeTicks now = base::TimeTicks::Now(); | |
766 UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", now - start_time); | |
767 return now; | |
768 } | |
769 | |
770 void PrintWebViewHelper::ReportTotalPreviewGenerationTime( | |
771 int selected_pages_length, int total_pages, | |
772 base::TimeDelta render_time, base::TimeDelta total_time) { | |
773 if (selected_pages_length == 0) | |
774 selected_pages_length = total_pages; | |
775 | |
776 if (selected_pages_length <= 0) { | |
777 // This shouldn't happen, but this makes sure it doesn't affect the | |
778 // statistics if it does. | |
779 return; | |
780 } | |
781 | |
782 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime", | |
783 render_time); | |
784 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime", | |
785 total_time); | |
786 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", | |
787 total_time / selected_pages_length); | |
788 } | |
789 | |
790 #if defined(OS_POSIX) | 798 #if defined(OS_POSIX) |
791 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( | 799 bool PrintWebViewHelper::CopyMetafileDataToSharedMem( |
792 printing::Metafile* metafile, | 800 printing::Metafile* metafile, |
793 base::SharedMemoryHandle* shared_mem_handle) { | 801 base::SharedMemoryHandle* shared_mem_handle) { |
794 uint32 buf_size = metafile->GetDataSize(); | 802 uint32 buf_size = metafile->GetDataSize(); |
795 base::SharedMemoryHandle mem_handle; | 803 base::SharedMemoryHandle mem_handle; |
796 Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, &mem_handle)); | 804 Send(new ViewHostMsg_AllocateSharedMemoryBuffer(buf_size, &mem_handle)); |
797 if (base::SharedMemory::IsHandleValid(mem_handle)) { | 805 if (base::SharedMemory::IsHandleValid(mem_handle)) { |
798 base::SharedMemory shared_buf(mem_handle, false); | 806 base::SharedMemory shared_buf(mem_handle, false); |
799 if (shared_buf.Map(buf_size)) { | 807 if (shared_buf.Map(buf_size)) { |
800 metafile->GetData(shared_buf.memory(), buf_size); | 808 metafile->GetData(shared_buf.memory(), buf_size); |
801 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | 809 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
802 shared_mem_handle); | 810 shared_mem_handle); |
803 return true; | 811 return true; |
804 } | 812 } |
805 } | 813 } |
806 NOTREACHED(); | 814 NOTREACHED(); |
807 return false; | 815 return false; |
808 } | 816 } |
809 #endif // defined(OS_POSIX) | 817 #endif // defined(OS_POSIX) |
810 | 818 |
811 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent( | 819 bool PrintWebViewHelper::IsScriptInitiatedPrintTooFrequent( |
812 WebKit::WebFrame* frame) { | 820 WebKit::WebFrame* frame) { |
813 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 821 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
814 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32; | 822 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 32; |
| 823 bool too_frequent = false; |
815 | 824 |
816 // Check if there is script repeatedly trying to print and ignore it if too | 825 // Check if there is script repeatedly trying to print and ignore it if too |
817 // frequent. The first 3 times, we use a constant wait time, but if this | 826 // frequent. The first 3 times, we use a constant wait time, but if this |
818 // gets excessive, we switch to exponential wait time. So for a page that | 827 // gets excessive, we switch to exponential wait time. So for a page that |
819 // calls print() in a loop the user will need to cancel the print dialog | 828 // calls print() in a loop the user will need to cancel the print dialog |
820 // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds. | 829 // after: [2, 2, 2, 4, 8, 16, 32, 32, ...] seconds. |
821 // This gives the user time to navigate from the page. | 830 // This gives the user time to navigate from the page. |
822 if (user_cancelled_scripted_print_count_ > 0) { | 831 if (user_cancelled_scripted_print_count_ > 0) { |
823 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; | 832 base::TimeDelta diff = base::Time::Now() - last_cancelled_script_print_; |
824 int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint; | 833 int min_wait_seconds = kMinSecondsToIgnoreJavascriptInitiatedPrint; |
825 if (user_cancelled_scripted_print_count_ > 3) { | 834 if (user_cancelled_scripted_print_count_ > 3) { |
826 min_wait_seconds = std::min( | 835 min_wait_seconds = std::min( |
827 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 836 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
828 (user_cancelled_scripted_print_count_ - 3), | 837 (user_cancelled_scripted_print_count_ - 3), |
829 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 838 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
830 } | 839 } |
831 if (diff.InSeconds() < min_wait_seconds) { | 840 if (diff.InSeconds() < min_wait_seconds) { |
832 WebString message(WebString::fromUTF8( | 841 too_frequent = true; |
833 "Ignoring too frequent calls to print().")); | |
834 frame->addMessageToConsole(WebConsoleMessage( | |
835 WebConsoleMessage::LevelWarning, | |
836 message)); | |
837 return true; | |
838 } | 842 } |
839 } | 843 } |
840 return false; | 844 |
| 845 if (!too_frequent && print_preview_context_.IsBusy()) |
| 846 too_frequent = true; |
| 847 |
| 848 if (!too_frequent) |
| 849 return false; |
| 850 |
| 851 WebString message(WebString::fromUTF8( |
| 852 "Ignoring too frequent calls to print().")); |
| 853 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, |
| 854 message)); |
| 855 return true; |
841 } | 856 } |
842 | 857 |
843 void PrintWebViewHelper::ResetScriptedPrintCount() { | 858 void PrintWebViewHelper::ResetScriptedPrintCount() { |
844 // Reset cancel counter on successful print. | 859 // Reset cancel counter on successful print. |
845 user_cancelled_scripted_print_count_ = 0; | 860 user_cancelled_scripted_print_count_ = 0; |
846 } | 861 } |
847 | 862 |
848 void PrintWebViewHelper::IncrementScriptedPrintCount() { | 863 void PrintWebViewHelper::IncrementScriptedPrintCount() { |
849 ++user_cancelled_scripted_print_count_; | 864 ++user_cancelled_scripted_print_count_; |
850 last_cancelled_script_print_ = base::Time::Now(); | 865 last_cancelled_script_print_ = base::Time::Now(); |
851 } | 866 } |
852 | 867 |
853 void PrintWebViewHelper::DisplayPrintJobError() { | 868 void PrintWebViewHelper::DisplayPrintJobError() { |
854 WebView* web_view = print_web_view_; | 869 WebView* web_view = print_web_view_; |
855 if (!web_view) | 870 if (!web_view) |
856 web_view = render_view()->webview(); | 871 web_view = render_view()->webview(); |
857 | 872 |
858 render_view()->runModalAlertDialog( | 873 render_view()->runModalAlertDialog( |
859 web_view->mainFrame(), | 874 web_view->mainFrame(), |
860 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 875 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
861 } | 876 } |
862 | 877 |
863 void PrintWebViewHelper::RequestPrintPreview() { | 878 void PrintWebViewHelper::RequestPrintPreview() { |
864 old_print_pages_params_.reset(); | 879 old_print_pages_params_.reset(); |
865 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 880 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
866 } | 881 } |
867 | 882 |
868 bool PrintWebViewHelper::PreviewPageRendered(int page_number) { | 883 void PrintWebViewHelper::PreviewPageRendered(int page_number) { |
869 bool cancel = false; | 884 Send(new PrintHostMsg_DidPreviewPage(routing_id(), page_number)); |
870 Send(new PrintHostMsg_DidPreviewPage(routing_id(), page_number, &cancel)); | |
871 if (cancel) | |
872 notify_browser_of_print_failure_ = false; | |
873 return !cancel; | |
874 } | 885 } |
| 886 |
| 887 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
| 888 : frame_(NULL), |
| 889 total_page_count_(0), |
| 890 actual_page_count_(0), |
| 891 current_page_number_(0), |
| 892 state_(UNINITIALIZED) { |
| 893 } |
| 894 |
| 895 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
| 896 } |
| 897 |
| 898 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
| 899 WebKit::WebFrame* web_frame) { |
| 900 DCHECK(web_frame); |
| 901 if (IsReadyToRender()) |
| 902 return; |
| 903 state_ = INITIALIZED; |
| 904 frame_ = web_frame; |
| 905 node_.reset(); |
| 906 } |
| 907 |
| 908 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
| 909 const WebKit::WebNode& web_node) { |
| 910 DCHECK(!web_node.isNull()); |
| 911 if (IsReadyToRender()) |
| 912 return; |
| 913 state_ = INITIALIZED; |
| 914 frame_ = web_node.document().frame(); |
| 915 node_.reset(new WebNode(web_node)); |
| 916 } |
| 917 |
| 918 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
| 919 DCHECK(IsReadyToRender()); |
| 920 ClearContext(); |
| 921 } |
| 922 |
| 923 bool PrintWebViewHelper::PrintPreviewContext::CreatePreviewDocument( |
| 924 const PrintMsg_Print_Params& printParams, |
| 925 const std::vector<int>& pages) { |
| 926 DCHECK(IsReadyToRender()); |
| 927 state_ = RENDERING; |
| 928 |
| 929 print_params_.reset(new PrintMsg_Print_Params(printParams)); |
| 930 |
| 931 metafile_.reset(new printing::PreviewMetafile); |
| 932 if (!metafile_->Init()) |
| 933 return false; |
| 934 |
| 935 // Need to make sure old object gets destroyed first. |
| 936 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(printParams, frame(), |
| 937 node())); |
| 938 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
| 939 if (total_page_count_ == 0) |
| 940 return false; |
| 941 |
| 942 current_page_number_ = 0; |
| 943 if (pages.empty()) { |
| 944 actual_page_count_ = total_page_count_; |
| 945 rendered_pages_ = std::vector<bool>(total_page_count_, false); |
| 946 } else { |
| 947 actual_page_count_ = pages.size(); |
| 948 rendered_pages_ = std::vector<bool>(total_page_count_, true); |
| 949 for (int i = 0; i < actual_page_count_; ++i) { |
| 950 int page_number = pages[i]; |
| 951 if (page_number < 0 || page_number >= total_page_count_) |
| 952 return false; |
| 953 rendered_pages_[page_number] = false; |
| 954 } |
| 955 } |
| 956 |
| 957 document_render_time_ = base::TimeDelta(); |
| 958 begin_time_ = base::TimeTicks::Now(); |
| 959 |
| 960 return true; |
| 961 } |
| 962 |
| 963 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( |
| 964 const base::TimeDelta& page_time) { |
| 965 DCHECK_EQ(RENDERING, state_); |
| 966 document_render_time_ += page_time; |
| 967 UMA_HISTOGRAM_TIMES("PrintPreview.RenderPDFPageTime", page_time); |
| 968 } |
| 969 |
| 970 void PrintWebViewHelper::PrintPreviewContext::FinalizePreviewDocument() { |
| 971 DCHECK_EQ(RENDERING, state_); |
| 972 state_ = DONE; |
| 973 |
| 974 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 975 |
| 976 prep_frame_view_->FinishPrinting(); |
| 977 metafile_->FinishDocument(); |
| 978 |
| 979 if (actual_page_count_ <= 0) { |
| 980 NOTREACHED(); |
| 981 return; |
| 982 } |
| 983 |
| 984 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderToPDFTime", |
| 985 document_render_time_); |
| 986 base::TimeDelta total_time = (base::TimeTicks::Now() - begin_time) + |
| 987 document_render_time_; |
| 988 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTime", |
| 989 total_time); |
| 990 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", |
| 991 total_time / actual_page_count_); |
| 992 } |
| 993 |
| 994 void PrintWebViewHelper::PrintPreviewContext::Finished() { |
| 995 DCHECK_EQ(DONE, state_); |
| 996 ClearContext(); |
| 997 } |
| 998 |
| 999 void PrintWebViewHelper::PrintPreviewContext::Abort() { |
| 1000 state_ = UNINITIALIZED; |
| 1001 ClearContext(); |
| 1002 frame_ = NULL; |
| 1003 node_.reset(); |
| 1004 } |
| 1005 |
| 1006 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
| 1007 DCHECK_EQ(RENDERING, state_); |
| 1008 while (current_page_number_ < total_page_count_ && |
| 1009 rendered_pages_[current_page_number_]) { |
| 1010 ++current_page_number_; |
| 1011 } |
| 1012 if (current_page_number_ == total_page_count_) |
| 1013 return -1; |
| 1014 rendered_pages_[current_page_number_] = true; |
| 1015 return current_page_number_++; |
| 1016 } |
| 1017 |
| 1018 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
| 1019 return state_ != UNINITIALIZED; |
| 1020 } |
| 1021 |
| 1022 bool PrintWebViewHelper::PrintPreviewContext::IsBusy() const { |
| 1023 return state_ == INITIALIZED || state_ == RENDERING; |
| 1024 } |
| 1025 |
| 1026 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
| 1027 if (node()) |
| 1028 return false; |
| 1029 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
| 1030 return mime != "application/pdf"; |
| 1031 } |
| 1032 |
| 1033 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { |
| 1034 return frame_; |
| 1035 } |
| 1036 |
| 1037 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { |
| 1038 return node_.get(); |
| 1039 } |
| 1040 |
| 1041 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| 1042 return total_page_count_; |
| 1043 } |
| 1044 |
| 1045 printing::Metafile* PrintWebViewHelper::PrintPreviewContext::metafile() const { |
| 1046 return metafile_.get(); |
| 1047 } |
| 1048 |
| 1049 const PrintMsg_Print_Params& |
| 1050 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
| 1051 return *print_params_; |
| 1052 } |
| 1053 |
| 1054 const gfx::Size& |
| 1055 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
| 1056 return prep_frame_view_->GetPrintCanvasSize(); |
| 1057 } |
| 1058 |
| 1059 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
| 1060 prep_frame_view_.reset(); |
| 1061 metafile_.reset(); |
| 1062 rendered_pages_.clear(); |
| 1063 } |
OLD | NEW |