| 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" |
| 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_PrintSetupWhitelist, |
| 176 OnPrintSetupWhitelist) |
| 177 IPC_MESSAGE_HANDLER(PrintMsg_PrintTeardownWhitelist, |
| 178 OnPrintTeardownWhitelist) |
| 179 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, |
| 180 OnPrintForPrintPreview) |
| 169 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) | 181 IPC_MESSAGE_HANDLER(PrintMsg_PrintPages, OnPrintPages) |
| 170 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, | 182 IPC_MESSAGE_HANDLER(PrintMsg_InitiatePrintPreview, |
| 171 OnInitiatePrintPreview) | 183 OnInitiatePrintPreview) |
| 172 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, | 184 IPC_MESSAGE_HANDLER(PrintMsg_PrintNodeUnderContextMenu, |
| 173 OnPrintNodeUnderContextMenu) | 185 OnPrintNodeUnderContextMenu) |
| 174 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) | 186 IPC_MESSAGE_HANDLER(PrintMsg_PrintPreview, OnPrintPreview) |
| 175 IPC_MESSAGE_HANDLER(PrintMsg_PrintForPrintPreview, | |
| 176 OnPrintForPrintPreview) | |
| 177 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) | 187 IPC_MESSAGE_HANDLER(PrintMsg_PrintingDone, OnPrintingDone) |
| 178 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, | 188 IPC_MESSAGE_HANDLER(PrintMsg_ResetScriptedPrintCount, |
| 179 ResetScriptedPrintCount) | 189 ResetScriptedPrintCount) |
| 180 IPC_MESSAGE_UNHANDLED(handled = false) | 190 IPC_MESSAGE_UNHANDLED(handled = false) |
| 181 IPC_END_MESSAGE_MAP() | 191 IPC_END_MESSAGE_MAP() |
| 182 return handled; | 192 return handled; |
| 183 } | 193 } |
| 184 | 194 |
| 195 void PrintWebViewHelper::OnPrintSetupWhitelist(std::string host) { |
| 196 WebKit::WebSecurityPolicy::addOriginAccessWhitelistEntry( |
| 197 GURL(chrome::kChromeUIPrintHost), |
| 198 WebKit::WebString::fromUTF8(chrome::kHttpsScheme), |
| 199 WebKit::WebString::fromUTF8(host), |
| 200 true); |
| 201 } |
| 202 |
| 203 void PrintWebViewHelper::OnPrintTeardownWhitelist(std::string host) { |
| 204 WebKit::WebSecurityPolicy::removeOriginAccessWhitelistEntry( |
| 205 GURL(chrome::kChromeUIPrintHost), |
| 206 WebKit::WebString::fromUTF8(chrome::kHttpsScheme), |
| 207 WebKit::WebString::fromUTF8(host), |
| 208 true); |
| 209 } |
| 210 |
| 185 void PrintWebViewHelper::OnPrintForPrintPreview( | 211 void PrintWebViewHelper::OnPrintForPrintPreview( |
| 186 const DictionaryValue& job_settings) { | 212 const DictionaryValue& job_settings) { |
| 187 DCHECK(is_preview_); | 213 DCHECK(is_preview_); |
| 188 // If still not finished with earlier print request simply ignore. | 214 // If still not finished with earlier print request simply ignore. |
| 189 if (print_web_view_) | 215 if (print_web_view_) |
| 190 return; | 216 return; |
| 191 | 217 |
| 192 if (!render_view()->webview()) | 218 if (!render_view()->webview()) |
| 193 return; | 219 return; |
| 194 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 220 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
| 195 if (!main_frame) | 221 if (!main_frame) |
| 196 return; | 222 return; |
| 197 | 223 |
| 198 WebDocument document = main_frame->document(); | 224 WebDocument document = main_frame->document(); |
| 199 // <object> with id="pdf-viewer" is created in | 225 // <object> with id="pdf-viewer" is created in |
| 200 // chrome/browser/resources/print_preview.js | 226 // chrome/browser/resources/print_preview.js |
| 201 WebElement pdf_element = document.getElementById("pdf-viewer"); | 227 WebElement pdf_element = document.getElementById("pdf-viewer"); |
| 202 if (pdf_element.isNull()) { | 228 if (pdf_element.isNull()) { |
| 203 NOTREACHED(); | 229 NOTREACHED(); |
| 204 return; | 230 return; |
| 205 } | 231 } |
| 206 | 232 |
| 207 WebFrame* pdf_frame = pdf_element.document().frame(); | 233 WebFrame* pdf_frame = pdf_element.document().frame(); |
| 208 if (!InitPrintSettings(pdf_frame, &pdf_element)) { | 234 if (!InitPrintSettings(pdf_frame, &pdf_element)) { |
| 209 NOTREACHED() << "Failed to initialize print page settings"; | 235 NOTREACHED() << "Failed to initialize print page settings"; |
| 210 return; | 236 return; |
| 211 } | 237 } |
| 212 | |
| 213 if (!UpdatePrintSettings(job_settings)) { | 238 if (!UpdatePrintSettings(job_settings)) { |
| 214 DidFinishPrinting(FAIL_PRINT); | 239 DidFinishPrinting(FAIL_PRINT); |
| 215 return; | 240 return; |
| 216 } | 241 } |
| 217 | 242 |
| 218 // Render Pages for printing. | 243 // Render Pages for printing. |
| 219 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) | 244 if (!RenderPagesForPrint(pdf_frame, &pdf_element)) |
| 220 DidFinishPrinting(FAIL_PRINT); | 245 DidFinishPrinting(FAIL_PRINT); |
| 221 } | 246 } |
| 222 | 247 |
| (...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 &settings.params)); | 607 &settings.params)); |
| 583 // Check if the printer returned any settings, if the settings is empty, we | 608 // 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 | 609 // can safely assume there are no printer drivers configured. So we safely |
| 585 // terminate. | 610 // terminate. |
| 586 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { | 611 if (PrintMsg_Print_Params_IsEmpty(settings.params)) { |
| 587 render_view()->runModalAlertDialog( | 612 render_view()->runModalAlertDialog( |
| 588 frame, | 613 frame, |
| 589 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); | 614 l10n_util::GetStringUTF16(IDS_DEFAULT_PRINTER_NOT_FOUND_WARNING)); |
| 590 return false; | 615 return false; |
| 591 } | 616 } |
| 592 if (!settings.params.dpi || !settings.params.document_cookie) { | 617 if (settings.params.dpi < kMinDpi || settings.params.document_cookie == 0) { |
| 593 // Invalid print page settings. | 618 // Invalid print page settings. |
| 594 NOTREACHED(); | 619 NOTREACHED(); |
| 595 return false; | 620 return false; |
| 596 } | 621 } |
| 597 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); | 622 UpdatePrintableSizeInPrintParameters(frame, node, &settings.params); |
| 598 settings.pages.clear(); | 623 settings.pages.clear(); |
| 599 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 624 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 600 return true; | 625 return true; |
| 601 } | 626 } |
| 602 | 627 |
| 603 bool PrintWebViewHelper::UpdatePrintSettings( | 628 bool PrintWebViewHelper::UpdatePrintSettings( |
| 604 const DictionaryValue& job_settings) { | 629 const DictionaryValue& job_settings) { |
| 605 PrintMsg_PrintPages_Params settings; | 630 bool print_to_cloud = job_settings.HasKey(printing::kSettingCloudPrintId); |
| 606 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), | 631 if (print_to_cloud) { |
| 607 print_pages_params_->params.document_cookie, job_settings, &settings)); | 632 // TODO(abodenha@chromium.org) Really update the settings for a cloud |
| 633 // printer. |
| 634 return true; |
| 635 } else { |
| 636 PrintMsg_PrintPages_Params settings; |
| 637 Send(new PrintHostMsg_UpdatePrintSettings(routing_id(), |
| 638 print_pages_params_->params.document_cookie, job_settings, &settings)); |
| 608 | 639 |
| 609 if (!settings.params.dpi) | 640 if (settings.params.dpi < kMinDpi) |
| 610 return false; | 641 return false; |
| 611 | 642 |
| 612 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); | 643 print_pages_params_.reset(new PrintMsg_PrintPages_Params(settings)); |
| 613 return true; | 644 return true; |
| 645 } |
| 614 } | 646 } |
| 615 | 647 |
| 616 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, | 648 bool PrintWebViewHelper::GetPrintSettingsFromUser(WebKit::WebFrame* frame, |
| 617 int expected_pages_count, | 649 int expected_pages_count, |
| 618 bool use_browser_overlays) { | 650 bool use_browser_overlays) { |
| 619 PrintHostMsg_ScriptedPrint_Params params; | 651 PrintHostMsg_ScriptedPrint_Params params; |
| 620 PrintMsg_PrintPages_Params print_settings; | 652 PrintMsg_PrintPages_Params print_settings; |
| 621 | 653 |
| 622 // The routing id is sent across as it is needed to look up the | 654 // The routing id is sent across as it is needed to look up the |
| 623 // corresponding RenderViewHost instance to signal and reset the | 655 // corresponding RenderViewHost instance to signal and reset the |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 744 | 776 |
| 745 void PrintWebViewHelper::ResetScriptedPrintCount() { | 777 void PrintWebViewHelper::ResetScriptedPrintCount() { |
| 746 // Reset cancel counter on successful print. | 778 // Reset cancel counter on successful print. |
| 747 user_cancelled_scripted_print_count_ = 0; | 779 user_cancelled_scripted_print_count_ = 0; |
| 748 } | 780 } |
| 749 | 781 |
| 750 void PrintWebViewHelper::IncrementScriptedPrintCount() { | 782 void PrintWebViewHelper::IncrementScriptedPrintCount() { |
| 751 ++user_cancelled_scripted_print_count_; | 783 ++user_cancelled_scripted_print_count_; |
| 752 last_cancelled_script_print_ = base::Time::Now(); | 784 last_cancelled_script_print_ = base::Time::Now(); |
| 753 } | 785 } |
| OLD | NEW |