Chromium Code Reviews| Index: chrome/utility/media_router/dial_device_description_parser_impl.cc |
| diff --git a/chrome/utility/media_router/dial_device_description_parser_impl.cc b/chrome/utility/media_router/dial_device_description_parser_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..292d90b0806ba86eafb357f2fd1ea452ffc527eb |
| --- /dev/null |
| +++ b/chrome/utility/media_router/dial_device_description_parser_impl.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright 2017 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/utility/media_router/dial_device_description_parser_impl.h" |
| + |
| +#include <libxml/parser.h> |
| +#include <libxml/tree.h> |
| +#include <libxml/xpath.h> |
| +#include <utility> |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "third_party/libxml/chromium/libxml_utils.h" |
| + |
| +namespace { |
| + |
| +enum ErrorType { |
| + NONE, |
| + MISSING_UNIQUE_ID, |
| + MISSING_FRIENDLY_NAME, |
| +}; |
| + |
| +ErrorType Validate(const chrome::mojom::DialDeviceDescription& description) { |
| + if (description.unique_id.empty()) { |
| + return ErrorType::MISSING_UNIQUE_ID; |
| + } |
| + if (description.friendly_name.empty()) { |
| + return ErrorType::MISSING_FRIENDLY_NAME; |
| + } |
| + return ErrorType::NONE; |
| +} |
| + |
| +// If friendly name does not exist, fall back to use model name + last 4 |
| +// digits of UUID as friendly name. |
| +void ComputeFriendlyName(const chrome::mojom::DialDeviceDescriptionPtr& out) { |
| + auto& model_name = out->model_name; |
| + auto& unique_id = out->unique_id; |
| + |
| + if (model_name.empty() || unique_id.length() < 4) |
| + return; |
| + |
| + std::string trimmed_unique_id = unique_id.substr(unique_id.length() - 4); |
| + out->friendly_name = base::StringPrintf("%s [%s]", model_name.c_str(), |
| + trimmed_unique_id.c_str()); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace media_router { |
| + |
| +DialDeviceDescriptionParserImpl::DialDeviceDescriptionParserImpl() = default; |
| +DialDeviceDescriptionParserImpl::~DialDeviceDescriptionParserImpl() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| +} |
| + |
| +// static |
| +void DialDeviceDescriptionParserImpl::Create( |
| + chrome::mojom::DialDeviceDescriptionParserRequest request) { |
| + mojo::MakeStrongBinding(base::MakeUnique<DialDeviceDescriptionParserImpl>(), |
| + std::move(request)); |
| +} |
| + |
| +void DialDeviceDescriptionParserImpl::ParseDialDeviceDescription( |
| + const std::string& device_description_xml_data, |
| + const ParseDialDeviceDescriptionCallback& callback) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(!callback.is_null()); |
| + |
| + chrome::mojom::DialDeviceDescriptionPtr device_description = |
| + chrome::mojom::DialDeviceDescription::New(); |
| + bool result = Parse(device_description_xml_data, device_description); |
| + |
| + if (result) { |
| + callback.Run(result, std::move(device_description)); |
| + } else { |
| + callback.Run(result, chrome::mojom::DialDeviceDescriptionPtr()); |
| + } |
| +} |
| + |
| +bool DialDeviceDescriptionParserImpl::Parse( |
| + const std::string& xml, |
| + chrome::mojom::DialDeviceDescriptionPtr& out) { |
|
dcheng
2017/03/25 00:59:38
Nit: mutable ref arguments aren't permitted in Chr
zhaobin
2017/03/27 19:16:55
Done.
|
| + DCHECK(out); |
| + |
| + XmlReader xml_reader; |
| + if (!xml_reader.Load(xml)) |
| + return false; |
| + |
| + while (xml_reader.Read()) { |
| + xml_reader.SkipToElement(); |
| + std::string node_name(xml_reader.NodeName()); |
| + |
| + if (node_name == "UDN") { |
| + if (!xml_reader.ReadElementContent(&out->unique_id)) |
| + return false; |
| + } else if (node_name == "friendlyName") { |
| + if (!xml_reader.ReadElementContent(&out->friendly_name)) |
| + return false; |
| + } else if (node_name == "modelName") { |
| + if (!xml_reader.ReadElementContent(&out->model_name)) |
| + return false; |
| + } else if (node_name == "deviceType") { |
| + if (!xml_reader.ReadElementContent(&out->device_type)) |
| + return false; |
| + } |
| + } |
| + |
| + if (out->friendly_name.empty()) |
| + ComputeFriendlyName(out); |
| + |
| + ErrorType error = Validate(*out); |
| + if (error != ErrorType::NONE) { |
| + DLOG(WARNING) << "Device description failed to validate: " << error; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace media_router |