| 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/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/web_ui_mojo_context_state.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 WebUIMojo::MainFrameObserver::~MainFrameObserver() { | 35 WebUIMojo::MainFrameObserver::~MainFrameObserver() { |
| 36 } | 36 } |
| 37 | 37 |
| 38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext( | 38 void WebUIMojo::MainFrameObserver::WillReleaseScriptContext( |
| 39 v8::Handle<v8::Context> context, | 39 v8::Handle<v8::Context> context, |
| 40 int world_id) { | 40 int world_id) { |
| 41 web_ui_mojo_->DestroyContextState(context); | 41 web_ui_mojo_->DestroyContextState(context); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void WebUIMojo::MainFrameObserver::DidFinishDocumentLoad() { |
| 45 web_ui_mojo_->OnDidFinishDocumentLoad(); |
| 46 } |
| 47 |
| 44 WebUIMojo::WebUIMojo(RenderView* render_view) | 48 WebUIMojo::WebUIMojo(RenderView* render_view) |
| 45 : RenderViewObserver(render_view), | 49 : RenderViewObserver(render_view), |
| 46 RenderViewObserverTracker<WebUIMojo>(render_view), | 50 RenderViewObserverTracker<WebUIMojo>(render_view), |
| 47 main_frame_observer_(this) { | 51 main_frame_observer_(this), |
| 52 did_finish_document_load_(false) { |
| 48 CreateContextState(); | 53 CreateContextState(); |
| 49 } | 54 } |
| 50 | 55 |
| 51 void WebUIMojo::SetBrowserHandle(mojo::ScopedMessagePipeHandle handle) { | 56 void WebUIMojo::SetBrowserHandle(mojo::ScopedMessagePipeHandle handle) { |
| 52 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 57 if (did_finish_document_load_) |
| 53 WebUIMojoContextState* state = GetContextState(); | 58 SetHandleOnContextState(handle.Pass()); |
| 54 if (state) | 59 else |
| 55 state->SetHandle(handle.Pass()); | 60 pending_handle_ = handle.Pass(); |
| 56 } | 61 } |
| 57 | 62 |
| 58 WebUIMojo::~WebUIMojo() { | 63 WebUIMojo::~WebUIMojo() { |
| 59 } | 64 } |
| 60 | 65 |
| 61 void WebUIMojo::CreateContextState() { | 66 void WebUIMojo::CreateContextState() { |
| 62 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 67 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 63 blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); | 68 blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 64 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 69 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 65 gin::PerContextData* context_data = gin::PerContextData::From(context); | 70 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 66 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; | 71 WebUIMojoContextStateData* data = new WebUIMojoContextStateData; |
| 67 data->state.reset(new WebUIMojoContextState( | 72 data->state.reset(new WebUIMojoContextState( |
| 68 render_view()->GetWebView()->mainFrame(), context)); | 73 render_view()->GetWebView()->mainFrame(), context)); |
| 69 context_data->SetUserData(kWebUIMojoContextStateKey, data); | 74 context_data->SetUserData(kWebUIMojoContextStateKey, data); |
| 70 } | 75 } |
| 71 | 76 |
| 72 void WebUIMojo::DestroyContextState(v8::Handle<v8::Context> context) { | 77 void WebUIMojo::DestroyContextState(v8::Handle<v8::Context> context) { |
| 73 gin::PerContextData* context_data = gin::PerContextData::From(context); | 78 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 74 if (!context_data) | 79 if (!context_data) |
| 75 return; | 80 return; |
| 76 context_data->RemoveUserData(kWebUIMojoContextStateKey); | 81 context_data->RemoveUserData(kWebUIMojoContextStateKey); |
| 77 } | 82 } |
| 78 | 83 |
| 84 void WebUIMojo::OnDidFinishDocumentLoad() { |
| 85 did_finish_document_load_ = true; |
| 86 if (pending_handle_.is_valid()) |
| 87 SetHandleOnContextState(pending_handle_.Pass()); |
| 88 } |
| 89 |
| 90 void WebUIMojo::SetHandleOnContextState(mojo::ScopedMessagePipeHandle handle) { |
| 91 DCHECK(did_finish_document_load_); |
| 92 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 93 WebUIMojoContextState* state = GetContextState(); |
| 94 if (state) |
| 95 state->SetHandle(handle.Pass()); |
| 96 } |
| 97 |
| 79 WebUIMojoContextState* WebUIMojo::GetContextState() { | 98 WebUIMojoContextState* WebUIMojo::GetContextState() { |
| 80 blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); | 99 blink::WebFrame* frame = render_view()->GetWebView()->mainFrame(); |
| 81 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 100 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 82 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); | 101 v8::Handle<v8::Context> context = frame->mainWorldScriptContext(); |
| 83 gin::PerContextData* context_data = gin::PerContextData::From(context); | 102 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 84 if (!context_data) | 103 if (!context_data) |
| 85 return NULL; | 104 return NULL; |
| 86 WebUIMojoContextStateData* context_state = | 105 WebUIMojoContextStateData* context_state = |
| 87 static_cast<WebUIMojoContextStateData*>( | 106 static_cast<WebUIMojoContextStateData*>( |
| 88 context_data->GetUserData(kWebUIMojoContextStateKey)); | 107 context_data->GetUserData(kWebUIMojoContextStateKey)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 WebUIMojoContextState* state = GetContextState(); | 122 WebUIMojoContextState* state = GetContextState(); |
| 104 if (state && !state->module_added()) | 123 if (state && !state->module_added()) |
| 105 return; | 124 return; |
| 106 | 125 |
| 107 v8::HandleScope handle_scope(blink::mainThreadIsolate()); | 126 v8::HandleScope handle_scope(blink::mainThreadIsolate()); |
| 108 DestroyContextState(frame->mainWorldScriptContext()); | 127 DestroyContextState(frame->mainWorldScriptContext()); |
| 109 CreateContextState(); | 128 CreateContextState(); |
| 110 } | 129 } |
| 111 | 130 |
| 112 } // namespace content | 131 } // namespace content |
| OLD | NEW |