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

Side by Side Diff: chrome/utility/media_router/dial_device_description_parser.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.h"
6
7 #include <libxml/parser.h>
8 #include <libxml/tree.h>
9 #include <libxml/xpath.h>
10 #include <libxslt/transform.h>
11 #include <libxslt/xslt.h>
12 #include <libxslt/xsltutils.h>
13
14 #include "base/strings/string_util.h"
15 #include "third_party/libxml/chromium/libxml_utils.h"
16
17 namespace {
18
19 constexpr char xslt_text[] =
20 "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
21 "<xsl:stylesheet version=\"1.0\" "
22 "xmlns:t=\"urn:schemas-upnp-org:device-1-0\" "
23 "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
24 "<xsl:template match=\"node()|@*\">"
25 "<xsl:copy>"
26 "<xsl:apply-templates select=\"node()|@*\"/>"
27 "</xsl:copy>"
28 "</xsl:template>"
29 "<xsl:template match=\"t:root/t:device/t:UDN/text()\">***</xsl:template>"
30 "<xsl:template "
31 "match=\"t:root/t:device/t:serialNumber/text()\">***</xsl:template>"
32 "</xsl:stylesheet>";
33
34 static std::string Validate(
35 const media_router::DialDeviceDescription& description) {
36 if (description.unique_id.empty()) {
37 return "Missing uniqueId";
38 }
39 if (description.friendly_name.empty()) {
40 return "Missing friendlyName";
41 }
42 return "";
43 }
44
45 } // namespace
46
47 namespace media_router {
48
49 DialDeviceDescriptionParser::DialDeviceDescriptionParser() = default;
50 DialDeviceDescriptionParser::~DialDeviceDescriptionParser() = default;
51
52 bool DialDeviceDescriptionParser::Parse(
53 const std::string& xml,
54 media_router::DialDeviceDescription* out) {
55 XmlReader xml_reader;
mark a. foltz 2017/03/18 18:56:57 DCHECK(out)
zhaobin 2017/03/20 21:25:46 Done.
56 if (!xml_reader.Load(xml))
57 return false;
58
59 while (xml_reader.Read()) {
60 xml_reader.SkipToElement();
61 std::string node_name(xml_reader.NodeName());
62
63 if (node_name == "UDN") {
64 if (!xml_reader.ReadElementContent(&out->unique_id))
65 return false;
66 } else if (node_name == "friendlyName") {
67 if (!xml_reader.ReadElementContent(&out->friendly_name))
68 return false;
69 } else if (node_name == "modelName") {
70 if (!xml_reader.ReadElementContent(&out->model_name))
71 return false;
72 } else if (node_name == "deviceType") {
73 if (!xml_reader.ReadElementContent(&out->device_type))
74 return false;
75 }
76 }
77
78 // If friendly name does not exist, fall back to use model name + last 4
79 // digits of UUID as friendly name.
80 if (out->friendly_name.empty()) {
81 auto model_name = out->model_name;
mark a. foltz 2017/03/18 18:56:57 Slight preference to factor out a function like Co
zhaobin 2017/03/20 21:25:46 Done.
82 auto unique_id = out->unique_id;
83
84 if (!model_name.empty() && unique_id.length() >= 4) {
85 out->friendly_name =
86 model_name + "[" + unique_id.substr(unique_id.length() - 4) + "]";
87 }
88 }
89
90 std::string error = Validate(*out);
91 if (!error.empty()) {
92 DLOG(WARNING) << "Device description failed to validate: " << error;
93 return false;
94 }
95
96 return true;
97 }
98
99 std::string DialDeviceDescriptionParser::ScrubXmlForLogging(
100 const std::string& xml) {
101 xmlDocPtr doc = xmlParseMemory(xml.c_str(), xml.size());
102
103 if (doc == nullptr) {
104 LOG(WARNING) << "Document not parsed successfully";
105 return "";
106 }
107
108 xmlDocPtr xslt_doc = xmlParseMemory(xslt_text, sizeof(xslt_text));
mark a. foltz 2017/03/18 18:56:57 I'm leaning towards using string substitution for
zhaobin 2017/03/20 21:25:46 Done.
109 CHECK(xslt_doc);
110
111 xsltStylesheetPtr style_ptr = xsltParseStylesheetDoc(xslt_doc);
112 xmlDocPtr res = xsltApplyStylesheet(style_ptr, doc, nullptr);
113
114 std::string logging_xml = "";
115
116 xmlChar* output;
117 int size;
118 if (xsltSaveResultToString(&output, &size, res, style_ptr) < 0) {
119 LOG(WARNING) << "Fail to output result";
120 } else {
121 logging_xml = std::string(reinterpret_cast<char*>(output));
122 }
123
124 xmlFreeDoc(xslt_doc);
125 xmlFreeDoc(doc);
126 xmlFree(output);
127
128 xsltCleanupGlobals();
129 xmlCleanupParser();
130
131 return logging_xml;
132 }
133
134 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698