| 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/web_ui_runner.h" | 10 #include "content/renderer/web_ui_runner.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 namespace content { | 30 namespace content { |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // All modules have this prefixed to them when downloading. | 34 // All modules have this prefixed to them when downloading. |
| 35 // TODO(sky): move this into some common place. | 35 // TODO(sky): move this into some common place. |
| 36 const char kModulePrefix[] = "chrome://mojo/"; | 36 const char kModulePrefix[] = "chrome://mojo/"; |
| 37 | 37 |
| 38 void RunMain(base::WeakPtr<gin::Runner> runner, | 38 void RunMain(base::WeakPtr<gin::Runner> runner, |
| 39 mojo::ScopedMessagePipeHandle* handle, | |
| 40 v8::Handle<v8::Value> module) { | 39 v8::Handle<v8::Value> module) { |
| 41 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); | 40 v8::Isolate* isolate = runner->GetContextHolder()->isolate(); |
| 42 v8::Handle<v8::Function> start; | 41 v8::Handle<v8::Function> start; |
| 43 CHECK(gin::ConvertFromV8(isolate, module, &start)); | 42 CHECK(gin::ConvertFromV8(isolate, module, &start)); |
| 44 v8::Handle<v8::Value> args[] = { | 43 runner->Call(start, runner->global(), 0, NULL); |
| 45 gin::ConvertToV8(isolate, mojo::Handle(handle->release().value())) }; | |
| 46 runner->Call(start, runner->global(), 1, args); | |
| 47 } | 44 } |
| 48 | 45 |
| 49 } // namespace | 46 } // namespace |
| 50 | 47 |
| 51 // WebUIMojo ------------------------------------------------------------------- | 48 // WebUIMojo ------------------------------------------------------------------- |
| 52 | 49 |
| 53 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, | 50 WebUIMojoContextState::WebUIMojoContextState(blink::WebFrame* frame, |
| 54 v8::Handle<v8::Context> context) | 51 v8::Handle<v8::Context> context) |
| 55 : frame_(frame), | 52 : frame_(frame), |
| 56 module_added_(false) { | 53 module_added_(false) { |
| 57 gin::PerContextData* context_data = gin::PerContextData::From(context); | 54 gin::PerContextData* context_data = gin::PerContextData::From(context); |
| 58 gin::ContextHolder* context_holder = context_data->context_holder(); | 55 gin::ContextHolder* context_holder = context_data->context_holder(); |
| 59 runner_.reset(new WebUIRunner(frame_, context_holder)); | 56 runner_.reset(new WebUIRunner(frame_, context_holder)); |
| 60 gin::Runner::Scope scoper(runner_.get()); | 57 gin::Runner::Scope scoper(runner_.get()); |
| 61 gin::ModuleRegistry::From(context)->AddObserver(this); | 58 gin::ModuleRegistry::From(context)->AddObserver(this); |
| 62 runner_->RegisterBuiltinModules(); | 59 runner_->RegisterBuiltinModules(); |
| 63 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); | 60 gin::ModuleRegistry::InstallGlobals(context->GetIsolate(), context->Global()); |
| 64 // Warning |frame| may be destroyed. | 61 // Warning |frame| may be destroyed. |
| 65 // TODO(sky): add test for this. | 62 // TODO(sky): add test for this. |
| 66 } | 63 } |
| 67 | 64 |
| 68 WebUIMojoContextState::~WebUIMojoContextState() { | 65 WebUIMojoContextState::~WebUIMojoContextState() { |
| 69 gin::Runner::Scope scoper(runner_.get()); | 66 gin::Runner::Scope scoper(runner_.get()); |
| 70 gin::ModuleRegistry::From( | 67 gin::ModuleRegistry::From( |
| 71 runner_->GetContextHolder()->context())->RemoveObserver(this); | 68 runner_->GetContextHolder()->context())->RemoveObserver(this); |
| 72 } | 69 } |
| 73 | 70 |
| 74 void WebUIMojoContextState::SetHandle(mojo::ScopedMessagePipeHandle handle) { | 71 void WebUIMojoContextState::Run() { |
| 75 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 72 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 76 mojo::ScopedMessagePipeHandle* passed_handle = | |
| 77 new mojo::ScopedMessagePipeHandle(handle.Pass()); | |
| 78 gin::ModuleRegistry::From(context_holder->context())->LoadModule( | 73 gin::ModuleRegistry::From(context_holder->context())->LoadModule( |
| 79 context_holder->isolate(), | 74 context_holder->isolate(), |
| 80 "main", | 75 "main", |
| 81 base::Bind(RunMain, runner_->GetWeakPtr(), base::Owned(passed_handle))); | 76 base::Bind(RunMain, runner_->GetWeakPtr())); |
| 82 } | 77 } |
| 83 | 78 |
| 84 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { | 79 void WebUIMojoContextState::FetchModules(const std::vector<std::string>& ids) { |
| 85 gin::Runner::Scope scoper(runner_.get()); | 80 gin::Runner::Scope scoper(runner_.get()); |
| 86 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 81 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 87 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 82 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 88 context_holder->context()); | 83 context_holder->context()); |
| 89 for (size_t i = 0; i < ids.size(); ++i) { | 84 for (size_t i = 0; i < ids.size(); ++i) { |
| 90 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && | 85 if (fetched_modules_.find(ids[i]) == fetched_modules_.end() && |
| 91 registry->available_modules().count(ids[i]) == 0) { | 86 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) { | 128 const std::vector<std::string>& dependencies) { |
| 134 FetchModules(dependencies); | 129 FetchModules(dependencies); |
| 135 | 130 |
| 136 gin::ContextHolder* context_holder = runner_->GetContextHolder(); | 131 gin::ContextHolder* context_holder = runner_->GetContextHolder(); |
| 137 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( | 132 gin::ModuleRegistry* registry = gin::ModuleRegistry::From( |
| 138 context_holder->context()); | 133 context_holder->context()); |
| 139 registry->AttemptToLoadMoreModules(context_holder->isolate()); | 134 registry->AttemptToLoadMoreModules(context_holder->isolate()); |
| 140 } | 135 } |
| 141 | 136 |
| 142 } // namespace content | 137 } // namespace content |
| OLD | NEW |