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

Side by Side 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 3 years, 11 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 2016 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 "components/physical_web/webui/physical_web_base_message_handler.h"
6
7 #include "base/bind.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "base/metrics/user_metrics.h"
10 #include "components/physical_web/data_source/physical_web_data_source.h"
11 #include "components/physical_web/webui/physical_web_ui_constants.h"
12
13 namespace physical_web_ui {
14
15 PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {}
16
17 PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default;
18
19 void PhysicalWebBaseMessageHandler::RegisterMessages() {
20 RegisterMessageCallback(
21 kRequestNearbyUrls,
22 base::BindRepeating(
23 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs,
24 base::Unretained(this)));
25 RegisterMessageCallback(kPhysicalWebItemClicked,
26 base::BindRepeating(
27 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
28 base::Unretained(this)));
29 }
30
31 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs(
32 const base::ListValue* args) {
33 base::DictionaryValue results;
34
35 std::unique_ptr<base::ListValue> metadata =
36 GetPhysicalWebDataSource()->GetMetadata();
37
38 // Add item indices. When an item is selected, the index of the selected item
39 // is recorded in a UMA histogram.
40 for (size_t i = 0; i < metadata->GetSize(); i++) {
41 base::DictionaryValue* metadata_item = nullptr;
42 metadata->GetDictionary(i, &metadata_item);
43 metadata_item->SetInteger(physical_web_ui::kIndex, i);
44 }
45
46 results.Set(physical_web_ui::kMetadata, metadata.release());
47
48 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will
49 // create a list view with an item for each URL.
50 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results);
51 }
52
53 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
54 const base::ListValue* args) {
55 int index = 0;
56 if (!args->GetInteger(0, &index)) {
57 DLOG(ERROR) << "Invalid selection index";
58 return;
59 }
60
61 // Record the index of the selected item.
62 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
63 50);
64
65 // Count the number of selections.
66 base::RecordAction(
67 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
68 }
69
70 } // namespace physical_web_ui
OLDNEW
« 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