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

Side by Side Diff: chrome/renderer/printing/print_web_view_helper.cc

Issue 740983002: Implement window.print() on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/printing/print_web_view_helper.h" 5 #include "chrome/renderer/printing/print_web_view_helper.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/auto_reset.h" 9 #include "base/auto_reset.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after
788 788
789 PrintWebViewHelper::~PrintWebViewHelper() {} 789 PrintWebViewHelper::~PrintWebViewHelper() {}
790 790
791 // static 791 // static
792 void PrintWebViewHelper::DisablePreview() { 792 void PrintWebViewHelper::DisablePreview() {
793 g_is_preview_enabled_ = false; 793 g_is_preview_enabled_ = false;
794 } 794 }
795 795
796 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed( 796 bool PrintWebViewHelper::IsScriptInitiatedPrintAllowed(
797 blink::WebFrame* frame, bool user_initiated) { 797 blink::WebFrame* frame, bool user_initiated) {
798 #if defined(OS_ANDROID)
799 return false;
800 #endif // defined(OS_ANDROID)
801 // If preview is enabled, then the print dialog is tab modal, and the user 798 // If preview is enabled, then the print dialog is tab modal, and the user
802 // can always close the tab on a mis-behaving page (the system print dialog 799 // can always close the tab on a mis-behaving page (the system print dialog
803 // is app modal). If the print was initiated through user action, don't 800 // is app modal). If the print was initiated through user action, don't
804 // throttle. Or, if the command line flag to skip throttling has been set. 801 // throttle. Or, if the command line flag to skip throttling has been set.
805 return !is_scripted_printing_blocked_ && 802 return !is_scripted_printing_blocked_ &&
806 (user_initiated || g_is_preview_enabled_ || 803 (user_initiated || g_is_preview_enabled_ ||
807 scripting_throttler_.IsAllowed(frame)); 804 scripting_throttler_.IsAllowed(frame));
808 } 805 }
809 806
810 void PrintWebViewHelper::DidStartLoading() { 807 void PrintWebViewHelper::DidStartLoading() {
811 is_loading_ = true; 808 is_loading_ = true;
812 } 809 }
813 810
814 void PrintWebViewHelper::DidStopLoading() { 811 void PrintWebViewHelper::DidStopLoading() {
815 is_loading_ = false; 812 is_loading_ = false;
816 if (!on_stop_loading_closure_.is_null()) { 813 if (!on_stop_loading_closure_.is_null()) {
817 on_stop_loading_closure_.Run(); 814 on_stop_loading_closure_.Run();
818 on_stop_loading_closure_.Reset(); 815 on_stop_loading_closure_.Reset();
819 } 816 }
820 } 817 }
821 818
822 // Prints |frame| which called window.print(). 819 // Prints |frame| which called window.print().
823 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame, 820 void PrintWebViewHelper::PrintPage(blink::WebLocalFrame* frame,
824 bool user_initiated) { 821 bool user_initiated) {
822
823 DLOG(INFO) << "DGN PrintPage - From Script - routing_id: " << routing_id();
824
825 DCHECK(frame); 825 DCHECK(frame);
826 826
827 // Allow Prerendering to cancel this print request if necessary. 827 // Allow Prerendering to cancel this print request if necessary.
828 if (prerender::PrerenderHelper::IsPrerendering( 828 if (prerender::PrerenderHelper::IsPrerendering(
829 render_view()->GetMainRenderFrame())) { 829 render_view()->GetMainRenderFrame())) {
830 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id())); 830 Send(new ChromeViewHostMsg_CancelPrerenderForPrinting(routing_id()));
831 return; 831 return;
832 } 832 }
833 833
834 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) 834 if (!IsScriptInitiatedPrintAllowed(frame, user_initiated)) {
835 DLOG(INFO) << "DGN PrintPage - Script printing not allowed";
835 return; 836 return;
837 }
836 838
837 if (!g_is_preview_enabled_) { 839 if (!g_is_preview_enabled_) {
838 Print(frame, blink::WebNode()); 840 bool use_system_specific_flow = false;
dgn 2014/11/25 17:07:42 past that point, it I didn't find any way of knowi
841
842 #if defined(OS_ANDROID)
843 use_system_specific_flow = true;
844 #endif
845
846 Print(frame, blink::WebNode(), use_system_specific_flow);
839 } else { 847 } else {
840 print_preview_context_.InitWithFrame(frame); 848 print_preview_context_.InitWithFrame(frame);
841 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED); 849 RequestPrintPreview(PRINT_PREVIEW_SCRIPTED);
842 } 850 }
843 } 851 }
844 852
845 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { 853 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
846 bool handled = true; 854 bool handled = true;
847 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) 855 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
848 #if defined(ENABLE_BASIC_PRINTING) 856 #if defined(ENABLE_BASIC_PRINTING)
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
940 blink::WebLocalFrame* focusedFrame = 948 blink::WebLocalFrame* focusedFrame =
941 webView->focusedFrame()->toWebLocalFrame(); 949 webView->focusedFrame()->toWebLocalFrame();
942 *frame = focusedFrame->hasSelection() 950 *frame = focusedFrame->hasSelection()
943 ? focusedFrame 951 ? focusedFrame
944 : webView->mainFrame()->toWebLocalFrame(); 952 : webView->mainFrame()->toWebLocalFrame();
945 return true; 953 return true;
946 } 954 }
947 955
948 #if defined(ENABLE_BASIC_PRINTING) 956 #if defined(ENABLE_BASIC_PRINTING)
949 void PrintWebViewHelper::OnPrintPages() { 957 void PrintWebViewHelper::OnPrintPages() {
958 DLOG(INFO) << "DGN - OnPrintPages - Call from PrintManager";
950 blink::WebLocalFrame* frame; 959 blink::WebLocalFrame* frame;
951 if (GetPrintFrame(&frame)) 960 if (GetPrintFrame(&frame))
952 Print(frame, blink::WebNode()); 961 Print(frame, blink::WebNode());
953 } 962 }
954 963
955 void PrintWebViewHelper::OnPrintForSystemDialog() { 964 void PrintWebViewHelper::OnPrintForSystemDialog() {
956 blink::WebLocalFrame* frame = print_preview_context_.source_frame(); 965 blink::WebLocalFrame* frame = print_preview_context_.source_frame();
957 if (!frame) { 966 if (!frame) {
958 NOTREACHED(); 967 NOTREACHED();
959 return; 968 return;
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
1236 Print(duplicate_node.document().frame(), duplicate_node); 1245 Print(duplicate_node.document().frame(), duplicate_node);
1237 } else { 1246 } else {
1238 print_preview_context_.InitWithNode(node); 1247 print_preview_context_.InitWithNode(node);
1239 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE); 1248 RequestPrintPreview(PRINT_PREVIEW_USER_INITIATED_CONTEXT_NODE);
1240 } 1249 }
1241 1250
1242 print_node_in_progress_ = false; 1251 print_node_in_progress_ = false;
1243 } 1252 }
1244 1253
1245 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, 1254 void PrintWebViewHelper::Print(blink::WebLocalFrame* frame,
1246 const blink::WebNode& node) { 1255 const blink::WebNode& node,
1256 const bool use_system_specific_flow) {
1247 // If still not finished with earlier print request simply ignore. 1257 // If still not finished with earlier print request simply ignore.
1248 if (prep_frame_view_) 1258 DLOG(INFO) << "DGN Print";
1259
1260 if (prep_frame_view_) {
1261 DLOG(INFO) << "DGN Print request ignored";
1249 return; 1262 return;
1263 }
1250 1264
1251 FrameReference frame_ref(frame); 1265 FrameReference frame_ref(frame);
1252 1266
1253 int expected_page_count = 0; 1267 int expected_page_count = 0;
1254 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { 1268 if (!CalculateNumberOfPages(frame, node, &expected_page_count)) {
1255 DidFinishPrinting(FAIL_PRINT_INIT); 1269 DidFinishPrinting(FAIL_PRINT_INIT);
1256 return; // Failed to init print page settings. 1270 return; // Failed to init print page settings.
1257 } 1271 }
1258 1272
1273 DLOG(INFO) << "DGN CalculateNumberOfPages OK";
1274
1259 // Some full screen plugins can say they don't want to print. 1275 // Some full screen plugins can say they don't want to print.
1260 if (!expected_page_count) { 1276 if (!expected_page_count) {
1261 DidFinishPrinting(FAIL_PRINT); 1277 DidFinishPrinting(FAIL_PRINT);
1262 return; 1278 return;
1263 } 1279 }
1264 1280
1265 // Ask the browser to show UI to retrieve the final print settings. 1281 // Ask the browser to show UI to retrieve the final print settings.
1266 if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node, 1282 if (!GetPrintSettingsFromUser(frame_ref.GetFrame(), node,
1267 expected_page_count)) { 1283 expected_page_count,
1284 use_system_specific_flow)) {
1268 DidFinishPrinting(OK); // Release resources and fail silently. 1285 DidFinishPrinting(OK); // Release resources and fail silently.
1269 return; 1286 return;
1270 } 1287 }
1271 1288
1289 DLOG(INFO) << "DGN GetPrintSettingsFromUser OK";
1290
1272 // Render Pages for printing. 1291 // Render Pages for printing.
1273 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) { 1292 if (!RenderPagesForPrint(frame_ref.GetFrame(), node)) {
1274 LOG(ERROR) << "RenderPagesForPrint failed"; 1293 LOG(ERROR) << "RenderPagesForPrint failed";
1275 DidFinishPrinting(FAIL_PRINT); 1294 DidFinishPrinting(FAIL_PRINT);
1276 } 1295 }
1277 scripting_throttler_.Reset(); 1296 scripting_throttler_.Reset();
1278 } 1297 }
1279 1298
1280 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) { 1299 void PrintWebViewHelper::DidFinishPrinting(PrintingResult result) {
1300 if (result == OK) DLOG(INFO) << "DGN DidFinishPrinting";
1301 else DLOG(INFO) << "DGN DidFinishPrinting - Failure: " << result;
1302
1281 switch (result) { 1303 switch (result) {
1282 case OK: 1304 case OK:
1283 break; 1305 break;
1284 1306
1285 case FAIL_PRINT_INIT: 1307 case FAIL_PRINT_INIT:
1286 DCHECK(!notify_browser_of_print_failure_); 1308 DCHECK(!notify_browser_of_print_failure_);
1287 break; 1309 break;
1288 1310
1289 case FAIL_PRINT: 1311 case FAIL_PRINT:
1290 if (notify_browser_of_print_failure_ && print_pages_params_) { 1312 if (notify_browser_of_print_failure_ && print_pages_params_) {
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1534 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); 1556 Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id()));
1535 1557
1536 return false; 1558 return false;
1537 } 1559 }
1538 1560
1539 return true; 1561 return true;
1540 } 1562 }
1541 1563
1542 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame, 1564 bool PrintWebViewHelper::GetPrintSettingsFromUser(blink::WebFrame* frame,
1543 const blink::WebNode& node, 1565 const blink::WebNode& node,
1544 int expected_pages_count) { 1566 int expected_pages_count,
1567 const bool system_specific) {
1545 PrintHostMsg_ScriptedPrint_Params params; 1568 PrintHostMsg_ScriptedPrint_Params params;
1546 PrintMsg_PrintPages_Params print_settings; 1569 PrintMsg_PrintPages_Params print_settings;
1547 1570
1548 params.cookie = print_pages_params_->params.document_cookie; 1571 params.cookie = print_pages_params_->params.document_cookie;
1549 params.has_selection = frame->hasSelection(); 1572 params.has_selection = frame->hasSelection();
1550 params.expected_pages_count = expected_pages_count; 1573 params.expected_pages_count = expected_pages_count;
1551 MarginType margin_type = DEFAULT_MARGINS; 1574 MarginType margin_type = DEFAULT_MARGINS;
1552 if (PrintingNodeOrPdfFrame(frame, node)) 1575 if (PrintingNodeOrPdfFrame(frame, node))
1553 margin_type = GetMarginsForPdf(frame, node); 1576 margin_type = GetMarginsForPdf(frame, node);
1554 params.margin_type = margin_type; 1577 params.margin_type = margin_type;
1578 params.use_system_specific_flow = system_specific;
1555 1579
1556 Send(new PrintHostMsg_DidShowPrintDialog(routing_id())); 1580 Send(new PrintHostMsg_DidShowPrintDialog(routing_id()));
1557 1581
1558 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the 1582 // PrintHostMsg_ScriptedPrint will reset print_scaling_option, so we save the
1559 // value before and restore it afterwards. 1583 // value before and restore it afterwards.
1560 blink::WebPrintScalingOption scaling_option = 1584 blink::WebPrintScalingOption scaling_option =
1561 print_pages_params_->params.print_scaling_option; 1585 print_pages_params_->params.print_scaling_option;
1562 1586
1563 print_pages_params_.reset(); 1587 print_pages_params_.reset();
1564 IPC::SyncMessage* msg = 1588 IPC::SyncMessage* msg =
1565 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings); 1589 new PrintHostMsg_ScriptedPrint(routing_id(), params, &print_settings);
1566 msg->EnableMessagePumping(); 1590 msg->EnableMessagePumping();
1567 Send(msg); 1591 Send(msg);
1568 print_settings.params.print_scaling_option = scaling_option; 1592 print_settings.params.print_scaling_option = scaling_option;
1569 SetPrintPagesParams(print_settings); 1593 SetPrintPagesParams(print_settings);
1570 return (print_settings.params.dpi && print_settings.params.document_cookie); 1594 return (print_settings.params.dpi && print_settings.params.document_cookie);
1571 } 1595 }
1572 1596
1573 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame, 1597 bool PrintWebViewHelper::RenderPagesForPrint(blink::WebLocalFrame* frame,
1574 const blink::WebNode& node) { 1598 const blink::WebNode& node) {
1575 if (!frame || prep_frame_view_) 1599 if (!frame || prep_frame_view_)
1576 return false; 1600 return false;
1601
1577 const PrintMsg_PrintPages_Params& params = *print_pages_params_; 1602 const PrintMsg_PrintPages_Params& params = *print_pages_params_;
1578 const PrintMsg_Print_Params& print_params = params.params; 1603 const PrintMsg_Print_Params& print_params = params.params;
1579 prep_frame_view_.reset(new PrepareFrameAndViewForPrint( 1604 prep_frame_view_.reset(new PrepareFrameAndViewForPrint(
1580 print_params, frame, node, ignore_css_margins_)); 1605 print_params, frame, node, ignore_css_margins_));
1581 DCHECK(!print_pages_params_->params.selection_only || 1606 DCHECK(!print_pages_params_->params.selection_only ||
1582 print_pages_params_->pages.empty()); 1607 print_pages_params_->pages.empty());
1583 prep_frame_view_->CopySelectionIfNeeded( 1608 prep_frame_view_->CopySelectionIfNeeded(
1584 render_view()->GetWebkitPreferences(), 1609 render_view()->GetWebkitPreferences(),
1585 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages, 1610 base::Bind(&PrintWebViewHelper::OnFramePreparedForPrintPages,
1586 base::Unretained(this))); 1611 base::Unretained(this)));
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
2023 blink::WebConsoleMessage::LevelWarning, message)); 2048 blink::WebConsoleMessage::LevelWarning, message));
2024 return false; 2049 return false;
2025 } 2050 }
2026 2051
2027 void PrintWebViewHelper::ScriptingThrottler::Reset() { 2052 void PrintWebViewHelper::ScriptingThrottler::Reset() {
2028 // Reset counter on successful print. 2053 // Reset counter on successful print.
2029 count_ = 0; 2054 count_ = 0;
2030 } 2055 }
2031 2056
2032 } // namespace printing 2057 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698