Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / web_ui_mojo.h
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 CONTENT_RENDERER_WEB_UI_MOJO_H_
6 #define CONTENT_RENDERER_WEB_UI_MOJO_H_
7
8 #include <string>
9
10 #include "content/public/renderer/render_frame_observer.h"
11 #include "content/public/renderer/render_view_observer.h"
12 #include "content/public/renderer/render_view_observer_tracker.h"
13 #include "mojo/public/cpp/system/core.h"
14
15 namespace gin {
16 class PerContextData;
17 }
18
19 namespace content {
20
21 class WebUIMojoContextState;
22
23 // WebUIMojo is responsible for enabling the renderer side of mojo bindings.
24 // It creates (and destroys) a WebUIMojoContextState at the appropriate times
25 // and handles the necessary browser messages. WebUIMojo destroys itself when
26 // the RendererView it is created with is destroyed.
27 class WebUIMojo
28     : public RenderViewObserver,
29       public RenderViewObserverTracker<WebUIMojo> {
30  public:
31   explicit WebUIMojo(RenderView* render_view);
32
33   // Sets the handle to the current WebUI.
34   void SetBrowserHandle(mojo::ScopedMessagePipeHandle handle);
35
36  private:
37   class MainFrameObserver : public RenderFrameObserver {
38    public:
39     explicit MainFrameObserver(WebUIMojo* web_ui_mojo);
40     virtual ~MainFrameObserver();
41
42     // RenderFrameObserver overrides:
43     virtual void WillReleaseScriptContext(v8::Handle<v8::Context> context,
44                                           int world_id) OVERRIDE;
45     virtual void DidFinishDocumentLoad() OVERRIDE;
46
47    private:
48     WebUIMojo* web_ui_mojo_;
49
50     DISALLOW_COPY_AND_ASSIGN(MainFrameObserver);
51   };
52
53   virtual ~WebUIMojo();
54
55   void CreateContextState();
56   void DestroyContextState(v8::Handle<v8::Context> context);
57
58   // Invoked when the frame finishes loading. Invokes SetHandleOnContextState()
59   // if necessary.
60   void OnDidFinishDocumentLoad();
61
62   // Invokes SetHandle() on the WebUIMojoContextState (if there is one).
63   void SetHandleOnContextState(mojo::ScopedMessagePipeHandle handle);
64
65   WebUIMojoContextState* GetContextState();
66
67   // RenderViewObserver overrides:
68   virtual void DidClearWindowObject(blink::WebLocalFrame* frame,
69                                     int world_id) OVERRIDE;
70
71   MainFrameObserver main_frame_observer_;
72
73   // Set to true in DidFinishDocumentLoad(). 'main' is only executed once this
74   // happens.
75   bool did_finish_document_load_;
76
77   // If SetBrowserHandle() is invoked before the document finishes loading the
78   // MessagePipeHandle is stored here. When the document finishes loading
79   // SetHandleOnContextState() is invoked to send the handle to the
80   // WebUIMojoContextState and ultimately the page.
81   mojo::ScopedMessagePipeHandle pending_handle_;
82
83   DISALLOW_COPY_AND_ASSIGN(WebUIMojo);
84 };
85
86 }  // namespace content
87
88 #endif  // CONTENT_RENDERER_WEB_UI_MOJO_H_