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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_
10 #include "base/memory/scoped_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"
15 #include "content/public/browser/web_ui_controller.h"
16 #include "content/public/common/service_registry.h"
17 #include "third_party/mojo/src/mojo/public/cpp/system/core.h"
19 class MojoWebUIHandler
;
22 class WebUIDataSource
;
25 class MojoWebUIControllerBase
: public content::WebUIController
{
27 explicit MojoWebUIControllerBase(content::WebUI
* contents
);
28 ~MojoWebUIControllerBase() override
;
30 // WebUIController overrides:
31 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
34 // Invoke to register mapping between binding file and resource id (IDR_...).
35 void AddMojoResourcePath(const std::string
& path
, int resource_id
);
38 // Bindings files are registered here.
39 content::WebUIDataSource
* mojo_data_source_
;
41 DISALLOW_COPY_AND_ASSIGN(MojoWebUIControllerBase
);
44 // MojoWebUIController is intended for web ui pages that use mojo. It is
45 // expected that subclasses will do two things:
46 // . In the constructor invoke AddMojoResourcePath() to register the bindings
48 // AddMojoResourcePath("chrome/browser/ui/webui/omnibox/omnibox.mojom",
49 // IDR_OMNIBOX_MOJO_JS);
50 // . Override BindUIHandler() to create and bind the implementation of the
52 template <typename Interface
>
53 class MojoWebUIController
: public MojoWebUIControllerBase
{
55 explicit MojoWebUIController(content::WebUI
* contents
)
56 : MojoWebUIControllerBase(contents
), weak_factory_(this) {}
57 ~MojoWebUIController() override
{}
58 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
{
59 MojoWebUIControllerBase::RenderViewCreated(render_view_host
);
60 render_view_host
->GetMainFrame()->GetServiceRegistry()->
61 AddService
<Interface
>(
62 base::Bind(&MojoWebUIController::BindUIHandler
,
63 weak_factory_
.GetWeakPtr()));
67 // Invoked to create the specific bindings implementation.
68 virtual void BindUIHandler(mojo::InterfaceRequest
<Interface
> request
) = 0;
71 base::WeakPtrFactory
<MojoWebUIController
> weak_factory_
;
73 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController
);
76 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_