Andy Phan | d8ca243 | 2022-10-14 21:11:52 | [diff] [blame] | 1 | // 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 Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame^] | 10 | #include "base/functional/callback.h" |
Andy Phan | d8ca243 | 2022-10-14 21:11:52 | [diff] [blame] | 11 | #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 | |
| 19 | namespace printing { |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | void 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 | |
| 39 | PrinterXmlParserImpl::PrinterXmlParserImpl() = default; |
| 40 | |
| 41 | PrinterXmlParserImpl::~PrinterXmlParserImpl() = default; |
| 42 | |
| 43 | void 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 | |
| 54 | mojo::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 |