| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/web_ui_mojo.h" | 5 #include "content/renderer/mojo_bindings_controller.h" |
| 6 | 6 |
| 7 #include "content/common/view_messages.h" | 7 #include "content/common/view_messages.h" |
| 8 #include "content/public/renderer/render_frame.h" | 8 #include "content/public/renderer/render_frame.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "content/renderer/web_ui_mojo_context_state.h" | 10 #include "content/renderer/mojo_context_state.h" |
| 11 #include "gin/per_context_data.h" | 11 #include "gin/per_context_data.h" |
| 12 #include "third_party/WebKit/public/web/WebKit.h" | 12 #include "third_party/WebKit/public/web/WebKit.h" |
| 13 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 13 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 14 #include "third_party/WebKit/public/web/WebView.h" | 14 #include "third_party/WebKit/public/web/WebView.h" |
| 15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char kWebUIMojoContextStateKey[] = "WebUIMojoContextState"; | 21 const char kMojoContextStateKey[] = "MojoContextState"; |
| 22 | 22 |
| 23 struct WebUIMojoContextStateData : public base::SupportsUserData::Data { | 23 struct MojoContextStateData : public base::SupportsUserData::Data { |
| 24 scoped_ptr<WebUIMojoContextState> state; | 24 scoped_ptr<MojoContextState> state; |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 WebUIMojo::MainFrameObserver::MainFrameObserver(WebUIMojo* web_ui_mojo) | 29 MojoBindingsController::MainFrameObserver::MainFrameObserver( |
| 30 MojoBindingsController* mojo_bindings_controller) |
| 30 : RenderFrameObserver(RenderFrame::FromWebFrame( | 31 : RenderFrameObserver(RenderFrame::FromWebFrame( |
| 31 web_ui_mojo->render_view()->GetWebView()->mainFrame())), | 32 mojo_bindings_controller->render_view()->GetWebView()->mainFrame())), |
| 32 web_ui_mojo_(web_ui_mojo) { | 33 mojo_bindings_controller_(mojo_bindings_controller) { |
| 33 } | 34 } |
| 34 | 35 |
| 35 WebUIMojo::MainFrameObserver::~MainFrameObserver() { | 36 MojoBindingsController::MainFrameObserver::~MainFrameObserver() { |
| 36 } | 37 } |
| 37 | 38 |
| 38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext( | 39 void MojoBindingsController::MainFrameObserver::WillReleaseScriptContext( |
| 39 v8::Local<v8::Context> context, | 40 v8::Local<v8::Context> context, |
| 40 int world_id) { | 41 int world_id) { |
| 41 web_ui_mojo_->DestroyContextState(context); | 42 mojo_bindings_controller_->DestroyContextState(context); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() { | 45 void MojoBindingsController::MainFrameObserver::DidFinishDocumentLoad() { |
| 45 web_ui_mojo_->OnDidFinishDocumentLoad(); | 46 mojo_bindings_controller_->OnDidFinishDocumentLoad(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void WebUIMojo::MainFrameObserver::OnDestruct() { | 49 void MojoBindingsController::MainFrameObserver::OnDestruct() { |
| 49 } | 50 } |
| 50 | 51 |
| 51 WebUIMojo::WebUIMojo(RenderView* render_view) | 52 MojoBindingsController::MojoBindingsController(RenderView* render_view) |
| 52 : RenderViewObserver(render_view), | 53 : RenderViewObserver(render_view), |
| 53 RenderViewObserverTracker<WebUIMojo>(render_view), | 54 RenderViewObserverTracker<MojoBindingsController>(render_view), |
| 54 main_frame_observer_(this) { | 55 main_frame_observer_(this) { |
| 55 } | 56 } |
| 56 | 57 |
| 57 WebUIMojo::~WebUIMojo() { | 58 MojoBindingsController::~MojoBindingsController() { |
| 58 } | 59 } |
| 59 | 60 |
| 60 void WebUIMojo::CreateContextState() { | 61 void MojoBindingsController::CreateContextState() { |
| 61 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 62 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 62 blink::WebLocalFrame* frame = | 63 blink::WebLocalFrame* frame = |
| 63 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | 64 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); |
| 64 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 65 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 65 gin::PerContextData* context_data = gin::PerContextData::From(context); | 66 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 66 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; | 67 MojoContextStateData* data = new MojoContextStateData; |
| 67 data->state.reset(new WebUIMojoContextState( | 68 data->state.reset( |
| 68 render_view()->GetWebView()->mainFrame(), context)); | 69 new MojoContextState(render_view()->GetWebView()->mainFrame(), context)); |
| 69 context_data->SetUserData(kWebUIMojoContextStateKey, data); | 70 context_data->SetUserData(kMojoContextStateKey, data); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void WebUIMojo::DestroyContextState(v8::Local<v8::Context> context) { | 73 void MojoBindingsController::DestroyContextState( |
| 74 v8::Local<v8::Context> context) { |
| 73 gin::PerContextData* context_data = gin::PerContextData::From(context); | 75 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 74 if (!context_data) | 76 if (!context_data) |
| 75 return; | 77 return; |
| 76 context_data->RemoveUserData(kWebUIMojoContextStateKey); | 78 context_data->RemoveUserData(kMojoContextStateKey); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void WebUIMojo::OnDidFinishDocumentLoad() { | 81 void MojoBindingsController::OnDidFinishDocumentLoad() { |
| 80 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 82 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 81 WebUIMojoContextState* state = GetContextState(); | 83 MojoContextState* state = GetContextState(); |
| 82 if (state) | 84 if (state) |
| 83 state->Run(); | 85 state->Run(); |
| 84 } | 86 } |
| 85 | 87 |
| 86 WebUIMojoContextState* WebUIMojo::GetContextState() { | 88 MojoContextState* MojoBindingsController::GetContextState() { |
| 87 blink::WebLocalFrame* frame = | 89 blink::WebLocalFrame* frame = |
| 88 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | 90 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); |
| 89 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 91 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 90 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | 92 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 91 gin::PerContextData* context_data = gin::PerContextData::From(context); | 93 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 92 if (!context_data) | 94 if (!context_data) |
| 93 return NULL; | 95 return NULL; |
| 94 WebUIMojoContextStateData* context_state = | 96 MojoContextStateData* context_state = static_cast<MojoContextStateData*>( |
| 95 static_cast<WebUIMojoContextStateData*>( | 97 context_data->GetUserData(kMojoContextStateKey)); |
| 96 context_data->GetUserData(kWebUIMojoContextStateKey)); | |
| 97 return context_state ? context_state->state.get() : NULL; | 98 return context_state ? context_state->state.get() : NULL; |
| 98 } | 99 } |
| 99 | 100 |
| 100 void WebUIMojo::DidCreateDocumentElement(blink::WebLocalFrame* frame) { | 101 void MojoBindingsController::DidCreateDocumentElement( |
| 102 blink::WebLocalFrame* frame) { |
| 101 CreateContextState(); | 103 CreateContextState(); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void WebUIMojo::DidClearWindowObject(blink::WebLocalFrame* frame) { | 106 void MojoBindingsController::DidClearWindowObject(blink::WebLocalFrame* frame) { |
| 105 if (frame != render_view()->GetWebView()->mainFrame()) | 107 if (frame != render_view()->GetWebView()->mainFrame()) |
| 106 return; | 108 return; |
| 107 | 109 |
| 108 // NOTE: this function may be called early on twice. From the constructor | 110 // NOTE: this function may be called early on twice. From the constructor |
| 109 // mainWorldScriptContext() may trigger this to be called. If we are created | 111 // 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 | 112 // 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 | 113 // 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 | 114 // 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 | 115 // 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. | 116 // be empty and we don't need to destroy it. |
| 115 WebUIMojoContextState* state = GetContextState(); | 117 MojoContextState* state = GetContextState(); |
| 116 if (state && !state->module_added()) | 118 if (state && !state->module_added()) |
| 117 return; | 119 return; |
| 118 | 120 |
| 119 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 121 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 120 DestroyContextState(frame->mainWorldScriptContext()); | 122 DestroyContextState(frame->mainWorldScriptContext()); |
| 121 } | 123 } |
| 122 | 124 |
| 123 } // namespace content | 125 } // namespace content |
| OLD | NEW |