| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "chrome/browser/ui/webui/mojo_web_ui_handler.h" | |
| 13 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
| 14 #include "content/public/browser/render_view_host.h" | 13 #include "content/public/browser/render_view_host.h" |
| 15 #include "content/public/browser/web_ui_controller.h" | 14 #include "content/public/browser/web_ui_controller.h" |
| 16 #include "content/public/common/service_registry.h" | 15 #include "content/public/common/service_registry.h" |
| 17 #include "mojo/public/cpp/system/core.h" | 16 #include "mojo/common/strong_binding_set.h" |
| 18 | 17 |
| 19 class MojoWebUIHandler; | 18 class MojoWebUIHandler; |
| 20 | 19 |
| 21 namespace content { | |
| 22 class WebUIDataSource; | |
| 23 } | |
| 24 | |
| 25 class MojoWebUIControllerBase : public content::WebUIController { | 20 class MojoWebUIControllerBase : public content::WebUIController { |
| 26 public: | 21 public: |
| 27 explicit MojoWebUIControllerBase(content::WebUI* contents); | 22 explicit MojoWebUIControllerBase(content::WebUI* contents); |
| 28 ~MojoWebUIControllerBase() override; | 23 ~MojoWebUIControllerBase() override; |
| 29 | 24 |
| 30 // WebUIController overrides: | 25 // WebUIController overrides: |
| 31 void RenderViewCreated(content::RenderViewHost* render_view_host) override; | 26 void RenderViewCreated(content::RenderViewHost* render_view_host) override; |
| 32 | 27 |
| 33 private: | 28 private: |
| 34 // Bindings files are registered here. | |
| 35 content::WebUIDataSource* mojo_data_source_; | |
| 36 | |
| 37 DISALLOW_COPY_AND_ASSIGN(MojoWebUIControllerBase); | 29 DISALLOW_COPY_AND_ASSIGN(MojoWebUIControllerBase); |
| 38 }; | 30 }; |
| 39 | 31 |
| 40 // MojoWebUIController is intended for web ui pages that use mojo. It is | 32 // MojoWebUIController is intended for web ui pages that use mojo. It is |
| 41 // expected that subclasses will do two things: | 33 // expected that |Impl| is constructible from a content::WebUI. A subclass |
| 42 // . In the constructor invoke AddMojoResourcePath() to register the bindings | 34 // should, in its constructor, invoke AddMojoResources() on its |
| 43 // files, eg: | 35 // content::WebUIDataSource to register the bindings files. |
| 44 // AddMojoResourcePath("chrome/browser/ui/webui/omnibox/omnibox.mojom", | 36 template <typename Impl> |
| 45 // IDR_OMNIBOX_MOJO_JS); | |
| 46 // . Override BindUIHandler() to create and bind the implementation of the | |
| 47 // bindings. | |
| 48 template <typename Interface> | |
| 49 class MojoWebUIController : public MojoWebUIControllerBase { | 37 class MojoWebUIController : public MojoWebUIControllerBase { |
| 50 public: | 38 public: |
| 51 explicit MojoWebUIController(content::WebUI* contents) | 39 explicit MojoWebUIController(content::WebUI* contents) |
| 52 : MojoWebUIControllerBase(contents), weak_factory_(this) {} | 40 : MojoWebUIControllerBase(contents), weak_factory_(this) {} |
| 53 ~MojoWebUIController() override {} | 41 ~MojoWebUIController() override = default; |
| 42 |
| 54 void RenderViewCreated(content::RenderViewHost* render_view_host) override { | 43 void RenderViewCreated(content::RenderViewHost* render_view_host) override { |
| 55 MojoWebUIControllerBase::RenderViewCreated(render_view_host); | 44 MojoWebUIControllerBase::RenderViewCreated(render_view_host); |
| 56 render_view_host->GetMainFrame()->GetServiceRegistry()-> | 45 render_view_host->GetMainFrame()->GetServiceRegistry()->AddService( |
| 57 AddService<Interface>( | 46 base::Bind(&MojoWebUIController::BindUIHandler, |
| 58 base::Bind(&MojoWebUIController::BindUIHandler, | 47 weak_factory_.GetWeakPtr())); |
| 59 weak_factory_.GetWeakPtr())); | |
| 60 } | 48 } |
| 61 | 49 |
| 62 protected: | 50 private: |
| 63 // Invoked to create the specific bindings implementation. | 51 // Invoked to create the specific bindings implementation. |
| 64 virtual void BindUIHandler(mojo::InterfaceRequest<Interface> request) = 0; | 52 void BindUIHandler( |
| 53 mojo::InterfaceRequest<typename mojo::StrongBindingSet<Impl>::Interface> |
| 54 request) { |
| 55 services_.EmplaceService(std::move(request), web_ui()); |
| 56 } |
| 65 | 57 |
| 66 private: | 58 mojo::StrongBindingSet<Impl> services_; |
| 59 |
| 67 base::WeakPtrFactory<MojoWebUIController> weak_factory_; | 60 base::WeakPtrFactory<MojoWebUIController> weak_factory_; |
| 68 | 61 |
| 69 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController); | 62 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController); |
| 70 }; | 63 }; |
| 71 | |
| 72 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ | 64 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ |
| OLD | NEW |