blob: 2a645429a2f3500c2e07c886debc70db243780fd [file] [log] [blame]
[email protected]4fa18202014-03-13 06:19:261// 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]5dddd192014-03-28 00:53:2713#include "mojo/public/cpp/system/core.h"
[email protected]4fa18202014-03-13 06:19:2614
15namespace gin {
16class PerContextData;
17}
18
19namespace content {
20
21class 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.
27class WebUIMojo
28 : public RenderViewObserver,
29 public RenderViewObserverTracker<WebUIMojo> {
30 public:
31 explicit WebUIMojo(RenderView* render_view);
32
[email protected]69a0a132014-03-26 16:45:0233 // Sets the handle to the current WebUI.
34 void SetBrowserHandle(mojo::ScopedMessagePipeHandle handle);
35
[email protected]4fa18202014-03-13 06:19:2636 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]23dc93c2014-03-28 20:45:2545 virtual void DidFinishDocumentLoad() OVERRIDE;
[email protected]4fa18202014-03-13 06:19:2646
47 private:
48 WebUIMojo* web_ui_mojo_;
49
50 DISALLOW_COPY_AND_ASSIGN(MainFrameObserver);
51 };
52
53 virtual ~WebUIMojo();
54
[email protected]4fa18202014-03-13 06:19:2655 void CreateContextState();
56 void DestroyContextState(v8::Handle<v8::Context> context);
57
[email protected]23dc93c2014-03-28 20:45:2558 // 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]4fa18202014-03-13 06:19:2665 WebUIMojoContextState* GetContextState();
66
67 // RenderViewObserver overrides:
[email protected]06181e52014-05-10 11:59:0968 virtual void DidClearWindowObject(blink::WebLocalFrame* frame) OVERRIDE;
[email protected]4fa18202014-03-13 06:19:2669
70 MainFrameObserver main_frame_observer_;
71
[email protected]23dc93c2014-03-28 20:45:2572 // 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]4fa18202014-03-13 06:19:2682 DISALLOW_COPY_AND_ASSIGN(WebUIMojo);
83};
84
85} // namespace content
86
87#endif // CONTENT_RENDERER_WEB_UI_MOJO_H_