| 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" |
| 11 #include "components/physical_web/webui/physical_web_base_message_handler.h" |
| 14 #include "components/physical_web/webui/physical_web_ui_constants.h" | 12 #include "components/physical_web/webui/physical_web_ui_constants.h" |
| 15 #include "components/strings/grit/components_strings.h" | 13 #include "components/strings/grit/components_strings.h" |
| 16 #include "ios/chrome/browser/application_context.h" | 14 #include "ios/chrome/browser/application_context.h" |
| 17 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 15 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 18 #include "ios/chrome/browser/chrome_url_constants.h" | 16 #include "ios/chrome/browser/chrome_url_constants.h" |
| 19 #include "ios/web/public/web_ui_ios_data_source.h" | 17 #include "ios/web/public/web_ui_ios_data_source.h" |
| 20 #include "ios/web/public/webui/web_ui_ios.h" | 18 #include "ios/web/public/webui/web_ui_ios.h" |
| 21 #include "ios/web/public/webui/web_ui_ios_message_handler.h" | 19 #include "ios/web/public/webui/web_ui_ios_message_handler.h" |
| 22 | 20 |
| 23 namespace { | 21 namespace { |
| 24 | 22 |
| 25 // Maximum value for the "selected position" histogram. | |
| 26 const int kSelectedPositionMaxValue = 50; | |
| 27 | |
| 28 web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() { | 23 web::WebUIIOSDataSource* CreatePhysicalWebUIDataSource() { |
| 29 web::WebUIIOSDataSource* html_source = | 24 web::WebUIIOSDataSource* html_source = |
| 30 web::WebUIIOSDataSource::Create(kChromeUIPhysicalWebHost); | 25 web::WebUIIOSDataSource::Create(kChromeUIPhysicalWebHost); |
| 31 | 26 |
| 32 // Localized and data strings. | 27 // Localized and data strings. |
| 33 html_source->AddLocalizedString(physical_web_ui::kTitle, | 28 html_source->AddLocalizedString(physical_web_ui::kTitle, |
| 34 IDS_PHYSICAL_WEB_UI_TITLE); | 29 IDS_PHYSICAL_WEB_UI_TITLE); |
| 35 | 30 |
| 36 html_source->SetJsonPath("strings.js"); | 31 html_source->SetJsonPath("strings.js"); |
| 37 html_source->AddResourcePath(physical_web_ui::kPhysicalWebJS, | 32 html_source->AddResourcePath(physical_web_ui::kPhysicalWebJS, |
| 38 IDR_PHYSICAL_WEB_UI_JS); | 33 IDR_PHYSICAL_WEB_UI_JS); |
| 39 html_source->AddResourcePath(physical_web_ui::kPhysicalWebCSS, | 34 html_source->AddResourcePath(physical_web_ui::kPhysicalWebCSS, |
| 40 IDR_PHYSICAL_WEB_UI_CSS); | 35 IDR_PHYSICAL_WEB_UI_CSS); |
| 41 html_source->SetDefaultResource(IDR_PHYSICAL_WEB_UI_HTML); | 36 html_source->SetDefaultResource(IDR_PHYSICAL_WEB_UI_HTML); |
| 42 return html_source; | 37 return html_source; |
| 43 } | 38 } |
| 44 | 39 |
| 40 // Implements all MessageHandler core functionality. This is extends the |
| 41 // PhysicalWebBaseMessageHandler and implements functions to manipulate an |
| 42 // iOS-specific WebUI object. |
| 43 class MessageHandlerImpl |
| 44 : public physical_web_ui::PhysicalWebBaseMessageHandler { |
| 45 public: |
| 46 explicit MessageHandlerImpl(web::WebUIIOS* web_ui) : web_ui_(web_ui) {} |
| 47 ~MessageHandlerImpl() override {} |
| 48 |
| 49 private: |
| 50 void RegisterMessageCallback( |
| 51 const std::string& message, |
| 52 const physical_web_ui::MessageCallback& callback) override { |
| 53 web_ui_->RegisterMessageCallback(message, callback); |
| 54 } |
| 55 void CallJavaScriptFunction(const std::string& function, |
| 56 const base::Value& arg) override { |
| 57 web_ui_->CallJavascriptFunction(function, arg); |
| 58 } |
| 59 physical_web::PhysicalWebDataSource* GetPhysicalWebDataSource() override { |
| 60 return GetApplicationContext()->GetPhysicalWebDataSource(); |
| 61 } |
| 62 |
| 63 web::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() {} |
| 55 ~PhysicalWebDOMHandler() override {} | 77 ~PhysicalWebDOMHandler() override {} |
| 56 | 78 |
| 57 void RegisterMessages() override; | 79 void RegisterMessages() override { |
| 58 | 80 impl_.reset(new MessageHandlerImpl(web_ui())); |
| 59 void HandleRequestNearbyURLs(const base::ListValue* args); | 81 impl_->RegisterMessages(); |
| 60 void HandlePhysicalWebItemClicked(const base::ListValue* args); | 82 } |
| 61 | 83 |
| 62 private: | 84 private: |
| 85 std::unique_ptr<MessageHandlerImpl> impl_; |
| 63 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); | 86 DISALLOW_COPY_AND_ASSIGN(PhysicalWebDOMHandler); |
| 64 }; | 87 }; |
| 65 | 88 |
| 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 | 89 } // namespace |
| 121 | 90 |
| 122 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
| 123 // | 92 // |
| 124 // PhysicalWebUI | 93 // PhysicalWebUI |
| 125 // | 94 // |
| 126 //////////////////////////////////////////////////////////////////////////////// | 95 //////////////////////////////////////////////////////////////////////////////// |
| 127 | 96 |
| 128 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) | 97 PhysicalWebUI::PhysicalWebUI(web::WebUIIOS* web_ui) |
| 129 : web::WebUIIOSController(web_ui) { | 98 : web::WebUIIOSController(web_ui) { |
| 130 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); | 99 PhysicalWebDOMHandler* handler = new PhysicalWebDOMHandler(); |
| 131 web_ui->AddMessageHandler(handler); | 100 web_ui->AddMessageHandler(handler); |
| 132 | 101 |
| 133 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), | 102 web::WebUIIOSDataSource::Add(ios::ChromeBrowserState::FromWebUIIOS(web_ui), |
| 134 CreatePhysicalWebUIDataSource()); | 103 CreatePhysicalWebUIDataSource()); |
| 135 | 104 |
| 136 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); | 105 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); |
| 137 } | 106 } |
| 138 | 107 |
| 139 PhysicalWebUI::~PhysicalWebUI() {} | 108 PhysicalWebUI::~PhysicalWebUI() {} |
| OLD | NEW |