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 #include "content/renderer/web_ui_mojo.h"
7 #include "content/common/view_messages.h"
8 #include "content/public/renderer/render_frame.h"
9 #include "content/public/renderer/render_view.h"
10 #include "content/renderer/web_ui_mojo_context_state.h"
11 #include "gin/per_context_data.h"
12 #include "third_party/WebKit/public/web/WebKit.h"
13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15 #include "v8/include/v8.h"
21 const char kWebUIMojoContextStateKey
[] = "WebUIMojoContextState";
23 struct WebUIMojoContextStateData
: public base::SupportsUserData::Data
{
24 scoped_ptr
<WebUIMojoContextState
> state
;
29 WebUIMojo::MainFrameObserver::MainFrameObserver(WebUIMojo
* web_ui_mojo
)
30 : RenderFrameObserver(RenderFrame::FromWebFrame(
31 web_ui_mojo
->render_view()->GetWebView()->mainFrame())),
32 web_ui_mojo_(web_ui_mojo
) {
35 WebUIMojo::MainFrameObserver::~MainFrameObserver() {
38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext(
39 v8::Handle
<v8::Context
> context
,
41 web_ui_mojo_
->DestroyContextState(context
);
44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() {
45 web_ui_mojo_
->OnDidFinishDocumentLoad();
48 WebUIMojo::WebUIMojo(RenderView
* render_view
)
49 : RenderViewObserver(render_view
),
50 RenderViewObserverTracker
<WebUIMojo
>(render_view
),
51 main_frame_observer_(this) {
54 WebUIMojo::~WebUIMojo() {
57 void WebUIMojo::CreateContextState() {
58 v8::HandleScope
handle_scope(blink::mainThreadIsolate());
59 blink::WebLocalFrame
* frame
=
60 render_view()->GetWebView()->mainFrame()->toWebLocalFrame();
61 v8::Handle
<v8::Context
> context
= frame
->mainWorldScriptContext();
62 gin::PerContextData
* context_data
= gin::PerContextData::From(context
);
63 WebUIMojoContextStateData
* data
= new WebUIMojoContextStateData
;
64 data
->state
.reset(new WebUIMojoContextState(
65 render_view()->GetWebView()->mainFrame(), context
));
66 context_data
->SetUserData(kWebUIMojoContextStateKey
, data
);
69 void WebUIMojo::DestroyContextState(v8::Handle
<v8::Context
> context
) {
70 gin::PerContextData
* context_data
= gin::PerContextData::From(context
);
73 context_data
->RemoveUserData(kWebUIMojoContextStateKey
);
76 void WebUIMojo::OnDidFinishDocumentLoad() {
77 v8::HandleScope
handle_scope(blink::mainThreadIsolate());
78 WebUIMojoContextState
* state
= GetContextState();
83 WebUIMojoContextState
* WebUIMojo::GetContextState() {
84 blink::WebLocalFrame
* frame
=
85 render_view()->GetWebView()->mainFrame()->toWebLocalFrame();
86 v8::HandleScope
handle_scope(blink::mainThreadIsolate());
87 v8::Handle
<v8::Context
> context
= frame
->mainWorldScriptContext();
88 gin::PerContextData
* context_data
= gin::PerContextData::From(context
);
91 WebUIMojoContextStateData
* context_state
=
92 static_cast<WebUIMojoContextStateData
*>(
93 context_data
->GetUserData(kWebUIMojoContextStateKey
));
94 return context_state
? context_state
->state
.get() : NULL
;
97 void WebUIMojo::DidCreateDocumentElement(blink::WebLocalFrame
* frame
) {
101 void WebUIMojo::DidClearWindowObject(blink::WebLocalFrame
* frame
) {
102 if (frame
!= render_view()->GetWebView()->mainFrame())
105 // NOTE: this function may be called early on twice. From the constructor
106 // mainWorldScriptContext() may trigger this to be called. If we are created
107 // before the page is loaded (which is very likely), then on first load this
108 // is called. In the case of the latter we may have already supplied the
109 // handle to the context state so that if we destroy now the handle is
110 // lost. If this is the result of the first load then the contextstate should
111 // be empty and we don't need to destroy it.
112 WebUIMojoContextState
* state
= GetContextState();
113 if (state
&& !state
->module_added())
116 v8::HandleScope
handle_scope(blink::mainThreadIsolate());
117 DestroyContextState(frame
->mainWorldScriptContext());
120 } // namespace content