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

Side by Side Diff: components/physical_web/webui/physical_web_base_message_handler.cc

Issue 2741823002: Update Physical Web WebUI to show new results automatically (Closed)
Patch Set: fix test Created 3 years, 8 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/physical_web/webui/physical_web_base_message_handler.h" 5 #include "components/physical_web/webui/physical_web_base_message_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/metrics/histogram_macros.h" 9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/user_metrics.h" 10 #include "base/metrics/user_metrics.h"
11 #include "components/physical_web/data_source/physical_web_data_source.h" 11 #include "components/physical_web/data_source/physical_web_data_source.h"
12 #include "components/physical_web/webui/physical_web_ui_constants.h" 12 #include "components/physical_web/webui/physical_web_ui_constants.h"
13 13
14 namespace physical_web_ui { 14 namespace physical_web_ui {
15 15
16 PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {} 16 PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler()
17 : data_source_(nullptr) {}
17 18
18 PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default; 19 PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() {
20 if (data_source_)
21 data_source_->UnregisterListener(this);
22 }
23
24 void PhysicalWebBaseMessageHandler::OnFound(const GURL& url) {
25 PushNearbyURLs();
26 }
27
28 void PhysicalWebBaseMessageHandler::OnLost(const GURL& url) {
29 // do nothing
30 }
31
32 void PhysicalWebBaseMessageHandler::OnDistanceChanged(
33 const GURL& url,
34 double distance_estimate) {
35 // do nothing
36 }
19 37
20 void PhysicalWebBaseMessageHandler::RegisterMessages() { 38 void PhysicalWebBaseMessageHandler::RegisterMessages() {
21 RegisterMessageCallback( 39 RegisterMessageCallback(
22 kRequestNearbyUrls, 40 kPhysicalWebPageLoaded,
23 base::BindRepeating( 41 base::BindRepeating(
24 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, 42 &PhysicalWebBaseMessageHandler::HandlePhysicalWebPageLoaded,
25 base::Unretained(this))); 43 base::Unretained(this)));
26 RegisterMessageCallback(kPhysicalWebItemClicked, 44
45 RegisterMessageCallback(
46 kPhysicalWebItemClicked,
27 base::BindRepeating( 47 base::BindRepeating(
28 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, 48 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
29 base::Unretained(this))); 49 base::Unretained(this)));
50
51 data_source_ = GetPhysicalWebDataSource();
52 if (data_source_)
53 data_source_->RegisterListener(this, physical_web::OPPORTUNISTIC);
30 } 54 }
31 55
32 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( 56 void PhysicalWebBaseMessageHandler::PushNearbyURLs() {
33 const base::ListValue* args) {
34 base::DictionaryValue results; 57 base::DictionaryValue results;
35 58
36 std::unique_ptr<physical_web::MetadataList> metadata_list = 59 std::unique_ptr<physical_web::MetadataList> metadata_list =
37 GetPhysicalWebDataSource()->GetMetadataList(); 60 GetPhysicalWebDataSource()->GetMetadataList();
38 61
62 // Append new metadata at the end of the list.
63 for (const auto& metadata_list_item : *metadata_list) {
64 const std::string& group_id = metadata_list_item.group_id;
65 if (metadata_map_.find(group_id) == metadata_map_.end()) {
66 ordered_group_ids_.push_back(group_id);
67 metadata_map_.insert(std::make_pair(group_id, metadata_list_item));
68 }
69 }
70
39 auto metadata = base::MakeUnique<base::ListValue>(); 71 auto metadata = base::MakeUnique<base::ListValue>();
40 int index = 0; 72 int index = 0;
41 for (const auto& metadata_list_item : *metadata_list) { 73 for (const auto& group_id : ordered_group_ids_) {
74 auto metadata_list_item = metadata_map_[group_id];
42 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); 75 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
43 metadata_item->SetString(physical_web_ui::kResolvedUrl, 76 metadata_item->SetString(physical_web_ui::kResolvedUrl,
44 metadata_list_item.resolved_url.spec()); 77 metadata_list_item.resolved_url.spec());
45 metadata_item->SetString(physical_web_ui::kPageInfoIcon, 78 metadata_item->SetString(physical_web_ui::kPageInfoIcon,
46 metadata_list_item.icon_url.spec()); 79 metadata_list_item.icon_url.spec());
47 metadata_item->SetString(physical_web_ui::kPageInfoTitle, 80 metadata_item->SetString(physical_web_ui::kPageInfoTitle,
48 metadata_list_item.title); 81 metadata_list_item.title);
49 metadata_item->SetString(physical_web_ui::kPageInfoDescription, 82 metadata_item->SetString(physical_web_ui::kPageInfoDescription,
50 metadata_list_item.description); 83 metadata_list_item.description);
51 // Add the item index so when an item is selected, the index can be recorded 84 // Add the item index so when an item is selected, the index can be recorded
52 // in a UMA histogram. 85 // in a UMA histogram.
53 metadata_item->SetInteger(physical_web_ui::kIndex, index); 86 metadata_item->SetInteger(physical_web_ui::kIndex, index);
54 metadata->Append(std::move(metadata_item)); 87 metadata->Append(std::move(metadata_item));
55 ++index; 88 ++index;
56 } 89 }
57 90
58 results.Set(physical_web_ui::kMetadata, metadata.release()); 91 results.Set(physical_web_ui::kMetadata, metadata.release());
59 92
60 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay",
61 (int)metadata_list->size(), 50);
62
63 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will 93 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will
64 // create a list view with an item for each URL. 94 // create a list view with an item for each URL.
65 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); 95 CallJavaScriptFunction(physical_web_ui::kPushNearbyUrls, results);
96 }
97
98 void PhysicalWebBaseMessageHandler::HandlePhysicalWebPageLoaded(
99 const base::ListValue* args) {
100 PushNearbyURLs();
101 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay",
102 (int)ordered_group_ids_.size(), 50);
66 } 103 }
67 104
68 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( 105 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
69 const base::ListValue* args) { 106 const base::ListValue* args) {
70 int index = 0; 107 int index = 0;
71 if (!args->GetInteger(0, &index)) { 108 if (!args->GetInteger(0, &index)) {
72 DLOG(ERROR) << "Invalid selection index"; 109 DLOG(ERROR) << "Invalid selection index";
73 return; 110 return;
74 } 111 }
75 112
76 // Record the index of the selected item. 113 // Record the index of the selected item.
77 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, 114 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
78 50); 115 50);
79 116
80 // Count the number of selections. 117 // Count the number of selections.
81 base::RecordAction( 118 base::RecordAction(
82 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); 119 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
83 } 120 }
84 121
85 } // namespace physical_web_ui 122 } // namespace physical_web_ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698