electron_api_printing.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2020 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "chrome/browser/browser_process.h"
  5. #include "gin/converter.h"
  6. #include "printing/buildflags/buildflags.h"
  7. #include "shell/common/gin_helper/dictionary.h"
  8. #include "shell/common/node_includes.h"
  9. #include "shell/common/thread_restrictions.h"
  10. #if BUILDFLAG(ENABLE_PRINTING)
  11. #include "base/task/task_traits.h"
  12. #include "base/task/thread_pool.h"
  13. #include "printing/backend/print_backend.h"
  14. #include "shell/common/gin_helper/promise.h"
  15. #include "shell/common/process_util.h"
  16. #endif
  17. namespace gin {
  18. #if BUILDFLAG(ENABLE_PRINTING)
  19. template <>
  20. struct Converter<printing::PrinterBasicInfo> {
  21. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  22. const printing::PrinterBasicInfo& val) {
  23. auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
  24. dict.Set("name", val.printer_name);
  25. dict.Set("displayName", val.display_name);
  26. dict.Set("description", val.printer_description);
  27. dict.Set("options", val.options);
  28. return dict.GetHandle();
  29. }
  30. };
  31. #endif
  32. } // namespace gin
  33. namespace electron::api {
  34. #if BUILDFLAG(ENABLE_PRINTING)
  35. v8::Local<v8::Promise> GetPrinterListAsync(v8::Isolate* isolate) {
  36. gin_helper::Promise<printing::PrinterList> promise(isolate);
  37. v8::Local<v8::Promise> handle = promise.GetHandle();
  38. base::ThreadPool::PostTaskAndReplyWithResult(
  39. FROM_HERE, {base::TaskPriority::USER_VISIBLE, base::MayBlock()},
  40. base::BindOnce([]() {
  41. printing::PrinterList printers;
  42. auto print_backend = printing::PrintBackend::CreateInstance(
  43. g_browser_process->GetApplicationLocale());
  44. printing::mojom::ResultCode code =
  45. print_backend->EnumeratePrinters(printers);
  46. if (code != printing::mojom::ResultCode::kSuccess)
  47. LOG(INFO) << "Failed to enumerate printers";
  48. return printers;
  49. }),
  50. base::BindOnce(
  51. [](gin_helper::Promise<printing::PrinterList> promise,
  52. const printing::PrinterList& printers) {
  53. promise.Resolve(printers);
  54. },
  55. std::move(promise)));
  56. return handle;
  57. }
  58. #endif
  59. } // namespace electron::api
  60. namespace {
  61. #if BUILDFLAG(ENABLE_PRINTING)
  62. using electron::api::GetPrinterListAsync;
  63. #endif
  64. void Initialize(v8::Local<v8::Object> exports,
  65. v8::Local<v8::Value> unused,
  66. v8::Local<v8::Context> context,
  67. void* priv) {
  68. v8::Isolate* isolate = context->GetIsolate();
  69. gin_helper::Dictionary dict(isolate, exports);
  70. #if BUILDFLAG(ENABLE_PRINTING)
  71. dict.SetMethod("getPrinterListAsync",
  72. base::BindRepeating(&GetPrinterListAsync));
  73. #endif
  74. }
  75. } // namespace
  76. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_printing, Initialize)