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

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 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 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..0181320eda6193e0754eb4741f0ad95d2c4957ae
--- /dev/null
+++ b/chrome/utility/media_router/dial_device_description_parser_impl.cc
@@ -0,0 +1,108 @@
+// 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/string_util.h"
+#include "mojo/public/cpp/bindings/strong_binding.h"
+#include "third_party/libxml/chromium/libxml_utils.h"
+
+namespace {
+
+static std::string Validate(
imcheng 2017/03/22 00:31:47 This does not need the static keyword.
zhaobin 2017/03/22 01:55:32 Done.
+ const media_router::DialDeviceDescription& description) {
+ if (description.unique_id.empty()) {
+ return "Missing uniqueId";
+ }
+ if (description.friendly_name.empty()) {
+ return "Missing friendlyName";
+ }
+ return "";
+}
+
+// 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;
+ 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) + "]";
imcheng 2017/03/22 00:31:47 Consider using base::StringPrintf here.
zhaobin 2017/03/22 01:55:32 Done.
+ }
+}
+
+} // 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>(),
+ std::move(request));
+}
+
+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);
imcheng 2017/03/22 00:31:47 If Parse returned false we shouldn't be sending ba
zhaobin 2017/03/22 01:55:32 Done.
+
+ callback.Run(result, device_description);
+}
+
+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);
+ 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