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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/process_util.h" | 10 #include "base/process_util.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/common/render_messages.h" | 12 #include "chrome/common/render_messages.h" |
13 #include "chrome/common/render_messages_params.h" | 13 #include "chrome/common/render_messages_params.h" |
14 #include "chrome/renderer/render_view.h" | 14 #include "chrome/renderer/render_view.h" |
15 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 16 #include "printing/page_range.h" |
16 #include "printing/units.h" | 17 #include "printing/units.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScreenInfo.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
27 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
28 #include "webkit/glue/webkit_glue.h" | 29 #include "webkit/glue/webkit_glue.h" |
29 | 30 |
30 using printing::ConvertPixelsToPoint; | 31 using printing::ConvertPixelsToPoint; |
31 using printing::ConvertPixelsToPointDouble; | 32 using printing::ConvertPixelsToPointDouble; |
32 using printing::ConvertUnit; | 33 using printing::ConvertUnit; |
33 using printing::ConvertUnitDouble; | 34 using printing::ConvertUnitDouble; |
34 using WebKit::WebConsoleMessage; | 35 using WebKit::WebConsoleMessage; |
| 36 using WebKit::WebDocument; |
| 37 using WebKit::WebElement; |
35 using WebKit::WebFrame; | 38 using WebKit::WebFrame; |
36 using WebKit::WebNode; | 39 using WebKit::WebNode; |
37 using WebKit::WebRect; | 40 using WebKit::WebRect; |
38 using WebKit::WebSize; | 41 using WebKit::WebSize; |
39 using WebKit::WebScreenInfo; | 42 using WebKit::WebScreenInfo; |
40 using WebKit::WebString; | 43 using WebKit::WebString; |
41 using WebKit::WebURLRequest; | 44 using WebKit::WebURLRequest; |
42 using WebKit::WebView; | 45 using WebKit::WebView; |
43 | 46 |
44 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( | 47 PrepareFrameAndViewForPrint::PrepareFrameAndViewForPrint( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 PrintWebViewHelper::~PrintWebViewHelper() {} | 101 PrintWebViewHelper::~PrintWebViewHelper() {} |
99 | 102 |
100 void PrintWebViewHelper::PrintFrame(WebFrame* frame, | 103 void PrintWebViewHelper::PrintFrame(WebFrame* frame, |
101 bool script_initiated, | 104 bool script_initiated, |
102 bool is_preview) { | 105 bool is_preview) { |
103 Print(frame, NULL, script_initiated, is_preview); | 106 Print(frame, NULL, script_initiated, is_preview); |
104 } | 107 } |
105 | 108 |
106 void PrintWebViewHelper::PrintNode(WebNode* node, | 109 void PrintWebViewHelper::PrintNode(WebNode* node, |
107 bool script_initiated, | 110 bool script_initiated, |
108 bool is_preview) { | 111 bool is_preview, |
109 Print(node->document().frame(), node, script_initiated, is_preview); | 112 bool is_print_for_print_preview) { |
| 113 if (is_print_for_print_preview) |
| 114 PrintForPrintPreview(node->document().frame(), node); |
| 115 else |
| 116 Print(node->document().frame(), node, script_initiated, is_preview); |
110 } | 117 } |
111 | 118 |
112 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 119 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
113 bool handled = true; | 120 bool handled = true; |
114 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 121 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
| 122 IPC_MESSAGE_HANDLER(ViewMsg_PrintForPrintPreview, |
| 123 OnPrintForPrintPreview) |
115 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) | 124 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) |
116 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) | 125 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) |
117 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) | 126 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) |
118 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, | 127 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, |
119 OnPrintNodeUnderContextMenu) | 128 OnPrintNodeUnderContextMenu) |
120 IPC_MESSAGE_UNHANDLED(handled = false) | 129 IPC_MESSAGE_UNHANDLED(handled = false) |
121 IPC_END_MESSAGE_MAP() | 130 IPC_END_MESSAGE_MAP() |
122 return handled; | 131 return handled; |
123 } | 132 } |
124 | 133 |
(...skipping 26 matching lines...) Expand all Loading... |
151 | 160 |
152 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { | 161 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { |
153 if (render_view()->context_menu_node().isNull()) { | 162 if (render_view()->context_menu_node().isNull()) { |
154 NOTREACHED(); | 163 NOTREACHED(); |
155 return; | 164 return; |
156 } | 165 } |
157 | 166 |
158 // Make a copy of the node, since we will do a sync call to the browser and | 167 // Make a copy of the node, since we will do a sync call to the browser and |
159 // during that time OnContextMenuClosed might reset context_menu_node_. | 168 // during that time OnContextMenuClosed might reset context_menu_node_. |
160 WebNode context_menu_node(render_view()->context_menu_node()); | 169 WebNode context_menu_node(render_view()->context_menu_node()); |
161 PrintNode(&context_menu_node, false, false); | 170 PrintNode(&context_menu_node, false, false, false); |
| 171 } |
| 172 |
| 173 void PrintWebViewHelper::OnPrintForPrintPreview( |
| 174 const std::string& job_settings) { |
| 175 #if defined(OS_MACOSX) |
| 176 WebFrame* main_frame = render_view()->webview() ? |
| 177 render_view()->webview()->mainFrame() : NULL; |
| 178 if (!main_frame) |
| 179 return; |
| 180 |
| 181 WebDocument document = main_frame->document(); |
| 182 // <object> with id="pdf-viewer" is created in |
| 183 // chrome/browser/resources/print_preview.js |
| 184 WebElement element = document.getElementById("pdf-viewer"); |
| 185 if (element.isNull()) { |
| 186 NOTREACHED(); |
| 187 return; |
| 188 } |
| 189 InitPrintSettings(element.document().frame(), &element, |
| 190 PrintWebViewHelper::DEFAULT); |
| 191 UpdatePrintSettings(job_settings); |
| 192 PrintNode(&element, false, false, true); |
| 193 #endif |
| 194 } |
| 195 |
| 196 void PrintWebViewHelper::PrintForPrintPreview(WebKit::WebFrame* frame, |
| 197 WebNode* node) { |
| 198 // If still not finished with earlier print request simply ignore. |
| 199 if (print_web_view_) |
| 200 return; |
| 201 |
| 202 // Initialize print params with current print settings. |
| 203 if (!InitPrintSettings(frame, node, PrintWebViewHelper::CURRENT)) { |
| 204 NOTREACHED(); |
| 205 DidFinishPrinting(true); // Release all printing resources. |
| 206 return; // Failed to init print page settings. |
| 207 } |
| 208 |
| 209 // Render Pages for printing. |
| 210 RenderPagesForPrint(frame, node); |
162 } | 211 } |
163 | 212 |
164 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, | 213 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, |
165 WebNode* node, | 214 WebNode* node, |
166 bool script_initiated, | 215 bool script_initiated, |
167 bool is_preview) { | 216 bool is_preview) { |
168 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 217 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
169 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. | 218 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. |
170 | 219 |
171 // If still not finished with earlier print request simply ignore. | 220 // If still not finished with earlier print request simply ignore. |
(...skipping 18 matching lines...) Expand all Loading... |
190 WebConsoleMessage::LevelWarning, | 239 WebConsoleMessage::LevelWarning, |
191 message)); | 240 message)); |
192 return; | 241 return; |
193 } | 242 } |
194 } | 243 } |
195 | 244 |
196 bool print_cancelled = false; | 245 bool print_cancelled = false; |
197 is_preview_ = is_preview; | 246 is_preview_ = is_preview; |
198 | 247 |
199 // Initialize print settings. | 248 // Initialize print settings. |
200 if (!InitPrintSettings(frame, node)) | 249 if (!InitPrintSettings(frame, node, PrintWebViewHelper::DEFAULT)) |
201 return; // Failed to init print page settings. | 250 return; // Failed to init print page settings. |
202 | 251 |
203 int expected_pages_count = 0; | 252 int expected_pages_count = 0; |
204 bool use_browser_overlays = true; | 253 bool use_browser_overlays = true; |
205 | 254 |
206 // Prepare once to calculate the estimated page count. This must be in | 255 // Prepare once to calculate the estimated page count. This must be in |
207 // a scope for itself (see comments on PrepareFrameAndViewForPrint). | 256 // a scope for itself (see comments on PrepareFrameAndViewForPrint). |
208 { | 257 { |
209 PrepareFrameAndViewForPrint prep_frame_view( | 258 PrepareFrameAndViewForPrint prep_frame_view( |
210 (*print_pages_params_).params, frame, node, frame->view()); | 259 (*print_pages_params_).params, frame, node, frame->view()); |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 static_cast<int>(ConvertUnitDouble( | 506 static_cast<int>(ConvertUnitDouble( |
458 page_height_in_points, printing::kPointsPerInch, dpi))); | 507 page_height_in_points, printing::kPointsPerInch, dpi))); |
459 | 508 |
460 params->margin_top = static_cast<int>(ConvertUnitDouble( | 509 params->margin_top = static_cast<int>(ConvertUnitDouble( |
461 margin_top_in_points, printing::kPointsPerInch, dpi)); | 510 margin_top_in_points, printing::kPointsPerInch, dpi)); |
462 params->margin_left = static_cast<int>(ConvertUnitDouble( | 511 params->margin_left = static_cast<int>(ConvertUnitDouble( |
463 margin_left_in_points, printing::kPointsPerInch, dpi)); | 512 margin_left_in_points, printing::kPointsPerInch, dpi)); |
464 } | 513 } |
465 | 514 |
466 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame, | 515 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame, |
467 WebNode* node) { | 516 WebNode* node, |
| 517 GetSettingsParam setting_type) { |
468 ViewMsg_PrintPages_Params settings; | 518 ViewMsg_PrintPages_Params settings; |
469 if (GetDefaultPrintSettings(frame, node, &settings.params)) { | 519 if (GetPrintSettings(frame, node, setting_type, &settings.params)) { |
470 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); | 520 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); |
471 print_pages_params_->pages.clear(); | 521 print_pages_params_->pages.clear(); |
472 return true; | 522 return true; |
473 } | 523 } |
474 return false; | 524 return false; |
475 } | 525 } |
476 | 526 |
477 bool PrintWebViewHelper::GetDefaultPrintSettings( | 527 bool PrintWebViewHelper::UpdatePrintSettings(const std::string& job_settings) { |
478 WebFrame* frame, | 528 ViewMsg_PrintPages_Params settings; |
479 WebNode* node, | 529 IPC::SyncMessage* msg = new ViewHostMsg_UpdatePrintSettings( |
480 ViewMsg_Print_Params* params) { | 530 render_view()->routing_id(), |
481 if (!render_view()->Send(new ViewHostMsg_GetDefaultPrintSettings( | 531 (*print_pages_params_).params.document_cookie, |
482 render_view()->routing_id(), params))) { | 532 job_settings, &settings.params); |
| 533 if (!render_view()->Send(msg)) { |
483 NOTREACHED(); | 534 NOTREACHED(); |
484 return false; | 535 return false; |
485 } | 536 } |
| 537 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); |
| 538 return true; |
| 539 } |
| 540 |
| 541 bool PrintWebViewHelper::GetPrintSettings(WebFrame* frame, WebNode* node, |
| 542 GetSettingsParam setting_type, ViewMsg_Print_Params* params) { |
| 543 IPC::SyncMessage* msg = NULL; |
| 544 if (setting_type == CURRENT) { |
| 545 msg = new ViewHostMsg_GetCurrentPrintSettings(render_view()->routing_id(), |
| 546 (*print_pages_params_).params.document_cookie, |
| 547 params); |
| 548 } else { |
| 549 msg = new ViewHostMsg_GetDefaultPrintSettings(render_view()->routing_id(), |
| 550 params); |
| 551 } |
| 552 if (!render_view()->Send(msg)) { |
| 553 NOTREACHED(); |
| 554 return false; |
| 555 } |
486 // Check if the printer returned any settings, if the settings is empty, we | 556 // Check if the printer returned any settings, if the settings is empty, we |
487 // can safely assume there are no printer drivers configured. So we safely | 557 // can safely assume there are no printer drivers configured. So we safely |
488 // terminate. | 558 // terminate. |
489 if (params->IsEmpty()) { | 559 if (params->IsEmpty()) { |
490 render_view()->runModalAlertDialog( | 560 render_view()->runModalAlertDialog( |
491 frame, | 561 frame, |
492 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); | 562 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); |
493 return false; | 563 return false; |
494 } | 564 } |
495 if (!(params->dpi && params->document_cookie)) { | 565 if (!(params->dpi && params->document_cookie)) { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | 647 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
578 shared_mem_handle); | 648 shared_mem_handle); |
579 return true; | 649 return true; |
580 } | 650 } |
581 } | 651 } |
582 } | 652 } |
583 NOTREACHED(); | 653 NOTREACHED(); |
584 return false; | 654 return false; |
585 } | 655 } |
586 #endif | 656 #endif |
OLD | NEW |