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