Chromium Code Reviews| Index: components/physical_web/webui/physical_web_base_message_handler.cc |
| diff --git a/components/physical_web/webui/physical_web_base_message_handler.cc b/components/physical_web/webui/physical_web_base_message_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7208b8494e4d430a1b508d251bb1ed422b011da7 |
| --- /dev/null |
| +++ b/components/physical_web/webui/physical_web_base_message_handler.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/physical_web/webui/physical_web_base_message_handler.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "base/metrics/user_metrics.h" |
| +#include "components/physical_web/data_source/physical_web_data_source.h" |
| +#include "components/physical_web/webui/physical_web_ui_constants.h" |
| + |
| +namespace { |
| + |
| +// Maximum value for the "selected position" histogram. |
| +const int kSelectedPositionMaxValue = 50; |
| + |
| +} // namespace |
| + |
| +namespace physical_web_ui { |
| + |
| +PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {} |
| + |
| +PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default; |
| + |
| +void PhysicalWebBaseMessageHandler::RegisterMessagesImpl() { |
| + RegisterMessageCallback( |
| + kRequestNearbyUrls, |
| + base::Bind(&PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, |
|
mattreynolds
2016/12/13 22:40:13
src/docs/callback.md suggests we should use base::
cco3
2016/12/21 18:06:58
Done.
|
| + base::Unretained(this))); |
| + RegisterMessageCallback(kPhysicalWebItemClicked, |
| + base::Bind(&PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( |
| + const base::ListValue* args) { |
| + base::DictionaryValue results; |
| + |
| + std::unique_ptr<base::ListValue> metadata = |
| + GetPhysicalWebDataSource()->GetMetadata(); |
| + |
| + // Add item indices. When an item is selected, the index of the selected item |
| + // is recorded in a UMA histogram. |
| + for (size_t i = 0; i < metadata->GetSize(); i++) { |
| + base::DictionaryValue* metadata_item = NULL; |
| + metadata->GetDictionary(i, &metadata_item); |
| + metadata_item->SetInteger(physical_web_ui::kIndex, i); |
| + } |
| + |
| + results.Set(physical_web_ui::kMetadata, metadata.release()); |
| + |
| + // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will |
| + // create a list view with an item for each URL. |
| + CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); |
| +} |
| + |
| +void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( |
| + const base::ListValue* args) { |
| + int index; |
| + if (!args->GetInteger(0, &index)) { |
| + DLOG(ERROR) << "Invalid selection index"; |
| + return; |
| + } |
| + |
| + // Record the index of the selected item. The index must be strictly less than |
| + // the maximum enumeration value. |
| + if (index > kSelectedPositionMaxValue) { |
|
mattreynolds
2016/12/13 22:40:13
Turns out this is incorrect, UMA_HISTOGRAM_EXACT_L
cco3
2016/12/21 18:06:58
Done.
|
| + index = kSelectedPositionMaxValue; |
| + } |
| + UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, |
| + kSelectedPositionMaxValue); |
|
mattreynolds
2016/12/13 22:40:13
Let's remove the kSelectedPositionMaxValue constan
cco3
2016/12/21 18:06:58
Done.
|
| + |
| + // Count the number of selections. |
| + base::RecordAction( |
| + base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); |
| +} |
| + |
| +} // namespace physical_web_ui |