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" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) | 132 IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) |
133 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) | 133 IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) |
134 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) | 134 IPC_MESSAGE_HANDLER(ViewMsg_PrintPreview, OnPrintPreview) |
135 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, | 135 IPC_MESSAGE_HANDLER(ViewMsg_PrintNodeUnderContextMenu, |
136 OnPrintNodeUnderContextMenu) | 136 OnPrintNodeUnderContextMenu) |
137 IPC_MESSAGE_UNHANDLED(handled = false) | 137 IPC_MESSAGE_UNHANDLED(handled = false) |
138 IPC_END_MESSAGE_MAP() | 138 IPC_END_MESSAGE_MAP() |
139 return handled; | 139 return handled; |
140 } | 140 } |
141 | 141 |
142 void PrintWebViewHelper::OnPrintForPrintPreview() { | 142 void PrintWebViewHelper::OnPrintForPrintPreview( |
143 const DictionaryValue& job_settings) { | |
144 #if defined(OS_MACOSX) | |
143 if (!render_view()->webview()) | 145 if (!render_view()->webview()) |
144 return; | 146 return; |
145 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 147 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
146 if (!main_frame) | 148 if (!main_frame) |
147 return; | 149 return; |
148 | 150 |
149 WebDocument document = main_frame->document(); | 151 WebDocument document = main_frame->document(); |
150 // <object> with id="pdf-viewer" is created in | 152 // <object> with id="pdf-viewer" is created in |
151 // chrome/browser/resources/print_preview.js | 153 // chrome/browser/resources/print_preview.js |
152 WebElement element = document.getElementById("pdf-viewer"); | 154 WebElement element = document.getElementById("pdf-viewer"); |
153 if (element.isNull()) { | 155 if (element.isNull()) { |
154 NOTREACHED(); | 156 NOTREACHED(); |
155 return; | 157 return; |
156 } | 158 } |
157 | 159 |
158 PrintNode(&element, false, false); | 160 InitPrintSettings(element.document().frame(), &element, |
Lei Zhang
2011/03/03 00:22:44
What happens when InitPrintSettings() or UpdatePri
kmadhusu
2011/03/04 19:39:02
Fixed.
| |
161 PrintWebViewHelper::DEFAULT); | |
162 UpdatePrintSettings(job_settings); | |
163 PrintForPrintPreview(element.document().frame(), &element); | |
164 #endif | |
159 } | 165 } |
160 | 166 |
161 void PrintWebViewHelper::OnPrint(bool is_preview) { | 167 void PrintWebViewHelper::OnPrint(bool is_preview) { |
162 DCHECK(render_view()->webview()); | 168 DCHECK(render_view()->webview()); |
163 if (!render_view()->webview()) | 169 if (!render_view()->webview()) |
164 return; | 170 return; |
165 | 171 |
166 // 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 |
167 // only that frame (this makes print selection work for multiple frames). | 173 // only that frame (this makes print selection work for multiple frames). |
168 if (render_view()->webview()->focusedFrame()->hasSelection()) | 174 if (render_view()->webview()->focusedFrame()->hasSelection()) |
(...skipping 21 matching lines...) Expand all Loading... | |
190 NOTREACHED(); | 196 NOTREACHED(); |
191 return; | 197 return; |
192 } | 198 } |
193 | 199 |
194 // 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 |
195 // during that time OnContextMenuClosed might reset context_menu_node_. | 201 // during that time OnContextMenuClosed might reset context_menu_node_. |
196 WebNode context_menu_node(render_view()->context_menu_node()); | 202 WebNode context_menu_node(render_view()->context_menu_node()); |
197 PrintNode(&context_menu_node, false, false); | 203 PrintNode(&context_menu_node, false, false); |
198 } | 204 } |
199 | 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)) { | |
Lei Zhang
2011/03/03 00:22:44
Didn't we just call InitPrintSettings() and Update
kmadhusu
2011/03/04 19:39:02
Removed this function. Calling RenderPagesForPrint
| |
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); | |
221 } | |
222 | |
200 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, | 223 void PrintWebViewHelper::Print(WebKit::WebFrame* frame, |
201 WebNode* node, | 224 WebNode* node, |
202 bool script_initiated, | 225 bool script_initiated, |
203 bool is_preview) { | 226 bool is_preview) { |
204 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; | 227 const int kMinSecondsToIgnoreJavascriptInitiatedPrint = 2; |
205 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. | 228 const int kMaxSecondsToIgnoreJavascriptInitiatedPrint = 2 * 60; // 2 Minutes. |
206 | 229 |
207 // If still not finished with earlier print request simply ignore. | 230 // If still not finished with earlier print request simply ignore. |
208 if (print_web_view_) | 231 if (print_web_view_) |
209 return; | 232 return; |
(...skipping 16 matching lines...) Expand all Loading... | |
226 WebConsoleMessage::LevelWarning, | 249 WebConsoleMessage::LevelWarning, |
227 message)); | 250 message)); |
228 return; | 251 return; |
229 } | 252 } |
230 } | 253 } |
231 | 254 |
232 bool print_cancelled = false; | 255 bool print_cancelled = false; |
233 is_preview_ = is_preview; | 256 is_preview_ = is_preview; |
234 | 257 |
235 // Initialize print settings. | 258 // Initialize print settings. |
236 if (!InitPrintSettings(frame, node)) | 259 if (!InitPrintSettings(frame, node, PrintWebViewHelper::DEFAULT)) |
237 return; // Failed to init print page settings. | 260 return; // Failed to init print page settings. |
238 | 261 |
239 int expected_pages_count = 0; | 262 int expected_pages_count = 0; |
240 bool use_browser_overlays = true; | 263 bool use_browser_overlays = true; |
241 | 264 |
242 // 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 |
243 // a scope for itself (see comments on PrepareFrameAndViewForPrint). | 266 // a scope for itself (see comments on PrepareFrameAndViewForPrint). |
244 { | 267 { |
245 PrepareFrameAndViewForPrint prep_frame_view( | 268 PrepareFrameAndViewForPrint prep_frame_view( |
246 (*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 Loading... | |
482 static_cast<int>(ConvertUnitDouble( | 505 static_cast<int>(ConvertUnitDouble( |
483 page_height_in_points, printing::kPointsPerInch, dpi))); | 506 page_height_in_points, printing::kPointsPerInch, dpi))); |
484 | 507 |
485 params->margin_top = static_cast<int>(ConvertUnitDouble( | 508 params->margin_top = static_cast<int>(ConvertUnitDouble( |
486 margin_top_in_points, printing::kPointsPerInch, dpi)); | 509 margin_top_in_points, printing::kPointsPerInch, dpi)); |
487 params->margin_left = static_cast<int>(ConvertUnitDouble( | 510 params->margin_left = static_cast<int>(ConvertUnitDouble( |
488 margin_left_in_points, printing::kPointsPerInch, dpi)); | 511 margin_left_in_points, printing::kPointsPerInch, dpi)); |
489 } | 512 } |
490 | 513 |
491 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame, | 514 bool PrintWebViewHelper::InitPrintSettings(WebFrame* frame, |
492 WebNode* node) { | 515 WebNode* node, |
516 GetSettingsParam setting_type) { | |
493 ViewMsg_PrintPages_Params settings; | 517 ViewMsg_PrintPages_Params settings; |
494 if (GetDefaultPrintSettings(frame, node, &settings.params)) { | 518 if (GetPrintSettings(frame, node, setting_type, &settings.params)) { |
495 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); | 519 print_pages_params_.reset(new ViewMsg_PrintPages_Params(settings)); |
496 print_pages_params_->pages.clear(); | 520 print_pages_params_->pages.clear(); |
497 return true; | 521 return true; |
498 } | 522 } |
499 return false; | 523 return false; |
500 } | 524 } |
501 | 525 |
502 bool PrintWebViewHelper::GetDefaultPrintSettings( | 526 bool PrintWebViewHelper::UpdatePrintSettings( |
503 WebFrame* frame, | 527 const DictionaryValue& job_settings) { |
504 WebNode* node, | 528 ViewMsg_PrintPages_Params settings; |
505 ViewMsg_Print_Params* params) { | 529 IPC::SyncMessage* msg = new ViewHostMsg_UpdatePrintSettings( |
506 if (!render_view()->Send(new ViewHostMsg_GetDefaultPrintSettings( | 530 render_view()->routing_id(), |
507 render_view()->routing_id(), params))) { | 531 (*print_pages_params_).params.document_cookie, |
532 job_settings, &settings.params); | |
533 if (!render_view()->Send(msg)) { | |
508 NOTREACHED(); | 534 NOTREACHED(); |
509 return false; | 535 return false; |
510 } | 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 } | |
511 // 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 |
512 // 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 |
513 // terminate. | 558 // terminate. |
514 if (params->IsEmpty()) { | 559 if (params->IsEmpty()) { |
515 render_view()->runModalAlertDialog( | 560 render_view()->runModalAlertDialog( |
516 frame, | 561 frame, |
517 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); | 562 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); |
518 return false; | 563 return false; |
519 } | 564 } |
520 if (!(params->dpi && params->document_cookie)) { | 565 if (!(params->dpi && params->document_cookie)) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), | 636 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), |
592 shared_mem_handle); | 637 shared_mem_handle); |
593 return true; | 638 return true; |
594 } | 639 } |
595 } | 640 } |
596 } | 641 } |
597 NOTREACHED(); | 642 NOTREACHED(); |
598 return false; | 643 return false; |
599 } | 644 } |
600 #endif // defined(OS_POSIX) | 645 #endif // defined(OS_POSIX) |
OLD | NEW |