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 | 9 |
10 #include "base/memory/scoped_ptr.h" | 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" |
| 14 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/browser/web_ui_controller.h" | 15 #include "content/public/browser/web_ui_controller.h" |
| 16 #include "content/public/common/service_registry.h" |
13 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
14 | 18 |
15 class MojoWebUIHandler; | 19 class MojoWebUIHandler; |
16 | 20 |
17 namespace content { | 21 namespace content { |
18 class WebUIDataSource; | 22 class WebUIDataSource; |
19 } | 23 } |
20 | 24 |
| 25 class MojoWebUIControllerBase : public content::WebUIController { |
| 26 public: |
| 27 explicit MojoWebUIControllerBase(content::WebUI* contents); |
| 28 virtual ~MojoWebUIControllerBase(); |
| 29 |
| 30 // WebUIController overrides: |
| 31 virtual void RenderViewCreated( |
| 32 content::RenderViewHost* render_view_host) OVERRIDE; |
| 33 |
| 34 protected: |
| 35 // Invoke to register mapping between binding file and resource id (IDR_...). |
| 36 void AddMojoResourcePath(const std::string& path, int resource_id); |
| 37 |
| 38 private: |
| 39 // Bindings files are registered here. |
| 40 content::WebUIDataSource* mojo_data_source_; |
| 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(MojoWebUIControllerBase); |
| 43 }; |
| 44 |
21 // MojoWebUIController is intended for web ui pages that use mojo. It is | 45 // MojoWebUIController is intended for web ui pages that use mojo. It is |
22 // expected that subclasses will do two things: | 46 // expected that subclasses will do two things: |
23 // . In the constructor invoke AddMojoResourcePath() to register the bindings | 47 // . In the constructor invoke AddMojoResourcePath() to register the bindings |
24 // files, eg: | 48 // files, eg: |
25 // AddMojoResourcePath("chrome/browser/ui/webui/omnibox/omnibox.mojom", | 49 // AddMojoResourcePath("chrome/browser/ui/webui/omnibox/omnibox.mojom", |
26 // IDR_OMNIBOX_MOJO_JS); | 50 // IDR_OMNIBOX_MOJO_JS); |
27 // . Override CreateUIHandler() to create the implementation of the bindings. | 51 // . Override CreateUIHandler() to create the implementation of the bindings. |
28 class MojoWebUIController : public content::WebUIController { | 52 template <typename Interface> |
| 53 class MojoWebUIController : public MojoWebUIControllerBase { |
29 public: | 54 public: |
30 explicit MojoWebUIController(content::WebUI* contents); | 55 explicit MojoWebUIController(content::WebUI* contents) |
31 virtual ~MojoWebUIController(); | 56 : MojoWebUIControllerBase(contents), weak_factory_(this) {} |
32 | 57 virtual ~MojoWebUIController() {} |
33 // WebUIController overrides: | |
34 virtual void RenderViewCreated( | 58 virtual void RenderViewCreated( |
35 content::RenderViewHost* render_view_host) OVERRIDE; | 59 content::RenderViewHost* render_view_host) OVERRIDE { |
| 60 MojoWebUIControllerBase::RenderViewCreated(render_view_host); |
| 61 render_view_host->GetMainFrame()->GetServiceRegistry()-> |
| 62 AddService<Interface>( |
| 63 base::Bind(&MojoWebUIController::CreateAndStoreUIHandler, |
| 64 weak_factory_.GetWeakPtr())); |
| 65 } |
36 | 66 |
37 protected: | 67 protected: |
38 // Invoke to register mapping between binding file and resource id (IDR_...). | |
39 void AddMojoResourcePath(const std::string& path, int resource_id); | |
40 | |
41 // Invoked to create the specific bindings implementation. | 68 // Invoked to create the specific bindings implementation. |
42 virtual scoped_ptr<MojoWebUIHandler> CreateUIHandler( | 69 virtual scoped_ptr<MojoWebUIHandler> CreateUIHandler( |
43 mojo::ScopedMessagePipeHandle handle_to_page) = 0; | 70 mojo::InterfaceRequest<Interface> request) = 0; |
44 | 71 |
45 private: | 72 private: |
46 // Invoked in response to a connection from the renderer. | 73 void CreateAndStoreUIHandler(mojo::InterfaceRequest<Interface> request) { |
47 void CreateAndStoreUIHandler(mojo::ScopedMessagePipeHandle handle); | 74 ui_handler_ = CreateUIHandler(request.Pass()); |
48 | 75 } |
49 // Bindings files are registered here. | |
50 content::WebUIDataSource* mojo_data_source_; | |
51 | 76 |
52 scoped_ptr<MojoWebUIHandler> ui_handler_; | 77 scoped_ptr<MojoWebUIHandler> ui_handler_; |
53 | 78 |
54 base::WeakPtrFactory<MojoWebUIController> weak_factory_; | 79 base::WeakPtrFactory<MojoWebUIController> weak_factory_; |
55 | 80 |
56 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController); | 81 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController); |
57 }; | 82 }; |
58 | 83 |
59 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ | 84 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_ |
OLD | NEW |