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

Unified Diff: components/physical_web/webui/physical_web_base_message_handler.cc

Issue 2571853003: Move PW message handler logic to components (Closed)
Patch Set: Set WebUI at handler Registration time Created 4 years 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: components/physical_web/webui/physical_web_base_message_handler.cc
diff --git a/components/physical_web/webui/physical_web_base_message_handler.cc b/components/physical_web/webui/physical_web_base_message_handler.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b6706c9bd374c18fe4ac86d3f0c29982279977c2
--- /dev/null
+++ b/components/physical_web/webui/physical_web_base_message_handler.cc
@@ -0,0 +1,70 @@
+// Copyright 2016 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 "components/physical_web/webui/physical_web_base_message_handler.h"
+
+#include "base/bind.h"
+#include "base/metrics/histogram_macros.h"
+#include "base/metrics/user_metrics.h"
+#include "components/physical_web/data_source/physical_web_data_source.h"
+#include "components/physical_web/webui/physical_web_ui_constants.h"
+
+namespace physical_web_ui {
+
+PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {}
+
+PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default;
+
+void PhysicalWebBaseMessageHandler::RegisterMessages() {
+ RegisterMessageCallback(
+ kRequestNearbyUrls,
+ base::BindRepeating(
+ &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs,
+ base::Unretained(this)));
+ RegisterMessageCallback(kPhysicalWebItemClicked,
+ base::BindRepeating(
+ &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
+ base::Unretained(this)));
+}
+
+void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs(
+ const base::ListValue* args) {
+ base::DictionaryValue results;
+
+ std::unique_ptr<base::ListValue> metadata =
+ GetPhysicalWebDataSource()->GetMetadata();
+
+ // Add item indices. When an item is selected, the index of the selected item
+ // is recorded in a UMA histogram.
+ for (size_t i = 0; i < metadata->GetSize(); i++) {
+ base::DictionaryValue* metadata_item = nullptr;
+ metadata->GetDictionary(i, &metadata_item);
+ metadata_item->SetInteger(physical_web_ui::kIndex, i);
+ }
+
+ results.Set(physical_web_ui::kMetadata, metadata.release());
+
+ // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will
+ // create a list view with an item for each URL.
+ CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results);
+}
+
+void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
+ const base::ListValue* args) {
+ int index = 0;
+ if (!args->GetInteger(0, &index)) {
+ DLOG(ERROR) << "Invalid selection index";
+ return;
+ }
+
+ // Record the index of the selected item.
+ UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
+ 50);
+
+ // Count the number of selections.
+ base::RecordAction(
+ base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
+}
+
+} // namespace physical_web_ui
« no previous file with comments | « components/physical_web/webui/physical_web_base_message_handler.h ('k') | ios/chrome/browser/ui/webui/physical_web_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698