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

Unified Diff: ios/chrome/browser/ui/webui/physical_web_ui.cc

Issue 2571853003: Move PW message handler logic to components (Closed)
Patch Set: Address style/syntax comments Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: ios/chrome/browser/ui/webui/physical_web_ui.cc
diff --git a/ios/chrome/browser/ui/webui/physical_web_ui.cc b/ios/chrome/browser/ui/webui/physical_web_ui.cc
index b7423277307c5f04a07695de39dd688c07256095..4759dd7e6b66de5fa429a215ae405b130c754a28 100644
--- a/ios/chrome/browser/ui/webui/physical_web_ui.cc
+++ b/ios/chrome/browser/ui/webui/physical_web_ui.cc
@@ -4,11 +4,8 @@
#include "ios/chrome/browser/ui/webui/physical_web_ui.h"
-#include "base/bind.h"
#include "base/macros.h"
-#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
-#include "base/values.h"
#include "components/grit/components_resources.h"
#include "components/physical_web/data_source/physical_web_data_source.h"
#include "components/physical_web/webui/physical_web_ui_constants.h"
@@ -22,9 +19,6 @@
namespace {
-// Maximum value for the "selected position" histogram.
-const int kSelectedPositionMaxValue = 50;
-
web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() {
web::WebUIIOSDataSource* html_source =
web::WebUIIOSDataSource::Create(kChromeUIPhysicalWebHost);
@@ -42,6 +36,34 @@ web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() {
return html_source;
}
+// The mixin for the MessageHandler that implements all MessageHandler
Eugene But (OOO till 7-30) 2016/12/22 21:26:33 nit: Is this actually mix-in? C++ mix-ins are usua
cco3 2016/12/22 22:07:07 Sorry, I wasn't sure if that term translated over
+// functionality. This is extends the PhysicalWebBaseMessageHandler and
+// implements functions to manipulate an iOS-specific WebUi object.
Eugene But (OOO till 7-30) 2016/12/22 21:26:33 nit: s/WebUi/WebUI
cco3 2016/12/22 22:07:07 Done.
+class MessageHandlerImpl
+ : public physical_web_ui::PhysicalWebBaseMessageHandler {
+ public:
+ explicit MessageHandlerImpl(WebUIIOS* web_ui) : web_ui_(web_ui) {}
+ ~MessageHandlerImpl() override {}
+
+ private:
+ void RegisterMessageCallback(
+ const std::string& message,
+ const content::WebUI::MessageCallback& callback) override {
+ web_ui_->RegisterMessageCallback(message, callback);
+ }
+ void CallJavaScriptFunction(const std::string& function,
+ const base::Value& arg) override {
+ web_ui_->CallJavascriptFunction(function, arg);
+ }
+ physical_web::PhysicalWebDataSource* const GetPhysicalWebDataSource()
+ override {
+ return GetApplicationContext()->GetPhysicalWebDataSource();
+ }
+
+ WebUIIOS* web_ui_;
+ DISALLOW_COPY_AND_ASSIGN(MessageHandlerImpl);
+}
+
////////////////////////////////////////////////////////////////////////////////
//
// PhysicalWebDOMHandler
@@ -51,72 +73,16 @@ web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() {
// The handler for Javascript messages for the chrome:physical-web page.
class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler {
public:
- PhysicalWebDOMHandler() {}
+ PhysicalWebDOMHandler() : impl_(web_ui()) {}
~PhysicalWebDOMHandler() override {}
- void RegisterMessages() override;
-
- void HandleRequestNearbyURLs(const base::ListValue* args);
- void HandlePhysicalWebItemClicked(const base::ListValue* args);
+ void RegisterMessages() override { impl_.RegisterMessages(); }
private:
+ MessageHandlerImpl impl_;
DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler);
};
-void PhysicalWebDOMHandler::RegisterMessages() {
- web_ui()->RegisterMessageCallback(
- physical_web_ui::kRequestNearbyUrls,
- base::Bind(&PhysicalWebDOMHandler::HandleRequestNearbyURLs,
- base::Unretained(this)));
- web_ui()->RegisterMessageCallback(
- physical_web_ui::kPhysicalWebItemClicked,
- base::Bind(&PhysicalWebDOMHandler::HandlePhysicalWebItemClicked,
- base::Unretained(this)));
-}
-
-void PhysicalWebDOMHandler::HandleRequestNearbyURLs(
- const base::ListValue* args) {
- base::DictionaryValue results;
-
- std::unique_ptr<base::ListValue> metadata =
- GetApplicationContext()->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.
- web_ui()->CallJavascriptFunction(physical_web_ui::kReturnNearbyUrls, results);
-}
-
-void PhysicalWebDOMHandler::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
Eugene But (OOO till 7-30) 2016/12/22 21:26:33 This code was removed. Is it ok?
cco3 2016/12/22 22:07:07 Good catch, but yes. Matt made a comment on an ea
- // the maximum enumeration value.
- if (index > kSelectedPositionMaxValue) {
- index = kSelectedPositionMaxValue;
- }
- UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
- kSelectedPositionMaxValue);
-
- // Count the number of selections.
- base::RecordAction(
- base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
-}
-
} // namespace
////////////////////////////////////////////////////////////////////////////////

Powered by Google App Engine
This is Rietveld 408576698