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

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: rename requestNearbyUrls to physicalWebPageLoaded 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 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 ReturnNearbyURLs(nullptr);
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() {
39 base::Callback<void(const base::ListValue* args)> PhysicalWebPageLoadedCb =
40 base::BindRepeating(
41 [](PhysicalWebBaseMessageHandler* current,
42 const base::ListValue* args) {
43 current->ReturnNearbyURLs(nullptr);
44 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay",
45 (int)current->GetPhysicalWebDataSource()
46 ->GetMetadataList()
47 ->size(),
48 50);
49 },
50 this);
51 RegisterMessageCallback(kPhysicalWebPageLoaded, PhysicalWebPageLoadedCb);
mmocny 2017/03/24 17:15:12 I think you should create a member function called
Ran 2017/03/24 18:45:19 Done.
52
21 RegisterMessageCallback( 53 RegisterMessageCallback(
22 kRequestNearbyUrls, 54 kPhysicalWebItemClicked,
23 base::BindRepeating(
24 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs,
25 base::Unretained(this)));
26 RegisterMessageCallback(kPhysicalWebItemClicked,
27 base::BindRepeating( 55 base::BindRepeating(
28 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, 56 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
29 base::Unretained(this))); 57 base::Unretained(this)));
58
59 data_source_ = GetPhysicalWebDataSource();
60 if (data_source_)
61 data_source_->RegisterListener(this);
mmocny 2017/03/24 17:15:12 Should we perhaps do this in the constructor inste
Ran 2017/03/24 18:45:19 Can't call it in constructor as GetPhysicalWebData
mmocny 2017/03/24 18:54:14 Okay fair enough. We could create a "start" metho
mattreynolds 2017/03/27 21:46:59 The name comes from the web_ui message handler whi
30 } 62 }
31 63
32 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( 64 void PhysicalWebBaseMessageHandler::ReturnNearbyURLs(
33 const base::ListValue* args) { 65 const base::ListValue* args) {
34 base::DictionaryValue results; 66 base::DictionaryValue results;
35 67
36 std::unique_ptr<physical_web::MetadataList> metadata_list = 68 std::unique_ptr<physical_web::MetadataList> metadata_list =
37 GetPhysicalWebDataSource()->GetMetadataList(); 69 GetPhysicalWebDataSource()->GetMetadataList();
38 70
71 // Append new metadata at the end of the list.
72 for (const auto& metadata_list_item : *metadata_list) {
73 const std::string& group_id = metadata_list_item.group_id;
74 if (metadata_map_.find(group_id) == metadata_map_.end()) {
75 ordered_group_ids_.push_back(group_id);
76 metadata_map_.emplace(group_id, metadata_list_item);
77 }
78 }
79
39 auto metadata = base::MakeUnique<base::ListValue>(); 80 auto metadata = base::MakeUnique<base::ListValue>();
40 int index = 0; 81 int index = 0;
41 for (const auto& metadata_list_item : *metadata_list) { 82 for (const auto& group_id : ordered_group_ids_) {
83 auto metadata_list_item = metadata_map_[group_id];
42 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); 84 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
43 metadata_item->SetString(physical_web_ui::kResolvedUrl, 85 metadata_item->SetString(physical_web_ui::kResolvedUrl,
44 metadata_list_item.resolved_url.spec()); 86 metadata_list_item.resolved_url.spec());
45 metadata_item->SetString(physical_web_ui::kPageInfoIcon, 87 metadata_item->SetString(physical_web_ui::kPageInfoIcon,
46 metadata_list_item.icon_url.spec()); 88 metadata_list_item.icon_url.spec());
47 metadata_item->SetString(physical_web_ui::kPageInfoTitle, 89 metadata_item->SetString(physical_web_ui::kPageInfoTitle,
48 metadata_list_item.title); 90 metadata_list_item.title);
49 metadata_item->SetString(physical_web_ui::kPageInfoDescription, 91 metadata_item->SetString(physical_web_ui::kPageInfoDescription,
50 metadata_list_item.description); 92 metadata_list_item.description);
51 // Add the item index so when an item is selected, the index can be recorded 93 // Add the item index so when an item is selected, the index can be recorded
52 // in a UMA histogram. 94 // in a UMA histogram.
53 metadata_item->SetInteger(physical_web_ui::kIndex, index); 95 metadata_item->SetInteger(physical_web_ui::kIndex, index);
54 metadata->Append(std::move(metadata_item)); 96 metadata->Append(std::move(metadata_item));
55 ++index; 97 ++index;
56 } 98 }
57 99
58 results.Set(physical_web_ui::kMetadata, metadata.release()); 100 results.Set(physical_web_ui::kMetadata, metadata.release());
59 101
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 102 // 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. 103 // create a list view with an item for each URL.
65 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); 104 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results);
66 } 105 }
67 106
68 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( 107 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
69 const base::ListValue* args) { 108 const base::ListValue* args) {
70 int index = 0; 109 int index = 0;
71 if (!args->GetInteger(0, &index)) { 110 if (!args->GetInteger(0, &index)) {
72 DLOG(ERROR) << "Invalid selection index"; 111 DLOG(ERROR) << "Invalid selection index";
73 return; 112 return;
74 } 113 }
75 114
76 // Record the index of the selected item. 115 // Record the index of the selected item.
77 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, 116 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
78 50); 117 50);
79 118
80 // Count the number of selections. 119 // Count the number of selections.
81 base::RecordAction( 120 base::RecordAction(
82 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); 121 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
83 } 122 }
84 123
85 } // namespace physical_web_ui 124 } // namespace physical_web_ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698