| OLD | NEW |
| 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/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/metrics/user_metrics.h" | 10 #include "base/metrics/user_metrics.h" |
| 10 #include "components/physical_web/data_source/physical_web_data_source.h" | 11 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 11 #include "components/physical_web/webui/physical_web_ui_constants.h" | 12 #include "components/physical_web/webui/physical_web_ui_constants.h" |
| 12 | 13 |
| 13 namespace physical_web_ui { | 14 namespace physical_web_ui { |
| 14 | 15 |
| 15 PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {} | 16 PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {} |
| 16 | 17 |
| 17 PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default; | 18 PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default; |
| 18 | 19 |
| 19 void PhysicalWebBaseMessageHandler::RegisterMessages() { | 20 void PhysicalWebBaseMessageHandler::RegisterMessages() { |
| 20 RegisterMessageCallback( | 21 RegisterMessageCallback( |
| 21 kRequestNearbyUrls, | 22 kRequestNearbyUrls, |
| 22 base::BindRepeating( | 23 base::BindRepeating( |
| 23 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, | 24 &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, |
| 24 base::Unretained(this))); | 25 base::Unretained(this))); |
| 25 RegisterMessageCallback(kPhysicalWebItemClicked, | 26 RegisterMessageCallback(kPhysicalWebItemClicked, |
| 26 base::BindRepeating( | 27 base::BindRepeating( |
| 27 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, | 28 &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, |
| 28 base::Unretained(this))); | 29 base::Unretained(this))); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( | 32 void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( |
| 32 const base::ListValue* args) { | 33 const base::ListValue* args) { |
| 33 base::DictionaryValue results; | 34 base::DictionaryValue results; |
| 34 | 35 |
| 35 std::unique_ptr<base::ListValue> metadata = | 36 std::unique_ptr<physical_web::MetadataList> metadata_list = |
| 36 GetPhysicalWebDataSource()->GetMetadata(); | 37 GetPhysicalWebDataSource()->GetMetadataList(); |
| 37 | 38 |
| 38 // Add item indices. When an item is selected, the index of the selected item | 39 auto metadata = base::MakeUnique<base::ListValue>(); |
| 39 // is recorded in a UMA histogram. | 40 int index = 0; |
| 40 for (size_t i = 0; i < metadata->GetSize(); i++) { | 41 for (const auto& metadata_list_item : *metadata_list) { |
| 41 base::DictionaryValue* metadata_item = nullptr; | 42 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); |
| 42 metadata->GetDictionary(i, &metadata_item); | 43 metadata_item->SetString(physical_web_ui::kResolvedUrl, |
| 43 metadata_item->SetInteger(physical_web_ui::kIndex, static_cast<int>(i)); | 44 metadata_list_item.resolved_url.spec()); |
| 45 metadata_item->SetString(physical_web_ui::kPageInfoIcon, |
| 46 metadata_list_item.icon_url.spec()); |
| 47 metadata_item->SetString(physical_web_ui::kPageInfoTitle, |
| 48 metadata_list_item.title); |
| 49 metadata_item->SetString(physical_web_ui::kPageInfoDescription, |
| 50 metadata_list_item.description); |
| 51 // Add the item index so when an item is selected, the index can be recorded |
| 52 // in a UMA histogram. |
| 53 metadata_item->SetInteger(physical_web_ui::kIndex, index); |
| 54 metadata->Append(std::move(metadata_item)); |
| 55 ++index; |
| 44 } | 56 } |
| 45 | 57 |
| 46 results.Set(physical_web_ui::kMetadata, metadata.release()); | 58 results.Set(physical_web_ui::kMetadata, metadata.release()); |
| 47 | 59 |
| 48 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will | 60 // 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. | 61 // create a list view with an item for each URL. |
| 50 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); | 62 CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); |
| 51 } | 63 } |
| 52 | 64 |
| 53 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( | 65 void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( |
| 54 const base::ListValue* args) { | 66 const base::ListValue* args) { |
| 55 int index = 0; | 67 int index = 0; |
| 56 if (!args->GetInteger(0, &index)) { | 68 if (!args->GetInteger(0, &index)) { |
| 57 DLOG(ERROR) << "Invalid selection index"; | 69 DLOG(ERROR) << "Invalid selection index"; |
| 58 return; | 70 return; |
| 59 } | 71 } |
| 60 | 72 |
| 61 // Record the index of the selected item. | 73 // Record the index of the selected item. |
| 62 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, | 74 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, |
| 63 50); | 75 50); |
| 64 | 76 |
| 65 // Count the number of selections. | 77 // Count the number of selections. |
| 66 base::RecordAction( | 78 base::RecordAction( |
| 67 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); | 79 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); |
| 68 } | 80 } |
| 69 | 81 |
| 70 } // namespace physical_web_ui | 82 } // namespace physical_web_ui |
| OLD | NEW |