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/renderer/prerender/prerender_helper.h" | 17 #include "chrome/renderer/prerender/prerender_helper.h" |
18 #include "content/renderer/render_view.h" | 18 #include "content/renderer/render_view.h" |
19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
20 #include "printing/metafile.h" | 20 #include "printing/metafile.h" |
21 #include "printing/print_job_constants.h" | |
21 #include "printing/units.h" | 22 #include "printing/units.h" |
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebConsoleMessage.h" |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | |
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" | |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 33 #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" | 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
32 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
33 | 36 |
34 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
35 #include "content/common/view_messages.h" | 38 #include "content/common/view_messages.h" |
36 #endif | 39 #endif |
37 | 40 |
38 using printing::ConvertPixelsToPoint; | 41 using printing::ConvertPixelsToPoint; |
39 using printing::ConvertPixelsToPointDouble; | 42 using printing::ConvertPixelsToPointDouble; |
40 using printing::ConvertUnit; | 43 using printing::ConvertUnit; |
41 using printing::ConvertUnitDouble; | 44 using printing::ConvertUnitDouble; |
42 using WebKit::WebConsoleMessage; | 45 using WebKit::WebConsoleMessage; |
43 using WebKit::WebDocument; | 46 using WebKit::WebDocument; |
44 using WebKit::WebElement; | 47 using WebKit::WebElement; |
45 using WebKit::WebFrame; | 48 using WebKit::WebFrame; |
46 using WebKit::WebNode; | 49 using WebKit::WebNode; |
47 using WebKit::WebSize; | 50 using WebKit::WebSize; |
48 using WebKit::WebString; | 51 using WebKit::WebString; |
49 using WebKit::WebURLRequest; | 52 using WebKit::WebURLRequest; |
50 using WebKit::WebView; | 53 using WebKit::WebView; |
51 | 54 |
52 namespace { | 55 namespace { |
53 | 56 |
57 const double kMinDpi = 1.0; | |
58 | |
54 int GetDPI(const PrintMsg_Print_Params* print_params) { | 59 int GetDPI(const PrintMsg_Print_Params* print_params) { |
55 #if defined(OS_MACOSX) | 60 #if defined(OS_MACOSX) |
56 // On the Mac, the printable area is in points, don't do any scaling based | 61 // On the Mac, the printable area is in points, don't do any scaling based |
57 // on dpi. | 62 // on dpi. |
58 return printing::kPointsPerInch; | 63 return printing::kPointsPerInch; |
59 #else | 64 #else |
60 return static_cast<int>(print_params->dpi); | 65 return static_cast<int>(print_params->dpi); |
61 #endif // defined(OS_MACOSX) | 66 #endif // defined(OS_MACOSX) |
62 } | 67 } |
63 | 68 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 context_menu_preview_node_.reset(); | 164 context_menu_preview_node_.reset(); |
160 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); | 165 Send(new PrintHostMsg_RequestPrintPreview(routing_id())); |
161 } else { | 166 } else { |
162 Print(frame, NULL); | 167 Print(frame, NULL); |
163 } | 168 } |
164 } | 169 } |
165 | 170 |
166 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { | 171 bool PrintWebViewHelper::OnMessageReceived(const IPC::Message& message) { |
167 bool handled = true; | 172 bool handled = true; |
168 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) | 173 IPC_BEGIN_MESSAGE_MAP(PrintWebViewHelper, message) |
174 IPC_MESSAGE_HANDLER(PrintMsg_PrintSetupWhitelist, | |
175 OnPrintSetupWhitelist) | |
176 IPC_MESSAGE_HANDLER(PrintMsg_PrintTeardownWhitelist, | |
177 OnPrintTeardownWhitelist) | |
178 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | |
179 OnPrintForPrintPreview) | |
169 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 180 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
170 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, | 181 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, |
171 OnInitiatePrintPreview) | 182 OnInitiatePrintPreview) |
172 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 183 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
173 OnPrintNodeUnderContextMenu) | 184 OnPrintNodeUnderContextMenu) |
174 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 185 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
175 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | |
176 OnPrintForPrintPreview) | |
177 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 186 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
178 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 187 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
179 ResetScriptedPrintCount) | 188 ResetScriptedPrintCount) |
180 IPC_MESSAGE_UNHANDLED(handled = false) | 189 IPC_MESSAGE_UNHANDLED(handled = false) |
181 IPC_END_MESSAGE_MAP() | 190 IPC_END_MESSAGE_MAP() |
182 return handled; | 191 return handled; |
183 } | 192 } |
184 | 193 |
194 void PrintWebViewHelper::OnPrintSetupWhitelist(std::string host) { | |
195 WebKit::WebSecurityPolicy::addOriginAccessWhitelistEntry( | |
Lei Zhang
2011/06/09 21:23:30
I'm not a web security expert. Is this ok to do?
Albert Bodenhamer
2011/06/10 01:29:36
I'm still trying to figure out the best person to
| |
196 GURL("chrome://print"), | |
Lei Zhang
2011/06/09 21:23:30
chrome://print -> chrome::kChromeUIPrintHost
| |
197 WebKit::WebString::fromUTF8("HTTPS"), | |
Lei Zhang
2011/06/09 21:23:30
HTTPS -> chrome::kHttpsScheme
| |
198 WebKit::WebString::fromUTF8(host), | |
199 true); | |
200 } | |
201 | |
202 void PrintWebViewHelper::OnPrintTeardownWhitelist(std::string host) { | |
203 WebKit::WebSecurityPolicy::removeOriginAccessWhitelistEntry( | |
204 GURL("chrome://print"), | |
205 WebKit::WebString::fromUTF8("HTTPS"), | |
206 WebKit::WebString::fromUTF8(host), | |
207 true); | |
208 } | |
209 | |
185 void PrintWebViewHelper::OnPrintForPrintPreview( | 210 void PrintWebViewHelper::OnPrintForPrintPreview( |
186 const DictionaryValue& job_settings) { | 211 const DictionaryValue& job_settings) { |
187 DCHECK(is_preview_); | 212 DCHECK(is_preview_); |
188 // If still not finished with earlier print request simply ignore. | 213 // If still not finished with earlier print request simply ignore. |
189 if (print_web_view_) | 214 if (print_web_view_) |
190 return; | 215 return; |
191 | 216 |
192 if (!render_view()->webview()) | 217 if (!render_view()->webview()) |
193 return; | 218 return; |
194 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 219 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
195 if (!main_frame) | 220 if (!main_frame) |
196 return; | 221 return; |
197 | 222 |
198 WebDocument document = main_frame->document(); | 223 WebDocument document = main_frame->document(); |
199 // <object> with id="pdf-viewer" is created in | 224 // <object> with id="pdf-viewer" is created in |
200 // chrome/browser/resources/print_preview.js | 225 // chrome/browser/resources/print_preview.js |
201 WebElement pdf_element = document.getElementById("pdf-viewer"); | 226 WebElement pdf_element = document.getElementById("pdf-viewer"); |
202 if (pdf_element.isNull()) { | 227 if (pdf_element.isNull()) { |
203 NOTREACHED(); | 228 NOTREACHED(); |
204 return; | 229 return; |
205 } | 230 } |
206 | 231 |
207 WebFrame* pdf_frame = pdf_element.document().frame(); | 232 WebFrame* pdf_frame = pdf_element.document().frame(); |
208 if (!InitPrintSettings(pdf_frame, &pdf_element)) { | 233 if (!InitPrintSettings(pdf_frame, &pdf_element)) { |
209 NOTREACHED() << "Failed to initialize print page settings"; | 234 NOTREACHED() << "Failed to initialize print page settings"; |
210 return; | 235 return; |
211 } | 236 } |
212 | |
213 if (!UpdatePrintSettings(job_settings)) { | 237 if (!UpdatePrintSettings(job_settings)) { |
214 DidFinishPrinting(FAIL_PRINT); | 238 DidFinishPrinting(FAIL_PRINT); |
215 return; | 239 return; |
216 } | 240 } |
217 | 241 |
218 // Render Pages for printing. | 242 // Render Pages for printing. |
219 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) | 243 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) |
220 DidFinishPrinting(FAIL_PRINT); | 244 DidFinishPrinting(FAIL_PRINT); |
221 } | 245 } |
222 | 246 |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 &settings.params)); | 606 &settings.params)); |
583 // Check if the printer returned any settings, if the settings is empty, we | 607 // Check if the printer returned any settings, if the settings is empty, we |
584 // can safely assume there are no printer drivers configured. So we safely | 608 // can safely assume there are no printer drivers configured. So we safely |
585 // terminate. | 609 // terminate. |
586 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 610 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
587 render_view()->runModalAlertDialog( | 611 render_view()->runModalAlertDialog( |
588 frame, | 612 frame, |
589 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); | 613 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); |
590 return false; | 614 return false; |
591 } | 615 } |
592 if (!settings.params.dpi || !settings.params.document_cookie) { | 616 if (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0) { |
593 // Invalid print page settings. | 617 // Invalid print page settings. |
594 NOTREACHED(); | 618 NOTREACHED(); |
595 return false; | 619 return false; |
596 } | 620 } |
597 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); | 621 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); |
598 settings.pages.clear(); | 622 settings.pages.clear(); |
599 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 623 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
600 return true; | 624 return true; |
601 } | 625 } |
602 | 626 |
603 bool PrintWebViewHelper::UpdatePrintSettings( | 627 bool PrintWebViewHelper::UpdatePrintSettings( |
604 const DictionaryValue& job_settings) { | 628 const DictionaryValue& job_settings) { |
605 PrintMsg_PrintPages_Params settings; | 629 bool print_to_cloud = job_settings.HasKey(printing::kSettingCloudPrintId); |
606 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 630 if (print_to_cloud) { |
607 print_pages_params_->params.document_cookie, job_settings, &settings)); | 631 // TODO(abodenha@chromium.org) Really update the settings for a cloud |
632 // printer. | |
633 return true; | |
634 } else { | |
635 PrintMsg_PrintPages_Params settings; | |
636 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | |
637 print_pages_params_->params.document_cookie, job_settings, &settings)); | |
608 | 638 |
609 if (!settings.params.dpi) | 639 if (settings.params.dpi < kMinDpi) |
610 return false; | 640 return false; |
611 | 641 |
612 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 642 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
613 return true; | 643 return true; |
644 } | |
614 } | 645 } |
615 | 646 |
616 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 647 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
617 int expected_pages_count, | 648 int expected_pages_count, |
618 bool use_browser_overlays) { | 649 bool use_browser_overlays) { |
619 PrintHostMsg_ScriptedPrint_Params params; | 650 PrintHostMsg_ScriptedPrint_Params params; |
620 PrintMsg_PrintPages_Params print_settings; | 651 PrintMsg_PrintPages_Params print_settings; |
621 | 652 |
622 // The routing id is sent across as it is needed to look up the | 653 // The routing id is sent across as it is needed to look up the |
623 // corresponding RenderViewHost instance to signal and reset the | 654 // corresponding RenderViewHost instance to signal and reset the |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
744 | 775 |
745 void PrintWebViewHelper::ResetScriptedPrintCount() { | 776 void PrintWebViewHelper::ResetScriptedPrintCount() { |
746 // Reset cancel counter on successful print. | 777 // Reset cancel counter on successful print. |
747 user_cancelled_scripted_print_count_ = 0; | 778 user_cancelled_scripted_print_count_ = 0; |
748 } | 779 } |
749 | 780 |
750 void PrintWebViewHelper::IncrementScriptedPrintCount() { | 781 void PrintWebViewHelper::IncrementScriptedPrintCount() { |
751 ++user_cancelled_scripted_print_count_; | 782 ++user_cancelled_scripted_print_count_; |
752 last_cancelled_script_print_ = base::Time::Now(); | 783 last_cancelled_script_print_ = base::Time::Now(); |
753 } | 784 } |
OLD | NEW |