| OLD | NEW |
| (Empty) |
| 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 | |
| 17 namespace content { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const char kWebUIMojoContextStateKey[] = "WebUIMojoContextState"; | |
| 22 | |
| 23 struct WebUIMojoContextStateData : public base::SupportsUserData::Data { | |
| 24 scoped_ptr<WebUIMojoContextState> state; | |
| 25 }; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 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) { | |
| 33 } | |
| 34 | |
| 35 WebUIMojo::MainFrameObserver::~MainFrameObserver() { | |
| 36 } | |
| 37 | |
| 38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext( | |
| 39 v8::Local<v8::Context> context, | |
| 40 int world_id) { | |
| 41 web_ui_mojo_->DestroyContextState(context); | |
| 42 } | |
| 43 | |
| 44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() { | |
| 45 web_ui_mojo_->OnDidFinishDocumentLoad(); | |
| 46 } | |
| 47 | |
| 48 void WebUIMojo::MainFrameObserver::OnDestruct() { | |
| 49 } | |
| 50 | |
| 51 WebUIMojo::WebUIMojo(RenderView* render_view) | |
| 52 : RenderViewObserver(render_view), | |
| 53 RenderViewObserverTracker<WebUIMojo>(render_view), | |
| 54 main_frame_observer_(this) { | |
| 55 } | |
| 56 | |
| 57 WebUIMojo::~WebUIMojo() { | |
| 58 } | |
| 59 | |
| 60 void WebUIMojo::CreateContextState() { | |
| 61 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 62 blink::WebLocalFrame* frame = | |
| 63 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | |
| 64 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
| 65 gin::PerContextData* context_data = gin::PerContextData::From(context); | |
| 66 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; | |
| 67 data->state.reset(new WebUIMojoContextState( | |
| 68 render_view()->GetWebView()->mainFrame(), context)); | |
| 69 context_data->SetUserData(kWebUIMojoContextStateKey, data); | |
| 70 } | |
| 71 | |
| 72 void WebUIMojo::DestroyContextState(v8::Local<v8::Context> context) { | |
| 73 gin::PerContextData* context_data = gin::PerContextData::From(context); | |
| 74 if (!context_data) | |
| 75 return; | |
| 76 context_data->RemoveUserData(kWebUIMojoContextStateKey); | |
| 77 } | |
| 78 | |
| 79 void WebUIMojo::OnDidFinishDocumentLoad() { | |
| 80 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 81 WebUIMojoContextState* state = GetContextState(); | |
| 82 if (state) | |
| 83 state->Run(); | |
| 84 } | |
| 85 | |
| 86 WebUIMojoContextState* WebUIMojo::GetContextState() { | |
| 87 blink::WebLocalFrame* frame = | |
| 88 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | |
| 89 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 90 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
| 91 gin::PerContextData* context_data = gin::PerContextData::From(context); | |
| 92 if (!context_data) | |
| 93 return NULL; | |
| 94 WebUIMojoContextStateData* context_state = | |
| 95 static_cast<WebUIMojoContextStateData*>( | |
| 96 context_data->GetUserData(kWebUIMojoContextStateKey)); | |
| 97 return context_state ? context_state->state.get() : NULL; | |
| 98 } | |
| 99 | |
| 100 void WebUIMojo::DidCreateDocumentElement(blink::WebLocalFrame* frame) { | |
| 101 CreateContextState(); | |
| 102 } | |
| 103 | |
| 104 void WebUIMojo::DidClearWindowObject(blink::WebLocalFrame* frame) { | |
| 105 if (frame != render_view()->GetWebView()->mainFrame()) | |
| 106 return; | |
| 107 | |
| 108 // NOTE: this function may be called early on twice. From the constructor | |
| 109 // mainWorldScriptContext() may trigger this to be called. If we are created | |
| 110 // before the page is loaded (which is very likely), then on first load this | |
| 111 // is called. In the case of the latter we may have already supplied the | |
| 112 // handle to the context state so that if we destroy now the handle is | |
| 113 // lost. If this is the result of the first load then the contextstate should | |
| 114 // be empty and we don't need to destroy it. | |
| 115 WebUIMojoContextState* state = GetContextState(); | |
| 116 if (state && !state->module_added()) | |
| 117 return; | |
| 118 | |
| 119 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | |
| 120 DestroyContextState(frame->mainWorldScriptContext()); | |
| 121 } | |
| 122 | |
| 123 } // namespace content | |
| OLD | NEW |