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 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 PendingModule::~PendingModule() { | 49 PendingModule::~PendingModule() { |
50 factory.Reset(); | 50 factory.Reset(); |
51 } | 51 } |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 // Key for base::SupportsUserData::Data. | 55 // Key for base::SupportsUserData::Data. |
56 const char kModuleRegistryKey[] = "ModuleRegistry"; | 56 const char kModuleRegistryKey[] = "ModuleRegistry"; |
57 | 57 |
58 struct ModuleRegistryData : public base::SupportsUserData::Data { | 58 struct ModuleRegistryData : public base::SupportsUserData::Data { |
59 scoped_ptr<ModuleRegistry> registry; | 59 std::unique_ptr<ModuleRegistry> registry; |
60 }; | 60 }; |
61 | 61 |
62 void Define(const v8::FunctionCallbackInfo<Value>& info) { | 62 void Define(const v8::FunctionCallbackInfo<Value>& info) { |
63 Arguments args(info); | 63 Arguments args(info); |
64 | 64 |
65 if (!info.Length()) | 65 if (!info.Length()) |
66 return args.ThrowTypeError("At least one argument is required."); | 66 return args.ThrowTypeError("At least one argument is required."); |
67 | 67 |
68 std::string id; | 68 std::string id; |
69 std::vector<std::string> dependencies; | 69 std::vector<std::string> dependencies; |
70 v8::Local<Value> factory; | 70 v8::Local<Value> factory; |
71 | 71 |
72 if (!args.PeekNext().IsEmpty() && args.PeekNext()->IsString()) | 72 if (!args.PeekNext().IsEmpty() && args.PeekNext()->IsString()) |
73 args.GetNext(&id); | 73 args.GetNext(&id); |
74 if (!args.PeekNext().IsEmpty() && args.PeekNext()->IsArray()) | 74 if (!args.PeekNext().IsEmpty() && args.PeekNext()->IsArray()) |
75 args.GetNext(&dependencies); | 75 args.GetNext(&dependencies); |
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 std::unique_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(), std::move(pending)); | 86 registry->AddPendingModule(args.isolate(), std::move(pending)); |
87 } | 87 } |
88 | 88 |
89 WrapperInfo g_wrapper_info = { kEmbedderNativeGin }; | 89 WrapperInfo g_wrapper_info = { kEmbedderNativeGin }; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 observer_list_.RemoveObserver(observer); | 151 observer_list_.RemoveObserver(observer); |
152 } | 152 } |
153 | 153 |
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 std::unique_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, std::move(pending)); | 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) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 for (size_t i = 0; i < len; ++i) { | 220 for (size_t i = 0; i < len; ++i) { |
221 const std::string& dependency = pending->dependencies[i]; | 221 const std::string& dependency = pending->dependencies[i]; |
222 if (available_modules_.count(dependency)) | 222 if (available_modules_.count(dependency)) |
223 continue; | 223 continue; |
224 unsatisfied_dependencies_.insert(dependency); | 224 unsatisfied_dependencies_.insert(dependency); |
225 num_missing_dependencies++; | 225 num_missing_dependencies++; |
226 } | 226 } |
227 return num_missing_dependencies == 0; | 227 return num_missing_dependencies == 0; |
228 } | 228 } |
229 | 229 |
230 bool ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { | 230 bool ModuleRegistry::Load(Isolate* isolate, |
| 231 std::unique_ptr<PendingModule> pending) { |
231 if (!pending->id.empty() && available_modules_.count(pending->id)) | 232 if (!pending->id.empty() && available_modules_.count(pending->id)) |
232 return true; // We've already loaded this module. | 233 return true; // We've already loaded this module. |
233 | 234 |
234 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); | 235 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); |
235 std::vector<v8::Local<Value> > argv(argc); | 236 std::vector<v8::Local<Value> > argv(argc); |
236 for (uint32_t i = 0; i < argc; ++i) | 237 for (uint32_t i = 0; i < argc; ++i) |
237 argv[i] = GetModule(isolate, pending->dependencies[i]); | 238 argv[i] = GetModule(isolate, pending->dependencies[i]); |
238 | 239 |
239 v8::Local<Value> module = Local<Value>::New(isolate, pending->factory); | 240 v8::Local<Value> module = Local<Value>::New(isolate, pending->factory); |
240 | 241 |
241 v8::Local<Function> factory; | 242 v8::Local<Function> factory; |
242 if (ConvertFromV8(isolate, module, &factory)) { | 243 if (ConvertFromV8(isolate, module, &factory)) { |
243 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); | 244 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); |
244 Runner* runner = data->runner(); | 245 Runner* runner = data->runner(); |
245 module = runner->Call(factory, runner->global(), argc, | 246 module = runner->Call(factory, runner->global(), argc, |
246 argv.empty() ? NULL : &argv.front()); | 247 argv.empty() ? NULL : &argv.front()); |
247 if (pending->id.empty()) | 248 if (pending->id.empty()) |
248 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), | 249 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), |
249 &pending->id); | 250 &pending->id); |
250 } | 251 } |
251 | 252 |
252 return RegisterModule(isolate, pending->id, module); | 253 return RegisterModule(isolate, pending->id, module); |
253 } | 254 } |
254 | 255 |
255 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, | 256 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, |
256 scoped_ptr<PendingModule> pending) { | 257 std::unique_ptr<PendingModule> pending) { |
257 if (!CheckDependencies(pending.get())) { | 258 if (!CheckDependencies(pending.get())) { |
258 pending_modules_.push_back(pending.release()); | 259 pending_modules_.push_back(pending.release()); |
259 return false; | 260 return false; |
260 } | 261 } |
261 return Load(isolate, std::move(pending)); | 262 return Load(isolate, std::move(pending)); |
262 } | 263 } |
263 | 264 |
264 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, | 265 v8::Local<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate, |
265 const std::string& id) { | 266 const std::string& id) { |
266 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); | 267 v8::Local<Object> modules = Local<Object>::New(isolate, modules_); |
267 v8::Local<String> key = StringToSymbol(isolate, id); | 268 v8::Local<String> key = StringToSymbol(isolate, id); |
268 DCHECK(modules->HasOwnProperty(isolate->GetCurrentContext(), key).FromJust()); | 269 DCHECK(modules->HasOwnProperty(isolate->GetCurrentContext(), key).FromJust()); |
269 return modules->Get(isolate->GetCurrentContext(), key).ToLocalChecked(); | 270 return modules->Get(isolate->GetCurrentContext(), key).ToLocalChecked(); |
270 } | 271 } |
271 | 272 |
272 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { | 273 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { |
273 bool keep_trying = true; | 274 bool keep_trying = true; |
274 while (keep_trying) { | 275 while (keep_trying) { |
275 keep_trying = false; | 276 keep_trying = false; |
276 PendingModuleVector pending_modules; | 277 PendingModuleVector pending_modules; |
277 pending_modules.swap(pending_modules_); | 278 pending_modules.swap(pending_modules_); |
278 for (size_t i = 0; i < pending_modules.size(); ++i) { | 279 for (size_t i = 0; i < pending_modules.size(); ++i) { |
279 scoped_ptr<PendingModule> pending(pending_modules[i]); | 280 std::unique_ptr<PendingModule> pending(pending_modules[i]); |
280 pending_modules[i] = NULL; | 281 pending_modules[i] = NULL; |
281 if (AttemptToLoad(isolate, std::move(pending))) | 282 if (AttemptToLoad(isolate, std::move(pending))) |
282 keep_trying = true; | 283 keep_trying = true; |
283 } | 284 } |
284 } | 285 } |
285 } | 286 } |
286 | 287 |
287 } // namespace gin | 288 } // namespace gin |
OLD | NEW |