[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 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" |
[email protected] | 5dddd19 | 2014-03-28 00:53:27 | [diff] [blame] | 13 | #include "mojo/public/cpp/system/core.h" |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 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 | |
[email protected] | 69a0a13 | 2014-03-26 16:45:02 | [diff] [blame] | 33 | // Sets the handle to the current WebUI. |
| 34 | void SetBrowserHandle(mojo::ScopedMessagePipeHandle handle); |
| 35 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 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; |
[email protected] | 23dc93c | 2014-03-28 20:45:25 | [diff] [blame] | 45 | virtual void DidFinishDocumentLoad() OVERRIDE; |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | WebUIMojo* web_ui_mojo_; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(MainFrameObserver); |
| 51 | }; |
| 52 | |
| 53 | virtual ~WebUIMojo(); |
| 54 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 55 | void CreateContextState(); |
| 56 | void DestroyContextState(v8::Handle<v8::Context> context); |
| 57 | |
[email protected] | 23dc93c | 2014-03-28 20:45:25 | [diff] [blame] | 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 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 65 | WebUIMojoContextState* GetContextState(); |
| 66 | |
| 67 | // RenderViewObserver overrides: |
[email protected] | 06181e5 | 2014-05-10 11:59:09 | [diff] [blame] | 68 | virtual void DidClearWindowObject(blink::WebLocalFrame* frame) OVERRIDE; |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 69 | |
| 70 | MainFrameObserver main_frame_observer_; |
| 71 | |
[email protected] | 23dc93c | 2014-03-28 20:45:25 | [diff] [blame] | 72 | // Set to true in DidFinishDocumentLoad(). 'main' is only executed once this |
| 73 | // happens. |
| 74 | bool did_finish_document_load_; |
| 75 | |
| 76 | // If SetBrowserHandle() is invoked before the document finishes loading the |
| 77 | // MessagePipeHandle is stored here. When the document finishes loading |
| 78 | // SetHandleOnContextState() is invoked to send the handle to the |
| 79 | // WebUIMojoContextState and ultimately the page. |
| 80 | mojo::ScopedMessagePipeHandle pending_handle_; |
| 81 | |
[email protected] | 4fa1820 | 2014-03-13 06:19:26 | [diff] [blame] | 82 | DISALLOW_COPY_AND_ASSIGN(WebUIMojo); |
| 83 | }; |
| 84 | |
| 85 | } // namespace content |
| 86 | |
| 87 | #endif // CONTENT_RENDERER_WEB_UI_MOJO_H_ |