From 6e099af5fee8da4bcfed982f35688191ec975e14 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 10 Jun 2015 11:34:16 +0800 Subject: [PATCH] Move PDF printing setting in JS part. --- atom/browser/api/atom_api_web_contents.cc | 17 ++--- atom/browser/api/atom_api_web_contents.h | 5 +- atom/browser/api/lib/web-contents.coffee | 43 ++++++++++++ atom/browser/native_window.h | 1 - .../printing/print_preview_message_handler.cc | 69 ++----------------- .../printing/print_preview_message_handler.h | 12 ++-- 6 files changed, 64 insertions(+), 83 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index e325ed902aed..d72e5539a6de 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -582,6 +582,7 @@ void WebContents::UnregisterServiceWorker( callback); } +<<<<<<< HEAD <<<<<<< HEAD void WebContents::SetAudioMuted(bool muted) { web_contents()->SetAudioMuted(muted); @@ -591,18 +592,10 @@ bool WebContents::IsAudioMuted() { return web_contents()->IsAudioMuted(); } -void WebContents::PrintToPDF(mate::Arguments* args) { - mate::Dictionary options; - base::Callback callback; - if (!(args->Length() == 1 && args->GetNext(&callback)) && - !(args->Length() == 2 && args->GetNext(&options) - && args->GetNext(&callback))) { - args->ThrowError(); - return; - } - +void WebContents::PrintToPDF(const base::DictionaryValue& setting, + const PrintToPDFCallback& callback) { printing::PrintPreviewMessageHandler::FromWebContents(web_contents())-> - PrintToPDF(options, callback); + PrintToPDF(setting, callback); } void WebContents::Undo() { @@ -777,7 +770,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("unregisterServiceWorker", &WebContents::UnregisterServiceWorker) .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) - .SetMethod("printToPDF", &WebContents::PrintToPDF) + .SetMethod("_printToPDF", &WebContents::PrintToPDF) .Build()); return mate::ObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 0f8f08889fef..13abf5184d15 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -52,6 +52,8 @@ class WebContents : public mate::EventEmitter, public content::WebContentsObserver, public content::GpuDataManagerObserver { public: + typedef base::Callback PrintToPDFCallback; + // Create from an existing WebContents. static mate::Handle CreateFrom( v8::Isolate* isolate, brightray::InspectableWebContents* web_contents); @@ -91,7 +93,8 @@ class WebContents : public mate::EventEmitter, bool IsAudioMuted(); // Print current page as PDF. - void PrintToPDF(mate::Arguments* args); + void PrintToPDF(const base::DictionaryValue& setting, + const PrintToPDFCallback& callback); // Editing commands. void Undo(); diff --git a/atom/browser/api/lib/web-contents.coffee b/atom/browser/api/lib/web-contents.coffee index b8c101423d04..5725f0011908 100644 --- a/atom/browser/api/lib/web-contents.coffee +++ b/atom/browser/api/lib/web-contents.coffee @@ -3,6 +3,9 @@ NavigationController = require './navigation-controller' binding = process.atomBinding 'web_contents' ipc = require 'ipc' +nextId = 0 +getNextId = -> ++nextId + wrapWebContents = (webContents) -> # webContents is an EventEmitter. webContents.__proto__ = EventEmitter.prototype @@ -58,6 +61,46 @@ wrapWebContents = (webContents) -> Object.defineProperty event, 'sender', value: webContents ipc.emit channel, event, args... + webContents.printToPDF = (options, callback) -> + printingSetting = + pageRage:[], + mediaSize: + height_microns:297000, + is_default:true, + name:"ISO_A4", + width_microns:210000, + custom_display_name:"A4", + landscape:false, + color:2, + headerFooterEnabled:false, + marginsType:0, + isFirstRequest:false, + requestID: getNextId(), + previewModifiable:true, + printToPDF:true, + printWithCloudPrint:false, + printWithPrivet:false, + printWithExtension:false, + deviceName:"Save as PDF", + generateDraftData:true, + fitToPageEnabled:false, + duplex:0, + copies:1, + collate:true, + shouldPrintBackgrounds:false, + shouldPrintSelectionOnly:false + + if options.landscape + printingSetting.landscape = options.landscape + if options.marginsType + printingSetting.marginsType = options.marginsType + if options.printSelectionOnly + printingSetting.shouldPrintSelectionOnly = options.printSelectionOnly + if options.printCSSBackgrounds + printingSetting.shouldPrintBackgrounds = options.printBackgrounds + + webContents._printToPDF printingSetting, callback + webContents binding._setWrapWebContents wrapWebContents diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index a5b8b677b2d5..2038775f8d9a 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -55,7 +55,6 @@ class NativeWindow : public CommonWebContentsDelegate, public content::NotificationObserver { public: typedef base::Callback CapturePageCallback; - typedef base::Callback PrintToPDFCallback; class DialogScope { public: diff --git a/chromium_src/chrome/browser/printing/print_preview_message_handler.cc b/chromium_src/chrome/browser/printing/print_preview_message_handler.cc index 0ba9ea662e45..dc7052cab1a1 100644 --- a/chromium_src/chrome/browser/printing/print_preview_message_handler.cc +++ b/chromium_src/chrome/browser/printing/print_preview_message_handler.cc @@ -79,8 +79,7 @@ namespace printing { PrintPreviewMessageHandler::PrintPreviewMessageHandler( WebContents* web_contents) - : request_id_(0), - content::WebContentsObserver(web_contents) { + : content::WebContentsObserver(web_contents) { DCHECK(web_contents); } @@ -162,68 +161,14 @@ bool PrintPreviewMessageHandler::OnMessageReceived( } void PrintPreviewMessageHandler::PrintToPDF( - const mate::Dictionary& options, - const atom::NativeWindow::PrintToPDFCallback& callback) { - // A simulated Chromium print preivew setting. - const std::string setting_json_str = "{ \ - \"pageRage\":[], \ - \"mediaSize\":{ \ - \"height_microns\":297000, \ - \"is_default\":true, \ - \"name\":\"ISO_A4\", \ - \"width_microns\":210000, \ - \"custom_display_name\":\"A4\" \ - }, \ - \"landscape\":false, \ - \"color\":2, \ - \"headerFooterEnabled\":false, \ - \"marginsType\":0, \ - \"isFirstRequest\":false, \ - \"requestID\":1, \ - \"previewModifiable\":true, \ - \"printToPDF\":true, \ - \"printWithCloudPrint\":false, \ - \"printWithPrivet\":false, \ - \"printWithExtension\":false, \ - \"deviceName\":\"Save as PDF\", \ - \"generateDraftData\":true, \ - \"fitToPageEnabled\":false, \ - \"duplex\":0, \ - \"copies\":1, \ - \"collate\":true, \ - \"shouldPrintBackgrounds\":false, \ - \"shouldPrintSelectionOnly\":false \ - }"; + const base::DictionaryValue& options, + const atom::api::WebContents::PrintToPDFCallback& callback) { + int request_id; + options.GetInteger(printing::kPreviewRequestID, &request_id); + print_to_pdf_callback_map_[request_id] = callback; - scoped_ptr settings( - static_cast( - base::JSONReader::Read(setting_json_str))); - settings->SetInteger(printing::kPreviewRequestID, request_id_); - print_to_pdf_callback_map_[request_id_] = callback; - ++request_id_; - - - // Default Print PDF settings: - int margins_type = 0; // DEFAULT_MARGINS - bool print_background = false; - bool print_selection_only = false; - bool is_landscape = false; - - if (!options.IsEmpty()) { - options.Get(printing::kSettingMarginsType, &margins_type); - options.Get(printing::kSettingShouldPrintBackgrounds, &print_background); - options.Get(printing::kSettingShouldPrintSelectionOnly, - &print_selection_only); - options.Get(printing::kSettingLandscape, &is_landscape); - } - settings->SetInteger(printing::kSettingMarginsType, margins_type); - settings->SetBoolean(printing::kSettingShouldPrintBackgrounds, - print_background); - settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly, - print_selection_only); - settings->SetBoolean(printing::kSettingLandscape, is_landscape); content::RenderViewHost* rvh = web_contents()->GetRenderViewHost(); - rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings)); + rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), options)); } void PrintPreviewMessageHandler::RunPrintToPDFCallback( diff --git a/chromium_src/chrome/browser/printing/print_preview_message_handler.h b/chromium_src/chrome/browser/printing/print_preview_message_handler.h index 585eca7e0546..692baba36ee7 100644 --- a/chromium_src/chrome/browser/printing/print_preview_message_handler.h +++ b/chromium_src/chrome/browser/printing/print_preview_message_handler.h @@ -7,7 +7,7 @@ #include -#include "atom/browser/native_window.h" +#include "atom/browser/api/atom_api_web_contents.h" #include "base/compiler_specific.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" @@ -49,11 +49,12 @@ class PrintPreviewMessageHandler // content::WebContentsObserver implementation. bool OnMessageReceived(const IPC::Message& message) override; - void PrintToPDF(const mate::Dictionary& options, - const atom::NativeWindow::PrintToPDFCallback& callback); + void PrintToPDF(const base::DictionaryValue& options, + const atom::api::WebContents::PrintToPDFCallback& callback); private: - typedef std::map PrintToPDFCallbackMap; + typedef std::map + PrintToPDFCallbackMap; explicit PrintPreviewMessageHandler(content::WebContents* web_contents); friend class content::WebContentsUserData; @@ -68,9 +69,6 @@ class PrintPreviewMessageHandler void RunPrintToPDFCallback(int request_id, PrintPDFResult result); - // PrintToPDF request id counter. - int request_id_; - PrintToPDFCallbackMap print_to_pdf_callback_map_; DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);