Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: chrome/utility/media_router/dial_device_description_parser_impl.cc

Issue 2745653008: [Media Router] Parse device description xml in utility process (Closed)
Patch Set: resolve code review comments from Mark Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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(
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 if (!out->friendly_name.empty())
mark a. foltz 2017/03/21 01:39:21 For clarity I would move this check back out to th
zhaobin 2017/03/21 03:15:38 Done.
32 return;
33
34 auto model_name = out->model_name;
35 auto unique_id = out->unique_id;
36
37 if (!model_name.empty() && unique_id.length() >= 4) {
38 out->friendly_name =
39 model_name + "[" + unique_id.substr(unique_id.length() - 4) + "]";
mark a. foltz 2017/03/21 01:39:21 Nit: Can you add a space before [
zhaobin 2017/03/21 03:15:38 Done.
40 }
41 }
42
43 // Replaces "<element_name>content</element_name>" with
44 // "<element_name>***</element_name>"
45 void Scrub(const std::string& element_name, std::string& xml_text) {
46 size_t pos = xml_text.find("<" + element_name + ">");
47 size_t end_pos = xml_text.find("</" + element_name + ">");
48 size_t start_pos = pos + element_name.length() + 2;
49
50 if (pos != std::string::npos && end_pos != std::string::npos)
mark a. foltz 2017/03/21 01:39:22 && end_pos > start_pos
zhaobin 2017/03/21 03:15:38 Done.
51 xml_text.replace(start_pos, end_pos - start_pos, "***");
52 }
53
54 } // namespace
55
56 namespace media_router {
57
58 DialDeviceDescriptionParserImpl::DialDeviceDescriptionParserImpl() = default;
59 DialDeviceDescriptionParserImpl::~DialDeviceDescriptionParserImpl() {
60 DCHECK(thread_checker_.CalledOnValidThread());
61 }
62
63 // static
64 void DialDeviceDescriptionParserImpl::Create(
65 chrome::mojom::DialDeviceDescriptionParserRequest request) {
66 mojo::MakeStrongBinding(base::MakeUnique<DialDeviceDescriptionParserImpl>(),
67 std::move(request));
68 }
69
70 void DialDeviceDescriptionParserImpl::ParseDialDeviceDescription(
71 const std::string& device_description_xml_data,
72 const ParseDialDeviceDescriptionCallback& callback) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 DCHECK(!callback.is_null());
75
76 media_router::DialDeviceDescription device_description;
77 bool result = Parse(device_description_xml_data, &device_description);
78 std::string logging_xml = ScrubXmlForLogging(device_description_xml_data);
79
80 callback.Run(result, device_description, logging_xml);
81 }
82
83 bool DialDeviceDescriptionParserImpl::Parse(
84 const std::string& xml,
85 media_router::DialDeviceDescription* out) {
86 DCHECK(out);
87
88 XmlReader xml_reader;
89 if (!xml_reader.Load(xml))
90 return false;
91
92 while (xml_reader.Read()) {
93 xml_reader.SkipToElement();
94 std::string node_name(xml_reader.NodeName());
95
96 if (node_name == "UDN") {
97 if (!xml_reader.ReadElementContent(&out->unique_id))
98 return false;
99 } else if (node_name == "friendlyName") {
100 if (!xml_reader.ReadElementContent(&out->friendly_name))
101 return false;
102 } else if (node_name == "modelName") {
103 if (!xml_reader.ReadElementContent(&out->model_name))
104 return false;
105 } else if (node_name == "deviceType") {
106 if (!xml_reader.ReadElementContent(&out->device_type))
107 return false;
108 }
109 }
110
111 ComputeFriendlyName(out);
112
113 std::string error = Validate(*out);
114 if (!error.empty()) {
115 DLOG(WARNING) << "Device description failed to validate: " << error;
116 return false;
117 }
118
119 return true;
120 }
121
122 std::string DialDeviceDescriptionParserImpl::ScrubXmlForLogging(
123 const std::string& xml_text) {
124 std::string scrubbed_xml(xml_text);
125 Scrub("UDN", scrubbed_xml);
126 Scrub("serialNumber", scrubbed_xml);
127 return scrubbed_xml;
128 }
129
130 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698