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 CONTENT_RENDERER_WEB_UI_MOJO_H_
6 #define CONTENT_RENDERER_WEB_UI_MOJO_H_
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"
21 class WebUIMojoContextState
;
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.
28 : public RenderViewObserver
,
29 public RenderViewObserverTracker
<WebUIMojo
> {
31 explicit WebUIMojo(RenderView
* render_view
);
33 // Sets the handle to the current WebUI.
34 void SetBrowserHandle(mojo::ScopedMessagePipeHandle handle
);
37 class MainFrameObserver
: public RenderFrameObserver
{
39 explicit MainFrameObserver(WebUIMojo
* web_ui_mojo
);
40 virtual ~MainFrameObserver();
42 // RenderFrameObserver overrides:
43 virtual void WillReleaseScriptContext(v8::Handle
<v8::Context
> context
,
44 int world_id
) OVERRIDE
;
45 virtual void DidFinishDocumentLoad() OVERRIDE
;
48 WebUIMojo
* web_ui_mojo_
;
50 DISALLOW_COPY_AND_ASSIGN(MainFrameObserver
);
55 void CreateContextState();
56 void DestroyContextState(v8::Handle
<v8::Context
> context
);
58 // Invoked when the frame finishes loading. Invokes SetHandleOnContextState()
60 void OnDidFinishDocumentLoad();
62 // Invokes SetHandle() on the WebUIMojoContextState (if there is one).
63 void SetHandleOnContextState(mojo::ScopedMessagePipeHandle handle
);
65 WebUIMojoContextState
* GetContextState();
67 // RenderViewObserver overrides:
68 virtual void DidClearWindowObject(blink::WebLocalFrame
* frame
,
69 int world_id
) OVERRIDE
;
71 MainFrameObserver main_frame_observer_
;
73 // Set to true in DidFinishDocumentLoad(). 'main' is only executed once this
75 bool did_finish_document_load_
;
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_
;
83 DISALLOW_COPY_AND_ASSIGN(WebUIMojo
);
86 } // namespace content
88 #endif // CONTENT_RENDERER_WEB_UI_MOJO_H_