Chromium Code Reviews| 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/public/renderer/resource_fetcher.h" | 9 #include "content/public/renderer/resource_fetcher.h" |
| 10 #include "content/renderer/render_view_impl.h" | |
| 10 #include "content/renderer/web_ui_runner.h" | 11 #include "content/renderer/web_ui_runner.h" |
| 11 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 12 #include "gin/modules/module_registry.h" | 13 #include "gin/modules/module_registry.h" |
| 13 #include "gin/per_context_data.h" | 14 #include "gin/per_context_data.h" |
| 14 #include "gin/public/context_holder.h" | 15 #include "gin/public/context_holder.h" |
| 15 #include "gin/try_catch.h" | 16 #include "gin/try_catch.h" |
| 16 #include "mojo/bindings/js/core.h" | 17 #include "mojo/bindings/js/core.h" |
| 17 #include "mojo/bindings/js/handle.h" | 18 #include "mojo/bindings/js/handle.h" |
| 18 #include "mojo/bindings/js/support.h" | 19 #include "mojo/bindings/js/support.h" |
| 19 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 20 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 20 #include "third_party/WebKit/public/web/WebFrame.h" | 21 #include "third_party/WebKit/public/web/WebFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebScriptSource.h" | 22 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 23 #include "third_party/WebKit/public/web/WebView.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(RenderViewImpl* render_view, |
| 54 v8::Handle<v8::Context> context) | 45 v8::Handle<v8::Context> context) |
| 55 : frame_(frame), | 46 : frame_(render_view->GetWebView()->mainFrame()), |
| 47 render_view_impl_(render_view), | |
| 56 module_added_(false) { | 48 module_added_(false) { |
| 57 gin::PerContextData* context_data = gin::PerContextData::From(context); | 49 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 58 gin::ContextHolder* context_holder = context_data->context_holder(); | 50 gin::ContextHolder* context_holder = context_data->context_holder(); |
| 59 runner_.reset(new WebUIRunner(frame_, context_holder)); | 51 runner_.reset(new WebUIRunner(frame_, context_holder)); |
| 60 gin::Runner::Scope scoper(runner_.get()); | 52 gin::Runner::Scope scoper(runner_.get()); |
| 61 gin::ModuleRegistry::From(context)->AddObserver(this); | 53 gin::ModuleRegistry::From(context)->AddObserver(this); |
| 62 runner_->RegisterBuiltinModules(); | 54 runner_->RegisterBuiltinModules(); |
| 63 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); | 55 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); |
| 64 // Warning |frame| may be destroyed. | 56 // Warning |frame| may be destroyed. |
| 65 // TODO(sky): add test for this. | 57 // TODO(sky): add test for this. |
| 66 } | 58 } |
| 67 | 59 |
| 68 WebUIMojoContextState::~WebUIMojoContextState() { | 60 WebUIMojoContextState::~WebUIMojoContextState() { |
| 69 gin::Runner::Scope scoper(runner_.get()); | 61 gin::Runner::Scope scoper(runner_.get()); |
| 70 gin::ModuleRegistry::From( | 62 gin::ModuleRegistry::From( |
| 71 runner_->GetContextHolder()->context())->RemoveObserver(this); | 63 runner_->GetContextHolder()->context())->RemoveObserver(this); |
| 72 } | 64 } |
| 73 | 65 |
| 74 void WebUIMojoContextState::SetHandle(mojo::ScopedMessagePipeHandle handle) { | 66 void WebUIMojoContextState::SetHandle(mojo::ScopedMessagePipeHandle handle) { |
| 75 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 67 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 76 mojo::ScopedMessagePipeHandle* passed_handle = | 68 mojo::ScopedMessagePipeHandle* passed_handle = |
| 77 new mojo::ScopedMessagePipeHandle(handle.Pass()); | 69 new mojo::ScopedMessagePipeHandle(handle.Pass()); |
| 78 gin::ModuleRegistry::From(context_holder->context())->LoadModule( | 70 gin::ModuleRegistry::From(context_holder->context())->LoadModule( |
| 79 context_holder->isolate(), | 71 context_holder->isolate(), |
| 80 "main", | 72 "main", |
| 81 base::Bind(RunMain, runner_->GetWeakPtr(), base::Owned(passed_handle))); | 73 base::Bind(&WebUIMojoContextState::RunMain, |
| 74 AsWeakPtr(), | |
| 75 runner_->GetWeakPtr(), | |
| 76 base::Owned(passed_handle))); | |
| 77 } | |
| 78 | |
| 79 void WebUIMojoContextState::RunMain(base::WeakPtr<gin::Runner> runner, | |
|
sky
2014/07/23 23:35:10
You shouldn't need to pass in runner_ since it's a
| |
| 80 mojo::ScopedMessagePipeHandle* handle, | |
| 81 v8::Handle<v8::Value> module) { | |
| 82 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); | |
| 83 v8::Handle<v8::Function> start; | |
| 84 CHECK(gin::ConvertFromV8(isolate, module, &start)); | |
| 85 v8::Handle<v8::Value> args[] = { | |
| 86 gin::ConvertToV8(isolate, mojo::Handle(handle->release().value())) }; | |
| 87 runner->Call(start, runner->global(), 1, args); | |
| 88 | |
| 89 render_view_impl_->WebUIMojoMainRan(); | |
| 82 } | 90 } |
| 83 | 91 |
| 84 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { | 92 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { |
| 85 gin::Runner::Scope scoper(runner_.get()); | 93 gin::Runner::Scope scoper(runner_.get()); |
| 86 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 94 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 87 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 95 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 88 context_holder->context()); | 96 context_holder->context()); |
| 89 for (size_t i = 0; i < ids.size(); ++i) { | 97 for (size_t i = 0; i < ids.size(); ++i) { |
| 90 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && | 98 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
| 91 registry->available_modules().count(ids[i]) == 0) { | 99 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) { | 141 const std::vector<std::string>& dependencies) { |
| 134 FetchModules(dependencies); | 142 FetchModules(dependencies); |
| 135 | 143 |
| 136 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 144 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 137 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 145 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 138 context_holder->context()); | 146 context_holder->context()); |
| 139 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 147 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 140 } | 148 } |
| 141 | 149 |
| 142 } // namespace content | 150 } // namespace content |
| OLD | NEW |