refactor: move GetPrinterList off WebContents (#26518)

This commit is contained in:
Jeremy Rose 2020-11-17 14:14:09 -08:00 committed by GitHub
parent dbe0f06c3d
commit a303813d15
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 37 deletions

View file

@ -283,6 +283,7 @@ filenames = {
"shell/browser/api/electron_api_power_monitor.h",
"shell/browser/api/electron_api_power_save_blocker.cc",
"shell/browser/api/electron_api_power_save_blocker.h",
"shell/browser/api/electron_api_printing.cc",
"shell/browser/api/electron_api_protocol.cc",
"shell/browser/api/electron_api_protocol.h",
"shell/browser/api/electron_api_screen.cc",

View file

@ -123,6 +123,7 @@ const defaultPrintingSetting = {
// JavaScript implementations of WebContents.
const binding = process._linkedBinding('electron_browser_web_contents');
const printing = process._linkedBinding('electron_browser_printing');
const { WebContents } = binding as { WebContents: { prototype: Electron.WebContents } };
WebContents.prototype.send = function (channel, ...args) {
@ -416,8 +417,10 @@ WebContents.prototype.print = function (options = {}, callback) {
};
WebContents.prototype.getPrinters = function () {
if (this._getPrinters) {
return this._getPrinters();
// TODO(nornagon): this API has nothing to do with WebContents and should be
// moved.
if (printing.getPrinterList) {
return printing.getPrinterList();
} else {
console.error('Error: Printing feature is disabled.');
return [];

View file

@ -0,0 +1,77 @@
// Copyright (c) 2020 Slack Technologies, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/browser_process.h"
#include "gin/converter.h"
#include "printing/buildflags/buildflags.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#if BUILDFLAG(ENABLE_PRINTING)
#include "printing/backend/print_backend.h"
#endif
namespace gin {
#if BUILDFLAG(ENABLE_PRINTING)
template <>
struct Converter<printing::PrinterBasicInfo> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const printing::PrinterBasicInfo& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("name", val.printer_name);
dict.Set("displayName", val.display_name);
dict.Set("description", val.printer_description);
dict.Set("status", val.printer_status);
dict.Set("isDefault", val.is_default ? true : false);
dict.Set("options", val.options);
return dict.GetHandle();
}
};
#endif
} // namespace gin
namespace electron {
namespace api {
#if BUILDFLAG(ENABLE_PRINTING)
printing::PrinterList GetPrinterList() {
printing::PrinterList printers;
auto print_backend = printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
{
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
}
return printers;
}
#endif
} // namespace api
} // namespace electron
namespace {
using electron::api::GetPrinterList;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
#if BUILDFLAG(ENABLE_PRINTING)
dict.SetMethod("getPrinterList", base::BindRepeating(&GetPrinterList));
#endif
}
} // namespace
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_printing, Initialize)

View file

@ -161,8 +161,13 @@
#if BUILDFLAG(ENABLE_PRINTING)
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "components/printing/browser/print_manager_utils.h"
#include "printing/backend/print_backend.h" // nogncheck
#include "printing/mojom/print.mojom.h"
#include "shell/browser/printing/print_preview_message_handler.h"
#if defined(OS_WIN)
#include "printing/backend/win_helper.h"
#endif
#endif
#if BUILDFLAG(ENABLE_COLOR_CHOOSER)
@ -185,21 +190,6 @@
namespace gin {
#if BUILDFLAG(ENABLE_PRINTING)
template <>
struct Converter<printing::PrinterBasicInfo> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const printing::PrinterBasicInfo& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.Set("name", val.printer_name);
dict.Set("displayName", val.display_name);
dict.Set("description", val.printer_description);
dict.Set("status", val.printer_status);
dict.Set("isDefault", val.is_default ? true : false);
dict.Set("options", val.options);
return dict.GetHandle();
}
};
template <>
struct Converter<printing::mojom::MarginType> {
static bool FromV8(v8::Isolate* isolate,
@ -2555,19 +2545,6 @@ void WebContents::Print(gin::Arguments* args) {
std::move(callback), device_name, silent));
}
printing::PrinterList WebContents::GetPrinterList() {
printing::PrinterList printers;
auto print_backend = printing::PrintBackend::CreateInstance(
g_browser_process->GetApplicationLocale());
{
// TODO(deepak1556): Deprecate this api in favor of an
// async version and post a non blocing task call.
base::ThreadRestrictions::ScopedAllowIO allow_io;
print_backend->EnumeratePrinters(&printers);
}
return printers;
}
v8::Local<v8::Promise> WebContents::PrintToPDF(base::DictionaryValue settings) {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
gin_helper::Promise<v8::Local<v8::Value>> promise(isolate);
@ -3708,7 +3685,6 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
.SetMethod("getAllSharedWorkers", &WebContents::GetAllSharedWorkers)
#if BUILDFLAG(ENABLE_PRINTING)
.SetMethod("_print", &WebContents::Print)
.SetMethod("_getPrinters", &WebContents::GetPrinterList)
.SetMethod("_printToPDF", &WebContents::PrintToPDF)
#endif
.SetMethod("_setNextChildWebPreferences",

View file

@ -45,12 +45,7 @@
#if BUILDFLAG(ENABLE_PRINTING)
#include "chrome/browser/printing/print_view_manager_basic.h"
#include "components/printing/common/print_messages.h"
#include "printing/backend/print_backend.h" // nogncheck
#include "shell/browser/printing/print_preview_message_handler.h"
#if defined(OS_WIN)
#include "printing/backend/win_helper.h"
#endif
#endif
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
@ -223,7 +218,6 @@ class WebContents : public gin::Wrappable<WebContents>,
bool silent,
base::string16 default_printer);
void Print(gin::Arguments* args);
printing::PrinterList GetPrinterList();
// Print current page as PDF.
v8::Local<v8::Promise> PrintToPDF(base::DictionaryValue settings);
#endif

View file

@ -56,6 +56,7 @@
V(electron_browser_power_monitor) \
V(electron_browser_power_save_blocker) \
V(electron_browser_protocol) \
V(electron_browser_printing) \
V(electron_browser_session) \
V(electron_browser_system_preferences) \
V(electron_browser_base_window) \