Home | History | Annotate | Download | only in webui
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_
      7 
      8 #include <string>
      9 
     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 "mojo/public/cpp/system/core.h"
     18 
     19 class MojoWebUIHandler;
     20 
     21 namespace content {
     22 class WebUIDataSource;
     23 }
     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 
     45 // MojoWebUIController is intended for web ui pages that use mojo. It is
     46 // expected that subclasses will do two things:
     47 // . In the constructor invoke AddMojoResourcePath() to register the bindings
     48 //   files, eg:
     49 //     AddMojoResourcePath("chrome/browser/ui/webui/omnibox/omnibox.mojom",
     50 //                         IDR_OMNIBOX_MOJO_JS);
     51 // . Override CreateUIHandler() to create the implementation of the 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::CreateAndStoreUIHandler,
     64                        weak_factory_.GetWeakPtr()));
     65   }
     66 
     67  protected:
     68   // Invoked to create the specific bindings implementation.
     69   virtual scoped_ptr<MojoWebUIHandler> CreateUIHandler(
     70       mojo::InterfaceRequest<Interface> request) = 0;
     71 
     72  private:
     73   void CreateAndStoreUIHandler(mojo::InterfaceRequest<Interface> request) {
     74     ui_handler_ = CreateUIHandler(request.Pass());
     75   }
     76 
     77   scoped_ptr<MojoWebUIHandler> ui_handler_;
     78 
     79   base::WeakPtrFactory<MojoWebUIController> weak_factory_;
     80 
     81   DISALLOW_COPY_AND_ASSIGN(MojoWebUIController);
     82 };
     83 
     84 #endif  // CHROME_BROWSER_UI_WEBUI_MOJO_WEB_UI_CONTROLLER_H_
     85