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

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: Restore url_constants.h include 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 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
45 //////////////////////////////////////////////////////////////////////////////// 39 ////////////////////////////////////////////////////////////////////////////////
46 // 40 //
47 // PhysicalWebDOMHandler 41 // PhysicalWebDOMHandler
48 // 42 //
49 //////////////////////////////////////////////////////////////////////////////// 43 ////////////////////////////////////////////////////////////////////////////////
50 44
51 // The handler for Javascript messages for the chrome:physical-web page. 45 // The handler for Javascript messages for the chrome:physical-web page.
52 class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler { 46 class PhysicalWebDOMHandler
47 : public web::WebUIIOSMessageHandler,
48 public physical_web_ui::PhysicalWebBaseMessageHandler {
mattreynolds 2016/12/13 22:40:13 Multiple inheritance is allowed only when all supe
cco3 2016/12/21 18:06:58 Done.
53 public: 49 public:
54 PhysicalWebDOMHandler() {} 50 PhysicalWebDOMHandler() {}
55 ~PhysicalWebDOMHandler() override {} 51 ~PhysicalWebDOMHandler() override {}
56 52
57 void RegisterMessages() override; 53 void RegisterMessages() override { RegisterMessagesImpl(); }
58
59 void HandleRequestNearbyURLs(const base::ListValue* args);
60 void HandlePhysicalWebItemClicked(const base::ListValue* args);
61 54
62 private: 55 private:
56 void RegisterMessageCallback(
57 const std::string& message,
58 const content::WebUI::MessageCallback& callback) override {
59 web_ui()->RegisterMessageCallback(message, callback);
60 }
61 void CallJavaScriptFunction(const std::string& function,
62 const base::Value& arg) override {
63 web_ui()->CallJavascriptFunctionUnsafe(function, arg);
mattreynolds 2016/12/13 22:40:13 Why does this need to call the Unsafe version?
cco3 2016/12/21 18:06:58 Done.
64 }
65 physical_web::PhysicalWebDataSource* GetPhysicalWebDataSource() override {
66 return GetApplicationContext()->GetPhysicalWebDataSource();
67 }
68
63 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); 69 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler);
64 }; 70 };
65 71
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
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 72 } // namespace
121 73
122 //////////////////////////////////////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////////
123 // 75 //
124 // PhysicalWebUI 76 // PhysicalWebUI
125 // 77 //
126 //////////////////////////////////////////////////////////////////////////////// 78 ////////////////////////////////////////////////////////////////////////////////
127 79
128 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) 80 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui)
129 : web::WebUIIOSController(web_ui) { 81 : web::WebUIIOSController(web_ui) {
130 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); 82 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler();
131 web_ui->AddMessageHandler(handler); 83 web_ui->AddMessageHandler(handler);
132 84
133 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), 85 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui),
134 CreatePhysicalWebUIDataSource()); 86 CreatePhysicalWebUIDataSource());
135 87
136 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); 88 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open"));
137 } 89 }
138 90
139 PhysicalWebUI::~PhysicalWebUI() {} 91 PhysicalWebUI::~PhysicalWebUI() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698