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

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

Powered by Google App Engine
This is Rietveld 408576698