| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "gin/modules/module_registry.h" | 5 #include "gin/modules/module_registry.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | |
| 10 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "gin/arguments.h" | 14 #include "gin/arguments.h" |
| 15 #include "gin/converter.h" | 15 #include "gin/converter.h" |
| 16 #include "gin/modules/module_registry_observer.h" | 16 #include "gin/modules/module_registry_observer.h" |
| 17 #include "gin/per_context_data.h" | 17 #include "gin/per_context_data.h" |
| 18 #include "gin/per_isolate_data.h" | 18 #include "gin/per_isolate_data.h" |
| 19 #include "gin/public/wrapper_info.h" | 19 #include "gin/public/wrapper_info.h" |
| 20 #include "gin/runner.h" | 20 #include "gin/runner.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (!args.GetNext(&factory)) | 76 if (!args.GetNext(&factory)) |
| 77 return args.ThrowError(); | 77 return args.ThrowError(); |
| 78 | 78 |
| 79 scoped_ptr<PendingModule> pending(new PendingModule); | 79 scoped_ptr<PendingModule> pending(new PendingModule); |
| 80 pending->id = id; | 80 pending->id = id; |
| 81 pending->dependencies = dependencies; | 81 pending->dependencies = dependencies; |
| 82 pending->factory.Reset(args.isolate(), factory); | 82 pending->factory.Reset(args.isolate(), factory); |
| 83 | 83 |
| 84 ModuleRegistry* registry = | 84 ModuleRegistry* registry = |
| 85 ModuleRegistry::From(args.isolate()->GetCurrentContext()); | 85 ModuleRegistry::From(args.isolate()->GetCurrentContext()); |
| 86 registry->AddPendingModule(args.isolate(), pending.Pass()); | 86 registry->AddPendingModule(args.isolate(), std::move(pending)); |
| 87 } | 87 } |
| 88 | 88 |
| 89 WrapperInfo g_wrapper_info = { kEmbedderNativeGin }; | 89 WrapperInfo g_wrapper_info = { kEmbedderNativeGin }; |
| 90 | 90 |
| 91 Local<FunctionTemplate> GetDefineTemplate(Isolate* isolate) { | 91 Local<FunctionTemplate> GetDefineTemplate(Isolate* isolate) { |
| 92 PerIsolateData* data = PerIsolateData::From(isolate); | 92 PerIsolateData* data = PerIsolateData::From(isolate); |
| 93 Local<FunctionTemplate> templ = data->GetFunctionTemplate( | 93 Local<FunctionTemplate> templ = data->GetFunctionTemplate( |
| 94 &g_wrapper_info); | 94 &g_wrapper_info); |
| 95 if (templ.IsEmpty()) { | 95 if (templ.IsEmpty()) { |
| 96 templ = FunctionTemplate::New(isolate, Define); | 96 templ = FunctionTemplate::New(isolate, Define); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, const std::string& id, | 154 void ModuleRegistry::AddBuiltinModule(Isolate* isolate, const std::string& id, |
| 155 v8::Local<Value> module) { | 155 v8::Local<Value> module) { |
| 156 DCHECK(!id.empty()); | 156 DCHECK(!id.empty()); |
| 157 RegisterModule(isolate, id, module); | 157 RegisterModule(isolate, id, module); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void ModuleRegistry::AddPendingModule(Isolate* isolate, | 160 void ModuleRegistry::AddPendingModule(Isolate* isolate, |
| 161 scoped_ptr<PendingModule> pending) { | 161 scoped_ptr<PendingModule> pending) { |
| 162 const std::string pending_id = pending->id; | 162 const std::string pending_id = pending->id; |
| 163 const std::vector<std::string> pending_dependencies = pending->dependencies; | 163 const std::vector<std::string> pending_dependencies = pending->dependencies; |
| 164 AttemptToLoad(isolate, pending.Pass()); | 164 AttemptToLoad(isolate, std::move(pending)); |
| 165 FOR_EACH_OBSERVER(ModuleRegistryObserver, observer_list_, | 165 FOR_EACH_OBSERVER(ModuleRegistryObserver, observer_list_, |
| 166 OnDidAddPendingModule(pending_id, pending_dependencies)); | 166 OnDidAddPendingModule(pending_id, pending_dependencies)); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void ModuleRegistry::LoadModule(Isolate* isolate, | 169 void ModuleRegistry::LoadModule(Isolate* isolate, |
| 170 const std::string& id, | 170 const std::string& id, |
| 171 LoadModuleCallback callback) { | 171 LoadModuleCallback callback) { |
| 172 if (available_modules_.find(id) != available_modules_.end()) { | 172 if (available_modules_.find(id) != available_modules_.end()) { |
| 173 // Should we call the callback asynchronously? | 173 // Should we call the callback asynchronously? |
| 174 callback.Run(GetModule(isolate, id)); | 174 callback.Run(GetModule(isolate, id)); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 251 |
| 252 return RegisterModule(isolate, pending->id, module); | 252 return RegisterModule(isolate, pending->id, module); |
| 253 } | 253 } |
| 254 | 254 |
| 255 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 255 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
| 256 scoped_ptr<PendingModule> pending) { | 256 scoped_ptr<PendingModule> pending) { |
| 257 if (!CheckDependencies(pending.get())) { | 257 if (!CheckDependencies(pending.get())) { |
| 258 pending_modules_.push_back(pending.release()); | 258 pending_modules_.push_back(pending.release()); |
| 259 return false; | 259 return false; |
| 260 } | 260 } |
| 261 return Load(isolate, pending.Pass()); | 261 return Load(isolate, std::move(pending)); |
| 262 } | 262 } |
| 263 | 263 |
| 264 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, | 264 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, |
| 265 const std::string& id) { | 265 const std::string& id) { |
| 266 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); | 266 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
| 267 v8::Local<String> key = StringToSymbol(isolate, id); | 267 v8::Local<String> key = StringToSymbol(isolate, id); |
| 268 DCHECK(modules->HasOwnProperty(isolate->GetCurrentContext(), key).FromJust()); | 268 DCHECK(modules->HasOwnProperty(isolate->GetCurrentContext(), key).FromJust()); |
| 269 return modules->Get(isolate->GetCurrentContext(), key).ToLocalChecked(); | 269 return modules->Get(isolate->GetCurrentContext(), key).ToLocalChecked(); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 272 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
| 273 bool keep_trying = true; | 273 bool keep_trying = true; |
| 274 while (keep_trying) { | 274 while (keep_trying) { |
| 275 keep_trying = false; | 275 keep_trying = false; |
| 276 PendingModuleVector pending_modules; | 276 PendingModuleVector pending_modules; |
| 277 pending_modules.swap(pending_modules_); | 277 pending_modules.swap(pending_modules_); |
| 278 for (size_t i = 0; i < pending_modules.size(); ++i) { | 278 for (size_t i = 0; i < pending_modules.size(); ++i) { |
| 279 scoped_ptr<PendingModule> pending(pending_modules[i]); | 279 scoped_ptr<PendingModule> pending(pending_modules[i]); |
| 280 pending_modules[i] = NULL; | 280 pending_modules[i] = NULL; |
| 281 if (AttemptToLoad(isolate, pending.Pass())) | 281 if (AttemptToLoad(isolate, std::move(pending))) |
| 282 keep_trying = true; | 282 keep_trying = true; |
| 283 } | 283 } |
| 284 } | 284 } |
| 285 } | 285 } |
| 286 | 286 |
| 287 } // namespace gin | 287 } // namespace gin |
| OLD | NEW |