Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / webui / mojo_web_ui_controller.h
blobbb934df4cb5619b6b4b7bc3035a6428a856951d6
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_
8 #include <string>
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;
21 namespace content {
22 class WebUIDataSource;
25 class MojoWebUIControllerBase : public content::WebUIController {
26 public:
27 explicit MojoWebUIControllerBase(content::WebUI* contents);
28 ~MojoWebUIControllerBase() override;
30 // WebUIController overrides:
31 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
33 protected:
34 // Invoke to register mapping between binding file and resource id (IDR_...).
35 void AddMojoResourcePath(const std::string& path, int resource_id);
37 private:
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
47 // files, eg:
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
51 // bindings.
52 template <typename Interface>
53 class MojoWebUIController : public MojoWebUIControllerBase {
54 public:
55 explicit MojoWebUIController(content::WebUI* contents)
56 : MojoWebUIControllerBase(contents), weak_factory_(this) {}
57 virtual ~MojoWebUIController() {}
58 virtual void RenderViewCreated(
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::BindUIHandler,
64 weak_factory_.GetWeakPtr()));
67 protected:
68 // Invoked to create the specific bindings implementation.
69 virtual void BindUIHandler(mojo::InterfaceRequest<Interface> request) = 0;
71 private:
72 base::WeakPtrFactory<MojoWebUIController> weak_factory_;
74 DISALLOW_COPY_AND_ASSIGN(MojoWebUIController);
77 #endif // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_