[go: nahoru, domu]

Introduce print backend service

This is to support moving print driver interactions to the operating
system outside of the browser process.  This improves stability so that
crashes related to misbehavior with third party drivers can be contained
in a utility process instead of taking down the browser.

Start off the interface with the query to get the current default
printer.  This is a common initial first query when starting the
printing sequence.

Bug: 809738
Change-Id: I819ecf71c889494f2aa05cf150359e68f799553c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2422820
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
Commit-Queue: Alan Screen <awscreen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822209}
diff --git a/chrome/services/printing/print_backend_service_impl.cc b/chrome/services/printing/print_backend_service_impl.cc
new file mode 100644
index 0000000..d38d57b
--- /dev/null
+++ b/chrome/services/printing/print_backend_service_impl.cc
@@ -0,0 +1,40 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/services/printing/print_backend_service_impl.h"
+
+#include <string>
+#include <utility>
+
+#include "base/logging.h"
+#include "base/notreached.h"
+#include "base/optional.h"
+#include "chrome/services/printing/public/mojom/print_backend_service.mojom.h"
+#include "mojo/public/cpp/bindings/pending_receiver.h"
+#include "printing/backend/print_backend.h"
+
+namespace printing {
+
+PrintBackendServiceImpl::PrintBackendServiceImpl(
+    mojo::PendingReceiver<mojom::PrintBackendService> receiver)
+    : receiver_(this, std::move(receiver)) {}
+
+PrintBackendServiceImpl::~PrintBackendServiceImpl() = default;
+
+void PrintBackendServiceImpl::Init(const std::string& locale) {
+  print_backend_ = PrintBackend::CreateInstance(locale);
+}
+
+void PrintBackendServiceImpl::GetDefaultPrinterName(
+    mojom::PrintBackendService::GetDefaultPrinterNameCallback callback) {
+  if (!print_backend_) {
+    DLOG(ERROR)
+        << "Print backend instance has not been initialized for locale.";
+    std::move(callback).Run(base::nullopt);
+    return;
+  }
+  std::move(callback).Run(print_backend_->GetDefaultPrinterName());
+}
+
+}  // namespace printing