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/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/print_messages.h" | 15 #include "chrome/common/print_messages.h" |
16 #include "chrome/common/render_messages.h" | 16 #include "chrome/common/render_messages.h" |
17 #include "chrome/common/url_constants.h" | |
17 #include "chrome/renderer/prerender/prerender_helper.h" | 18 #include "chrome/renderer/prerender/prerender_helper.h" |
18 #include "content/renderer/render_view.h" | 19 #include "content/renderer/render_view.h" |
19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
20 #include "printing/metafile.h" | 21 #include "printing/metafile.h" |
22 #include "printing/print_job_constants.h" | |
21 #include "printing/units.h" | 23 #include "printing/units.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | |
Lei Zhang
2011/06/14 01:08:08
no longer needed for this CL
Albert Bodenhamer
2011/06/14 19:43:54
Done.
| |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
32 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
33 | 37 |
34 #if defined(OS_POSIX) | 38 #if defined(OS_POSIX) |
35 #include "content/common/view_messages.h" | 39 #include "content/common/view_messages.h" |
36 #endif | 40 #endif |
37 | 41 |
38 using printing::ConvertPixelsToPoint; | 42 using printing::ConvertPixelsToPoint; |
39 using printing::ConvertPixelsToPointDouble; | 43 using printing::ConvertPixelsToPointDouble; |
40 using printing::ConvertUnit; | 44 using printing::ConvertUnit; |
41 using printing::ConvertUnitDouble; | 45 using printing::ConvertUnitDouble; |
42 using WebKit::WebConsoleMessage; | 46 using WebKit::WebConsoleMessage; |
43 using WebKit::WebDocument; | 47 using WebKit::WebDocument; |
44 using WebKit::WebElement; | 48 using WebKit::WebElement; |
45 using WebKit::WebFrame; | 49 using WebKit::WebFrame; |
46 using WebKit::WebNode; | 50 using WebKit::WebNode; |
47 using WebKit::WebSize; | 51 using WebKit::WebSize; |
48 using WebKit::WebString; | 52 using WebKit::WebString; |
49 using WebKit::WebURLRequest; | 53 using WebKit::WebURLRequest; |
50 using WebKit::WebView; | 54 using WebKit::WebView; |
51 | 55 |
52 namespace { | 56 namespace { |
53 | 57 |
58 const double kMinDpi = 1.0; | |
59 | |
54 int GetDPI(const PrintMsg_Print_Params* print_params) { | 60 int GetDPI(const PrintMsg_Print_Params* print_params) { |
55 #if defined(OS_MACOSX) | 61 #if defined(OS_MACOSX) |
56 // On the Mac, the printable area is in points, don't do any scaling based | 62 // On the Mac, the printable area is in points, don't do any scaling based |
57 // on dpi. | 63 // on dpi. |
58 return printing::kPointsPerInch; | 64 return printing::kPointsPerInch; |
59 #else | 65 #else |
60 return static_cast<int>(print_params->dpi); | 66 return static_cast<int>(print_params->dpi); |
61 #endif // defined(OS_MACOSX) | 67 #endif // defined(OS_MACOSX) |
62 } | 68 } |
63 | 69 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 context_menu_preview_node_.reset(); | 165 context_menu_preview_node_.reset(); |
160 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 166 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
161 } else { | 167 } else { |
162 Print(frame, NULL); | 168 Print(frame, NULL); |
163 } | 169 } |
164 } | 170 } |
165 | 171 |
166 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 172 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
167 bool handled = true; | 173 bool handled = true; |
168 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 174 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
175 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | |
176 OnPrintForPrintPreview) | |
169 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 177 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
170 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, | 178 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, |
171 OnInitiatePrintPreview) | 179 OnInitiatePrintPreview) |
172 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 180 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
173 OnPrintNodeUnderContextMenu) | 181 OnPrintNodeUnderContextMenu) |
174 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 182 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
175 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | |
176 OnPrintForPrintPreview) | |
177 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 183 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
178 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 184 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
179 ResetScriptedPrintCount) | 185 ResetScriptedPrintCount) |
180 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, | 186 IPC_MESSAGE_HANDLER(PrintMsg_PreviewPrintingRequestCancelled, |
181 DisplayPrintJobError) | 187 DisplayPrintJobError) |
182 IPC_MESSAGE_UNHANDLED(handled = false) | 188 IPC_MESSAGE_UNHANDLED(handled = false) |
183 IPC_END_MESSAGE_MAP() | 189 IPC_END_MESSAGE_MAP() |
184 return handled; | 190 return handled; |
185 } | 191 } |
186 | 192 |
(...skipping 17 matching lines...) Expand all Loading... | |
204 if (pdf_element.isNull()) { | 210 if (pdf_element.isNull()) { |
205 NOTREACHED(); | 211 NOTREACHED(); |
206 return; | 212 return; |
207 } | 213 } |
208 | 214 |
209 WebFrame* pdf_frame = pdf_element.document().frame(); | 215 WebFrame* pdf_frame = pdf_element.document().frame(); |
210 if (!InitPrintSettings(pdf_frame, &pdf_element)) { | 216 if (!InitPrintSettings(pdf_frame, &pdf_element)) { |
211 NOTREACHED() << "Failed to initialize print page settings"; | 217 NOTREACHED() << "Failed to initialize print page settings"; |
212 return; | 218 return; |
213 } | 219 } |
214 | |
215 if (!UpdatePrintSettings(job_settings)) { | 220 if (!UpdatePrintSettings(job_settings)) { |
216 DidFinishPrinting(FAIL_PRINT); | 221 DidFinishPrinting(FAIL_PRINT); |
217 return; | 222 return; |
218 } | 223 } |
219 | 224 |
220 // Render Pages for printing. | 225 // Render Pages for printing. |
221 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) | 226 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) |
222 DidFinishPrinting(FAIL_PRINT); | 227 DidFinishPrinting(FAIL_PRINT); |
223 } | 228 } |
224 | 229 |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
578 &settings.params)); | 583 &settings.params)); |
579 // Check if the printer returned any settings, if the settings is empty, we | 584 // Check if the printer returned any settings, if the settings is empty, we |
580 // can safely assume there are no printer drivers configured. So we safely | 585 // can safely assume there are no printer drivers configured. So we safely |
581 // terminate. | 586 // terminate. |
582 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 587 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
583 render_view()->runModalAlertDialog( | 588 render_view()->runModalAlertDialog( |
584 frame, | 589 frame, |
585 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); | 590 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); |
586 return false; | 591 return false; |
587 } | 592 } |
588 if (!settings.params.dpi || !settings.params.document_cookie) { | 593 if (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0) { |
589 // Invalid print page settings. | 594 // Invalid print page settings. |
590 NOTREACHED(); | 595 NOTREACHED(); |
591 return false; | 596 return false; |
592 } | 597 } |
593 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); | 598 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); |
594 settings.pages.clear(); | 599 settings.pages.clear(); |
595 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 600 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
596 return true; | 601 return true; |
597 } | 602 } |
598 | 603 |
599 bool PrintWebViewHelper::UpdatePrintSettings( | 604 bool PrintWebViewHelper::UpdatePrintSettings( |
600 const DictionaryValue& job_settings) { | 605 const DictionaryValue& job_settings) { |
601 PrintMsg_PrintPages_Params settings; | 606 bool print_to_cloud = job_settings.HasKey(printing::kSettingCloudPrintId); |
602 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 607 if (print_to_cloud) { |
603 print_pages_params_->params.document_cookie, job_settings, &settings)); | 608 // TODO(abodenha@chromium.org) Really update the settings for a cloud |
609 // printer. | |
610 return true; | |
611 } else { | |
612 PrintMsg_PrintPages_Params settings; | |
613 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | |
614 print_pages_params_->params.document_cookie, job_settings, &settings)); | |
604 | 615 |
605 if (!settings.params.dpi) | 616 if (settings.params.dpi < kMinDpi) |
606 return false; | 617 return false; |
607 | 618 |
608 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 619 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
609 return true; | 620 return true; |
621 } | |
610 } | 622 } |
611 | 623 |
612 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 624 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
613 int expected_pages_count, | 625 int expected_pages_count, |
614 bool use_browser_overlays) { | 626 bool use_browser_overlays) { |
615 PrintHostMsg_ScriptedPrint_Params params; | 627 PrintHostMsg_ScriptedPrint_Params params; |
616 PrintMsg_PrintPages_Params print_settings; | 628 PrintMsg_PrintPages_Params print_settings; |
617 | 629 |
618 // The routing id is sent across as it is needed to look up the | 630 // The routing id is sent across as it is needed to look up the |
619 // corresponding RenderViewHost instance to signal and reset the | 631 // corresponding RenderViewHost instance to signal and reset the |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
750 | 762 |
751 void PrintWebViewHelper::DisplayPrintJobError() { | 763 void PrintWebViewHelper::DisplayPrintJobError() { |
752 WebView* web_view = print_web_view_; | 764 WebView* web_view = print_web_view_; |
753 if (!web_view) | 765 if (!web_view) |
754 web_view = render_view()->webview(); | 766 web_view = render_view()->webview(); |
755 | 767 |
756 render_view()->runModalAlertDialog( | 768 render_view()->runModalAlertDialog( |
757 web_view->mainFrame(), | 769 web_view->mainFrame(), |
758 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); | 770 l10n_util::GetStringUTF16(IDS_PRINT_SPOOL_FAILED_ERROR_TEXT)); |
759 } | 771 } |
OLD | NEW |