blob: 973259be07f7b80236ab86525cd77052588689a5 [file] [log] [blame]
Andy Phand8ca2432022-10-14 21:11:521// Copyright 2022 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/printing/printer_xml_parser_impl.h"
6
7#include <memory>
8#include <utility>
9
Avi Drissman9269d4ed2023-01-07 01:38:0610#include "base/functional/callback.h"
Andy Phand8ca2432022-10-14 21:11:5211#include "base/values.h"
12#include "chrome/services/printing/public/mojom/printer_xml_parser.mojom.h"
13#include "mojo/public/cpp/bindings/pending_receiver.h"
14#include "mojo/public/cpp/bindings/pending_remote.h"
15#include "mojo/public/cpp/bindings/receiver_set.h"
16#include "printing/mojom/print.mojom.h"
17#include "services/data_decoder/public/cpp/data_decoder.h"
18
19namespace printing {
20
21namespace {
22
23void XmlPrinterCapabilitiesParsed(
24 PrinterXmlParserImpl::ParseXmlCallback callback,
25 data_decoder::DataDecoder::ValueOrError value_or_error) {
26 if (!value_or_error.has_value()) {
27 std::move(callback).Run(
28 mojom::PrinterCapabilitiesValueResult::NewResultCode(
29 mojom::ResultCode::kFailed));
30 return;
31 }
32 std::move(callback).Run(
33 mojom::PrinterCapabilitiesValueResult::NewCapabilities(
34 std::move(value_or_error.value())));
35}
36
37} // namespace
38
39PrinterXmlParserImpl::PrinterXmlParserImpl() = default;
40
41PrinterXmlParserImpl::~PrinterXmlParserImpl() = default;
42
43void PrinterXmlParserImpl::ParseXmlForPrinterCapabilities(
44 const std::string& capabilities_xml,
45 ParseXmlCallback callback) {
46 if (!decoder_)
47 decoder_ = std::make_unique<data_decoder::DataDecoder>();
48 decoder_->ParseXml(
49 capabilities_xml,
50 data_decoder::mojom::XmlParser::WhitespaceBehavior::kIgnore,
51 base::BindOnce(&XmlPrinterCapabilitiesParsed, std::move(callback)));
52}
53
54mojo::PendingRemote<mojom::PrinterXmlParser> PrinterXmlParserImpl::GetRemote() {
55 mojo::PendingRemote<mojom::PrinterXmlParser> pending_remote;
56 receivers_.Add(this, pending_remote.InitWithNewPipeAndPassReceiver());
57 return pending_remote;
58}
59
60} // namespace printing