Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 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/utility/media_router/dial_device_description_parser_impl.h" | |
| 6 | |
| 7 #include <libxml/parser.h> | |
| 8 #include <libxml/tree.h> | |
| 9 #include <libxml/xpath.h> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 15 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 enum ErrorType { | |
| 20 NONE, | |
| 21 MISSING_UNIQUE_ID, | |
| 22 MISSING_FRIENDLY_NAME, | |
| 23 }; | |
| 24 | |
| 25 ErrorType Validate(const chrome::mojom::DialDeviceDescription& description) { | |
| 26 if (description.unique_id.empty()) { | |
| 27 return ErrorType::MISSING_UNIQUE_ID; | |
| 28 } | |
| 29 if (description.friendly_name.empty()) { | |
| 30 return ErrorType::MISSING_FRIENDLY_NAME; | |
| 31 } | |
| 32 return ErrorType::NONE; | |
| 33 } | |
| 34 | |
| 35 // If friendly name does not exist, fall back to use model name + last 4 | |
| 36 // digits of UUID as friendly name. | |
| 37 void ComputeFriendlyName(const chrome::mojom::DialDeviceDescriptionPtr& out) { | |
| 38 auto& model_name = out->model_name; | |
| 39 auto& unique_id = out->unique_id; | |
| 40 | |
| 41 if (model_name.empty() || unique_id.length() < 4) | |
| 42 return; | |
| 43 | |
| 44 std::string trimmed_unique_id = unique_id.substr(unique_id.length() - 4); | |
| 45 out->friendly_name = base::StringPrintf("%s [%s]", model_name.c_str(), | |
| 46 trimmed_unique_id.c_str()); | |
| 47 } | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 namespace media_router { | |
| 52 | |
| 53 DialDeviceDescriptionParserImpl::DialDeviceDescriptionParserImpl() = default; | |
| 54 DialDeviceDescriptionParserImpl::~DialDeviceDescriptionParserImpl() { | |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 56 } | |
| 57 | |
| 58 // static | |
| 59 void DialDeviceDescriptionParserImpl::Create( | |
| 60 chrome::mojom::DialDeviceDescriptionParserRequest request) { | |
| 61 mojo::MakeStrongBinding(base::MakeUnique<DialDeviceDescriptionParserImpl>(), | |
| 62 std::move(request)); | |
| 63 } | |
| 64 | |
| 65 void DialDeviceDescriptionParserImpl::ParseDialDeviceDescription( | |
| 66 const std::string& device_description_xml_data, | |
| 67 const ParseDialDeviceDescriptionCallback& callback) { | |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 69 DCHECK(!callback.is_null()); | |
| 70 | |
| 71 chrome::mojom::DialDeviceDescriptionPtr device_description = | |
| 72 chrome::mojom::DialDeviceDescription::New(); | |
| 73 bool result = Parse(device_description_xml_data, device_description); | |
| 74 | |
| 75 if (result) { | |
| 76 callback.Run(result, std::move(device_description)); | |
| 77 } else { | |
| 78 callback.Run(result, chrome::mojom::DialDeviceDescriptionPtr()); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 bool DialDeviceDescriptionParserImpl::Parse( | |
| 83 const std::string& xml, | |
| 84 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.
| |
| 85 DCHECK(out); | |
| 86 | |
| 87 XmlReader xml_reader; | |
| 88 if (!xml_reader.Load(xml)) | |
| 89 return false; | |
| 90 | |
| 91 while (xml_reader.Read()) { | |
| 92 xml_reader.SkipToElement(); | |
| 93 std::string node_name(xml_reader.NodeName()); | |
| 94 | |
| 95 if (node_name == "UDN") { | |
| 96 if (!xml_reader.ReadElementContent(&out->unique_id)) | |
| 97 return false; | |
| 98 } else if (node_name == "friendlyName") { | |
| 99 if (!xml_reader.ReadElementContent(&out->friendly_name)) | |
| 100 return false; | |
| 101 } else if (node_name == "modelName") { | |
| 102 if (!xml_reader.ReadElementContent(&out->model_name)) | |
| 103 return false; | |
| 104 } else if (node_name == "deviceType") { | |
| 105 if (!xml_reader.ReadElementContent(&out->device_type)) | |
| 106 return false; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 if (out->friendly_name.empty()) | |
| 111 ComputeFriendlyName(out); | |
| 112 | |
| 113 ErrorType error = Validate(*out); | |
| 114 if (error != ErrorType::NONE) { | |
| 115 DLOG(WARNING) << "Device description failed to validate: " << error; | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 } // namespace media_router | |
| OLD | NEW |