Move PDF printing setting in JS part.

This commit is contained in:
Haojian Wu 2015-06-10 11:34:16 +08:00
parent c0a6cb69bf
commit 6e099af5fe
6 changed files with 64 additions and 83 deletions

View file

@ -582,6 +582,7 @@ void WebContents::UnregisterServiceWorker(
callback); callback);
} }
<<<<<<< HEAD
<<<<<<< HEAD <<<<<<< HEAD
void WebContents::SetAudioMuted(bool muted) { void WebContents::SetAudioMuted(bool muted) {
web_contents()->SetAudioMuted(muted); web_contents()->SetAudioMuted(muted);
@ -591,18 +592,10 @@ bool WebContents::IsAudioMuted() {
return web_contents()->IsAudioMuted(); return web_contents()->IsAudioMuted();
} }
void WebContents::PrintToPDF(mate::Arguments* args) { void WebContents::PrintToPDF(const base::DictionaryValue& setting,
mate::Dictionary options; const PrintToPDFCallback& callback) {
base::Callback<void(int)> callback;
if (!(args->Length() == 1 && args->GetNext(&callback)) &&
!(args->Length() == 2 && args->GetNext(&options)
&& args->GetNext(&callback))) {
args->ThrowError();
return;
}
printing::PrintPreviewMessageHandler::FromWebContents(web_contents())-> printing::PrintPreviewMessageHandler::FromWebContents(web_contents())->
PrintToPDF(options, callback); PrintToPDF(setting, callback);
} }
void WebContents::Undo() { void WebContents::Undo() {
@ -777,7 +770,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder(
.SetMethod("unregisterServiceWorker", .SetMethod("unregisterServiceWorker",
&WebContents::UnregisterServiceWorker) &WebContents::UnregisterServiceWorker)
.SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker)
.SetMethod("printToPDF", &WebContents::PrintToPDF) .SetMethod("_printToPDF", &WebContents::PrintToPDF)
.Build()); .Build());
return mate::ObjectTemplateBuilder( return mate::ObjectTemplateBuilder(

View file

@ -52,6 +52,8 @@ class WebContents : public mate::EventEmitter,
public content::WebContentsObserver, public content::WebContentsObserver,
public content::GpuDataManagerObserver { public content::GpuDataManagerObserver {
public: public:
typedef base::Callback<void(int)> PrintToPDFCallback;
// Create from an existing WebContents. // Create from an existing WebContents.
static mate::Handle<WebContents> CreateFrom( static mate::Handle<WebContents> CreateFrom(
v8::Isolate* isolate, brightray::InspectableWebContents* web_contents); v8::Isolate* isolate, brightray::InspectableWebContents* web_contents);
@ -91,7 +93,8 @@ class WebContents : public mate::EventEmitter,
bool IsAudioMuted(); bool IsAudioMuted();
// Print current page as PDF. // Print current page as PDF.
void PrintToPDF(mate::Arguments* args); void PrintToPDF(const base::DictionaryValue& setting,
const PrintToPDFCallback& callback);
// Editing commands. // Editing commands.
void Undo(); void Undo();

View file

@ -3,6 +3,9 @@ NavigationController = require './navigation-controller'
binding = process.atomBinding 'web_contents' binding = process.atomBinding 'web_contents'
ipc = require 'ipc' ipc = require 'ipc'
nextId = 0
getNextId = -> ++nextId
wrapWebContents = (webContents) -> wrapWebContents = (webContents) ->
# webContents is an EventEmitter. # webContents is an EventEmitter.
webContents.__proto__ = EventEmitter.prototype webContents.__proto__ = EventEmitter.prototype
@ -58,6 +61,46 @@ wrapWebContents = (webContents) ->
Object.defineProperty event, 'sender', value: webContents Object.defineProperty event, 'sender', value: webContents
ipc.emit channel, event, args... 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 webContents
binding._setWrapWebContents wrapWebContents binding._setWrapWebContents wrapWebContents

View file

@ -55,7 +55,6 @@ class NativeWindow : public CommonWebContentsDelegate,
public content::NotificationObserver { public content::NotificationObserver {
public: public:
typedef base::Callback<void(const SkBitmap& bitmap)> CapturePageCallback; typedef base::Callback<void(const SkBitmap& bitmap)> CapturePageCallback;
typedef base::Callback<void(int)> PrintToPDFCallback;
class DialogScope { class DialogScope {
public: public:

View file

@ -79,8 +79,7 @@ namespace printing {
PrintPreviewMessageHandler::PrintPreviewMessageHandler( PrintPreviewMessageHandler::PrintPreviewMessageHandler(
WebContents* web_contents) WebContents* web_contents)
: request_id_(0), : content::WebContentsObserver(web_contents) {
content::WebContentsObserver(web_contents) {
DCHECK(web_contents); DCHECK(web_contents);
} }
@ -162,68 +161,14 @@ bool PrintPreviewMessageHandler::OnMessageReceived(
} }
void PrintPreviewMessageHandler::PrintToPDF( void PrintPreviewMessageHandler::PrintToPDF(
const mate::Dictionary& options, const base::DictionaryValue& options,
const atom::NativeWindow::PrintToPDFCallback& callback) { const atom::api::WebContents::PrintToPDFCallback& callback) {
// A simulated Chromium print preivew setting. int request_id;
const std::string setting_json_str = "{ \ options.GetInteger(printing::kPreviewRequestID, &request_id);
\"pageRage\":[], \ print_to_pdf_callback_map_[request_id] = callback;
\"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 \
}";
scoped_ptr<base::DictionaryValue> settings(
static_cast<base::DictionaryValue*>(
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(); 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( void PrintPreviewMessageHandler::RunPrintToPDFCallback(

View file

@ -7,7 +7,7 @@
#include <map> #include <map>
#include "atom/browser/native_window.h" #include "atom/browser/api/atom_api_web_contents.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
@ -49,11 +49,12 @@ class PrintPreviewMessageHandler
// content::WebContentsObserver implementation. // content::WebContentsObserver implementation.
bool OnMessageReceived(const IPC::Message& message) override; bool OnMessageReceived(const IPC::Message& message) override;
void PrintToPDF(const mate::Dictionary& options, void PrintToPDF(const base::DictionaryValue& options,
const atom::NativeWindow::PrintToPDFCallback& callback); const atom::api::WebContents::PrintToPDFCallback& callback);
private: private:
typedef std::map<int, atom::NativeWindow::PrintToPDFCallback> PrintToPDFCallbackMap; typedef std::map<int, atom::api::WebContents::PrintToPDFCallback>
PrintToPDFCallbackMap;
explicit PrintPreviewMessageHandler(content::WebContents* web_contents); explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>; friend class content::WebContentsUserData<PrintPreviewMessageHandler>;
@ -68,9 +69,6 @@ class PrintPreviewMessageHandler
void RunPrintToPDFCallback(int request_id, PrintPDFResult result); void RunPrintToPDFCallback(int request_id, PrintPDFResult result);
// PrintToPDF request id counter.
int request_id_;
PrintToPDFCallbackMap print_to_pdf_callback_map_; PrintToPDFCallbackMap print_to_pdf_callback_map_;
DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler); DISALLOW_COPY_AND_ASSIGN(PrintPreviewMessageHandler);