Chromium Code Reviews| 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 "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 class MessageHandlerImpl : | |
|
Eugene But (OOO till 7-30)
2016/12/22 16:09:22
What is the reason for creating this class here? I
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
Please add comment to this class.
cco3
2016/12/22 20:14:45
Done.
cco3
2016/12/22 20:14:45
Acknowledged.
| |
| 40 public physical_web_ui::PhysicalWebBaseMessageHandler { | |
| 41 public: | |
| 42 MessageHandlerImpl(WebUIIOS* web_ui) { | |
|
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
explicit
cco3
2016/12/22 20:14:45
Done.
| |
| 43 web_ui_ = web_ui; | |
|
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
Please use initialization list.
cco3
2016/12/22 20:14:45
Done.
| |
| 44 } | |
| 45 ~MessageHandlerImpl() override {} | |
| 46 | |
| 47 private | |
|
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
private:
cco3
2016/12/22 20:14:45
Done.
| |
| 48 void RegisterMessageCallback( | |
| 49 const std::string& message, | |
| 50 const content::WebUI::MessageCallback& callback) override { | |
| 51 web_ui_->RegisterMessageCallback(message, callback); | |
| 52 } | |
| 53 void CallJavaScriptFunction(const std::string& function, | |
| 54 const base::Value& arg) override { | |
| 55 web_ui_->CallJavascriptFunction(function, arg); | |
| 56 } | |
| 57 physical_web::PhysicalWebDataSource* GetPhysicalWebDataSource() override { | |
| 58 return GetApplicationContext()->GetPhysicalWebDataSource(); | |
| 59 } | |
| 60 | |
| 61 WebUIIOS* web_ui_; | |
| 62 DISALLOW_COPY_AND_ASSIGN(MessageHandlerImpl); | |
| 63 } | |
| 64 | |
| 45 //////////////////////////////////////////////////////////////////////////////// | 65 //////////////////////////////////////////////////////////////////////////////// |
| 46 // | 66 // |
| 47 // PhysicalWebDOMHandler | 67 // PhysicalWebDOMHandler |
| 48 // | 68 // |
| 49 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
| 50 | 70 |
| 51 // The handler for Javascript messages for the chrome:physical-web page. | 71 // The handler for Javascript messages for the chrome:physical-web page. |
| 52 class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler { | 72 class PhysicalWebDOMHandler : public web::WebUIIOSMessageHandler { |
|
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
s/{/,
cco3
2016/12/22 20:14:45
Done.
| |
| 73 public physical_web_ui::PhysicalWebBaseMessageHandler { | |
| 53 public: | 74 public: |
| 54 PhysicalWebDOMHandler() {} | 75 PhysicalWebDOMHandler() : impl(web_ui()) {} |
|
Eugene But (OOO till 7-30)
2016/12/22 19:04:47
You need an ivar for impl
cco3
2016/12/22 20:14:45
Done.
| |
| 55 ~PhysicalWebDOMHandler() override {} | 76 ~PhysicalWebDOMHandler() override {} |
| 56 | 77 |
| 57 void RegisterMessages() override; | 78 void RegisterMessages() override { impl.RegisterMessages(); } |
| 58 | |
| 59 void HandleRequestNearbyURLs(const base::ListValue* args); | |
| 60 void HandlePhysicalWebItemClicked(const base::ListValue* args); | |
| 61 | 79 |
| 62 private: | 80 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); | 81 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); |
| 64 }; | 82 }; |
| 65 | 83 |
| 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 | 84 } // namespace |
| 121 | 85 |
| 122 //////////////////////////////////////////////////////////////////////////////// | 86 //////////////////////////////////////////////////////////////////////////////// |
| 123 // | 87 // |
| 124 // PhysicalWebUI | 88 // PhysicalWebUI |
| 125 // | 89 // |
| 126 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
| 127 | 91 |
| 128 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) | 92 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) |
| 129 : web::WebUIIOSController(web_ui) { | 93 : web::WebUIIOSController(web_ui) { |
| 130 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); | 94 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); |
| 131 web_ui->AddMessageHandler(handler); | 95 web_ui->AddMessageHandler(handler); |
| 132 | 96 |
| 133 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), | 97 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), |
| 134 CreatePhysicalWebUIDataSource()); | 98 CreatePhysicalWebUIDataSource()); |
| 135 | 99 |
| 136 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); | 100 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); |
| 137 } | 101 } |
| 138 | 102 |
| 139 PhysicalWebUI::~PhysicalWebUI() {} | 103 PhysicalWebUI::~PhysicalWebUI() {} |
| OLD | NEW |