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

Side by Side 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 3 years, 12 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 "ios/chrome/browser/ui/webui/physical_web_ui.h" 5 #include "ios/chrome/browser/ui/webui/physical_web_ui.h"
6 6
7 #include "base/bind.h"
8 #include "base/macros.h" 7 #include "base/macros.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/metrics/user_metrics.h" 8 #include "base/metrics/user_metrics.h"
11 #include "base/values.h"
12 #include "components/grit/components_resources.h" 9 #include "components/grit/components_resources.h"
13 #include "components/physical_web/data_source/physical_web_data_source.h" 10 #include "components/physical_web/data_source/physical_web_data_source.h"
14 #include "components/physical_web/webui/physical_web_ui_constants.h" 11 #include "components/physical_web/webui/physical_web_ui_constants.h"
15 #include "components/strings/grit/components_strings.h" 12 #include "components/strings/grit/components_strings.h"
16 #include "ios/chrome/browser/application_context.h" 13 #include "ios/chrome/browser/application_context.h"
17 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" 14 #include "ios/chrome/browser/browser_state/chrome_browser_state.h"
18 #include "ios/chrome/browser/chrome_url_constants.h" 15 #include "ios/chrome/browser/chrome_url_constants.h"
19 #include "ios/web/public/web_ui_ios_data_source.h" 16 #include "ios/web/public/web_ui_ios_data_source.h"
20 #include "ios/web/public/webui/web_ui_ios.h" 17 #include "ios/web/public/webui/web_ui_ios.h"
21 #include "ios/web/public/webui/web_ui_ios_message_handler.h" 18 #include "ios/web/public/webui/web_ui_ios_message_handler.h"
22 19
23 namespace { 20 namespace {
24 21
25 // Maximum value for the "selected position" histogram.
26 const int kSelectedPositionMaxValue = 50;
27
28 web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() { 22 web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() {
29 web::WebUIIOSDataSource* html_source = 23 web::WebUIIOSDataSource* html_source =
30 web::WebUIIOSDataSource::Create(kChromeUIPhysicalWebHost); 24 web::WebUIIOSDataSource::Create(kChromeUIPhysicalWebHost);
31 25
32 // Localized and data strings. 26 // Localized and data strings.
33 html_source->AddLocalizedString(physical_web_ui::kTitle, 27 html_source->AddLocalizedString(physical_web_ui::kTitle,
34 IDS_PHYSICAL_WEB_UI_TITLE); 28 IDS_PHYSICAL_WEB_UI_TITLE);
35 29
36 html_source->SetJsonPath("strings.js"); 30 html_source->SetJsonPath("strings.js");
37 html_source->AddResourcePath(physical_web_ui::kPhysicalWebJS, 31 html_source->AddResourcePath(physical_web_ui::kPhysicalWebJS,
38 IDR_PHYSICAL_WEB_UI_JS); 32 IDR_PHYSICAL_WEB_UI_JS);
39 html_source->AddResourcePath(physical_web_ui::kPhysicalWebCSS, 33 html_source->AddResourcePath(physical_web_ui::kPhysicalWebCSS,
40 IDR_PHYSICAL_WEB_UI_CSS); 34 IDR_PHYSICAL_WEB_UI_CSS);
41 html_source->SetDefaultResource(IDR_PHYSICAL_WEB_UI_HTML); 35 html_source->SetDefaultResource(IDR_PHYSICAL_WEB_UI_HTML);
42 return html_source; 36 return html_source;
43 } 37 }
44 38
39 // 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
40 // functionality. This is extends the PhysicalWebBaseMessageHandler and
41 // 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.
42 class MessageHandlerImpl
43 : public physical_web_ui::PhysicalWebBaseMessageHandler {
44 public:
45 explicit MessageHandlerImpl(WebUIIOS* web_ui) : web_ui_(web_ui) {}
46 ~MessageHandlerImpl() override {}
47
48 private:
49 void RegisterMessageCallback(
50 const std::string& message,
51 const content::WebUI::MessageCallback& callback) override {
52 web_ui_->RegisterMessageCallback(message, callback);
53 }
54 void CallJavaScriptFunction(const std::string& function,
55 const base::Value& arg) override {
56 web_ui_->CallJavascriptFunction(function, arg);
57 }
58 physical_web::PhysicalWebDataSource* const GetPhysicalWebDataSource()
59 override {
60 return GetApplicationContext()->GetPhysicalWebDataSource();
61 }
62
63 WebUIIOS* web_ui_;
64 DISALLOW_COPY_AND_ASSIGN(MessageHandlerImpl);
65 }
66
45 //////////////////////////////////////////////////////////////////////////////// 67 ////////////////////////////////////////////////////////////////////////////////
46 // 68 //
47 // PhysicalWebDOMHandler 69 // PhysicalWebDOMHandler
48 // 70 //
49 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
50 72
51 // The handler for Javascript messages for the chrome:physical-web page. 73 // The handler for Javascript messages for the chrome:physical-web page.
52 class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler { 74 class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler {
53 public: 75 public:
54 PhysicalWebDOMHandler() {} 76 PhysicalWebDOMHandler() : impl_(web_ui()) {}
55 ~PhysicalWebDOMHandler() override {} 77 ~PhysicalWebDOMHandler() override {}
56 78
57 void RegisterMessages() override; 79 void RegisterMessages() override { impl_.RegisterMessages(); }
58
59 void HandleRequestNearbyURLs(const base::ListValue* args);
60 void HandlePhysicalWebItemClicked(const base::ListValue* args);
61 80
62 private: 81 private:
82 MessageHandlerImpl impl_;
63 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); 83 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler);
64 }; 84 };
65 85
66 void PhysicalWebDOMHandler::RegisterMessages() {
67 web_ui()->RegisterMessageCallback(
68 physical_web_ui::kRequestNearbyUrls,
69 base::Bind(&PhysicalWebDOMHandler::HandleRequestNearbyURLs,
70 base::Unretained(this)));
71 web_ui()->RegisterMessageCallback(
72 physical_web_ui::kPhysicalWebItemClicked,
73 base::Bind(&PhysicalWebDOMHandler::HandlePhysicalWebItemClicked,
74 base::Unretained(this)));
75 }
76
77 void PhysicalWebDOMHandler::HandleRequestNearbyURLs(
78 const base::ListValue* args) {
79 base::DictionaryValue results;
80
81 std::unique_ptr<base::ListValue> metadata =
82 GetApplicationContext()->GetPhysicalWebDataSource()->GetMetadata();
83
84 // Add item indices. When an item is selected, the index of the selected item
85 // is recorded in a UMA histogram.
86 for (size_t i = 0; i < metadata->GetSize(); i++) {
87 base::DictionaryValue* metadata_item = NULL;
88 metadata->GetDictionary(i, &metadata_item);
89 metadata_item->SetInteger(physical_web_ui::kIndex, i);
90 }
91
92 results.Set(physical_web_ui::kMetadata, metadata.release());
93
94 // Pass the list of Physical Web URL metadata to the WebUI. A jstemplate will
95 // create a list view with an item for each URL.
96 web_ui()->CallJavascriptFunction(physical_web_ui::kReturnNearbyUrls, results);
97 }
98
99 void PhysicalWebDOMHandler::HandlePhysicalWebItemClicked(
100 const base::ListValue* args) {
101 int index;
102 if (!args->GetInteger(0, &index)) {
103 DLOG(ERROR) << "Invalid selection index";
104 return;
105 }
106
107 // 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
108 // the maximum enumeration value.
109 if (index > kSelectedPositionMaxValue) {
110 index = kSelectedPositionMaxValue;
111 }
112 UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index,
113 kSelectedPositionMaxValue);
114
115 // Count the number of selections.
116 base::RecordAction(
117 base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected"));
118 }
119
120 } // namespace 86 } // namespace
121 87
122 //////////////////////////////////////////////////////////////////////////////// 88 ////////////////////////////////////////////////////////////////////////////////
123 // 89 //
124 // PhysicalWebUI 90 // PhysicalWebUI
125 // 91 //
126 //////////////////////////////////////////////////////////////////////////////// 92 ////////////////////////////////////////////////////////////////////////////////
127 93
128 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) 94 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui)
129 : web::WebUIIOSController(web_ui) { 95 : web::WebUIIOSController(web_ui) {
130 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); 96 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler();
131 web_ui->AddMessageHandler(handler); 97 web_ui->AddMessageHandler(handler);
132 98
133 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), 99 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui),
134 CreatePhysicalWebUIDataSource()); 100 CreatePhysicalWebUIDataSource());
135 101
136 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); 102 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open"));
137 } 103 }
138 104
139 PhysicalWebUI::~PhysicalWebUI() {} 105 PhysicalWebUI::~PhysicalWebUI() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698