Chromium Code Reviews| Index: chrome/utility/media_router/dial_device_description_parser.cc |
| diff --git a/chrome/utility/media_router/dial_device_description_parser.cc b/chrome/utility/media_router/dial_device_description_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b804653e326c7cf151982dab8403bbb681b08e85 |
| --- /dev/null |
| +++ b/chrome/utility/media_router/dial_device_description_parser.cc |
| @@ -0,0 +1,134 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/utility/media_router/dial_device_description_parser.h" |
| + |
| +#include <libxml/parser.h> |
| +#include <libxml/tree.h> |
| +#include <libxml/xpath.h> |
| +#include <libxslt/transform.h> |
| +#include <libxslt/xslt.h> |
| +#include <libxslt/xsltutils.h> |
| + |
| +#include "base/strings/string_util.h" |
| +#include "third_party/libxml/chromium/libxml_utils.h" |
| + |
| +namespace { |
| + |
| +constexpr char xslt_text[] = |
| + "<?xml version=\"1.0\" encoding=\"utf-8\"?>" |
| + "<xsl:stylesheet version=\"1.0\" " |
| + "xmlns:t=\"urn:schemas-upnp-org:device-1-0\" " |
| + "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" |
| + "<xsl:template match=\"node()|@*\">" |
| + "<xsl:copy>" |
| + "<xsl:apply-templates select=\"node()|@*\"/>" |
| + "</xsl:copy>" |
| + "</xsl:template>" |
| + "<xsl:template match=\"t:root/t:device/t:UDN/text()\">***</xsl:template>" |
| + "<xsl:template " |
| + "match=\"t:root/t:device/t:serialNumber/text()\">***</xsl:template>" |
| + "</xsl:stylesheet>"; |
| + |
| +static std::string Validate( |
| + const media_router::DialDeviceDescription& description) { |
| + if (description.unique_id.empty()) { |
| + return "Missing uniqueId"; |
| + } |
| + if (description.friendly_name.empty()) { |
| + return "Missing friendlyName"; |
| + } |
| + return ""; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace media_router { |
| + |
| +DialDeviceDescriptionParser::DialDeviceDescriptionParser() = default; |
| +DialDeviceDescriptionParser::~DialDeviceDescriptionParser() = default; |
| + |
| +bool DialDeviceDescriptionParser::Parse( |
| + const std::string& xml, |
| + media_router::DialDeviceDescription* out) { |
| + XmlReader xml_reader; |
|
mark a. foltz
2017/03/18 18:56:57
DCHECK(out)
zhaobin
2017/03/20 21:25:46
Done.
|
| + if (!xml_reader.Load(xml)) |
| + return false; |
| + |
| + while (xml_reader.Read()) { |
| + xml_reader.SkipToElement(); |
| + std::string node_name(xml_reader.NodeName()); |
| + |
| + if (node_name == "UDN") { |
| + if (!xml_reader.ReadElementContent(&out->unique_id)) |
| + return false; |
| + } else if (node_name == "friendlyName") { |
| + if (!xml_reader.ReadElementContent(&out->friendly_name)) |
| + return false; |
| + } else if (node_name == "modelName") { |
| + if (!xml_reader.ReadElementContent(&out->model_name)) |
| + return false; |
| + } else if (node_name == "deviceType") { |
| + if (!xml_reader.ReadElementContent(&out->device_type)) |
| + return false; |
| + } |
| + } |
| + |
| + // If friendly name does not exist, fall back to use model name + last 4 |
| + // digits of UUID as friendly name. |
| + if (out->friendly_name.empty()) { |
| + 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.
|
| + auto unique_id = out->unique_id; |
| + |
| + if (!model_name.empty() && unique_id.length() >= 4) { |
| + out->friendly_name = |
| + model_name + "[" + unique_id.substr(unique_id.length() - 4) + "]"; |
| + } |
| + } |
| + |
| + std::string error = Validate(*out); |
| + if (!error.empty()) { |
| + DLOG(WARNING) << "Device description failed to validate: " << error; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +std::string DialDeviceDescriptionParser::ScrubXmlForLogging( |
| + const std::string& xml) { |
| + xmlDocPtr doc = xmlParseMemory(xml.c_str(), xml.size()); |
| + |
| + if (doc == nullptr) { |
| + LOG(WARNING) << "Document not parsed successfully"; |
| + return ""; |
| + } |
| + |
| + 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.
|
| + CHECK(xslt_doc); |
| + |
| + xsltStylesheetPtr style_ptr = xsltParseStylesheetDoc(xslt_doc); |
| + xmlDocPtr res = xsltApplyStylesheet(style_ptr, doc, nullptr); |
| + |
| + std::string logging_xml = ""; |
| + |
| + xmlChar* output; |
| + int size; |
| + if (xsltSaveResultToString(&output, &size, res, style_ptr) < 0) { |
| + LOG(WARNING) << "Fail to output result"; |
| + } else { |
| + logging_xml = std::string(reinterpret_cast<char*>(output)); |
| + } |
| + |
| + xmlFreeDoc(xslt_doc); |
| + xmlFreeDoc(doc); |
| + xmlFree(output); |
| + |
| + xsltCleanupGlobals(); |
| + xmlCleanupParser(); |
| + |
| + return logging_xml; |
| +} |
| + |
| +} // namespace media_router |