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

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: remove renderID 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 HandleRequestNearbyURLs(nullptr);
26 }
27
28 void PhysicalWebBaseMessageHandler::OnLost(const GURL& url) {
29 // do nothing
30 }
31
32 void PhysicalWebBaseMessageHandler::OnDistanceChanged(
33 const GURL& url, double distance_estimate) {
34 // do nothing
35 }
19 36
20 void PhysicalWebBaseMessageHandler::RegisterMessages() { 37 void PhysicalWebBaseMessageHandler::RegisterMessages() {
21 RegisterMessageCallback( 38 RegisterMessageCallback(
22 kRequestNearbyUrls, 39 kRequestNearbyUrls,
23 base::BindRepeating( 40 base::BindRepeating(
24 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, 41 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs,
25 base::Unretained(this))); 42 base::Unretained(this)));
26 RegisterMessageCallback(kPhysicalWebItemClicked, 43 RegisterMessageCallback(kPhysicalWebItemClicked,
27 base::BindRepeating( 44 base::BindRepeating(
28 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, 45 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked,
29 base::Unretained(this))); 46 base::Unretained(this)));
47
48 data_source_ = GetPhysicalWebDataSource();
49 if (data_source_)
50 data_source_->RegisterListener(this);
30 } 51 }
31 52
32 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( 53 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs(
33 const base::ListValue* args) { 54 const base::ListValue* args) {
34 base::DictionaryValue results; 55 base::DictionaryValue results;
35 56
36 std::unique_ptr<physical_web::MetadataList> metadata_list = 57 std::unique_ptr<physical_web::MetadataList> metadata_list =
37 GetPhysicalWebDataSource()->GetMetadataList(); 58 GetPhysicalWebDataSource()->GetMetadataList();
38 59
60 // Append new metadata at the end of the list.
61 for (const auto& metadata_list_item : *metadata_list) {
62 const std::string& group_id = metadata_list_item.group_id;
63 if (metadata_map_.find(group_id) == metadata_map_.end()) {
64 ordered_group_ids_.push_back(group_id);
65 metadata_map_.emplace(group_id, metadata_list_item);
66 }
67 }
68
39 auto metadata = base::MakeUnique<base::ListValue>(); 69 auto metadata = base::MakeUnique<base::ListValue>();
40 int index = 0; 70 int index = 0;
41 for (const auto& metadata_list_item : *metadata_list) { 71 for (const auto& group_id : ordered_group_ids_) {
72 auto metadata_list_item = metadata_map_[group_id];
42 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); 73 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
43 metadata_item->SetString(physical_web_ui::kResolvedUrl, 74 metadata_item->SetString(physical_web_ui::kResolvedUrl,
44 metadata_list_item.resolved_url.spec()); 75 metadata_list_item.resolved_url.spec());
45 metadata_item->SetString(physical_web_ui::kPageInfoIcon, 76 metadata_item->SetString(physical_web_ui::kPageInfoIcon,
46 metadata_list_item.icon_url.spec()); 77 metadata_list_item.icon_url.spec());
47 metadata_item->SetString(physical_web_ui::kPageInfoTitle, 78 metadata_item->SetString(physical_web_ui::kPageInfoTitle,
48 metadata_list_item.title); 79 metadata_list_item.title);
49 metadata_item->SetString(physical_web_ui::kPageInfoDescription, 80 metadata_item->SetString(physical_web_ui::kPageInfoDescription,
50 metadata_list_item.description); 81 metadata_list_item.description);
51 // Add the item index so when an item is selected, the index can be recorded 82 // Add the item index so when an item is selected, the index can be recorded
52 // in a UMA histogram. 83 // in a UMA histogram.
53 metadata_item->SetInteger(physical_web_ui::kIndex, index); 84 metadata_item->SetInteger(physical_web_ui::kIndex, index);
54 metadata->Append(std::move(metadata_item)); 85 metadata->Append(std::move(metadata_item));
55 ++index; 86 ++index;
56 } 87 }
57 88
58 results.Set(physical_web_ui::kMetadata, metadata.release()); 89 results.Set(physical_web_ui::kMetadata, metadata.release());
59 90
60 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay", 91 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.TotalUrls.OnInitialDisplay",
mmocny 2017/03/17 19:17:48 Just noticed, this is a bug now that we update res
61 (int)metadata_list->size(), 50); 92 (int)metadata_list->size(), 50);
62 93
63 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will 94 // 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. 95 // create a list view with an item for each URL.
65 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); 96 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results);
66 } 97 }
67 98
68 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( 99 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked(
69 const base::ListValue* args) { 100 const base::ListValue* args) {
70 int index = 0; 101 int index = 0;
71 if (!args->GetInteger(0, &index)) { 102 if (!args->GetInteger(0, &index)) {
72 DLOG(ERROR) << "Invalid selection index"; 103 DLOG(ERROR) << "Invalid selection index";
73 return; 104 return;
74 } 105 }
75 106
76 // Record the index of the selected item. 107 // Record the index of the selected item.
77 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, 108 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
78 50); 109 50);
79 110
80 // Count the number of selections. 111 // Count the number of selections.
81 base::RecordAction( 112 base::RecordAction(
82 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); 113 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
83 } 114 }
84 115
85 } // namespace physical_web_ui 116 } // namespace physical_web_ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698