OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/webui/physical_web/physical_web_ui.h" |
| 6 |
| 7 #include "base/metrics/user_metrics.h" |
| 8 #include "chrome/browser/browser_process_impl.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/url_constants.h" |
| 11 #include "components/grit/components_resources.h" |
| 12 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 13 #include "components/physical_web/webui/physical_web_base_message_handler.h" |
| 14 #include "components/physical_web/webui/physical_web_ui_constants.h" |
| 15 #include "components/strings/grit/components_strings.h" |
| 16 #include "content/public/browser/web_ui_data_source.h" |
| 17 #include "content/public/browser/web_ui_message_handler.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 content::WebUIDataSource* CreatePhysicalWebHTMLSource() { |
| 22 content::WebUIDataSource* source = content::WebUIDataSource::Create( |
| 23 chrome::kChromeUIPhysicalWebHost); |
| 24 |
| 25 source->AddLocalizedString(physical_web_ui::kTitle, |
| 26 IDS_PHYSICAL_WEB_UI_TITLE); |
| 27 source->SetJsonPath("strings.js"); |
| 28 source->AddResourcePath(physical_web_ui::kPhysicalWebJS, |
| 29 IDR_PHYSICAL_WEB_UI_JS); |
| 30 source->AddResourcePath(physical_web_ui::kPhysicalWebCSS, |
| 31 IDR_PHYSICAL_WEB_UI_CSS); |
| 32 source->SetDefaultResource(IDR_PHYSICAL_WEB_UI_HTML); |
| 33 return source; |
| 34 } |
| 35 |
| 36 // Implements all MessageHandler core functionality. This is extends the |
| 37 // PhysicalWebBaseMessageHandler and implements functions to manipulate an |
| 38 // Android-specific WebUI object. |
| 39 class MessageHandlerImpl |
| 40 : public physical_web_ui::PhysicalWebBaseMessageHandler { |
| 41 public: |
| 42 explicit MessageHandlerImpl(content::WebUI* web_ui) : web_ui_(web_ui) {} |
| 43 ~MessageHandlerImpl() override {} |
| 44 |
| 45 private: |
| 46 void RegisterMessageCallback( |
| 47 const std::string& message, |
| 48 const physical_web_ui::MessageCallback& callback) override { |
| 49 web_ui_->RegisterMessageCallback(message, callback); |
| 50 } |
| 51 void CallJavaScriptFunction(const std::string& function, |
| 52 const base::Value& arg) override { |
| 53 web_ui_->CallJavascriptFunctionUnsafe(function, arg); |
| 54 } |
| 55 physical_web::PhysicalWebDataSource* GetPhysicalWebDataSource() override { |
| 56 return g_browser_process->GetPhysicalWebDataSource(); |
| 57 } |
| 58 |
| 59 content::WebUI* const web_ui_; |
| 60 DISALLOW_COPY_AND_ASSIGN(MessageHandlerImpl); |
| 61 }; |
| 62 |
| 63 class PhysicalWebMessageHandler : public content::WebUIMessageHandler { |
| 64 public: |
| 65 PhysicalWebMessageHandler() {} |
| 66 ~PhysicalWebMessageHandler() override {} |
| 67 |
| 68 void RegisterMessages() override { |
| 69 impl_.reset(new MessageHandlerImpl(web_ui())); |
| 70 impl_->RegisterMessages(); |
| 71 } |
| 72 |
| 73 private: |
| 74 std::unique_ptr<MessageHandlerImpl> impl_; |
| 75 DISALLOW_COPY_AND_ASSIGN(PhysicalWebMessageHandler); |
| 76 }; |
| 77 |
| 78 } // namespace |
| 79 |
| 80 PhysicalWebUI::PhysicalWebUI(content::WebUI* web_ui) |
| 81 : WebUIController(web_ui) { |
| 82 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
| 83 CreatePhysicalWebHTMLSource()); |
| 84 web_ui->AddMessageHandler(new PhysicalWebMessageHandler()); |
| 85 base::RecordAction(base::UserMetricsAction("PhysicalWeb.WebUI.Open")); |
| 86 } |
| 87 |
| 88 PhysicalWebUI::~PhysicalWebUI() = default; |
OLD | NEW |