Chromium Code Reviews| 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; |
|
abarth-chromium
2014/07/08 14:20:30
Can you preallocate the correct number of slots in
Sam McNally
2014/07/09 00:22:51
Done.
| |
| 190 LoadModuleCallback callback = it->second; | 188 for (LoadModuleCallbackMap::iterator it = range.first; it != range.second; |
| 191 waiting_callbacks_.erase(it); | 189 ++it) { |
| 192 // Should we call the callback asynchronously? | 190 callbacks.push_back(it->second); |
| 193 callback.Run(module); | 191 } |
| 192 waiting_callbacks_.erase(range.first, range.second); | |
| 193 for (std::vector<LoadModuleCallback>::iterator it = callbacks.begin(); | |
| 194 it != callbacks.end(); | |
| 195 ++it) { | |
| 196 // Should we call the callback asynchronously? | |
| 197 it->Run(module); | |
| 198 } | |
| 194 } | 199 } |
| 195 | 200 |
| 196 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { | 201 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { |
| 197 size_t num_missing_dependencies = 0; | 202 size_t num_missing_dependencies = 0; |
| 198 size_t len = pending->dependencies.size(); | 203 size_t len = pending->dependencies.size(); |
| 199 for (size_t i = 0; i < len; ++i) { | 204 for (size_t i = 0; i < len; ++i) { |
| 200 const std::string& dependency = pending->dependencies[i]; | 205 const std::string& dependency = pending->dependencies[i]; |
| 201 if (available_modules_.count(dependency)) | 206 if (available_modules_.count(dependency)) |
| 202 continue; | 207 continue; |
| 203 unsatisfied_dependencies_.insert(dependency); | 208 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) { | 263 for (size_t i = 0; i < pending_modules.size(); ++i) { |
| 259 scoped_ptr<PendingModule> pending(pending_modules[i]); | 264 scoped_ptr<PendingModule> pending(pending_modules[i]); |
| 260 pending_modules[i] = NULL; | 265 pending_modules[i] = NULL; |
| 261 if (AttemptToLoad(isolate, pending.Pass())) | 266 if (AttemptToLoad(isolate, pending.Pass())) |
| 262 keep_trying = true; | 267 keep_trying = true; |
| 263 } | 268 } |
| 264 } | 269 } |
| 265 } | 270 } |
| 266 | 271 |
| 267 } // namespace gin | 272 } // namespace gin |
| OLD | NEW |