diff options
Diffstat (limited to 'chromium/components/physical_web/webui')
7 files changed, 147 insertions, 7 deletions
diff --git a/chromium/components/physical_web/webui/BUILD.gn b/chromium/components/physical_web/webui/BUILD.gn index f3d6432dd2b..a2fc20a4cae 100644 --- a/chromium/components/physical_web/webui/BUILD.gn +++ b/chromium/components/physical_web/webui/BUILD.gn @@ -4,11 +4,14 @@ source_set("webui") { sources = [ + "physical_web_base_message_handler.cc", + "physical_web_base_message_handler.h", "physical_web_ui_constants.cc", "physical_web_ui_constants.h", ] deps = [ "//base", + "//components/physical_web/data_source", ] } diff --git a/chromium/components/physical_web/webui/physical_web_base_message_handler.cc b/chromium/components/physical_web/webui/physical_web_base_message_handler.cc new file mode 100644 index 00000000000..fd7d4b61699 --- /dev/null +++ b/chromium/components/physical_web/webui/physical_web_base_message_handler.cc @@ -0,0 +1,82 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/physical_web/webui/physical_web_base_message_handler.h" + +#include "base/bind.h" +#include "base/memory/ptr_util.h" +#include "base/metrics/histogram_macros.h" +#include "base/metrics/user_metrics.h" +#include "components/physical_web/data_source/physical_web_data_source.h" +#include "components/physical_web/webui/physical_web_ui_constants.h" + +namespace physical_web_ui { + +PhysicalWebBaseMessageHandler::PhysicalWebBaseMessageHandler() {} + +PhysicalWebBaseMessageHandler::~PhysicalWebBaseMessageHandler() = default; + +void PhysicalWebBaseMessageHandler::RegisterMessages() { + RegisterMessageCallback( + kRequestNearbyUrls, + base::BindRepeating( + &PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs, + base::Unretained(this))); + RegisterMessageCallback(kPhysicalWebItemClicked, + base::BindRepeating( + &PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked, + base::Unretained(this))); +} + +void PhysicalWebBaseMessageHandler::HandleRequestNearbyURLs( + const base::ListValue* args) { + base::DictionaryValue results; + + std::unique_ptr<physical_web::MetadataList> metadata_list = + GetPhysicalWebDataSource()->GetMetadataList(); + + auto metadata = base::MakeUnique<base::ListValue>(); + int index = 0; + for (const auto& metadata_list_item : *metadata_list) { + auto metadata_item = base::MakeUnique<base::DictionaryValue>(); + metadata_item->SetString(physical_web_ui::kResolvedUrl, + metadata_list_item.resolved_url.spec()); + metadata_item->SetString(physical_web_ui::kPageInfoIcon, + metadata_list_item.icon_url.spec()); + metadata_item->SetString(physical_web_ui::kPageInfoTitle, + metadata_list_item.title); + metadata_item->SetString(physical_web_ui::kPageInfoDescription, + metadata_list_item.description); + // Add the item index so when an item is selected, the index can be recorded + // in a UMA histogram. + metadata_item->SetInteger(physical_web_ui::kIndex, index); + metadata->Append(std::move(metadata_item)); + ++index; + } + + 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. + CallJavaScriptFunction(physical_web_ui::kReturnNearbyUrls, results); +} + +void PhysicalWebBaseMessageHandler::HandlePhysicalWebItemClicked( + const base::ListValue* args) { + int index = 0; + if (!args->GetInteger(0, &index)) { + DLOG(ERROR) << "Invalid selection index"; + return; + } + + // Record the index of the selected item. + UMA_HISTOGRAM_EXACT_LINEAR("PhysicalWeb.WebUI.ListViewUrlPosition", index, + 50); + + // Count the number of selections. + base::RecordAction( + base::UserMetricsAction("PhysicalWeb.WebUI.ListViewUrlSelected")); +} + +} // namespace physical_web_ui diff --git a/chromium/components/physical_web/webui/physical_web_base_message_handler.h b/chromium/components/physical_web/webui/physical_web_base_message_handler.h new file mode 100644 index 00000000000..190cf294b11 --- /dev/null +++ b/chromium/components/physical_web/webui/physical_web_base_message_handler.h @@ -0,0 +1,58 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_PHYSICAL_WEB_WEBUI_PHYSICAL_WEB_BASE_MESSAGE_HANDLER_H_ +#define COMPONENTS_PHYSICAL_WEB_WEBUI_PHYSICAL_WEB_BASE_MESSAGE_HANDLER_H_ + +#include "base/callback.h" +#include "base/macros.h" +#include "base/values.h" + +namespace physical_web { + +class PhysicalWebDataSource; + +} // namespace physical_web + +namespace physical_web_ui { + +// This is the equivalent of content::WebUI::MessageCallback. +typedef base::Callback<void(const base::ListValue*)> MessageCallback; + +// The base handler for Javascript messages for the chrome://physical-web page. +// This does not implement WebUIMessageHandler or register its methods. +class PhysicalWebBaseMessageHandler { + public: + PhysicalWebBaseMessageHandler(); + virtual ~PhysicalWebBaseMessageHandler(); + + // Handles the RequestNearbyURLs message, returning URLs that are currently + // being broadcasted by Physical Web transports. + void HandleRequestNearbyURLs(const base::ListValue* args); + + // Handles a click on a Physical Web URL, recording the click and + // directing the user appropriately. + void HandlePhysicalWebItemClicked(const base::ListValue* args); + + // Registers the messages that this MessageHandler can handle. + void RegisterMessages(); + + protected: + // Subclasses should implement these protected methods as a pass through to + // the similarly named methods of the appropriate WebUI object (the exact + // WebUI class differs per platform). + virtual void RegisterMessageCallback( + const std::string& message, + const MessageCallback& callback) = 0; + virtual void CallJavaScriptFunction(const std::string& function, + const base::Value& arg) = 0; + virtual physical_web::PhysicalWebDataSource* GetPhysicalWebDataSource() = 0; + + private: + DISALLOW_COPY_AND_ASSIGN(PhysicalWebBaseMessageHandler); +}; + +} // namespace physical_web_ui + +#endif // COMPONENTS_PHYSICAL_WEB_WEBUI_PHYSICAL_WEB_BASE_MESSAGE_HANDLER_H_ diff --git a/chromium/components/physical_web/webui/physical_web_ui_constants.cc b/chromium/components/physical_web/webui/physical_web_ui_constants.cc index 47415b72e7a..82d1af39dab 100644 --- a/chromium/components/physical_web/webui/physical_web_ui_constants.cc +++ b/chromium/components/physical_web/webui/physical_web_ui_constants.cc @@ -16,12 +16,10 @@ const char kPhysicalWebItemClicked[] = "physicalWebItemClicked"; // Strings. const char kTitle[] = "title"; -const char kPageInfo[] = "pageInfo"; const char kPageInfoDescription[] = "pageInfoDescription"; const char kPageInfoIcon[] = "pageInfoIcon"; const char kPageInfoTitle[] = "pageInfoTitle"; const char kResolvedUrl[] = "resolvedUrl"; -const char kScannedUrl[] = "scannedUrl"; const char kIndex[] = "index"; const char kReturnNearbyUrls[] = "returnNearbyURLs"; const char kMetadata[] = "metadata"; diff --git a/chromium/components/physical_web/webui/physical_web_ui_constants.h b/chromium/components/physical_web/webui/physical_web_ui_constants.h index b63d7d44362..2eb85231496 100644 --- a/chromium/components/physical_web/webui/physical_web_ui_constants.h +++ b/chromium/components/physical_web/webui/physical_web_ui_constants.h @@ -20,12 +20,10 @@ extern const char kPhysicalWebItemClicked[]; // Strings. // Must match the constants used in the resource files. extern const char kTitle[]; -extern const char kPageInfo[]; extern const char kPageInfoDescription[]; extern const char kPageInfoIcon[]; extern const char kPageInfoTitle[]; extern const char kResolvedUrl[]; -extern const char kScannedUrl[]; extern const char kIndex[]; extern const char kReturnNearbyUrls[]; extern const char kMetadata[]; diff --git a/chromium/components/physical_web/webui/resources/physical_web.css b/chromium/components/physical_web/webui/resources/physical_web.css index 94a671e7014..29298209538 100644 --- a/chromium/components/physical_web/webui/resources/physical_web.css +++ b/chromium/components/physical_web/webui/resources/physical_web.css @@ -44,6 +44,7 @@ body { .physicalWebText .description { display: block; height: 2.5em; + line-height: 1.25; font-size: 90%; color: #444; overflow: hidden; diff --git a/chromium/components/physical_web/webui/resources/physical_web.html b/chromium/components/physical_web/webui/resources/physical_web.html index 434a6b89b96..a5a0ba69884 100644 --- a/chromium/components/physical_web/webui/resources/physical_web.html +++ b/chromium/components/physical_web/webui/resources/physical_web.html @@ -27,12 +27,12 @@ <a class="physicalWebTemplate" id="physicalWebTemplate" jsselect="metadata" jsvalues="href:resolvedUrl;onclick:'physicalWebItemClicked(' + index + ')'"> <div class="physicalWebIcon"> - <img jsvalues="src:icon" /> + <img jsvalues="src:pageInfoIcon" /> </div> <div class="physicalWebText"> - <div class="title" jscontent="title"></div> + <div class="title" jscontent="pageInfoTitle"></div> <div class="resolvedUrl" jscontent="resolvedUrl"></div> - <div class="description" jscontent="description"></div> + <div class="description" jscontent="pageInfoDescription"></div> </div> </a> |