| 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_context_state.h" | 5 #include "content/renderer/web_ui_mojo_context_state.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "content/common/frame_messages.h" |
| 10 #include "content/public/renderer/render_frame.h" |
| 9 #include "content/public/renderer/resource_fetcher.h" | 11 #include "content/public/renderer/resource_fetcher.h" |
| 10 #include "content/renderer/web_ui_runner.h" | 12 #include "content/renderer/web_ui_runner.h" |
| 11 #include "gin/converter.h" | 13 #include "gin/converter.h" |
| 12 #include "gin/modules/module_registry.h" | 14 #include "gin/modules/module_registry.h" |
| 13 #include "gin/per_context_data.h" | 15 #include "gin/per_context_data.h" |
| 14 #include "gin/public/context_holder.h" | 16 #include "gin/public/context_holder.h" |
| 15 #include "gin/try_catch.h" | 17 #include "gin/try_catch.h" |
| 16 #include "mojo/bindings/js/core.h" | 18 #include "mojo/bindings/js/core.h" |
| 17 #include "mojo/bindings/js/handle.h" | 19 #include "mojo/bindings/js/handle.h" |
| 18 #include "mojo/bindings/js/support.h" | 20 #include "mojo/bindings/js/support.h" |
| 19 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 21 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 20 #include "third_party/WebKit/public/web/WebFrame.h" | 22 #include "third_party/WebKit/public/web/WebFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebScriptSource.h" | 23 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 22 | 24 |
| 23 using v8::Context; | 25 using v8::Context; |
| 24 using v8::HandleScope; | 26 using v8::HandleScope; |
| 25 using v8::Isolate; | 27 using v8::Isolate; |
| 26 using v8::Object; | 28 using v8::Object; |
| 27 using v8::ObjectTemplate; | 29 using v8::ObjectTemplate; |
| 28 using v8::Script; | 30 using v8::Script; |
| 29 | 31 |
| 30 namespace content { | 32 namespace content { |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 // All modules have this prefixed to them when downloading. | 36 // All modules have this prefixed to them when downloading. |
| 35 // TODO(sky): move this into some common place. | 37 // TODO(sky): move this into some common place. |
| 36 const char kModulePrefix[] = "chrome://mojo/"; | 38 const char kModulePrefix[] = "chrome://mojo/"; |
| 37 | 39 |
| 38 void RunMain(base::WeakPtr<gin::Runner> runner, | |
| 39 mojo::ScopedMessagePipeHandle* handle, | |
| 40 v8::Handle<v8::Value> module) { | |
| 41 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); | |
| 42 v8::Handle<v8::Function> start; | |
| 43 CHECK(gin::ConvertFromV8(isolate, module, &start)); | |
| 44 v8::Handle<v8::Value> args[] = { | |
| 45 gin::ConvertToV8(isolate, mojo::Handle(handle->release().value())) }; | |
| 46 runner->Call(start, runner->global(), 1, args); | |
| 47 } | |
| 48 | |
| 49 } // namespace | 40 } // namespace |
| 50 | 41 |
| 51 // WebUIMojo ------------------------------------------------------------------- | 42 // WebUIMojo ------------------------------------------------------------------- |
| 52 | 43 |
| 53 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, | 44 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, |
| 54 v8::Handle<v8::Context> context) | 45 v8::Handle<v8::Context> context) |
| 55 : frame_(frame), | 46 : frame_(frame), |
| 56 module_added_(false) { | 47 module_added_(false) { |
| 57 gin::PerContextData* context_data = gin::PerContextData::From(context); | 48 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 58 gin::ContextHolder* context_holder = context_data->context_holder(); | 49 gin::ContextHolder* context_holder = context_data->context_holder(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 71 runner_->GetContextHolder()->context())->RemoveObserver(this); | 62 runner_->GetContextHolder()->context())->RemoveObserver(this); |
| 72 } | 63 } |
| 73 | 64 |
| 74 void WebUIMojoContextState::SetHandle(mojo::ScopedMessagePipeHandle handle) { | 65 void WebUIMojoContextState::SetHandle(mojo::ScopedMessagePipeHandle handle) { |
| 75 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 66 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 76 mojo::ScopedMessagePipeHandle* passed_handle = | 67 mojo::ScopedMessagePipeHandle* passed_handle = |
| 77 new mojo::ScopedMessagePipeHandle(handle.Pass()); | 68 new mojo::ScopedMessagePipeHandle(handle.Pass()); |
| 78 gin::ModuleRegistry::From(context_holder->context())->LoadModule( | 69 gin::ModuleRegistry::From(context_holder->context())->LoadModule( |
| 79 context_holder->isolate(), | 70 context_holder->isolate(), |
| 80 "main", | 71 "main", |
| 81 base::Bind(RunMain, runner_->GetWeakPtr(), base::Owned(passed_handle))); | 72 base::Bind(&WebUIMojoContextState::RunMain, |
| 73 AsWeakPtr(), |
| 74 base::Owned(passed_handle))); |
| 75 } |
| 76 |
| 77 void WebUIMojoContextState::RunMain(mojo::ScopedMessagePipeHandle* handle, |
| 78 v8::Handle<v8::Value> module) { |
| 79 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); |
| 80 v8::Handle<v8::Function> start; |
| 81 CHECK(gin::ConvertFromV8(isolate, module, &start)); |
| 82 v8::Handle<v8::Value> args[] = { |
| 83 gin::ConvertToV8(isolate, mojo::Handle(handle->release().value())) }; |
| 84 runner_->Call(start, runner_->global(), 1, args); |
| 85 |
| 86 RenderFrame* render_frame = RenderFrame::FromWebFrame(frame_); |
| 87 render_frame->Send(new FrameHostMsg_WebUIMojoMainRan( |
| 88 render_frame->GetRoutingID())); |
| 82 } | 89 } |
| 83 | 90 |
| 84 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { | 91 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { |
| 85 gin::Runner::Scope scoper(runner_.get()); | 92 gin::Runner::Scope scoper(runner_.get()); |
| 86 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 93 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 87 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 94 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 88 context_holder->context()); | 95 context_holder->context()); |
| 89 for (size_t i = 0; i < ids.size(); ++i) { | 96 for (size_t i = 0; i < ids.size(); ++i) { |
| 90 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && | 97 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
| 91 registry->available_modules().count(ids[i]) == 0) { | 98 registry->available_modules().count(ids[i]) == 0) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 const std::vector<std::string>& dependencies) { | 140 const std::vector<std::string>& dependencies) { |
| 134 FetchModules(dependencies); | 141 FetchModules(dependencies); |
| 135 | 142 |
| 136 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 143 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 137 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 144 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 138 context_holder->context()); | 145 context_holder->context()); |
| 139 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 146 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 140 } | 147 } |
| 141 | 148 |
| 142 } // namespace content | 149 } // namespace content |
| OLD | NEW |