From 023a3fd5473da4bca0cc73239ce7b8a9d3fa1ddf Mon Sep 17 00:00:00 2001 From: renaesop Date: Mon, 6 Feb 2017 17:18:04 +0800 Subject: [PATCH 01/10] Print enhancement: add webContents.printerList And a print option to select printer --- atom/browser/api/atom_api_web_contents.cc | 33 +++++- atom/browser/api/atom_api_web_contents.h | 2 + .../browser/printing/print_job_worker.cc | 108 +++++++++++++++++- .../browser/printing/print_job_worker.h | 6 +- .../printing/print_view_manager_base.cc | 5 +- .../printing/print_view_manager_base.h | 2 +- .../chrome/browser/printing/printer_query.cc | 26 ++++- .../chrome/browser/printing/printer_query.h | 10 +- .../printing/printing_message_filter.cc | 27 +++++ .../printing/printing_message_filter.h | 5 + chromium_src/chrome/common/print_messages.cc | 4 +- chromium_src/chrome/common/print_messages.h | 11 +- .../printing/print_web_view_helper.cc | 27 +++-- .../renderer/printing/print_web_view_helper.h | 11 +- docs/api/web-contents.md | 40 ++++++- 15 files changed, 290 insertions(+), 27 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index a8f864c775bd..4b8aba36c001 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -41,6 +41,7 @@ #include "atom/common/options_switches.h" #include "base/strings/utf_string_conversions.h" #include "base/threading/thread_task_runner_handle.h" +#include "base/values.h" #include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents_view.h" #include "chrome/browser/printing/print_preview_message_handler.h" @@ -65,6 +66,7 @@ #include "content/public/browser/storage_partition.h" #include "content/public/browser/web_contents.h" #include "content/public/common/context_menu_params.h" +#include "native_mate/converter.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" #include "net/url_request/url_request_context.h" @@ -83,6 +85,7 @@ namespace { struct PrintSettings { bool silent; bool print_background; + base::string16 device_name; }; } // namespace @@ -120,10 +123,25 @@ struct Converter { return false; dict.Get("silent", &(out->silent)); dict.Get("printBackground", &(out->print_background)); + dict.Get("deviceName", &(out->device_name)); return true; } }; +template<> +struct Converter { + static v8::Local + ToV8(v8::Isolate* isolate, const printing::PrinterBasicInfo& val) { + mate::Dictionary dict(isolate, v8::Object::New(isolate)); + dict.Set("printerName", val.printer_name); + dict.Set("printerDescription", val.printer_description); + dict.Set("printerStatus", val.printer_status); + dict.Set("isDefault", val.is_default); + dict.Set("options", val.options); + return dict.GetHandle(); + } +}; + template<> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, @@ -1114,14 +1132,24 @@ bool WebContents::IsAudioMuted() { } void WebContents::Print(mate::Arguments* args) { - PrintSettings settings = { false, false }; + PrintSettings settings = { false, false, base::string16() }; if (args->Length() == 1 && !args->GetNext(&settings)) { args->ThrowError(); return; } printing::PrintViewManagerBasic::FromWebContents(web_contents())-> - PrintNow(settings.silent, settings.print_background); + PrintNow(settings.silent, + settings.print_background, settings.device_name); +} + + +std::vector WebContents::GetPrinterList( + mate::Arguments* args) { + std::vector printerList; + auto printBackend = printing::PrintBackend::CreateInstance(nullptr); + printBackend->EnumeratePrinters(&printerList); + return printerList; } void WebContents::PrintToPDF(const base::DictionaryValue& setting, @@ -1616,6 +1644,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, &WebContents::UnregisterServiceWorker) .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("print", &WebContents::Print) + .SetMethod("printerList", &WebContents::GetPrinterList) .SetMethod("_printToPDF", &WebContents::PrintToPDF) .SetMethod("addWorkSpace", &WebContents::AddWorkSpace) .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 22f6b8775f99..50f217fcf0ef 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -16,6 +16,7 @@ #include "content/public/browser/web_contents_observer.h" #include "content/public/common/favicon_url.h" #include "native_mate/handle.h" +#include "printing/backend/print_backend.h" #include "ui/gfx/image/image.h" namespace blink { @@ -109,6 +110,7 @@ class WebContents : public mate::TrackableObject, void SetAudioMuted(bool muted); bool IsAudioMuted(); void Print(mate::Arguments* args); + std::vector GetPrinterList(mate::Arguments* args); void SetEmbedder(const WebContents* embedder); // Print current page as PDF. diff --git a/chromium_src/chrome/browser/printing/print_job_worker.cc b/chromium_src/chrome/browser/printing/print_job_worker.cc index 4a39272c9900..6fb18f32b223 100644 --- a/chromium_src/chrome/browser/printing/print_job_worker.cc +++ b/chromium_src/chrome/browser/printing/print_job_worker.cc @@ -24,6 +24,21 @@ #include "printing/printing_utils.h" #include "ui/base/l10n/l10n_util.h" +#include +#include +#include +#include +#include +#include + +#include "base/strings/string_number_conversions.h" +#include "base/strings/utf_string_conversions.h" +#include "base/time/time.h" +#include "printing/page_size_margins.h" +#include "printing/print_job_constants.h" +#include "printing/print_settings.h" +#include "printing/units.h" + using content::BrowserThread; namespace printing { @@ -36,6 +51,81 @@ void HoldRefCallback(const scoped_refptr& owner, callback.Run(); } +void SetCustomMarginsToJobSettings(const PageSizeMargins& page_size_margins, + base::DictionaryValue* settings) { + std::unique_ptr custom_margins(new base::DictionaryValue()); + custom_margins->SetDouble(kSettingMarginTop, page_size_margins.margin_top); + custom_margins->SetDouble(kSettingMarginBottom, page_size_margins.margin_bottom); + custom_margins->SetDouble(kSettingMarginLeft, page_size_margins.margin_left); + custom_margins->SetDouble(kSettingMarginRight, page_size_margins.margin_right); + settings->Set(kSettingMarginsCustom, std::move(custom_margins)); +} + +void PrintSettingsToJobSettings(const PrintSettings& settings, + base::DictionaryValue* job_settings) { + // header footer + job_settings->SetBoolean(kSettingHeaderFooterEnabled, + settings.display_header_footer()); + job_settings->SetString(kSettingHeaderFooterTitle, settings.title()); + job_settings->SetString(kSettingHeaderFooterURL, settings.url()); + + // bg + job_settings->SetBoolean(kSettingShouldPrintBackgrounds, + settings.should_print_backgrounds()); + job_settings->SetBoolean(kSettingShouldPrintSelectionOnly, + settings.selection_only()); + + // margin + auto margin_type = settings.margin_type(); + job_settings->SetInteger(kSettingMarginsType, settings.margin_type()); + if (margin_type == CUSTOM_MARGINS) { + const auto& margins_in_points = settings.requested_custom_margins_in_points(); + + PageSizeMargins page_size_margins; + + page_size_margins.margin_top = margins_in_points.top; + page_size_margins.margin_bottom = margins_in_points.bottom; + page_size_margins.margin_left = margins_in_points.left; + page_size_margins.margin_right = margins_in_points.right; + SetCustomMarginsToJobSettings(page_size_margins, job_settings); + } + job_settings->SetInteger(kSettingPreviewPageCount, 1); + + // range + + if (!settings.ranges().empty()) { + base::ListValue* page_range_array = new base::ListValue; + job_settings->Set(kSettingPageRange, page_range_array); + for (size_t i = 0; i < settings.ranges().size(); ++i) { + std::unique_ptr dict(new base::DictionaryValue); + dict->SetInteger(kSettingPageRangeFrom, settings.ranges()[i].from + 1); + dict->SetInteger(kSettingPageRangeTo, settings.ranges()[i].to + 1); + page_range_array->Append(std::move(dict)); + } + } + + job_settings->SetBoolean(kSettingCollate, settings.collate()); + job_settings->SetInteger(kSettingCopies, 1); + job_settings->SetInteger(kSettingColor, settings.color()); + job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); + job_settings->SetBoolean(kSettingLandscape, settings.landscape()); + job_settings->SetString(kSettingDeviceName, settings.device_name()); + job_settings->SetInteger("scaleFactor", 100); + job_settings->SetBoolean("rasterizePDF", false); + + job_settings->SetInteger("desiredDpi", settings.desired_dpi()); + job_settings->SetInteger("dpi", settings.dpi()); + + job_settings->SetBoolean(kSettingPrintToPDF, false); + job_settings->SetBoolean(kSettingCloudPrintDialog, false); + job_settings->SetBoolean(kSettingPrintWithPrivet, false); + job_settings->SetBoolean(kSettingPrintWithExtension, false); + + job_settings->SetBoolean("showSystemDialog", false); + job_settings->SetInteger(kSettingPreviewPageCount, 1); +} + + class PrintingContextDelegate : public PrintingContext::Delegate { public: PrintingContextDelegate(int render_process_id, int render_view_id); @@ -115,7 +205,8 @@ void PrintJobWorker::GetSettings( bool ask_user_for_settings, int document_page_count, bool has_selection, - MarginType margin_type) { + MarginType margin_type, + const base::string16& device_name) { DCHECK(task_runner_->RunsTasksOnCurrentThread()); DCHECK_EQ(page_number_, PageNumber::npos()); @@ -137,6 +228,13 @@ void PrintJobWorker::GetSettings( base::Unretained(this), document_page_count, has_selection))); + } else if (!device_name.empty()) { + BrowserThread::PostTask( + BrowserThread::UI, FROM_HERE, + base::Bind(&HoldRefCallback, make_scoped_refptr(owner_), + base::Bind(&PrintJobWorker::InitWithDeviceName, + base::Unretained(this), + device_name))); } else { BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -212,6 +310,14 @@ void PrintJobWorker::UseDefaultSettings() { GetSettingsDone(result); } +void PrintJobWorker::InitWithDeviceName(const base::string16& device_name) { + const auto& settings = printing_context_->settings(); + std::unique_ptr dic(new base::DictionaryValue); + PrintSettingsToJobSettings(settings, dic.get()); + dic->SetString(kSettingDeviceName, device_name); + UpdatePrintSettings(std::move(dic)); +} + void PrintJobWorker::StartPrinting(PrintedDocument* new_document) { DCHECK(task_runner_->RunsTasksOnCurrentThread()); DCHECK_EQ(page_number_, PageNumber::npos()); diff --git a/chromium_src/chrome/browser/printing/print_job_worker.h b/chromium_src/chrome/browser/printing/print_job_worker.h index 343c0fa15f29..fa3d8fc7b26c 100644 --- a/chromium_src/chrome/browser/printing/print_job_worker.h +++ b/chromium_src/chrome/browser/printing/print_job_worker.h @@ -46,7 +46,8 @@ class PrintJobWorker { bool ask_user_for_settings, int document_page_count, bool has_selection, - MarginType margin_type); + MarginType margin_type, + const base::string16& device_name); // Set the new print settings. void SetSettings(std::unique_ptr new_settings); @@ -127,6 +128,9 @@ class PrintJobWorker { // systems. void UseDefaultSettings(); + // set the printer name + void InitWithDeviceName(const base::string16& device_name); + // Printing context delegate. std::unique_ptr printing_context_delegate_; diff --git a/chromium_src/chrome/browser/printing/print_view_manager_base.cc b/chromium_src/chrome/browser/printing/print_view_manager_base.cc index e61cc10d5364..a726ef4784cd 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.cc +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.cc @@ -64,9 +64,10 @@ PrintViewManagerBase::~PrintViewManagerBase() { } #if !defined(DISABLE_BASIC_PRINTING) -bool PrintViewManagerBase::PrintNow(bool silent, bool print_background) { +bool PrintViewManagerBase::PrintNow(bool silent, bool print_background, + const base::string16& device_name) { return PrintNowInternal(new PrintMsg_PrintPages( - routing_id(), silent, print_background)); + routing_id(), silent, print_background, device_name)); } #endif // !DISABLE_BASIC_PRINTING diff --git a/chromium_src/chrome/browser/printing/print_view_manager_base.h b/chromium_src/chrome/browser/printing/print_view_manager_base.h index 78e5729a5fb1..a140b67fca24 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.h +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.h @@ -39,7 +39,7 @@ class PrintViewManagerBase : public content::NotificationObserver, // Prints the current document immediately. Since the rendering is // asynchronous, the actual printing will not be completed on the return of // this function. Returns false if printing is impossible at the moment. - virtual bool PrintNow(bool silent, bool print_background); + virtual bool PrintNow(bool silent, bool print_background, const base::string16&); #endif // !DISABLE_BASIC_PRINTING // PrintedPagesSource implementation. diff --git a/chromium_src/chrome/browser/printing/printer_query.cc b/chromium_src/chrome/browser/printing/printer_query.cc index 72e2b85f635c..be33d384dad7 100644 --- a/chromium_src/chrome/browser/printing/printer_query.cc +++ b/chromium_src/chrome/browser/printing/printer_query.cc @@ -82,7 +82,31 @@ void PrinterQuery::GetSettings( is_print_dialog_box_shown_, expected_page_count, has_selection, - margin_type)); + margin_type, + base::string16())); +} + +void PrinterQuery::GetSettings( + GetSettingsAskParam ask_user_for_settings, + int expected_page_count, + bool has_selection, + MarginType margin_type, + const base::string16& device_name, + const base::Closure& callback) { + DCHECK(RunsTasksOnCurrentThread()); + DCHECK(!is_print_dialog_box_shown_); + StartWorker(callback); + + // Real work is done in PrintJobWorker::GetSettings(). + is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; + worker_->PostTask(FROM_HERE, + base::Bind(&PrintJobWorker::GetSettings, + base::Unretained(worker_.get()), + is_print_dialog_box_shown_, + expected_page_count, + has_selection, + margin_type, + device_name)); } void PrinterQuery::SetSettings(std::unique_ptr new_settings, diff --git a/chromium_src/chrome/browser/printing/printer_query.h b/chromium_src/chrome/browser/printing/printer_query.h index d2f017d1895c..7cd8356a2ae9 100644 --- a/chromium_src/chrome/browser/printing/printer_query.h +++ b/chromium_src/chrome/browser/printing/printer_query.h @@ -50,7 +50,15 @@ class PrinterQuery : public PrintJobWorkerOwner { MarginType margin_type, const base::Closure& callback); - // Updates the current settings with |new_settings| dictionary values. + void GetSettings( + GetSettingsAskParam ask_user_for_settings, + int expected_page_count, + bool has_selection, + MarginType margin_type, + const base::string16& device_name, + const base::Closure& callback); + + // Updates the current settings with |new_settings| dictionary values. void SetSettings(std::unique_ptr new_settings, const base::Closure& callback); diff --git a/chromium_src/chrome/browser/printing/printing_message_filter.cc b/chromium_src/chrome/browser/printing/printing_message_filter.cc index 819c6af5b020..0d435e151010 100644 --- a/chromium_src/chrome/browser/printing/printing_message_filter.cc +++ b/chromium_src/chrome/browser/printing/printing_message_filter.cc @@ -123,6 +123,9 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) { #endif IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings) + + IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_InitSettingWithDeviceName, + OnInitSettingWithDeviceName) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings) @@ -278,6 +281,30 @@ void PrintingMessageFilter::OnGetDefaultPrintSettings(IPC::Message* reply_msg) { reply_msg)); } +void PrintingMessageFilter::OnInitSettingWithDeviceName(const base::string16& device_name, + IPC::Message* reply_msg) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + scoped_refptr printer_query; + printer_query = queue_->PopPrinterQuery(0); + if (!printer_query.get()) { + printer_query = + queue_->CreatePrinterQuery(render_process_id_, reply_msg->routing_id()); + } + + // Loads default settings. This is asynchronous, only the IPC message sender + // will hang until the settings are retrieved. + printer_query->GetSettings( + PrinterQuery::DEFAULTS, + 0, + false, + DEFAULT_MARGINS, + device_name, + base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, + this, + printer_query, + reply_msg)); +} + void PrintingMessageFilter::OnGetDefaultPrintSettingsReply( scoped_refptr printer_query, IPC::Message* reply_msg) { diff --git a/chromium_src/chrome/browser/printing/printing_message_filter.h b/chromium_src/chrome/browser/printing/printing_message_filter.h index e8536a69c5a3..cecf796785f4 100644 --- a/chromium_src/chrome/browser/printing/printing_message_filter.h +++ b/chromium_src/chrome/browser/printing/printing_message_filter.h @@ -85,6 +85,11 @@ class PrintingMessageFilter : public content::BrowserMessageFilter { // Get the default print setting. void OnGetDefaultPrintSettings(IPC::Message* reply_msg); + + // Set deviceName + void OnInitSettingWithDeviceName(const base::string16& device_name, + IPC::Message* reply_msg); + void OnGetDefaultPrintSettingsReply(scoped_refptr printer_query, IPC::Message* reply_msg); diff --git a/chromium_src/chrome/common/print_messages.cc b/chromium_src/chrome/common/print_messages.cc index b0ec282382d6..fb8397e3f580 100644 --- a/chromium_src/chrome/common/print_messages.cc +++ b/chromium_src/chrome/common/print_messages.cc @@ -23,7 +23,8 @@ PrintMsg_Print_Params::PrintMsg_Print_Params() print_scaling_option(blink::WebPrintScalingOptionSourceSize), title(), url(), - should_print_backgrounds(false) { + should_print_backgrounds(false), + device_name() { } PrintMsg_Print_Params::~PrintMsg_Print_Params() {} @@ -45,6 +46,7 @@ void PrintMsg_Print_Params::Reset() { title.clear(); url.clear(); should_print_backgrounds = false; + device_name.clear(); } PrintMsg_PrintPages_Params::PrintMsg_PrintPages_Params() diff --git a/chromium_src/chrome/common/print_messages.h b/chromium_src/chrome/common/print_messages.h index 034691b5b958..517ce3f6fbf2 100644 --- a/chromium_src/chrome/common/print_messages.h +++ b/chromium_src/chrome/common/print_messages.h @@ -52,6 +52,7 @@ struct PrintMsg_Print_Params { base::string16 title; base::string16 url; bool should_print_backgrounds; + base::string16 device_name; }; struct PrintMsg_PrintPages_Params { @@ -213,9 +214,10 @@ IPC_STRUCT_END() // Tells the render view to switch the CSS to print media type, renders every // requested pages and switch back the CSS to display media type. -IPC_MESSAGE_ROUTED2(PrintMsg_PrintPages, +IPC_MESSAGE_ROUTED3(PrintMsg_PrintPages, bool /* silent print */, - bool /* print page's background */) + bool /* print page's background */, + base::string16 /* device name*/) // Tells the render view that printing is done so it can clean up. IPC_MESSAGE_ROUTED1(PrintMsg_PrintingDone, @@ -260,6 +262,11 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPrintPage, IPC_SYNC_MESSAGE_ROUTED0_1(PrintHostMsg_GetDefaultPrintSettings, PrintMsg_Print_Params /* default_settings */) +// you can set the printer +IPC_SYNC_MESSAGE_ROUTED1_1(PrintHostMsg_InitSettingWithDeviceName, + base::string16, /* device name */ + PrintMsg_Print_Params /* default_settings */) + // The renderer wants to update the current print settings with new // |job_settings|. IPC_SYNC_MESSAGE_ROUTED2_2(PrintHostMsg_UpdatePrintSettings, diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc index 6fd28f6bb3af..fe9f9b10ecd9 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc @@ -661,13 +661,14 @@ void PrintWebViewHelper::OnDestruct() { } #if !defined(DISABLE_BASIC_PRINTING) -void PrintWebViewHelper::OnPrintPages(bool silent, bool print_background) { +void PrintWebViewHelper::OnPrintPages(bool silent, bool print_background, + const base::string16& device_name) { if (ipc_nesting_level_> 1) return; blink::WebLocalFrame* frame = render_view()->GetMainRenderFrame()->GetWebFrame(); - Print(frame, blink::WebNode(), silent, print_background); + Print(frame, blink::WebNode(), silent, print_background, device_name); } #endif // !DISABLE_BASIC_PRINTING @@ -851,7 +852,8 @@ void PrintWebViewHelper::PrintNode(const blink::WebNode& node) { void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, const blink::WebNode& node, bool silent, - bool print_background) { + bool print_background, + const base::string16& device_name) { // If still not finished with earlier print request simply ignore. if (prep_frame_view_) return; @@ -859,7 +861,7 @@ void PrintWebViewHelper::Print(blink::WebLocalFrame* frame, FrameReference frame_ref(frame); int expected_page_count = 0; - if (!CalculateNumberOfPages(frame, node, &expected_page_count)) { + if (!CalculateNumberOfPages(frame, node, &expected_page_count, device_name)) { DidFinishPrinting(FAIL_PRINT_INIT); return; // Failed to init print page settings. } @@ -995,10 +997,16 @@ void PrintWebViewHelper::ComputePageLayoutInPointsForCss( CalculatePageLayoutFromPrintParams(params, page_layout_in_points); } -bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { +bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size, + const base::string16& device_name) { PrintMsg_PrintPages_Params settings; - Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), - &settings.params)); + if (device_name.empty()) { + Send(new PrintHostMsg_GetDefaultPrintSettings(routing_id(), + &settings.params)); + } else { + Send(new PrintHostMsg_InitSettingWithDeviceName(routing_id(), device_name, + &settings.params)); + } // Check if the printer returned any settings, if the settings is empty, we // can safely assume there are no printer drivers configured. So we safely // terminate. @@ -1023,10 +1031,11 @@ bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size) { bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame, const blink::WebNode& node, - int* number_of_pages) { + int* number_of_pages, + const base::string16& device_name) { DCHECK(frame); bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); - if (!InitPrintSettings(fit_to_paper_size)) { + if (!InitPrintSettings(fit_to_paper_size, device_name)) { notify_browser_of_print_failure_ = false; Send(new PrintHostMsg_ShowInvalidPrinterSettingsError(routing_id())); return false; diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.h b/chromium_src/chrome/renderer/printing/print_web_view_helper.h index f91f7f3cb809..d98dde60c1c1 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.h +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.h @@ -98,7 +98,7 @@ class PrintWebViewHelper // Message handlers --------------------------------------------------------- #if !defined(DISABLE_BASIC_PRINTING) - void OnPrintPages(bool silent, bool print_background); + void OnPrintPages(bool silent, bool print_background, const base::string16&); void OnPrintingDone(bool success); #endif // !DISABLE_BASIC_PRINTING void OnPrintPreview(const base::DictionaryValue& settings); @@ -137,7 +137,8 @@ class PrintWebViewHelper void Print(blink::WebLocalFrame* frame, const blink::WebNode& node, bool silent = false, - bool print_background = false); + bool print_background = false, + const base::string16& device_name = base::string16()); // Notification when printing is done - signal tear-down/free resources. void DidFinishPrinting(PrintingResult result); @@ -146,12 +147,14 @@ class PrintWebViewHelper // Initialize print page settings with default settings. // Used only for native printing workflow. - bool InitPrintSettings(bool fit_to_paper_size); + bool InitPrintSettings(bool fit_to_paper_size, + const base::string16& device_name = base::string16()); // Calculate number of pages in source document. bool CalculateNumberOfPages(blink::WebLocalFrame* frame, const blink::WebNode& node, - int* number_of_pages); + int* number_of_pages, + const base::string16& device_name = base::string16()); // Update the current print settings with new |passed_job_settings|. // |passed_job_settings| dictionary contains print job details such as printer diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 38e5808594e8..9c2893b9eb7c 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -859,18 +859,54 @@ Unregisters any ServiceWorker if present and returns a boolean as response to `callback` when the JS promise is fulfilled or false when the JS promise is rejected. +#### `contents.printerList()` + +Get the system printer list, the result is an array of printer description. +eg: +````js +[{ printerName: 'Zebra_LP2844', + printerDescription: 'Zebra LP2844', + printerStatus: 3, + isDefault: 0, + options: + { copies: '1', + 'device-uri': 'usb://Zebra/LP2844?location=14200000', + finishings: '3', + 'job-cancel-after': '10800', + 'job-hold-until': 'no-hold', + 'job-priority': '50', + 'job-sheets': 'none,none', + 'marker-change-time': '0', + 'number-up': '1', + 'printer-commands': 'none', + 'printer-info': 'Zebra LP2844', + 'printer-is-accepting-jobs': 'true', + 'printer-is-shared': 'true', + 'printer-location': '', + 'printer-make-and-model': 'Zebra EPL2 Label Printer', + 'printer-state': '3', + 'printer-state-change-time': '1484872644', + 'printer-state-reasons': 'offline-report', + 'printer-type': '36932', + 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', + system_driverinfo: 'Z' } }] +```` + + #### `contents.print([options])` * `options` Object (optional) * `silent` Boolean - Don't ask user for print settings. Default is `false`. * `printBackground` Boolean - Also prints the background color and image of the web page. Default is `false`. + * `deviceName` String - Set the printer Prints window's web page. When `silent` is set to `true`, Electron will pick -up system's default printer and default settings for printing. +up printer depending on `deviceName`(when left will pick system's default printer) + and default settings for printing. Calling `window.print()` in web page is equivalent to calling -`webContents.print({silent: false, printBackground: false})`. +`webContents.print({silent: false, printBackground: false, deviceName: ''})`. Use `page-break-before: always; ` CSS style to force to print to a new page. From 7f2705b2d952a4c47fa18ac6190587d3f124877a Mon Sep 17 00:00:00 2001 From: renaesop Date: Tue, 7 Feb 2017 09:58:14 +0800 Subject: [PATCH 02/10] Print enhancement: add webContents.printerList And a print option to select printer Print enhancement: add webContents.printerList And a print option to select printer --- .../chrome/browser/printing/print_job_worker.cc | 4 ++-- .../chrome/browser/printing/printer_query.cc | 13 ++++--------- .../chrome/browser/printing/printer_query.h | 2 ++ .../browser/printing/printing_message_filter.cc | 4 +++- vendor/native_mate | 2 +- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/chromium_src/chrome/browser/printing/print_job_worker.cc b/chromium_src/chrome/browser/printing/print_job_worker.cc index fb720321cfcd..7965e9d5e594 100644 --- a/chromium_src/chrome/browser/printing/print_job_worker.cc +++ b/chromium_src/chrome/browser/printing/print_job_worker.cc @@ -115,7 +115,7 @@ void PrintSettingsToJobSettings(const PrintSettings& settings, job_settings->SetInteger(kSettingDuplexMode, settings.duplex_mode()); job_settings->SetBoolean(kSettingLandscape, settings.landscape()); job_settings->SetString(kSettingDeviceName, settings.device_name()); - job_settings->SetInteger("scaleFactor", 100); + job_settings->SetInteger(kSettingScaleFactor, 100); job_settings->SetBoolean("rasterizePDF", false); job_settings->SetInteger("desiredDpi", settings.desired_dpi()); @@ -126,7 +126,7 @@ void PrintSettingsToJobSettings(const PrintSettings& settings, job_settings->SetBoolean(kSettingPrintWithPrivet, false); job_settings->SetBoolean(kSettingPrintWithExtension, false); - job_settings->SetBoolean("showSystemDialog", false); + job_settings->SetBoolean(kSettingShowSystemDialog, false); job_settings->SetInteger(kSettingPreviewPageCount, 1); } diff --git a/chromium_src/chrome/browser/printing/printer_query.cc b/chromium_src/chrome/browser/printing/printer_query.cc index ac21df0d6bb9..acfbec320b3a 100644 --- a/chromium_src/chrome/browser/printing/printer_query.cc +++ b/chromium_src/chrome/browser/printing/printer_query.cc @@ -102,16 +102,11 @@ void PrinterQuery::GetSettings( DCHECK(!is_print_dialog_box_shown_); StartWorker(callback); - // Real work is done in PrintJobWorker::GetSettings(). - is_print_dialog_box_shown_ = ask_user_for_settings == ASK_USER; + is_print_dialog_box_shown_ = false; worker_->PostTask(FROM_HERE, - base::Bind(&PrintJobWorker::GetSettings, - base::Unretained(worker_.get()), - is_print_dialog_box_shown_, - expected_page_count, - has_selection, - margin_type, - device_name)); + base::Bind(&PrintJobWorker::GetSettings, base::Unretained(worker_.get()), + is_print_dialog_box_shown_, expected_page_count, has_selection, + margin_type, is_scripted, is_modifiable, device_name)); } void PrinterQuery::SetSettings( diff --git a/chromium_src/chrome/browser/printing/printer_query.h b/chromium_src/chrome/browser/printing/printer_query.h index c19bc899bb94..f150f1b278a8 100644 --- a/chromium_src/chrome/browser/printing/printer_query.h +++ b/chromium_src/chrome/browser/printing/printer_query.h @@ -58,6 +58,8 @@ class PrinterQuery : public PrintJobWorkerOwner { int expected_page_count, bool has_selection, MarginType margin_type, + bool is_scripted, + bool is_modifiable, const base::string16& device_name, const base::Closure& callback); diff --git a/chromium_src/chrome/browser/printing/printing_message_filter.cc b/chromium_src/chrome/browser/printing/printing_message_filter.cc index c7de9fae138a..b028818da9d3 100644 --- a/chromium_src/chrome/browser/printing/printing_message_filter.cc +++ b/chromium_src/chrome/browser/printing/printing_message_filter.cc @@ -190,10 +190,12 @@ void PrintingMessageFilter::OnInitSettingWithDeviceName(const base::string16& de // Loads default settings. This is asynchronous, only the IPC message sender // will hang until the settings are retrieved. printer_query->GetSettings( - PrinterQuery::DEFAULTS, + PrinterQuery::GetSettingsAskParam::DEFAULTS, 0, false, DEFAULT_MARGINS, + true, + true, device_name, base::Bind(&PrintingMessageFilter::OnGetDefaultPrintSettingsReply, this, diff --git a/vendor/native_mate b/vendor/native_mate index b5e5de626c6a..7197368c6d9e 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit b5e5de626c6a57e44c7e6448d8bbaaac475d493c +Subproject commit 7197368c6d9e36696d23d33ec603701789da329d From dfcc882dccf7f08423422074046e26dac3ec9506 Mon Sep 17 00:00:00 2001 From: renaesop Date: Tue, 9 May 2017 10:38:03 +0800 Subject: [PATCH 03/10] Add a test for `webcontents.printerList`. --- atom/browser/api/atom_api_web_contents.cc | 8 ++++---- spec/api-browser-window-spec.js | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 5f7b23f1d02a..b40deaee1c6c 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1282,10 +1282,10 @@ void WebContents::Print(mate::Arguments* args) { std::vector WebContents::GetPrinterList( mate::Arguments* args) { - std::vector printerList; - auto printBackend = printing::PrintBackend::CreateInstance(nullptr); - printBackend->EnumeratePrinters(&printerList); - return printerList; + std::vector printers; + auto print_backend = printing::PrintBackend::CreateInstance(nullptr); + print_backend->EnumeratePrinters(&printers); + return printers; } void WebContents::PrintToPDF(const base::DictionaryValue& setting, diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 0acbd4dd770b..059aa9089401 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1114,6 +1114,23 @@ describe('BrowserWindow module', function () { }) }) + it('can get printer list', function (done) { + w.destroy() + w = new BrowserWindow({ + show: false, + webPreferences: { + sandbox: true, + preload: preload + } + }) + w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E') + w.webContents.once('did-finish-load', function () { + const pl = w.webContents.printerList() + assert.equal(Array.isArray(pl), true) + done() + }) + }) + it('can print to PDF', function (done) { w.destroy() w = new BrowserWindow({ From 92f14f8d5217856504b0208e2ba73ec3435e4b37 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:14:55 -0700 Subject: [PATCH 04/10] printerList() -> getPrinters() --- atom/browser/api/atom_api_web_contents.cc | 8 +-- docs/api/web-contents.md | 64 ++++++++++++----------- spec/api-browser-window-spec.js | 4 +- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 3aba1babef54..56a3ab3d90e2 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -143,9 +143,9 @@ struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const printing::PrinterBasicInfo& val) { mate::Dictionary dict(isolate, v8::Object::New(isolate)); - dict.Set("printerName", val.printer_name); - dict.Set("printerDescription", val.printer_description); - dict.Set("printerStatus", val.printer_status); + dict.Set("name", val.printer_name); + dict.Set("description", val.printer_description); + dict.Set("status", val.printer_status); dict.Set("isDefault", val.is_default); dict.Set("options", val.options); return dict.GetHandle(); @@ -1872,7 +1872,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, &WebContents::UnregisterServiceWorker) .SetMethod("inspectServiceWorker", &WebContents::InspectServiceWorker) .SetMethod("print", &WebContents::Print) - .SetMethod("printerList", &WebContents::GetPrinterList) + .SetMethod("getPrinters", &WebContents::GetPrinterList) .SetMethod("_printToPDF", &WebContents::PrintToPDF) .SetMethod("addWorkSpace", &WebContents::AddWorkSpace) .SetMethod("removeWorkSpace", &WebContents::RemoveWorkSpace) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 15f0ebe66467..b0a1253aa9e3 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -916,39 +916,41 @@ Unregisters any ServiceWorker if present and returns a boolean as response to `callback` when the JS promise is fulfilled or false when the JS promise is rejected. -#### `contents.printerList()` +#### `contents.getPrinters()` -Get the system printer list, the result is an array of printer description. -eg: -````js -[{ printerName: 'Zebra_LP2844', - printerDescription: 'Zebra LP2844', - printerStatus: 3, - isDefault: 0, - options: - { copies: '1', - 'device-uri': 'usb://Zebra/LP2844?location=14200000', - finishings: '3', - 'job-cancel-after': '10800', - 'job-hold-until': 'no-hold', - 'job-priority': '50', - 'job-sheets': 'none,none', - 'marker-change-time': '0', - 'number-up': '1', - 'printer-commands': 'none', - 'printer-info': 'Zebra LP2844', - 'printer-is-accepting-jobs': 'true', - 'printer-is-shared': 'true', - 'printer-location': '', - 'printer-make-and-model': 'Zebra EPL2 Label Printer', - 'printer-state': '3', - 'printer-state-change-time': '1484872644', - 'printer-state-reasons': 'offline-report', - 'printer-type': '36932', - 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', - system_driverinfo: 'Z' } }] -```` +Get the system printer list, the result is an array of printer descriptions. +```js +[{ + name: 'Zebra_LP2844', + description: 'Zebra LP2844', + status: 3, + isDefault: 0, + options: { + copies: '1', + 'device-uri': 'usb://Zebra/LP2844?location=14200000', + finishings: '3', + 'job-cancel-after': '10800', + 'job-hold-until': 'no-hold', + 'job-priority': '50', + 'job-sheets': 'none,none', + 'marker-change-time': '0', + 'number-up': '1', + 'printer-commands': 'none', + 'printer-info': 'Zebra LP2844', + 'printer-is-accepting-jobs': 'true', + 'printer-is-shared': 'true', + 'printer-location': '', + 'printer-make-and-model': 'Zebra EPL2 Label Printer', + 'printer-state': '3', + 'printer-state-change-time': '1484872644', + 'printer-state-reasons': 'offline-report', + 'printer-type': '36932', + 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', + system_driverinfo: 'Z' + } +}] +``` #### `contents.print([options])` diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 70ed7ae3aa8f..6c54731aa70f 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1125,8 +1125,8 @@ describe('BrowserWindow module', function () { }) w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E') w.webContents.once('did-finish-load', function () { - const pl = w.webContents.printerList() - assert.equal(Array.isArray(pl), true) + const printers = w.webContents.getPrinters() + assert.equal(Array.isArray(printers), true) done() }) }) From e0b39247a99b600795079c4ef6d402d8bb6a1b1e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:17:08 -0700 Subject: [PATCH 05/10] Tweak print docs --- docs/api/web-contents.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index b0a1253aa9e3..dd0493d0d21b 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -958,11 +958,11 @@ Get the system printer list, the result is an array of printer descriptions. * `silent` Boolean - Don't ask user for print settings. Default is `false`. * `printBackground` Boolean - Also prints the background color and image of the web page. Default is `false`. - * `deviceName` String - Set the printer + * `deviceName` String - Set the printer device name to use. Default is `''`. Prints window's web page. When `silent` is set to `true`, Electron will pick -up printer depending on `deviceName`(when left will pick system's default printer) - and default settings for printing. +the system's default printer if `deviceName` is empty and the default settings +for printing. Calling `window.print()` in web page is equivalent to calling `webContents.print({silent: false, printBackground: false, deviceName: ''})`. From aa29bf8019571205989623f9a129b2d89b40b575 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:26:22 -0700 Subject: [PATCH 06/10] :art: --- atom/browser/api/atom_api_web_contents.cc | 24 +++++++++---------- atom/browser/api/atom_api_web_contents.h | 2 +- .../printing/print_view_manager_base.h | 2 +- .../printing/printing_message_filter.cc | 3 +-- .../printing/printing_message_filter.h | 2 +- .../printing/print_web_view_helper.cc | 3 ++- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 56a3ab3d90e2..df0334f12b61 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -140,16 +140,16 @@ struct Converter { template<> struct Converter { - static v8::Local - ToV8(v8::Isolate* isolate, const printing::PrinterBasicInfo& val) { - mate::Dictionary dict(isolate, v8::Object::New(isolate)); - dict.Set("name", val.printer_name); - dict.Set("description", val.printer_description); - dict.Set("status", val.printer_status); - dict.Set("isDefault", val.is_default); - dict.Set("options", val.options); - return dict.GetHandle(); - } + static v8::Local ToV8(v8::Isolate* isolate, + const printing::PrinterBasicInfo& val) { + mate::Dictionary dict(isolate, v8::Object::New(isolate)); + dict.Set("name", val.printer_name); + dict.Set("description", val.printer_description); + dict.Set("status", val.printer_status); + dict.Set("isDefault", val.is_default); + dict.Set("options", val.options); + return dict.GetHandle(); + } }; template<> @@ -1322,9 +1322,7 @@ void WebContents::Print(mate::Arguments* args) { settings.device_name); } - -std::vector WebContents::GetPrinterList( - mate::Arguments* args) { +std::vector WebContents::GetPrinterList() { std::vector printers; auto print_backend = printing::PrintBackend::CreateInstance(nullptr); print_backend->EnumeratePrinters(&printers); diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 4bb3b2c08948..6a04d6f518cf 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -122,7 +122,7 @@ class WebContents : public mate::TrackableObject, void SetAudioMuted(bool muted); bool IsAudioMuted(); void Print(mate::Arguments* args); - std::vector GetPrinterList(mate::Arguments* args); + std::vector GetPrinterList(); void SetEmbedder(const WebContents* embedder); // Print current page as PDF. diff --git a/chromium_src/chrome/browser/printing/print_view_manager_base.h b/chromium_src/chrome/browser/printing/print_view_manager_base.h index 7e5c33b09c11..61f42382bd4b 100644 --- a/chromium_src/chrome/browser/printing/print_view_manager_base.h +++ b/chromium_src/chrome/browser/printing/print_view_manager_base.h @@ -41,7 +41,7 @@ class PrintViewManagerBase : public content::NotificationObserver, // this function. Returns false if printing is impossible at the moment. virtual bool PrintNow(content::RenderFrameHost* rfh, bool silent, bool print_background, - const base::string16&); + const base::string16& device_name); #endif // !DISABLE_BASIC_PRINTING // PrintedPagesSource implementation. diff --git a/chromium_src/chrome/browser/printing/printing_message_filter.cc b/chromium_src/chrome/browser/printing/printing_message_filter.cc index b028818da9d3..1c9e9daadefa 100644 --- a/chromium_src/chrome/browser/printing/printing_message_filter.cc +++ b/chromium_src/chrome/browser/printing/printing_message_filter.cc @@ -116,9 +116,8 @@ bool PrintingMessageFilter::OnMessageReceived(const IPC::Message& message) { #endif IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, OnGetDefaultPrintSettings) - IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_InitSettingWithDeviceName, - OnInitSettingWithDeviceName) + OnInitSettingWithDeviceName) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings) diff --git a/chromium_src/chrome/browser/printing/printing_message_filter.h b/chromium_src/chrome/browser/printing/printing_message_filter.h index a4f4efba8170..fb7dced52e4e 100644 --- a/chromium_src/chrome/browser/printing/printing_message_filter.h +++ b/chromium_src/chrome/browser/printing/printing_message_filter.h @@ -64,7 +64,7 @@ class PrintingMessageFilter : public content::BrowserMessageFilter { // Set deviceName void OnInitSettingWithDeviceName(const base::string16& device_name, - IPC::Message* reply_msg); + IPC::Message* reply_msg); void OnGetDefaultPrintSettingsReply(scoped_refptr printer_query, IPC::Message* reply_msg); diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc index 939dc13711db..4014b3fa0ed3 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.cc +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.cc @@ -1026,7 +1026,8 @@ bool PrintWebViewHelper::InitPrintSettings(bool fit_to_paper_size, } bool PrintWebViewHelper::CalculateNumberOfPages(blink::WebLocalFrame* frame, - const blink::WebNode& node, int* number_of_pages, + const blink::WebNode& node, + int* number_of_pages, const base::string16& device_name) { DCHECK(frame); bool fit_to_paper_size = !(PrintingNodeOrPdfFrame(frame, node)); From d5e66f7cd924d9dd01da131798d65fc11287d52a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:40:03 -0700 Subject: [PATCH 07/10] Add parameter name --- chromium_src/chrome/renderer/printing/print_web_view_helper.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chromium_src/chrome/renderer/printing/print_web_view_helper.h b/chromium_src/chrome/renderer/printing/print_web_view_helper.h index 78aa0f7d4865..acda59b694ca 100644 --- a/chromium_src/chrome/renderer/printing/print_web_view_helper.h +++ b/chromium_src/chrome/renderer/printing/print_web_view_helper.h @@ -99,7 +99,9 @@ class PrintWebViewHelper // Message handlers --------------------------------------------------------- #if !defined(DISABLE_BASIC_PRINTING) - void OnPrintPages(bool silent, bool print_background, const base::string16&); + void OnPrintPages(bool silent, + bool print_background, + const base::string16& device_name); void OnPrintingDone(bool success); #endif // !DISABLE_BASIC_PRINTING void OnPrintPreview(const base::DictionaryValue& settings); From a8d25d6855a71fd816891d72cf97ebad3269bde5 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:50:08 -0700 Subject: [PATCH 08/10] Add printer info structure file --- docs/api/structures/printer-info.md | 40 +++++++++++++++++++++++++++++ docs/api/web-contents.md | 34 ++---------------------- 2 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 docs/api/structures/printer-info.md diff --git a/docs/api/structures/printer-info.md b/docs/api/structures/printer-info.md new file mode 100644 index 000000000000..a0a7383c0ae9 --- /dev/null +++ b/docs/api/structures/printer-info.md @@ -0,0 +1,40 @@ +# PrinterInfo Object + +* `name` String +* `description` String +* `status` Number +* `isDefault` Boolean +* `options` Object - Additional fields + +## Example + +```javascript +name: 'Zebra_LP2844', +description: 'Zebra LP2844', +status: 3, +isDefault: false, +options: { + copies: '1', + 'device-uri': 'usb://Zebra/LP2844?location=14200000', + finishings: '3', + 'job-cancel-after': '10800', + 'job-hold-until': 'no-hold', + 'job-priority': '50', + 'job-sheets': 'none,none', + 'marker-change-time': '0', + 'number-up': '1', + 'printer-commands': 'none', + 'printer-info': 'Zebra LP2844', + 'printer-is-accepting-jobs': 'true', + 'printer-is-shared': 'true', + 'printer-location': '', + 'printer-make-and-model': 'Zebra EPL2 Label Printer', + 'printer-state': '3', + 'printer-state-change-time': '1484872644', + 'printer-state-reasons': 'offline-report', + 'printer-type': '36932', + 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', + system_driverinfo: 'Z' +} +}] +``` diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index dd0493d0d21b..99d562d1b468 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -918,39 +918,9 @@ when the JS promise is rejected. #### `contents.getPrinters()` -Get the system printer list, the result is an array of printer descriptions. +Get the system printer list. -```js -[{ - name: 'Zebra_LP2844', - description: 'Zebra LP2844', - status: 3, - isDefault: 0, - options: { - copies: '1', - 'device-uri': 'usb://Zebra/LP2844?location=14200000', - finishings: '3', - 'job-cancel-after': '10800', - 'job-hold-until': 'no-hold', - 'job-priority': '50', - 'job-sheets': 'none,none', - 'marker-change-time': '0', - 'number-up': '1', - 'printer-commands': 'none', - 'printer-info': 'Zebra LP2844', - 'printer-is-accepting-jobs': 'true', - 'printer-is-shared': 'true', - 'printer-location': '', - 'printer-make-and-model': 'Zebra EPL2 Label Printer', - 'printer-state': '3', - 'printer-state-change-time': '1484872644', - 'printer-state-reasons': 'offline-report', - 'printer-type': '36932', - 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', - system_driverinfo: 'Z' - } -}] -``` +Returns [PrinterInfo[]](structures/printer-info.md) #### `contents.print([options])` From 97160ffa630624290a239a08f47b1a2f6ac527d9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 10:51:44 -0700 Subject: [PATCH 09/10] Make isDefault a bool --- atom/browser/api/atom_api_web_contents.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index df0334f12b61..d47a4da2fec6 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -146,7 +146,7 @@ struct Converter { dict.Set("name", val.printer_name); dict.Set("description", val.printer_description); dict.Set("status", val.printer_status); - dict.Set("isDefault", val.is_default); + dict.Set("isDefault", val.is_default ? true : false); dict.Set("options", val.options); return dict.GetHandle(); } From 73b49f0dfea83b07bd572e1c2eb8b6bb690f850b Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 18 May 2017 11:28:10 -0700 Subject: [PATCH 10/10] Fix JSON issue in example block --- docs/api/structures/printer-info.md | 58 +++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/docs/api/structures/printer-info.md b/docs/api/structures/printer-info.md index a0a7383c0ae9..2d8cab49eef1 100644 --- a/docs/api/structures/printer-info.md +++ b/docs/api/structures/printer-info.md @@ -8,33 +8,37 @@ ## Example +Below is an example of some of the additional options that may be set which +may be different on each platform. + ```javascript -name: 'Zebra_LP2844', -description: 'Zebra LP2844', -status: 3, -isDefault: false, -options: { - copies: '1', - 'device-uri': 'usb://Zebra/LP2844?location=14200000', - finishings: '3', - 'job-cancel-after': '10800', - 'job-hold-until': 'no-hold', - 'job-priority': '50', - 'job-sheets': 'none,none', - 'marker-change-time': '0', - 'number-up': '1', - 'printer-commands': 'none', - 'printer-info': 'Zebra LP2844', - 'printer-is-accepting-jobs': 'true', - 'printer-is-shared': 'true', - 'printer-location': '', - 'printer-make-and-model': 'Zebra EPL2 Label Printer', - 'printer-state': '3', - 'printer-state-change-time': '1484872644', - 'printer-state-reasons': 'offline-report', - 'printer-type': '36932', - 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', - system_driverinfo: 'Z' +{ + name: 'Zebra_LP2844', + description: 'Zebra LP2844', + status: 3, + isDefault: false, + options: { + copies: '1', + 'device-uri': 'usb://Zebra/LP2844?location=14200000', + finishings: '3', + 'job-cancel-after': '10800', + 'job-hold-until': 'no-hold', + 'job-priority': '50', + 'job-sheets': 'none,none', + 'marker-change-time': '0', + 'number-up': '1', + 'printer-commands': 'none', + 'printer-info': 'Zebra LP2844', + 'printer-is-accepting-jobs': 'true', + 'printer-is-shared': 'true', + 'printer-location': '', + 'printer-make-and-model': 'Zebra EPL2 Label Printer', + 'printer-state': '3', + 'printer-state-change-time': '1484872644', + 'printer-state-reasons': 'offline-report', + 'printer-type': '36932', + 'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844', + system_driverinfo: 'Z' + } } -}] ```