2020-11-17 22:14:09 +00:00
|
|
|
// 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 "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"
|
2022-11-17 19:59:23 +00:00
|
|
|
#include "shell/common/thread_restrictions.h"
|
2020-11-17 22:14:09 +00:00
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2021-10-25 18:16:58 +00:00
|
|
|
#include "base/task/task_traits.h"
|
|
|
|
#include "base/task/thread_pool.h"
|
2020-11-17 22:14:09 +00:00
|
|
|
#include "printing/backend/print_backend.h"
|
2021-10-25 18:16:58 +00:00
|
|
|
#include "shell/common/gin_helper/promise.h"
|
|
|
|
#include "shell/common/process_util.h"
|
2020-11-17 22:14:09 +00:00
|
|
|
#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
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2020-11-17 22:14:09 +00:00
|
|
|
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2021-10-25 18:16:58 +00:00
|
|
|
printing::PrinterList GetPrinterList(v8::Isolate* isolate) {
|
|
|
|
EmitWarning(node::Environment::GetCurrent(isolate),
|
|
|
|
"Deprecation Warning: getPrinters() is deprecated. "
|
|
|
|
"Use the asynchronous and non-blocking version, "
|
|
|
|
"getPrintersAsync(), instead.",
|
|
|
|
"electron");
|
2020-11-17 22:14:09 +00:00
|
|
|
printing::PrinterList printers;
|
|
|
|
auto print_backend = printing::PrintBackend::CreateInstance(
|
|
|
|
g_browser_process->GetApplicationLocale());
|
|
|
|
{
|
2022-11-17 19:59:23 +00:00
|
|
|
ScopedAllowBlockingForElectron allow_blocking;
|
2021-10-05 07:16:04 +00:00
|
|
|
printing::mojom::ResultCode code =
|
2022-06-27 20:50:08 +00:00
|
|
|
print_backend->EnumeratePrinters(printers);
|
2021-10-05 07:16:04 +00:00
|
|
|
if (code != printing::mojom::ResultCode::kSuccess)
|
|
|
|
LOG(INFO) << "Failed to enumerate printers";
|
2020-11-17 22:14:09 +00:00
|
|
|
}
|
|
|
|
return printers;
|
|
|
|
}
|
2021-10-25 18:16:58 +00:00
|
|
|
|
|
|
|
v8::Local<v8::Promise> GetPrinterListAsync(v8::Isolate* isolate) {
|
|
|
|
gin_helper::Promise<printing::PrinterList> promise(isolate);
|
|
|
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
|
|
|
|
|
|
|
base::ThreadPool::PostTaskAndReplyWithResult(
|
|
|
|
FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
|
|
|
|
base::BindOnce([]() {
|
|
|
|
printing::PrinterList printers;
|
|
|
|
auto print_backend = printing::PrintBackend::CreateInstance(
|
|
|
|
g_browser_process->GetApplicationLocale());
|
|
|
|
printing::mojom::ResultCode code =
|
2022-06-27 20:50:08 +00:00
|
|
|
print_backend->EnumeratePrinters(printers);
|
2021-10-25 18:16:58 +00:00
|
|
|
if (code != printing::mojom::ResultCode::kSuccess)
|
|
|
|
LOG(INFO) << "Failed to enumerate printers";
|
|
|
|
return printers;
|
|
|
|
}),
|
|
|
|
base::BindOnce(
|
|
|
|
[](gin_helper::Promise<printing::PrinterList> promise,
|
|
|
|
const printing::PrinterList& printers) {
|
|
|
|
promise.Resolve(printers);
|
|
|
|
},
|
|
|
|
std::move(promise)));
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
2020-11-17 22:14:09 +00:00
|
|
|
#endif
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
2020-11-17 22:14:09 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-11-19 15:05:49 +00:00
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
2020-11-17 22:14:09 +00:00
|
|
|
using electron::api::GetPrinterList;
|
2021-10-25 18:16:58 +00:00
|
|
|
using electron::api::GetPrinterListAsync;
|
2020-11-19 15:05:49 +00:00
|
|
|
#endif
|
2020-11-17 22:14:09 +00:00
|
|
|
|
|
|
|
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));
|
2021-10-25 18:16:58 +00:00
|
|
|
dict.SetMethod("getPrinterListAsync",
|
|
|
|
base::BindRepeating(&GetPrinterListAsync));
|
2020-11-17 22:14:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_printing, Initialize)
|