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#include "content/renderer/web_ui_mojo.h"
6
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"
16
17namespace content {
18
19namespace {
20
21const char kWebUIMojoContextStateKey[] = "WebUIMojoContextState";
22
23struct WebUIMojoContextStateData : public base::SupportsUserData::Data {
24  scoped_ptr<WebUIMojoContextState> state;
25};
26
27}  // namespace
28
29WebUIMojo::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) {
33}
34
35WebUIMojo::MainFrameObserver::~MainFrameObserver() {
36}
37
38void WebUIMojo::MainFrameObserver::WillReleaseScriptContext(
39    v8::Handle<v8::Context> context,
40    int world_id) {
41  web_ui_mojo_->DestroyContextState(context);
42}
43
44void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() {
45  web_ui_mojo_->OnDidFinishDocumentLoad();
46}
47
48WebUIMojo::WebUIMojo(RenderView* render_view)
49    : RenderViewObserver(render_view),
50      RenderViewObserverTracker<WebUIMojo>(render_view),
51      main_frame_observer_(this) {
52}
53
54WebUIMojo::~WebUIMojo() {
55}
56
57void 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);
67}
68
69void WebUIMojo::DestroyContextState(v8::Handle<v8::Context> context) {
70  gin::PerContextData* context_data = gin::PerContextData::From(context);
71  if (!context_data)
72    return;
73  context_data->RemoveUserData(kWebUIMojoContextStateKey);
74}
75
76void WebUIMojo::OnDidFinishDocumentLoad() {
77  v8::HandleScope handle_scope(blink::mainThreadIsolate());
78  WebUIMojoContextState* state = GetContextState();
79  if (state)
80    state->Run();
81}
82
83WebUIMojoContextState* 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);
89  if (!context_data)
90    return NULL;
91  WebUIMojoContextStateData* context_state =
92      static_cast<WebUIMojoContextStateData*>(
93          context_data->GetUserData(kWebUIMojoContextStateKey));
94  return context_state ? context_state->state.get() : NULL;
95}
96
97void WebUIMojo::DidCreateDocumentElement(blink::WebLocalFrame* frame) {
98  CreateContextState();
99}
100
101void WebUIMojo::DidClearWindowObject(blink::WebLocalFrame* frame) {
102  if (frame != render_view()->GetWebView()->mainFrame())
103    return;
104
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())
114    return;
115
116  v8::HandleScope handle_scope(blink::mainThreadIsolate());
117  DestroyContextState(frame->mainWorldScriptContext());
118}
119
120}  // namespace content
121