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

Unified 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 Derek 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 side-by-side diff with in-line comments
Download patch
Index: chrome/utility/media_router/dial_device_description_parser_impl.cc
diff --git a/chrome/utility/media_router/dial_device_description_parser_impl.cc b/chrome/utility/media_router/dial_device_description_parser_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..806a6a32ba3a1e8d222ad8495b32841a3a7720bd
--- /dev/null
+++ b/chrome/utility/media_router/dial_device_description_parser_impl.cc
@@ -0,0 +1,113 @@
+// 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_impl.h"
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xpath.h>
+
+#include "base/strings/stringprintf.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+#include "third_party/libxml/chromium/libxml_utils.h"
+
+namespace {
+
+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 "";
dcheng 2017/03/22 09:53:02 Nit: return std::string()
zhaobin 2017/03/22 19:18:52 Done.
+}
+
+// If friendly name does not exist, fall back to use model name + last 4
+// digits of UUID as friendly name.
+void ComputeFriendlyName(media_router::DialDeviceDescription* out) {
+ 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.
+ auto unique_id = out->unique_id;
+
+ if (model_name.empty() || unique_id.length() < 4)
+ return;
+
+ std::string trimmed_unique_id = unique_id.substr(unique_id.length() - 4);
+ out->friendly_name = base::StringPrintf("%s [%s]", model_name.c_str(),
+ trimmed_unique_id.c_str());
+}
+
+} // namespace
+
+namespace media_router {
+
+DialDeviceDescriptionParserImpl::DialDeviceDescriptionParserImpl() = default;
+DialDeviceDescriptionParserImpl::~DialDeviceDescriptionParserImpl() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+}
+
+// static
+void DialDeviceDescriptionParserImpl::Create(
+ chrome::mojom::DialDeviceDescriptionParserRequest request) {
+ 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.
+ std::move(request));
dcheng 2017/03/22 09:53:02 Nit: #include <utility>
zhaobin 2017/03/22 19:18:52 Done.
+}
+
+void DialDeviceDescriptionParserImpl::ParseDialDeviceDescription(
+ const std::string& device_description_xml_data,
+ const ParseDialDeviceDescriptionCallback& callback) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!callback.is_null());
+
+ media_router::DialDeviceDescription device_description;
+ bool result = Parse(device_description_xml_data, &device_description);
+
+ if (result) {
+ callback.Run(result, device_description);
+ } else {
+ callback.Run(result, base::nullopt);
+ }
+}
+
+bool DialDeviceDescriptionParserImpl::Parse(
+ const std::string& xml,
+ media_router::DialDeviceDescription* out) {
+ DCHECK(out);
+
+ XmlReader xml_reader;
+ 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 (out->friendly_name.empty())
+ ComputeFriendlyName(out);
+
+ 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.
+ if (!error.empty()) {
+ DLOG(WARNING) << "Device description failed to validate: " << error;
+ return false;
+ }
+
+ return true;
+}
+
+} // namespace media_router

Powered by Google App Engine
This is Rietveld 408576698