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" | |
13 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/print_messages.h" | 14 #include "chrome/common/print_messages.h" |
16 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
17 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
18 #include "chrome/renderer/prerender/prerender_helper.h" | 17 #include "chrome/renderer/prerender/prerender_helper.h" |
19 #include "content/renderer/render_view.h" | 18 #include "content/renderer/render_view.h" |
20 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
21 #include "printing/metafile_impl.h" | 20 #include "printing/metafile_impl.h" |
22 #include "printing/print_job_constants.h" | 21 #include "printing/print_job_constants.h" |
23 #include "printing/units.h" | 22 #include "printing/units.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
33 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
34 | 33 |
35 #if defined(OS_POSIX) | 34 #if defined(OS_POSIX) |
| 35 #include "base/process_util.h" |
36 #include "content/common/view_messages.h" | 36 #include "content/common/view_messages.h" |
37 #endif | 37 #endif |
38 | 38 |
39 using printing::ConvertPixelsToPoint; | 39 using printing::ConvertPixelsToPoint; |
40 using printing::ConvertPixelsToPointDouble; | 40 using printing::ConvertPixelsToPointDouble; |
41 using printing::ConvertUnit; | 41 using printing::ConvertUnit; |
42 using printing::ConvertUnitDouble; | 42 using printing::ConvertUnitDouble; |
43 using WebKit::WebConsoleMessage; | 43 using WebKit::WebConsoleMessage; |
44 using WebKit::WebDocument; | 44 using WebKit::WebDocument; |
45 using WebKit::WebElement; | 45 using WebKit::WebElement; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 211 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
212 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) | 212 IPC_MESSAGE_HANDLER(PrintMsg_PrintForSystemDialog, OnPrintForSystemDialog) |
213 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) | 213 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, OnInitiatePrintPreview) |
214 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 214 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
215 OnPrintNodeUnderContextMenu) | 215 OnPrintNodeUnderContextMenu) |
216 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 216 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
217 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) | 217 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, OnPrintForPrintPreview) |
218 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 218 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
219 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 219 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
220 ResetScriptedPrintCount) | 220 ResetScriptedPrintCount) |
221 IPC_MESSAGE_HANDLER(PrintMsg_ContinuePreview, OnContinuePreview) | |
222 IPC_MESSAGE_HANDLER(PrintMsg_AbortPreview, OnAbortPreview) | |
223 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, | 221 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, |
224 DisplayPrintJobError) | 222 DisplayPrintJobError) |
225 IPC_MESSAGE_UNHANDLED(handled = false) | 223 IPC_MESSAGE_UNHANDLED(handled = false) |
226 IPC_END_MESSAGE_MAP() | 224 IPC_END_MESSAGE_MAP() |
227 return handled; | 225 return handled; |
228 } | 226 } |
229 | 227 |
230 void PrintWebViewHelper::OnPrintForPrintPreview( | 228 void PrintWebViewHelper::OnPrintForPrintPreview( |
231 const DictionaryValue& job_settings) { | 229 const DictionaryValue& job_settings) { |
232 DCHECK(is_preview_); | 230 DCHECK(is_preview_); |
(...skipping 16 matching lines...) Expand all Loading... |
249 return; | 247 return; |
250 } | 248 } |
251 | 249 |
252 WebFrame* pdf_frame = pdf_element.document().frame(); | 250 WebFrame* pdf_frame = pdf_element.document().frame(); |
253 scoped_ptr<PrepareFrameAndViewForPrint> prepare; | 251 scoped_ptr<PrepareFrameAndViewForPrint> prepare; |
254 if (!InitPrintSettingsAndPrepareFrame(pdf_frame, &pdf_element, &prepare)) { | 252 if (!InitPrintSettingsAndPrepareFrame(pdf_frame, &pdf_element, &prepare)) { |
255 NOTREACHED() << "Failed to initialize print page settings"; | 253 NOTREACHED() << "Failed to initialize print page settings"; |
256 return; | 254 return; |
257 } | 255 } |
258 | 256 |
259 if (!UpdatePrintSettings(job_settings)) { | 257 if (!UpdatePrintSettings(job_settings, false)) { |
260 DidFinishPrinting(FAIL_PRINT); | 258 DidFinishPrinting(FAIL_PRINT); |
261 return; | 259 return; |
262 } | 260 } |
263 | 261 |
264 // Render Pages for printing. | 262 // Render Pages for printing. |
265 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) | 263 if (!RenderPagesForPrint(pdf_frame, &pdf_element, prepare.get())) |
266 DidFinishPrinting(FAIL_PRINT); | 264 DidFinishPrinting(FAIL_PRINT); |
267 } | 265 } |
268 | 266 |
269 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { | 267 bool PrintWebViewHelper::GetPrintFrame(WebKit::WebFrame** frame) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { | 305 void PrintWebViewHelper::OnPrintPreview(const DictionaryValue& settings) { |
308 DCHECK(is_preview_); | 306 DCHECK(is_preview_); |
309 print_preview_context_.OnPrintPreview(); | 307 print_preview_context_.OnPrintPreview(); |
310 | 308 |
311 if (!InitPrintSettings(print_preview_context_.frame(), | 309 if (!InitPrintSettings(print_preview_context_.frame(), |
312 print_preview_context_.node())) { | 310 print_preview_context_.node())) { |
313 NOTREACHED(); | 311 NOTREACHED(); |
314 return; | 312 return; |
315 } | 313 } |
316 | 314 |
317 if (!UpdatePrintSettings(settings)) { | 315 if (!UpdatePrintSettings(settings, true)) { |
318 DidFinishPrinting(FAIL_PREVIEW); | 316 DidFinishPrinting(FAIL_PREVIEW); |
319 return; | 317 return; |
320 } | 318 } |
321 | 319 |
322 if (!print_pages_params_->params.is_first_request && | 320 if (!print_pages_params_->params.is_first_request && |
323 old_print_pages_params_.get() && | 321 old_print_pages_params_.get() && |
324 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, | 322 PrintMsg_Print_Params_IsEqual(*old_print_pages_params_, |
325 *print_pages_params_)) { | 323 *print_pages_params_)) { |
326 PrintHostMsg_DidPreviewDocument_Params preview_params; | 324 PrintHostMsg_DidPreviewDocument_Params preview_params; |
327 preview_params.reuse_existing_data = true; | 325 preview_params.reuse_existing_data = true; |
328 preview_params.data_size = 0; | 326 preview_params.data_size = 0; |
329 preview_params.document_cookie = | 327 preview_params.document_cookie = |
330 print_pages_params_->params.document_cookie; | 328 print_pages_params_->params.document_cookie; |
331 preview_params.expected_pages_count = | 329 preview_params.expected_pages_count = |
332 print_preview_context_.total_page_count(); | 330 print_preview_context_.total_page_count(); |
333 preview_params.modifiable = print_preview_context_.IsModifiable(); | 331 preview_params.modifiable = print_preview_context_.IsModifiable(); |
334 preview_params.preview_request_id = | 332 preview_params.preview_request_id = |
335 print_pages_params_->params.preview_request_id; | 333 print_pages_params_->params.preview_request_id; |
336 | 334 |
337 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); | 335 Send(new PrintHostMsg_PagesReadyForPreview(routing_id(), preview_params)); |
338 return; | 336 return; |
339 } | 337 } |
340 // Always clear |old_print_pages_params_| before rendering the pages. | 338 // Always clear |old_print_pages_params_| before rendering the pages. |
341 old_print_pages_params_.reset(); | 339 old_print_pages_params_.reset(); |
342 | 340 |
343 // PDF printer device supports alpha blending. | 341 // PDF printer device supports alpha blending. |
344 print_pages_params_->params.supports_alpha_blend = true; | 342 print_pages_params_->params.supports_alpha_blend = true; |
345 if (!CreatePreviewDocument()) | 343 if (CreatePreviewDocument()) |
| 344 DidFinishPrinting(OK); |
| 345 else |
346 DidFinishPrinting(FAIL_PREVIEW); | 346 DidFinishPrinting(FAIL_PREVIEW); |
347 } | 347 } |
348 | 348 |
349 bool PrintWebViewHelper::CreatePreviewDocument() { | 349 bool PrintWebViewHelper::CreatePreviewDocument() { |
350 PrintMsg_Print_Params print_params = print_pages_params_->params; | 350 PrintMsg_Print_Params print_params = print_pages_params_->params; |
351 const std::vector<int>& pages = print_pages_params_->pages; | 351 const std::vector<int>& pages = print_pages_params_->pages; |
352 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) | 352 if (!print_preview_context_.CreatePreviewDocument(&print_params, pages)) |
353 return false; | 353 return false; |
354 PrintHostMsg_DidGetPreviewPageCount_Params params; | 354 PrintHostMsg_DidGetPreviewPageCount_Params params; |
355 params.page_count = print_preview_context_.total_page_count(); | 355 params.page_count = print_preview_context_.total_page_count(); |
356 params.is_modifiable = print_preview_context_.IsModifiable(); | 356 params.is_modifiable = print_preview_context_.IsModifiable(); |
357 params.document_cookie = print_pages_params_->params.document_cookie; | 357 params.document_cookie = print_pages_params_->params.document_cookie; |
358 params.preview_request_id = print_pages_params_->params.preview_request_id; | 358 params.preview_request_id = print_pages_params_->params.preview_request_id; |
359 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); | 359 Send(new PrintHostMsg_DidGetPreviewPageCount(routing_id(), params)); |
360 PreviewPageRendered(printing::INVALID_PAGE_INDEX, NULL); | 360 if (CheckForCancel()) |
361 return true; | 361 return false; |
362 } | |
363 | 362 |
364 void PrintWebViewHelper::OnContinuePreview(int requested_preview_page_index) { | |
365 // Spurious message. We already finished/cancelled/aborted the print preview. | |
366 if (!print_preview_context_.IsBusy()) | |
367 return; | |
368 int page_number; | 363 int page_number; |
369 #if defined(USE_SKIA) | 364 while ((page_number = print_preview_context_.GetNextPageNumber()) >= 0) { |
370 if (requested_preview_page_index >= printing::FIRST_PAGE_INDEX) { | 365 if (!RenderPreviewPage(page_number)) |
371 page_number = requested_preview_page_index; | 366 return false; |
372 } else | 367 if (CheckForCancel()) |
373 #endif | 368 return false; |
374 { | 369 }; |
375 page_number = print_preview_context_.GetNextPageNumber(); | |
376 } | |
377 | |
378 if (page_number >= printing::FIRST_PAGE_INDEX) { | |
379 // Continue generating the print preview. | |
380 RenderPreviewPage(page_number); | |
381 return; | |
382 } | |
383 | 370 |
384 // Finished generating preview. Finalize the document. | 371 // Finished generating preview. Finalize the document. |
385 if (FinalizePreviewDocument()) { | 372 if (!FinalizePreviewDocument()) |
386 print_preview_context_.Finished(); | 373 return false; |
387 DidFinishPrinting(OK); | 374 print_preview_context_.Finished(); |
388 } else { | 375 return true; |
389 DidFinishPrinting(FAIL_PREVIEW); | |
390 } | |
391 } | |
392 | |
393 void PrintWebViewHelper::OnAbortPreview() { | |
394 DidFinishPrinting(ABORT_PREVIEW); | |
395 return; | |
396 } | 376 } |
397 | 377 |
398 bool PrintWebViewHelper::FinalizePreviewDocument() { | 378 bool PrintWebViewHelper::FinalizePreviewDocument() { |
399 print_preview_context_.FinalizePreviewDocument(); | 379 print_preview_context_.FinalizePreviewDocument(); |
400 | 380 |
401 // Get the size of the resulting metafile. | 381 // Get the size of the resulting metafile. |
402 printing::Metafile* metafile = print_preview_context_.metafile(); | 382 printing::Metafile* metafile = print_preview_context_.metafile(); |
403 uint32 buf_size = metafile->GetDataSize(); | 383 uint32 buf_size = metafile->GetDataSize(); |
404 DCHECK_GT(buf_size, 0u); | 384 DCHECK_GT(buf_size, 0u); |
405 | 385 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 DisplayPrintJobError(); | 480 DisplayPrintJobError(); |
501 | 481 |
502 if (notify_browser_of_print_failure_) { | 482 if (notify_browser_of_print_failure_) { |
503 int cookie = print_pages_params_->params.document_cookie; | 483 int cookie = print_pages_params_->params.document_cookie; |
504 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); | 484 Send(new PrintHostMsg_PrintingFailed(routing_id(), cookie)); |
505 } | 485 } |
506 } else if (result == FAIL_PREVIEW) { | 486 } else if (result == FAIL_PREVIEW) { |
507 DCHECK(is_preview_); | 487 DCHECK(is_preview_); |
508 store_print_pages_params = false; | 488 store_print_pages_params = false; |
509 int cookie = print_pages_params_->params.document_cookie; | 489 int cookie = print_pages_params_->params.document_cookie; |
510 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); | 490 if (notify_browser_of_print_failure_) |
| 491 Send(new PrintHostMsg_PrintPreviewFailed(routing_id(), cookie)); |
| 492 else |
| 493 Send(new PrintHostMsg_PrintPreviewCancelled(routing_id(), cookie)); |
511 print_preview_context_.Failed(); | 494 print_preview_context_.Failed(); |
512 } else if (result == ABORT_PREVIEW) { | |
513 DCHECK(is_preview_); | |
514 store_print_pages_params = false; | |
515 print_preview_context_.Abort(); | |
516 } | 495 } |
517 | 496 |
518 if (print_web_view_) { | 497 if (print_web_view_) { |
519 print_web_view_->close(); | 498 print_web_view_->close(); |
520 print_web_view_ = NULL; | 499 print_web_view_ = NULL; |
521 } | 500 } |
522 | 501 |
523 if (store_print_pages_params) { | 502 if (store_print_pages_params) { |
524 old_print_pages_params_.reset(print_pages_params_.release()); | 503 old_print_pages_params_.reset(print_pages_params_.release()); |
525 } else { | 504 } else { |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 DCHECK(!prepare->get()); | 722 DCHECK(!prepare->get()); |
744 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, | 723 prepare->reset(new PrepareFrameAndViewForPrint(print_pages_params_->params, |
745 frame, node)); | 724 frame, node)); |
746 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), | 725 UpdatePrintableSizeInPrintParameters(frame, node, prepare->get(), |
747 &print_pages_params_->params); | 726 &print_pages_params_->params); |
748 Send(new PrintHostMsg_DidGetDocumentCookie( | 727 Send(new PrintHostMsg_DidGetDocumentCookie( |
749 routing_id(), print_pages_params_->params.document_cookie)); | 728 routing_id(), print_pages_params_->params.document_cookie)); |
750 return true; | 729 return true; |
751 } | 730 } |
752 | 731 |
753 bool PrintWebViewHelper::UpdatePrintSettingsRequestId( | |
754 const DictionaryValue& job_settings, | |
755 PrintMsg_Print_Params* params) { | |
756 if (!job_settings.GetInteger(printing::kPreviewRequestID, | |
757 &(params->preview_request_id))) { | |
758 NOTREACHED(); | |
759 return false; | |
760 } | |
761 return true; | |
762 } | |
763 | |
764 bool PrintWebViewHelper::UpdatePrintSettings( | 732 bool PrintWebViewHelper::UpdatePrintSettings( |
765 const DictionaryValue& job_settings) { | 733 const DictionaryValue& job_settings, bool is_preview) { |
766 PrintMsg_PrintPages_Params settings; | 734 PrintMsg_PrintPages_Params settings; |
767 | 735 |
768 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 736 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
769 print_pages_params_->params.document_cookie, job_settings, &settings)); | 737 print_pages_params_->params.document_cookie, job_settings, &settings)); |
770 | |
771 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) | 738 if (settings.params.dpi < kMinDpi || !settings.params.document_cookie) |
772 return false; | 739 return false; |
773 | 740 |
774 if (!UpdatePrintSettingsRequestId(job_settings, &(settings.params))) | 741 if (is_preview) { |
775 return false; | 742 // Validate expected print preview settings. |
776 | 743 if (!job_settings.GetString(printing::kPreviewUIAddr, |
777 if (!job_settings.GetBoolean(printing::kIsFirstRequest, | 744 &(settings.params.preview_ui_addr)) || |
778 &(settings.params.is_first_request))) { | 745 !job_settings.GetInteger(printing::kPreviewRequestID, |
779 NOTREACHED(); | 746 &(settings.params.preview_request_id)) || |
| 747 !job_settings.GetBoolean(printing::kIsFirstRequest, |
| 748 &(settings.params.is_first_request))) { |
| 749 NOTREACHED(); |
| 750 return false; |
| 751 } |
780 } | 752 } |
781 | 753 |
782 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 754 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
783 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 755 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
784 settings.params.document_cookie)); | 756 settings.params.document_cookie)); |
785 return true; | 757 return true; |
786 } | 758 } |
787 | 759 |
788 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 760 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
789 int expected_pages_count, | 761 int expected_pages_count, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
868 min_wait_seconds = std::min( | 840 min_wait_seconds = std::min( |
869 kMinSecondsToIgnoreJavascriptInitiatedPrint << | 841 kMinSecondsToIgnoreJavascriptInitiatedPrint << |
870 (user_cancelled_scripted_print_count_ - 3), | 842 (user_cancelled_scripted_print_count_ - 3), |
871 kMaxSecondsToIgnoreJavascriptInitiatedPrint); | 843 kMaxSecondsToIgnoreJavascriptInitiatedPrint); |
872 } | 844 } |
873 if (diff.InSeconds() < min_wait_seconds) { | 845 if (diff.InSeconds() < min_wait_seconds) { |
874 too_frequent = true; | 846 too_frequent = true; |
875 } | 847 } |
876 } | 848 } |
877 | 849 |
878 if (!too_frequent && print_preview_context_.IsBusy()) | |
879 too_frequent = true; | |
880 | |
881 if (!too_frequent) | 850 if (!too_frequent) |
882 return false; | 851 return false; |
883 | 852 |
884 WebString message(WebString::fromUTF8( | 853 WebString message(WebString::fromUTF8( |
885 "Ignoring too frequent calls to print().")); | 854 "Ignoring too frequent calls to print().")); |
886 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, | 855 frame->addMessageToConsole(WebConsoleMessage(WebConsoleMessage::LevelWarning, |
887 message)); | 856 message)); |
888 return true; | 857 return true; |
889 } | 858 } |
890 | 859 |
(...skipping 15 matching lines...) Expand all Loading... |
906 render_view()->runModalAlertDialog( | 875 render_view()->runModalAlertDialog( |
907 web_view->mainFrame(), | 876 web_view->mainFrame(), |
908 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 877 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
909 } | 878 } |
910 | 879 |
911 void PrintWebViewHelper::RequestPrintPreview() { | 880 void PrintWebViewHelper::RequestPrintPreview() { |
912 old_print_pages_params_.reset(); | 881 old_print_pages_params_.reset(); |
913 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 882 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
914 } | 883 } |
915 | 884 |
916 void PrintWebViewHelper::PreviewPageRendered(int page_number, | 885 bool PrintWebViewHelper::CheckForCancel() { |
| 886 bool cancel = false; |
| 887 Send(new PrintHostMsg_CheckForCancel( |
| 888 routing_id(), |
| 889 print_pages_params_->params.preview_ui_addr, |
| 890 print_pages_params_->params.preview_request_id, |
| 891 &cancel)); |
| 892 if (cancel) |
| 893 notify_browser_of_print_failure_ = false; |
| 894 return cancel; |
| 895 } |
| 896 |
| 897 bool PrintWebViewHelper::PreviewPageRendered(int page_number, |
917 printing::Metafile* metafile) { | 898 printing::Metafile* metafile) { |
918 if ((page_number == printing::INVALID_PAGE_INDEX && metafile) || | 899 DCHECK_GE(page_number, printing::FIRST_PAGE_INDEX); |
919 (page_number >= printing::FIRST_PAGE_INDEX && !metafile && | 900 |
920 print_preview_context_.IsModifiable())) { | 901 // For non-modifiable files, |metafile| should be NULL, so do not bother |
921 NOTREACHED(); | 902 // sending a message. |
922 DidFinishPrinting(FAIL_PREVIEW); | 903 if (!print_preview_context_.IsModifiable()) { |
923 return; | 904 DCHECK(!metafile); |
| 905 return true; |
924 } | 906 } |
925 | 907 |
926 uint32 buf_size = 0; | 908 if (!metafile) { |
| 909 NOTREACHED(); |
| 910 return false; |
| 911 } |
| 912 |
927 PrintHostMsg_DidPreviewPage_Params preview_page_params; | 913 PrintHostMsg_DidPreviewPage_Params preview_page_params; |
928 // Get the size of the resulting metafile. | 914 // Get the size of the resulting metafile. |
929 if (metafile) { | 915 uint32 buf_size = metafile->GetDataSize(); |
930 buf_size = metafile->GetDataSize(); | 916 DCHECK_GT(buf_size, 0u); |
931 DCHECK_GT(buf_size, 0u); | 917 if (!CopyMetafileDataToSharedMem( |
932 if (!CopyMetafileDataToSharedMem( | 918 metafile, &(preview_page_params.metafile_data_handle))) { |
933 metafile, &(preview_page_params.metafile_data_handle))) { | 919 return false; |
934 DidFinishPrinting(FAIL_PREVIEW); | |
935 return; | |
936 } | |
937 } | 920 } |
| 921 |
938 preview_page_params.data_size = buf_size; | 922 preview_page_params.data_size = buf_size; |
939 preview_page_params.page_number = page_number; | 923 preview_page_params.page_number = page_number; |
940 preview_page_params.preview_request_id = | 924 preview_page_params.preview_request_id = |
941 print_pages_params_->params.preview_request_id; | 925 print_pages_params_->params.preview_request_id; |
| 926 |
942 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); | 927 Send(new PrintHostMsg_DidPreviewPage(routing_id(), preview_page_params)); |
| 928 return true; |
943 } | 929 } |
944 | 930 |
945 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() | 931 PrintWebViewHelper::PrintPreviewContext::PrintPreviewContext() |
946 : frame_(NULL), | 932 : frame_(NULL), |
947 total_page_count_(0), | 933 total_page_count_(0), |
948 actual_page_count_(0), | 934 actual_page_count_(0), |
949 current_page_number_(0), | 935 current_page_index_(0), |
950 state_(UNINITIALIZED) { | 936 state_(UNINITIALIZED) { |
951 } | 937 } |
952 | 938 |
953 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { | 939 PrintWebViewHelper::PrintPreviewContext::~PrintPreviewContext() { |
954 } | 940 } |
955 | 941 |
956 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( | 942 void PrintWebViewHelper::PrintPreviewContext::InitWithFrame( |
957 WebKit::WebFrame* web_frame) { | 943 WebKit::WebFrame* web_frame) { |
958 DCHECK(web_frame); | 944 DCHECK(web_frame); |
959 if (IsReadyToRender()) | |
960 return; | |
961 state_ = INITIALIZED; | 945 state_ = INITIALIZED; |
962 frame_ = web_frame; | 946 frame_ = web_frame; |
963 node_.reset(); | 947 node_.reset(); |
964 } | 948 } |
965 | 949 |
966 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( | 950 void PrintWebViewHelper::PrintPreviewContext::InitWithNode( |
967 const WebKit::WebNode& web_node) { | 951 const WebKit::WebNode& web_node) { |
968 DCHECK(!web_node.isNull()); | 952 DCHECK(!web_node.isNull()); |
969 if (IsReadyToRender()) | |
970 return; | |
971 state_ = INITIALIZED; | 953 state_ = INITIALIZED; |
972 frame_ = web_node.document().frame(); | 954 frame_ = web_node.document().frame(); |
973 node_.reset(new WebNode(web_node)); | 955 node_.reset(new WebNode(web_node)); |
974 } | 956 } |
975 | 957 |
976 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { | 958 void PrintWebViewHelper::PrintPreviewContext::OnPrintPreview() { |
977 DCHECK(IsReadyToRender()); | 959 DCHECK(IsReadyToRender()); |
978 ClearContext(); | 960 ClearContext(); |
979 } | 961 } |
980 | 962 |
(...skipping 12 matching lines...) Expand all Loading... |
993 // Need to make sure old object gets destroyed first. | 975 // Need to make sure old object gets destroyed first. |
994 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), | 976 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(*print_params, frame(), |
995 node())); | 977 node())); |
996 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), | 978 UpdatePrintableSizeInPrintParameters(frame_, node_.get(), |
997 prep_frame_view_.get(), print_params); | 979 prep_frame_view_.get(), print_params); |
998 | 980 |
999 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); | 981 total_page_count_ = prep_frame_view_->GetExpectedPageCount(); |
1000 if (total_page_count_ == 0) | 982 if (total_page_count_ == 0) |
1001 return false; | 983 return false; |
1002 | 984 |
1003 current_page_number_ = 0; | 985 current_page_index_ = 0; |
1004 if (pages.empty()) { | 986 if (pages.empty()) { |
1005 actual_page_count_ = total_page_count_; | 987 actual_page_count_ = total_page_count_; |
1006 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | 988 for (int i = 0; i < actual_page_count_; ++i) |
1007 std::make_pair(false, -1)); | 989 pages_to_render_.push_back(i); |
1008 } else { | 990 } else { |
1009 actual_page_count_ = pages.size(); | 991 actual_page_count_ = pages.size(); |
1010 rendered_pages_ = std::vector<PreviewPageInfo>(total_page_count_, | |
1011 std::make_pair(true, -1)); | |
1012 for (int i = 0; i < actual_page_count_; ++i) { | 992 for (int i = 0; i < actual_page_count_; ++i) { |
1013 int page_number = pages[i]; | 993 int page_number = pages[i]; |
1014 if (page_number < printing::FIRST_PAGE_INDEX || | 994 if (page_number < printing::FIRST_PAGE_INDEX || |
1015 page_number >= total_page_count_) { | 995 page_number >= total_page_count_) { |
1016 return false; | 996 return false; |
1017 } | 997 } |
1018 rendered_pages_[page_number].first = false; | 998 pages_to_render_.push_back(page_number); |
1019 rendered_pages_[page_number].second = i; | |
1020 } | 999 } |
1021 } | 1000 } |
1022 | 1001 |
1023 document_render_time_ = base::TimeDelta(); | 1002 document_render_time_ = base::TimeDelta(); |
1024 begin_time_ = base::TimeTicks::Now(); | 1003 begin_time_ = base::TimeTicks::Now(); |
1025 | 1004 |
1026 return true; | 1005 return true; |
1027 } | 1006 } |
1028 | 1007 |
1029 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( | 1008 void PrintWebViewHelper::PrintPreviewContext::RenderedPreviewPage( |
(...skipping 26 matching lines...) Expand all Loading... |
1056 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", | 1035 UMA_HISTOGRAM_MEDIUM_TIMES("PrintPreview.RenderAndGeneratePDFTimeAvgPerPage", |
1057 total_time / actual_page_count_); | 1036 total_time / actual_page_count_); |
1058 } | 1037 } |
1059 | 1038 |
1060 void PrintWebViewHelper::PrintPreviewContext::Finished() { | 1039 void PrintWebViewHelper::PrintPreviewContext::Finished() { |
1061 DCHECK_EQ(DONE, state_); | 1040 DCHECK_EQ(DONE, state_); |
1062 ClearContext(); | 1041 ClearContext(); |
1063 } | 1042 } |
1064 | 1043 |
1065 void PrintWebViewHelper::PrintPreviewContext::Failed() { | 1044 void PrintWebViewHelper::PrintPreviewContext::Failed() { |
1066 DCHECK(IsBusy()); | 1045 DCHECK(state_ == INITIALIZED || state_ == RENDERING); |
1067 state_ = INITIALIZED; | 1046 state_ = INITIALIZED; |
1068 ClearContext(); | 1047 ClearContext(); |
1069 } | 1048 } |
1070 | 1049 |
1071 void PrintWebViewHelper::PrintPreviewContext::Abort() { | |
1072 state_ = UNINITIALIZED; | |
1073 ClearContext(); | |
1074 frame_ = NULL; | |
1075 node_.reset(); | |
1076 } | |
1077 | |
1078 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { | 1050 int PrintWebViewHelper::PrintPreviewContext::GetNextPageNumber() { |
1079 DCHECK_EQ(RENDERING, state_); | 1051 DCHECK_EQ(RENDERING, state_); |
1080 for (int i = 0; i < total_page_count_; i++) { | 1052 if (current_page_index_ >= actual_page_count_) |
1081 if (!rendered_pages_[current_page_number_].first) | 1053 return -1; |
1082 break; | 1054 return pages_to_render_[current_page_index_++]; |
1083 current_page_number_ = (current_page_number_ + 1) % total_page_count_; | |
1084 } | |
1085 if (rendered_pages_[current_page_number_].first) | |
1086 return printing::INVALID_PAGE_INDEX; | |
1087 rendered_pages_[current_page_number_].first = true; | |
1088 return current_page_number_; | |
1089 } | 1055 } |
1090 | 1056 |
1091 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { | 1057 bool PrintWebViewHelper::PrintPreviewContext::IsReadyToRender() const { |
1092 return state_ != UNINITIALIZED; | 1058 return state_ != UNINITIALIZED; |
1093 } | 1059 } |
1094 | 1060 |
1095 bool PrintWebViewHelper::PrintPreviewContext::IsBusy() const { | |
1096 return state_ == INITIALIZED || state_ == RENDERING; | |
1097 } | |
1098 | |
1099 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { | 1061 bool PrintWebViewHelper::PrintPreviewContext::IsModifiable() const { |
1100 // TODO(vandebo) I think this should only return false if the content is a | 1062 // TODO(vandebo) I think this should only return false if the content is a |
1101 // PDF, just because we are printing a particular node does not mean it's | 1063 // PDF, just because we are printing a particular node does not mean it's |
1102 // a PDF (right?), we should check the mime type of the node. | 1064 // a PDF (right?), we should check the mime type of the node. |
1103 if (node()) | 1065 if (node()) |
1104 return false; | 1066 return false; |
1105 std::string mime(frame()->dataSource()->response().mimeType().utf8()); | 1067 std::string mime(frame()->dataSource()->response().mimeType().utf8()); |
1106 return mime != "application/pdf"; | 1068 return mime != "application/pdf"; |
1107 } | 1069 } |
1108 | 1070 |
1109 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { | 1071 WebKit::WebFrame* PrintWebViewHelper::PrintPreviewContext::frame() const { |
1110 return frame_; | 1072 return frame_; |
1111 } | 1073 } |
1112 | 1074 |
1113 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { | 1075 WebKit::WebNode* PrintWebViewHelper::PrintPreviewContext::node() const { |
1114 return node_.get(); | 1076 return node_.get(); |
1115 } | 1077 } |
1116 | 1078 |
1117 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { | 1079 int PrintWebViewHelper::PrintPreviewContext::total_page_count() const { |
| 1080 DCHECK(IsReadyToRender()); |
1118 return total_page_count_; | 1081 return total_page_count_; |
1119 } | 1082 } |
1120 | 1083 |
1121 printing::Metafile* PrintWebViewHelper::PrintPreviewContext::metafile() const { | 1084 printing::Metafile* PrintWebViewHelper::PrintPreviewContext::metafile() const { |
1122 return metafile_.get(); | 1085 return metafile_.get(); |
1123 } | 1086 } |
1124 | 1087 |
1125 const PrintMsg_Print_Params& | 1088 const PrintMsg_Print_Params& |
1126 PrintWebViewHelper::PrintPreviewContext::print_params() const { | 1089 PrintWebViewHelper::PrintPreviewContext::print_params() const { |
1127 return *print_params_; | 1090 return *print_params_; |
1128 } | 1091 } |
1129 | 1092 |
1130 const gfx::Size& | 1093 const gfx::Size& |
1131 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { | 1094 PrintWebViewHelper::PrintPreviewContext::GetPrintCanvasSize() const { |
1132 return prep_frame_view_->GetPrintCanvasSize(); | 1095 return prep_frame_view_->GetPrintCanvasSize(); |
1133 } | 1096 } |
1134 | 1097 |
1135 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 1098 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
1136 prep_frame_view_.reset(); | 1099 prep_frame_view_.reset(); |
1137 metafile_.reset(); | 1100 metafile_.reset(); |
1138 rendered_pages_.clear(); | 1101 pages_to_render_.clear(); |
1139 } | 1102 } |
OLD | NEW |