OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 // TODO(sgurun) copied from chrome/renderer. Remove after crbug.com/322276 | 5 // TODO(sgurun) copied from chrome/renderer. Remove after crbug.com/322276 |
6 | 6 |
7 #include "android_webview/renderer/print_web_view_helper.h" | 7 #include "android_webview/renderer/print_web_view_helper.h" |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "android_webview/common/print_messages.h" | 11 #include "android_webview/common/print_messages.h" |
12 #include "base/auto_reset.h" | 12 #include "base/auto_reset.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/process/process_handle.h" | 18 #include "base/process/process_handle.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "content/public/common/web_preferences.h" | |
22 #include "content/public/renderer/render_thread.h" | 23 #include "content/public/renderer/render_thread.h" |
23 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
24 #include "content/public/renderer/web_preferences.h" | |
25 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
26 #include "printing/metafile.h" | 26 #include "printing/metafile.h" |
27 #include "printing/metafile_impl.h" | 27 #include "printing/metafile_impl.h" |
28 #include "printing/units.h" | 28 #include "printing/units.h" |
29 #include "skia/ext/vector_platform_device_skia.h" | 29 #include "skia/ext/vector_platform_device_skia.h" |
30 #include "third_party/WebKit/public/platform/WebSize.h" | 30 #include "third_party/WebKit/public/platform/WebSize.h" |
31 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 31 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
32 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | 32 #include "third_party/WebKit/public/web/WebConsoleMessage.h" |
33 #include "third_party/WebKit/public/web/WebDocument.h" | 33 #include "third_party/WebKit/public/web/WebDocument.h" |
34 #include "third_party/WebKit/public/web/WebElement.h" | 34 #include "third_party/WebKit/public/web/WebElement.h" |
35 #include "third_party/WebKit/public/web/WebFrameClient.h" | 35 #include "third_party/WebKit/public/web/WebFrameClient.h" |
36 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 36 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
37 #include "third_party/WebKit/public/web/WebPlugin.h" | 37 #include "third_party/WebKit/public/web/WebPlugin.h" |
38 #include "third_party/WebKit/public/web/WebPluginDocument.h" | 38 #include "third_party/WebKit/public/web/WebPluginDocument.h" |
39 #include "third_party/WebKit/public/web/WebPrintParams.h" | 39 #include "third_party/WebKit/public/web/WebPrintParams.h" |
40 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" | 40 #include "third_party/WebKit/public/web/WebPrintScalingOption.h" |
41 #include "third_party/WebKit/public/web/WebScriptSource.h" | 41 #include "third_party/WebKit/public/web/WebScriptSource.h" |
42 #include "third_party/WebKit/public/web/WebSettings.h" | 42 #include "third_party/WebKit/public/web/WebSettings.h" |
43 #include "third_party/WebKit/public/web/WebView.h" | 43 #include "third_party/WebKit/public/web/WebView.h" |
44 #include "third_party/WebKit/public/web/WebViewClient.h" | 44 #include "third_party/WebKit/public/web/WebViewClient.h" |
45 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" |
46 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
47 #include "webkit/common/webpreferences.h" | |
48 | 47 |
49 // This code is copied from chrome/renderer/printing. Code is slightly | 48 // This code is copied from chrome/renderer/printing. Code is slightly |
50 // modified to run it with webview, and the modifications are marked | 49 // modified to run it with webview, and the modifications are marked |
51 // using OS_ANDROID. | 50 // using OS_ANDROID. |
52 // TODO(sgurun): remove the code as part of componentization of printing. | 51 // TODO(sgurun): remove the code as part of componentization of printing. |
53 | 52 |
53 using content::WebPreferences; | |
54 | |
54 namespace printing { | 55 namespace printing { |
55 | 56 |
56 namespace { | 57 namespace { |
57 | 58 |
58 enum PrintPreviewHelperEvents { | 59 enum PrintPreviewHelperEvents { |
59 PREVIEW_EVENT_REQUESTED, | 60 PREVIEW_EVENT_REQUESTED, |
60 PREVIEW_EVENT_CACHE_HIT, // Unused | 61 PREVIEW_EVENT_CACHE_HIT, // Unused |
61 PREVIEW_EVENT_CREATE_DOCUMENT, | 62 PREVIEW_EVENT_CREATE_DOCUMENT, |
62 PREVIEW_EVENT_NEW_SETTINGS, // Unused | 63 PREVIEW_EVENT_NEW_SETTINGS, // Unused |
63 PREVIEW_EVENT_MAX, | 64 PREVIEW_EVENT_MAX, |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 } | 497 } |
497 return frame->printPage(page_number, canvas); | 498 return frame->printPage(page_number, canvas); |
498 } | 499 } |
499 | 500 |
500 // Class that calls the Begin and End print functions on the frame and changes | 501 // Class that calls the Begin and End print functions on the frame and changes |
501 // the size of the view temporarily to support full page printing.. | 502 // the size of the view temporarily to support full page printing.. |
502 class PrepareFrameAndViewForPrint : public blink::WebViewClient, | 503 class PrepareFrameAndViewForPrint : public blink::WebViewClient, |
503 public blink::WebFrameClient { | 504 public blink::WebFrameClient { |
504 public: | 505 public: |
505 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, | 506 PrepareFrameAndViewForPrint(const PrintMsg_Print_Params& params, |
507 content::RenderView* render_view, | |
506 blink::WebLocalFrame* frame, | 508 blink::WebLocalFrame* frame, |
507 const blink::WebNode& node, | 509 const blink::WebNode& node, |
508 bool ignore_css_margins); | 510 bool ignore_css_margins); |
509 virtual ~PrepareFrameAndViewForPrint(); | 511 virtual ~PrepareFrameAndViewForPrint(); |
510 | 512 |
511 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| | 513 // Optional. Replaces |frame_| with selection if needed. Will call |on_ready| |
512 // when completed. | 514 // when completed. |
513 void CopySelectionIfNeeded(const WebPreferences& preferences, | 515 void CopySelectionIfNeeded(const WebPreferences& preferences, |
514 const base::Closure& on_ready); | 516 const base::Closure& on_ready); |
515 | 517 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 virtual void frameDetached(blink::WebFrame* frame); | 554 virtual void frameDetached(blink::WebFrame* frame); |
553 | 555 |
554 private: | 556 private: |
555 void CallOnReady(); | 557 void CallOnReady(); |
556 void ResizeForPrinting(); | 558 void ResizeForPrinting(); |
557 void RestoreSize(); | 559 void RestoreSize(); |
558 void CopySelection(const WebPreferences& preferences); | 560 void CopySelection(const WebPreferences& preferences); |
559 | 561 |
560 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; | 562 base::WeakPtrFactory<PrepareFrameAndViewForPrint> weak_ptr_factory_; |
561 | 563 |
564 content::RenderView* render_view_; | |
562 FrameReference frame_; | 565 FrameReference frame_; |
563 blink::WebNode node_to_print_; | 566 blink::WebNode node_to_print_; |
564 bool owns_web_view_; | 567 bool owns_web_view_; |
565 blink::WebPrintParams web_print_params_; | 568 blink::WebPrintParams web_print_params_; |
566 gfx::Size prev_view_size_; | 569 gfx::Size prev_view_size_; |
567 gfx::Size prev_scroll_offset_; | 570 gfx::Size prev_scroll_offset_; |
568 int expected_pages_count_; | 571 int expected_pages_count_; |
569 base::Closure on_ready_; | 572 base::Closure on_ready_; |
570 bool should_print_backgrounds_; | 573 bool should_print_backgrounds_; |
571 bool should_print_selection_only_; | 574 bool should_print_selection_only_; |
572 bool is_printing_started_; | 575 bool is_printing_started_; |
573 | 576 |
574 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); | 577 DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint); |
575 }; | 578 }; |
576 | 579 |
577 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 580 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
578 const PrintMsg_Print_Params& params, | 581 const PrintMsg_Print_Params& params, |
582 content::RenderView* render_view, | |
579 blink::WebLocalFrame* frame, | 583 blink::WebLocalFrame* frame, |
580 const blink::WebNode& node, | 584 const blink::WebNode& node, |
581 bool ignore_css_margins) | 585 bool ignore_css_margins) |
582 : weak_ptr_factory_(this), | 586 : weak_ptr_factory_(this), |
587 render_view_(render_view), | |
583 frame_(frame), | 588 frame_(frame), |
584 node_to_print_(node), | 589 node_to_print_(node), |
585 owns_web_view_(false), | 590 owns_web_view_(false), |
586 expected_pages_count_(0), | 591 expected_pages_count_(0), |
587 should_print_backgrounds_(params.should_print_backgrounds), | 592 should_print_backgrounds_(params.should_print_backgrounds), |
588 should_print_selection_only_(params.selection_only), | 593 should_print_selection_only_(params.selection_only), |
589 is_printing_started_(false) { | 594 is_printing_started_(false) { |
590 PrintMsg_Print_Params print_params = params; | 595 PrintMsg_Print_Params print_params = params; |
591 if (!should_print_selection_only_ || | 596 if (!should_print_selection_only_ || |
592 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { | 597 !PrintingNodeOrPdfFrame(frame, node_to_print_)) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
658 RestoreSize(); | 663 RestoreSize(); |
659 // Create a new WebView with the same settings as the current display one. | 664 // Create a new WebView with the same settings as the current display one. |
660 // Except that we disable javascript (don't want any active content running | 665 // Except that we disable javascript (don't want any active content running |
661 // on the page). | 666 // on the page). |
662 WebPreferences prefs = preferences; | 667 WebPreferences prefs = preferences; |
663 prefs.javascript_enabled = false; | 668 prefs.javascript_enabled = false; |
664 prefs.java_enabled = false; | 669 prefs.java_enabled = false; |
665 | 670 |
666 blink::WebView* web_view = blink::WebView::create(this); | 671 blink::WebView* web_view = blink::WebView::create(this); |
667 owns_web_view_ = true; | 672 owns_web_view_ = true; |
668 content::ApplyWebPreferences(prefs, web_view); | 673 render_view_->ApplyWebPreferences(prefs); |
boliu
2014/07/03 00:17:35
Does this work?
frame_->GetRenderView()->ApplyWeb
tfarina
2014/07/03 00:50:14
I don't think this would work. As far as I can see
boliu
2014/07/03 00:59:13
Oops, my bad. How about...
content::RenderView::F
tfarina
2014/07/03 13:19:36
Done.
| |
669 | 674 |
670 web_view->setMainFrame(blink::WebLocalFrame::create(this)); | 675 web_view->setMainFrame(blink::WebLocalFrame::create(this)); |
671 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); | 676 frame_.Reset(web_view->mainFrame()->toWebLocalFrame()); |
672 node_to_print_.reset(); | 677 node_to_print_.reset(); |
673 | 678 |
674 // When loading is done this will call didStopLoading() and that will do the | 679 // When loading is done this will call didStopLoading() and that will do the |
675 // actual printing. | 680 // actual printing. |
676 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); | 681 frame()->loadRequest(blink::WebURLRequest(GURL(url_str))); |
677 } | 682 } |
678 | 683 |
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1031 // Don't reset loading frame or WebKit will fail assert. Just retry when | 1036 // Don't reset loading frame or WebKit will fail assert. Just retry when |
1032 // current selection is loaded. | 1037 // current selection is loaded. |
1033 if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) { | 1038 if (prep_frame_view_ && prep_frame_view_->IsLoadingSelection()) { |
1034 reset_prep_frame_view_ = true; | 1039 reset_prep_frame_view_ = true; |
1035 return; | 1040 return; |
1036 } | 1041 } |
1037 | 1042 |
1038 const PrintMsg_Print_Params& print_params = print_pages_params_->params; | 1043 const PrintMsg_Print_Params& print_params = print_pages_params_->params; |
1039 prep_frame_view_.reset( | 1044 prep_frame_view_.reset( |
1040 new PrepareFrameAndViewForPrint(print_params, | 1045 new PrepareFrameAndViewForPrint(print_params, |
1046 render_view(), | |
1041 print_preview_context_.source_frame(), | 1047 print_preview_context_.source_frame(), |
1042 print_preview_context_.source_node(), | 1048 print_preview_context_.source_node(), |
1043 ignore_css_margins_)); | 1049 ignore_css_margins_)); |
1044 prep_frame_view_->CopySelectionIfNeeded( | 1050 prep_frame_view_->CopySelectionIfNeeded( |
1045 render_view()->GetWebkitPreferences(), | 1051 render_view()->GetWebkitPreferences(), |
1046 base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, | 1052 base::Bind(&PrintWebViewHelper::OnFramePreparedForPreviewDocument, |
1047 base::Unretained(this))); | 1053 base::Unretained(this))); |
1048 } | 1054 } |
1049 | 1055 |
1050 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { | 1056 void PrintWebViewHelper::OnFramePreparedForPreviewDocument() { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1430 #if !defined(OS_ANDROID) | 1436 #if !defined(OS_ANDROID) |
1431 // TODO(sgurun) android_webview hack | 1437 // TODO(sgurun) android_webview hack |
1432 render_view()->RunModalAlertDialog( | 1438 render_view()->RunModalAlertDialog( |
1433 frame, | 1439 frame, |
1434 l10n_util::GetStringUTF16(IDS_PRINT_INVALID_PRINTER_SETTINGS)); | 1440 l10n_util::GetStringUTF16(IDS_PRINT_INVALID_PRINTER_SETTINGS)); |
1435 #endif // !defined(OS_ANDROID) | 1441 #endif // !defined(OS_ANDROID) |
1436 return false; | 1442 return false; |
1437 } | 1443 } |
1438 | 1444 |
1439 const PrintMsg_Print_Params& params = print_pages_params_->params; | 1445 const PrintMsg_Print_Params& params = print_pages_params_->params; |
1440 PrepareFrameAndViewForPrint prepare(params, frame, node, ignore_css_margins_); | 1446 PrepareFrameAndViewForPrint prepare( |
1447 params, render_view(), frame, node, ignore_css_margins_); | |
1441 prepare.StartPrinting(); | 1448 prepare.StartPrinting(); |
1442 | 1449 |
1443 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), | 1450 Send(new PrintHostMsg_DidGetDocumentCookie(routing_id(), |
1444 params.document_cookie)); | 1451 params.document_cookie)); |
1445 *number_of_pages = prepare.GetExpectedPageCount(); | 1452 *number_of_pages = prepare.GetExpectedPageCount(); |
1446 return true; | 1453 return true; |
1447 } | 1454 } |
1448 | 1455 |
1449 bool PrintWebViewHelper::UpdatePrintSettings( | 1456 bool PrintWebViewHelper::UpdatePrintSettings( |
1450 blink::WebLocalFrame* frame, | 1457 blink::WebLocalFrame* frame, |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1583 print_pages_params_->params.print_scaling_option = scaling_option; | 1590 print_pages_params_->params.print_scaling_option = scaling_option; |
1584 return (print_settings.params.dpi && print_settings.params.document_cookie); | 1591 return (print_settings.params.dpi && print_settings.params.document_cookie); |
1585 } | 1592 } |
1586 | 1593 |
1587 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame, | 1594 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame, |
1588 const blink::WebNode& node) { | 1595 const blink::WebNode& node) { |
1589 if (!frame || prep_frame_view_) | 1596 if (!frame || prep_frame_view_) |
1590 return false; | 1597 return false; |
1591 const PrintMsg_PrintPages_Params& params = *print_pages_params_; | 1598 const PrintMsg_PrintPages_Params& params = *print_pages_params_; |
1592 const PrintMsg_Print_Params& print_params = params.params; | 1599 const PrintMsg_Print_Params& print_params = params.params; |
1593 prep_frame_view_.reset( | 1600 prep_frame_view_.reset(new PrepareFrameAndViewForPrint( |
1594 new PrepareFrameAndViewForPrint(print_params, frame, node, | 1601 print_params, render_view(), frame, node, ignore_css_margins_)); |
1595 ignore_css_margins_)); | |
1596 DCHECK(!print_pages_params_->params.selection_only || | 1602 DCHECK(!print_pages_params_->params.selection_only || |
1597 print_pages_params_->pages.empty()); | 1603 print_pages_params_->pages.empty()); |
1598 prep_frame_view_->CopySelectionIfNeeded( | 1604 prep_frame_view_->CopySelectionIfNeeded( |
1599 render_view()->GetWebkitPreferences(), | 1605 render_view()->GetWebkitPreferences(), |
1600 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, | 1606 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, |
1601 base::Unretained(this))); | 1607 base::Unretained(this))); |
1602 return true; | 1608 return true; |
1603 } | 1609 } |
1604 | 1610 |
1605 #if defined(OS_POSIX) | 1611 #if defined(OS_POSIX) |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2017 } | 2023 } |
2018 | 2024 |
2019 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { | 2025 void PrintWebViewHelper::PrintPreviewContext::ClearContext() { |
2020 prep_frame_view_.reset(); | 2026 prep_frame_view_.reset(); |
2021 metafile_.reset(); | 2027 metafile_.reset(); |
2022 pages_to_render_.clear(); | 2028 pages_to_render_.clear(); |
2023 error_ = PREVIEW_ERROR_NONE; | 2029 error_ = PREVIEW_ERROR_NONE; |
2024 } | 2030 } |
2025 | 2031 |
2026 } // namespace printing | 2032 } // namespace printing |
OLD | NEW |