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

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

Issue 6533006: Print Preview: Hook up the print button to initiate printing without displaying a print dialog. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: '' Created 9 years, 10 months 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 | Annotate | Revision Log
OLDNEW
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"
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 PrintWebViewHelper::~PrintWebViewHelper() {} 109 PrintWebViewHelper::~PrintWebViewHelper() {}
110 110
111 void PrintWebViewHelper::PrintFrame(WebFrame* frame, 111 void PrintWebViewHelper::PrintFrame(WebFrame* frame,
112 bool script_initiated, 112 bool script_initiated,
113 bool is_preview) { 113 bool is_preview) {
114 Print(frame, NULL, script_initiated, is_preview); 114 Print(frame, NULL, script_initiated, is_preview);
115 } 115 }
116 116
117 void PrintWebViewHelper::PrintNode(WebNode* node, 117 void PrintWebViewHelper::PrintNode(WebNode* node,
118 bool script_initiated, 118 bool script_initiated,
119 bool is_preview) { 119 bool is_preview,
120 Print(node->document().frame(), node, script_initiated, is_preview); 120 bool is_print_for_print_preview) {
121 if (is_print_for_print_preview)
122 PrintForPrintPreview(node->document().frame(), node);
123 else
124 Print(node->document().frame(), node, script_initiated, is_preview);
121 } 125 }
122 126
123 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { 127 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) {
124 bool handled = true; 128 bool handled = true;
125 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) 129 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message)
126 IPC_MESSAGE_HANDLER(ViewMsg_PrintForPrintPreview, 130 IPC_MESSAGE_HANDLER(ViewMsg_PrintForPrintPreview,
127 OnPrintForPrintPreview) 131 OnPrintForPrintPreview)
128 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) 132 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages)
129 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) 133 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone)
130 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) 134 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview)
131 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, 135 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu,
132 OnPrintNodeUnderContextMenu) 136 OnPrintNodeUnderContextMenu)
133 IPC_MESSAGE_UNHANDLED(handled = false) 137 IPC_MESSAGE_UNHANDLED(handled = false)
134 IPC_END_MESSAGE_MAP() 138 IPC_END_MESSAGE_MAP()
135 return handled; 139 return handled;
136 } 140 }
137 141
138 void PrintWebViewHelper::OnPrintForPrintPreview() { 142 void PrintWebViewHelper::OnPrintForPrintPreview(
143 const std::string& job_settings) {
144 #if defined(OS_MACOSX)
139 if (!render_view()->webview()) 145 if (!render_view()->webview())
140 return; 146 return;
141 WebFrame* main_frame = render_view()->webview()->mainFrame(); 147 WebFrame* main_frame = render_view()->webview()->mainFrame();
142 if (!main_frame) 148 if (!main_frame)
143 return; 149 return;
144 150
145 WebDocument document = main_frame->document(); 151 WebDocument document = main_frame->document();
146 // <object> with id="pdf-viewer" is created in 152 // <object> with id="pdf-viewer" is created in
147 // chrome/browser/resources/print_preview.js 153 // chrome/browser/resources/print_preview.js
148 WebElement element = document.getElementById("pdf-viewer"); 154 WebElement element = document.getElementById("pdf-viewer");
149 if (element.isNull()) { 155 if (element.isNull()) {
150 NOTREACHED(); 156 NOTREACHED();
151 return; 157 return;
152 } 158 }
153 159
154 PrintNode(&element, false, false); 160 InitPrintSettings(element.document().frame(), &element,
161 PrintWebViewHelper::DEFAULT);
162 UpdatePrintSettings(job_settings);
163 PrintNode(&element, false, false, true);
Lei Zhang 2011/02/18 05:21:42 Why change PrintNode()? Why not just call PrintFor
kmadhusu 2011/02/21 01:30:26 Fixed.
164 #endif
155 } 165 }
156 166
157 void PrintWebViewHelper::OnPrint(bool is_preview) { 167 void PrintWebViewHelper::OnPrint(bool is_preview) {
158 DCHECK(render_view()->webview()); 168 DCHECK(render_view()->webview());
159 if (!render_view()->webview()) 169 if (!render_view()->webview())
160 return; 170 return;
161 171
162 // If the user has selected text in the currently focused frame we print 172 // If the user has selected text in the currently focused frame we print
163 // only that frame (this makes print selection work for multiple frames). 173 // only that frame (this makes print selection work for multiple frames).
164 if (render_view()->webview()->focusedFrame()->hasSelection()) 174 if (render_view()->webview()->focusedFrame()->hasSelection())
(...skipping 18 matching lines...) Expand all
183 193
184 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() { 194 void PrintWebViewHelper::OnPrintNodeUnderContextMenu() {
185 if (render_view()->context_menu_node().isNull()) { 195 if (render_view()->context_menu_node().isNull()) {
186 NOTREACHED(); 196 NOTREACHED();
187 return; 197 return;
188 } 198 }
189 199
190 // Make a copy of the node, since we will do a sync call to the browser and 200 // Make a copy of the node, since we will do a sync call to the browser and
191 // during that time OnContextMenuClosed might reset context_menu_node_. 201 // during that time OnContextMenuClosed might reset context_menu_node_.
192 WebNode context_menu_node(render_view()->context_menu_node()); 202 WebNode context_menu_node(render_view()->context_menu_node());
193 PrintNode(&context_menu_node, false, false); 203 PrintNode(&context_menu_node, false, false, false);
204 }
205
206 void PrintWebViewHelper::PrintForPrintPreview(WebKit::WebFrame* frame,
207 WebNode* node) {
208 // If still not finished with earlier print request simply ignore.
209 if (print_web_view_)
210 return;
211
212 // Initialize print params with current print settings.
213 if (!InitPrintSettings(frame, node, PrintWebViewHelper::CURRENT)) {
214 NOTREACHED();
215 DidFinishPrinting(true); // Release all printing resources.
216 return; // Failed to init print page settings.
217 }
218
219 // Render Pages for printing.
220 RenderPagesForPrint(frame, node);
194 } 221 }
195 222
196 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, 223 void PrintWebViewHelper::Print(WebKit::WebFrame* frame,
197 WebNode* node, 224 WebNode* node,
198 bool script_initiated, 225 bool script_initiated,
199 bool is_preview) { 226 bool is_preview) {
200 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; 227 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2;
201 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. 228 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes.
202 229
203 // If still not finished with earlier print request simply ignore. 230 // If still not finished with earlier print request simply ignore.
(...skipping 18 matching lines...) Expand all
222 WebConsoleMessage::LevelWarning, 249 WebConsoleMessage::LevelWarning,
223 message)); 250 message));
224 return; 251 return;
225 } 252 }
226 } 253 }
227 254
228 bool print_cancelled = false; 255 bool print_cancelled = false;
229 is_preview_ = is_preview; 256 is_preview_ = is_preview;
230 257
231 // Initialize print settings. 258 // Initialize print settings.
232 if (!InitPrintSettings(frame, node)) 259 if (!InitPrintSettings(frame, node, PrintWebViewHelper::DEFAULT))
233 return; // Failed to init print page settings. 260 return; // Failed to init print page settings.
234 261
235 int expected_pages_count = 0; 262 int expected_pages_count = 0;
236 bool use_browser_overlays = true; 263 bool use_browser_overlays = true;
237 264
238 // Prepare once to calculate the estimated page count. This must be in 265 // Prepare once to calculate the estimated page count. This must be in
239 // a scope for itself (see comments on PrepareFrameAndViewForPrint). 266 // a scope for itself (see comments on PrepareFrameAndViewForPrint).
240 { 267 {
241 PrepareFrameAndViewForPrint prep_frame_view( 268 PrepareFrameAndViewForPrint prep_frame_view(
242 (*print_pages_params_).params, frame, node, frame->view()); 269 (*print_pages_params_).params, frame, node, frame->view());
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 static_cast<int>(ConvertUnitDouble( 505 static_cast<int>(ConvertUnitDouble(
479 page_height_in_points, printing::kPointsPerInch, dpi))); 506 page_height_in_points, printing::kPointsPerInch, dpi)));
480 507
481 params->margin_top = static_cast<int>(ConvertUnitDouble( 508 params->margin_top = static_cast<int>(ConvertUnitDouble(
482 margin_top_in_points, printing::kPointsPerInch, dpi)); 509 margin_top_in_points, printing::kPointsPerInch, dpi));
483 params->margin_left = static_cast<int>(ConvertUnitDouble( 510 params->margin_left = static_cast<int>(ConvertUnitDouble(
484 margin_left_in_points, printing::kPointsPerInch, dpi)); 511 margin_left_in_points, printing::kPointsPerInch, dpi));
485 } 512 }
486 513
487 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame, 514 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame,
488 WebNode* node) { 515 WebNode* node,
516 GetSettingsParam setting_type) {
489 ViewMsg_PrintPages_Params settings; 517 ViewMsg_PrintPages_Params settings;
490 if (GetDefaultPrintSettings(frame, node, &settings.params)) { 518 if (GetPrintSettings(frame, node, setting_type, &settings.params)) {
491 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); 519 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings));
492 print_pages_params_->pages.clear(); 520 print_pages_params_->pages.clear();
493 return true; 521 return true;
494 } 522 }
495 return false; 523 return false;
496 } 524 }
497 525
498 bool PrintWebViewHelper::GetDefaultPrintSettings( 526 bool PrintWebViewHelper::UpdatePrintSettings(const std::string& job_settings) {
499 WebFrame* frame, 527 ViewMsg_PrintPages_Params settings;
500 WebNode* node, 528 IPC::SyncMessage* msg = new ViewHostMsg_UpdatePrintSettings(
501 ViewMsg_Print_Params* params) { 529 render_view()->routing_id(),
502 if (!render_view()->Send(new ViewHostMsg_GetDefaultPrintSettings( 530 (*print_pages_params_).params.document_cookie,
503 render_view()->routing_id(), params))) { 531 job_settings, &settings.params);
532 if (!render_view()->Send(msg)) {
504 NOTREACHED(); 533 NOTREACHED();
505 return false; 534 return false;
506 } 535 }
536 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings));
537 return true;
538 }
539
540 bool PrintWebViewHelper::GetPrintSettings(WebFrame* frame, WebNode* node,
541 GetSettingsParam setting_type, ViewMsg_Print_Params* params) {
542 IPC::SyncMessage* msg = NULL;
543 if (setting_type == CURRENT) {
544 msg = new ViewHostMsg_GetCurrentPrintSettings(render_view()->routing_id(),
545 (*print_pages_params_).params.document_cookie,
546 params);
547 } else {
548 msg = new ViewHostMsg_GetDefaultPrintSettings(render_view()->routing_id(),
549 params);
550 }
551 if (!render_view()->Send(msg)) {
552 NOTREACHED();
553 return false;
554 }
507 // Check if the printer returned any settings, if the settings is empty, we 555 // Check if the printer returned any settings, if the settings is empty, we
508 // can safely assume there are no printer drivers configured. So we safely 556 // can safely assume there are no printer drivers configured. So we safely
509 // terminate. 557 // terminate.
510 if (params->IsEmpty()) { 558 if (params->IsEmpty()) {
511 render_view()->runModalAlertDialog( 559 render_view()->runModalAlertDialog(
512 frame, 560 frame,
513 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); 561 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING));
514 return false; 562 return false;
515 } 563 }
516 if (!(params->dpi && params->document_cookie)) { 564 if (!(params->dpi && params->document_cookie)) {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), 646 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(),
599 shared_mem_handle); 647 shared_mem_handle);
600 return true; 648 return true;
601 } 649 }
602 } 650 }
603 } 651 }
604 NOTREACHED(); 652 NOTREACHED();
605 return false; 653 return false;
606 } 654 }
607 #endif 655 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698