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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 } | 160 } |
161 | 161 |
162 void ModuleRegistry::LoadModule(Isolate* isolate, | 162 void ModuleRegistry::LoadModule(Isolate* isolate, |
163 const std::string& id, | 163 const std::string& id, |
164 LoadModuleCallback callback) { | 164 LoadModuleCallback callback) { |
165 if (available_modules_.find(id) != available_modules_.end()) { | 165 if (available_modules_.find(id) != available_modules_.end()) { |
166 // Should we call the callback asynchronously? | 166 // Should we call the callback asynchronously? |
167 callback.Run(GetModule(isolate, id)); | 167 callback.Run(GetModule(isolate, id)); |
168 return; | 168 return; |
169 } | 169 } |
170 // Should we support multiple callers waiting on the same module? | 170 waiting_callbacks_.insert(std::make_pair(id, callback)); |
171 DCHECK(waiting_callbacks_.find(id) == waiting_callbacks_.end()); | |
172 waiting_callbacks_[id] = callback; | |
173 unsatisfied_dependencies_.insert(id); | 171 unsatisfied_dependencies_.insert(id); |
174 } | 172 } |
175 | 173 |
176 void ModuleRegistry::RegisterModule(Isolate* isolate, | 174 void ModuleRegistry::RegisterModule(Isolate* isolate, |
177 const std::string& id, | 175 const std::string& id, |
178 v8::Handle<Value> module) { | 176 v8::Handle<Value> module) { |
179 if (id.empty() || module.IsEmpty()) | 177 if (id.empty() || module.IsEmpty()) |
180 return; | 178 return; |
181 | 179 |
182 unsatisfied_dependencies_.erase(id); | 180 unsatisfied_dependencies_.erase(id); |
183 available_modules_.insert(id); | 181 available_modules_.insert(id); |
184 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); | 182 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); |
185 modules->Set(StringToSymbol(isolate, id), module); | 183 modules->Set(StringToSymbol(isolate, id), module); |
186 | 184 |
187 LoadModuleCallbackMap::iterator it = waiting_callbacks_.find(id); | 185 std::pair<LoadModuleCallbackMap::iterator, LoadModuleCallbackMap::iterator> |
188 if (it == waiting_callbacks_.end()) | 186 range = waiting_callbacks_.equal_range(id); |
189 return; | 187 std::vector<LoadModuleCallback> callbacks; |
190 LoadModuleCallback callback = it->second; | 188 callbacks.reserve(waiting_callbacks_.count(id)); |
191 waiting_callbacks_.erase(it); | 189 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; |
192 // Should we call the callback asynchronously? | 190 ++it) { |
193 callback.Run(module); | 191 callbacks.push_back(it->second); |
| 192 } |
| 193 waiting_callbacks_.erase(range.first, range.second); |
| 194 for (std::vector<LoadModuleCallback>::iterator it = callbacks.begin(); |
| 195 it != callbacks.end(); |
| 196 ++it) { |
| 197 // Should we call the callback asynchronously? |
| 198 it->Run(module); |
| 199 } |
194 } | 200 } |
195 | 201 |
196 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { | 202 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { |
197 size_t num_missing_dependencies = 0; | 203 size_t num_missing_dependencies = 0; |
198 size_t len = pending->dependencies.size(); | 204 size_t len = pending->dependencies.size(); |
199 for (size_t i = 0; i < len; ++i) { | 205 for (size_t i = 0; i < len; ++i) { |
200 const std::string& dependency = pending->dependencies[i]; | 206 const std::string& dependency = pending->dependencies[i]; |
201 if (available_modules_.count(dependency)) | 207 if (available_modules_.count(dependency)) |
202 continue; | 208 continue; |
203 unsatisfied_dependencies_.insert(dependency); | 209 unsatisfied_dependencies_.insert(dependency); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 for (size_t i = 0; i < pending_modules.size(); ++i) { | 264 for (size_t i = 0; i < pending_modules.size(); ++i) { |
259 scoped_ptr<PendingModule> pending(pending_modules[i]); | 265 scoped_ptr<PendingModule> pending(pending_modules[i]); |
260 pending_modules[i] = NULL; | 266 pending_modules[i] = NULL; |
261 if (AttemptToLoad(isolate, pending.Pass())) | 267 if (AttemptToLoad(isolate, pending.Pass())) |
262 keep_trying = true; | 268 keep_trying = true; |
263 } | 269 } |
264 } | 270 } |
265 } | 271 } |
266 | 272 |
267 } // namespace gin | 273 } // namespace gin |
OLD | NEW |