| 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/web_ui_mojo.h" |
| 6 | 6 |
| 7 #include "content/common/view_messages.h" | 7 #include "content/common/view_messages.h" |
| 8 #include "content/public/common/service_registry.h" | |
| 9 #include "content/public/renderer/render_frame.h" | 8 #include "content/public/renderer/render_frame.h" |
| 10 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 11 #include "content/renderer/web_ui_mojo_context_state.h" | 10 #include "content/renderer/web_ui_mojo_context_state.h" |
| 12 #include "gin/per_context_data.h" | 11 #include "gin/per_context_data.h" |
| 13 #include "third_party/WebKit/public/web/WebKit.h" | 12 #include "third_party/WebKit/public/web/WebKit.h" |
| 14 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 13 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 15 #include "third_party/WebKit/public/web/WebView.h" | 14 #include "third_party/WebKit/public/web/WebView.h" |
| 16 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 17 | 16 |
| 18 namespace content { | 17 namespace content { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 42 web_ui_mojo_->DestroyContextState(context); | 41 web_ui_mojo_->DestroyContextState(context); |
| 43 } | 42 } |
| 44 | 43 |
| 45 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() { | 44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() { |
| 46 web_ui_mojo_->OnDidFinishDocumentLoad(); | 45 web_ui_mojo_->OnDidFinishDocumentLoad(); |
| 47 } | 46 } |
| 48 | 47 |
| 49 WebUIMojo::WebUIMojo(RenderView* render_view) | 48 WebUIMojo::WebUIMojo(RenderView* render_view) |
| 50 : RenderViewObserver(render_view), | 49 : RenderViewObserver(render_view), |
| 51 RenderViewObserverTracker<WebUIMojo>(render_view), | 50 RenderViewObserverTracker<WebUIMojo>(render_view), |
| 52 main_frame_observer_(this), | 51 main_frame_observer_(this) { |
| 53 did_finish_document_load_(false) { | |
| 54 CreateContextState(); | 52 CreateContextState(); |
| 55 } | 53 } |
| 56 | 54 |
| 57 void WebUIMojo::SetBrowserHandle(mojo::ScopedMessagePipeHandle handle) { | |
| 58 if (did_finish_document_load_) | |
| 59 SetHandleOnContextState(handle.Pass()); | |
| 60 else | |
| 61 pending_handle_ = handle.Pass(); | |
| 62 } | |
| 63 | |
| 64 WebUIMojo::~WebUIMojo() { | 55 WebUIMojo::~WebUIMojo() { |
| 65 } | 56 } |
| 66 | 57 |
| 67 void WebUIMojo::CreateContextState() { | 58 void WebUIMojo::CreateContextState() { |
| 68 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 59 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 69 blink::WebLocalFrame* frame = | 60 blink::WebLocalFrame* frame = |
| 70 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | 61 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); |
| 71 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 62 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 72 gin::PerContextData* context_data = gin::PerContextData::From(context); | 63 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 73 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; | 64 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; |
| 74 data->state.reset(new WebUIMojoContextState( | 65 data->state.reset(new WebUIMojoContextState( |
| 75 render_view()->GetWebView()->mainFrame(), context)); | 66 render_view()->GetWebView()->mainFrame(), context)); |
| 76 context_data->SetUserData(kWebUIMojoContextStateKey, data); | 67 context_data->SetUserData(kWebUIMojoContextStateKey, data); |
| 77 } | 68 } |
| 78 | 69 |
| 79 void WebUIMojo::DestroyContextState(v8::Handle<v8::Context> context) { | 70 void WebUIMojo::DestroyContextState(v8::Handle<v8::Context> context) { |
| 80 gin::PerContextData* context_data = gin::PerContextData::From(context); | 71 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 81 if (!context_data) | 72 if (!context_data) |
| 82 return; | 73 return; |
| 83 context_data->RemoveUserData(kWebUIMojoContextStateKey); | 74 context_data->RemoveUserData(kWebUIMojoContextStateKey); |
| 84 } | 75 } |
| 85 | 76 |
| 86 void WebUIMojo::OnDidFinishDocumentLoad() { | 77 void WebUIMojo::OnDidFinishDocumentLoad() { |
| 87 did_finish_document_load_ = true; | |
| 88 mojo::MessagePipe pipe; | |
| 89 SetHandleOnContextState(pipe.handle0.Pass()); | |
| 90 RenderFrame::FromWebFrame(render_view()->GetWebView()->mainFrame())-> | |
| 91 GetServiceRegistry()-> | |
| 92 GetRemoteInterface("webui_controller", pipe.handle1.Pass()); | |
| 93 } | |
| 94 | |
| 95 void WebUIMojo::SetHandleOnContextState(mojo::ScopedMessagePipeHandle handle) { | |
| 96 DCHECK(did_finish_document_load_); | |
| 97 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 78 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 98 WebUIMojoContextState* state = GetContextState(); | 79 WebUIMojoContextState* state = GetContextState(); |
| 99 if (state) | 80 if (state) |
| 100 state->SetHandle(handle.Pass()); | 81 state->Run(); |
| 101 } | 82 } |
| 102 | 83 |
| 103 WebUIMojoContextState* WebUIMojo::GetContextState() { | 84 WebUIMojoContextState* WebUIMojo::GetContextState() { |
| 104 blink::WebLocalFrame* frame = | 85 blink::WebLocalFrame* frame = |
| 105 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); | 86 render_view()->GetWebView()->mainFrame()->toWebLocalFrame(); |
| 106 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 87 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 107 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 88 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 108 gin::PerContextData* context_data = gin::PerContextData::From(context); | 89 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 109 if (!context_data) | 90 if (!context_data) |
| 110 return NULL; | 91 return NULL; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 128 WebUIMojoContextState* state = GetContextState(); | 109 WebUIMojoContextState* state = GetContextState(); |
| 129 if (state && !state->module_added()) | 110 if (state && !state->module_added()) |
| 130 return; | 111 return; |
| 131 | 112 |
| 132 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 113 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 133 DestroyContextState(frame->mainWorldScriptContext()); | 114 DestroyContextState(frame->mainWorldScriptContext()); |
| 134 CreateContextState(); | 115 CreateContextState(); |
| 135 } | 116 } |
| 136 | 117 |
| 137 } // namespace content | 118 } // namespace content |
| OLD | NEW |