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 | |
| 11 #include "base/strings/string_util.h" | |
| 12 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 13 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 static std::string Validate( | |
|
imcheng
2017/03/22 00:31:47
This does not need the static keyword.
zhaobin
2017/03/22 01:55:32
Done.
| |
| 18 const media_router::DialDeviceDescription& description) { | |
| 19 if (description.unique_id.empty()) { | |
| 20 return "Missing uniqueId"; | |
| 21 } | |
| 22 if (description.friendly_name.empty()) { | |
| 23 return "Missing friendlyName"; | |
| 24 } | |
| 25 return ""; | |
| 26 } | |
| 27 | |
| 28 // If friendly name does not exist, fall back to use model name + last 4 | |
| 29 // digits of UUID as friendly name. | |
| 30 void ComputeFriendlyName(media_router::DialDeviceDescription* out) { | |
| 31 auto model_name = out->model_name; | |
| 32 auto unique_id = out->unique_id; | |
| 33 | |
| 34 if (!model_name.empty() && unique_id.length() >= 4) { | |
| 35 out->friendly_name = | |
| 36 model_name + " [" + unique_id.substr(unique_id.length() - 4) + "]"; | |
|
imcheng
2017/03/22 00:31:47
Consider using base::StringPrintf here.
zhaobin
2017/03/22 01:55:32
Done.
| |
| 37 } | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 namespace media_router { | |
| 43 | |
| 44 DialDeviceDescriptionParserImpl::DialDeviceDescriptionParserImpl() = default; | |
| 45 DialDeviceDescriptionParserImpl::~DialDeviceDescriptionParserImpl() { | |
| 46 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 void DialDeviceDescriptionParserImpl::Create( | |
| 51 chrome::mojom::DialDeviceDescriptionParserRequest request) { | |
| 52 mojo::MakeStrongBinding(base::MakeUnique<DialDeviceDescriptionParserImpl>(), | |
| 53 std::move(request)); | |
| 54 } | |
| 55 | |
| 56 void DialDeviceDescriptionParserImpl::ParseDialDeviceDescription( | |
| 57 const std::string& device_description_xml_data, | |
| 58 const ParseDialDeviceDescriptionCallback& callback) { | |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 60 DCHECK(!callback.is_null()); | |
| 61 | |
| 62 media_router::DialDeviceDescription device_description; | |
| 63 bool result = Parse(device_description_xml_data, &device_description); | |
|
imcheng
2017/03/22 00:31:47
If Parse returned false we shouldn't be sending ba
zhaobin
2017/03/22 01:55:32
Done.
| |
| 64 | |
| 65 callback.Run(result, device_description); | |
| 66 } | |
| 67 | |
| 68 bool DialDeviceDescriptionParserImpl::Parse( | |
| 69 const std::string& xml, | |
| 70 media_router::DialDeviceDescription* out) { | |
| 71 DCHECK(out); | |
| 72 | |
| 73 XmlReader xml_reader; | |
| 74 if (!xml_reader.Load(xml)) | |
| 75 return false; | |
| 76 | |
| 77 while (xml_reader.Read()) { | |
| 78 xml_reader.SkipToElement(); | |
| 79 std::string node_name(xml_reader.NodeName()); | |
| 80 | |
| 81 if (node_name == "UDN") { | |
| 82 if (!xml_reader.ReadElementContent(&out->unique_id)) | |
| 83 return false; | |
| 84 } else if (node_name == "friendlyName") { | |
| 85 if (!xml_reader.ReadElementContent(&out->friendly_name)) | |
| 86 return false; | |
| 87 } else if (node_name == "modelName") { | |
| 88 if (!xml_reader.ReadElementContent(&out->model_name)) | |
| 89 return false; | |
| 90 } else if (node_name == "deviceType") { | |
| 91 if (!xml_reader.ReadElementContent(&out->device_type)) | |
| 92 return false; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 if (out->friendly_name.empty()) | |
| 97 ComputeFriendlyName(out); | |
| 98 | |
| 99 std::string error = Validate(*out); | |
| 100 if (!error.empty()) { | |
| 101 DLOG(WARNING) << "Device description failed to validate: " << error; | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 return true; | |
| 106 } | |
| 107 | |
| 108 } // namespace media_router | |
| OLD | NEW |