Add Mojo interface to parse XML for OOP printer capabilities
Out-of-process PrintBackend runs in a utility process. In order to fetch
XPS capabilities, we must parse XML safely by using the DataDecoder
service. However, a utility process cannot communicate directly to a
DataDecoder service. Instead, PrintBackend must communicate to the
browser process to invoke the DataDecoder to parse the XML. Add a Mojo
interface to be able to do so.
Bug: 1291257
Change-Id: Iaa1b14bc79f841fb2349907e200d35f583f7ce9c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3803867
Reviewed-by: Lei Zhang <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Alan Screen <[email protected]>
Commit-Queue: Andy Phan <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1059458}
diff --git a/chrome/browser/printing/printer_xml_parser_impl.cc b/chrome/browser/printing/printer_xml_parser_impl.cc
new file mode 100644
index 0000000..de0efa8b
--- /dev/null
+++ b/chrome/browser/printing/printer_xml_parser_impl.cc
@@ -0,0 +1,60 @@
+// Copyright 2022 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/printing/printer_xml_parser_impl.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/callback.h"
+#include "base/values.h"
+#include "chrome/services/printing/public/mojom/printer_xml_parser.mojom.h"
+#include "mojo/public/cpp/bindings/pending_receiver.h"
+#include "mojo/public/cpp/bindings/pending_remote.h"
+#include "mojo/public/cpp/bindings/receiver_set.h"
+#include "printing/mojom/print.mojom.h"
+#include "services/data_decoder/public/cpp/data_decoder.h"
+
+namespace printing {
+
+namespace {
+
+void XmlPrinterCapabilitiesParsed(
+ PrinterXmlParserImpl::ParseXmlCallback callback,
+ data_decoder::DataDecoder::ValueOrError value_or_error) {
+ if (!value_or_error.has_value()) {
+ std::move(callback).Run(
+ mojom::PrinterCapabilitiesValueResult::NewResultCode(
+ mojom::ResultCode::kFailed));
+ return;
+ }
+ std::move(callback).Run(
+ mojom::PrinterCapabilitiesValueResult::NewCapabilities(
+ std::move(value_or_error.value())));
+}
+
+} // namespace
+
+PrinterXmlParserImpl::PrinterXmlParserImpl() = default;
+
+PrinterXmlParserImpl::~PrinterXmlParserImpl() = default;
+
+void PrinterXmlParserImpl::ParseXmlForPrinterCapabilities(
+ const std::string& capabilities_xml,
+ ParseXmlCallback callback) {
+ if (!decoder_)
+ decoder_ = std::make_unique<data_decoder::DataDecoder>();
+ decoder_->ParseXml(
+ capabilities_xml,
+ data_decoder::mojom::XmlParser::WhitespaceBehavior::kIgnore,
+ base::BindOnce(&XmlPrinterCapabilitiesParsed, std::move(callback)));
+}
+
+mojo::PendingRemote<mojom::PrinterXmlParser> PrinterXmlParserImpl::GetRemote() {
+ mojo::PendingRemote<mojom::PrinterXmlParser> pending_remote;
+ receivers_.Add(this, pending_remote.InitWithNewPipeAndPassReceiver());
+ return pending_remote;
+}
+
+} // namespace printing