Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: gin/modules/module_registry.cc

Issue 90203002: [Mojo] Remove static "bootstrap" state in mojo_js (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: CHECK, CHECK, CHECK Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « gin/modules/module_registry.h ('k') | gin/modules/module_runner_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 v8::Handle<ObjectTemplate> templ) { 128 v8::Handle<ObjectTemplate> templ) {
129 DCHECK(!id.empty()); 129 DCHECK(!id.empty());
130 RegisterModule(isolate, id, templ->NewInstance()); 130 RegisterModule(isolate, id, templ->NewInstance());
131 } 131 }
132 132
133 void ModuleRegistry::AddPendingModule(Isolate* isolate, 133 void ModuleRegistry::AddPendingModule(Isolate* isolate,
134 scoped_ptr<PendingModule> pending) { 134 scoped_ptr<PendingModule> pending) {
135 AttemptToLoad(isolate, pending.Pass()); 135 AttemptToLoad(isolate, pending.Pass());
136 } 136 }
137 137
138 void ModuleRegistry::LoadModule(Isolate* isolate,
139 const std::string& id,
140 LoadModuleCallback callback) {
141 if (available_modules_.find(id) != available_modules_.end()) {
142 // Should we call the callback asynchronously?
143 callback.Run(GetModule(isolate, id));
144 return;
145 }
146 // Should we support multiple callers waiting on the same module?
147 DCHECK(waiting_callbacks_.find(id) == waiting_callbacks_.end());
148 waiting_callbacks_[id] = callback;
149 unsatisfied_dependencies_.insert(id);
150 }
151
138 void ModuleRegistry::RegisterModule(Isolate* isolate, 152 void ModuleRegistry::RegisterModule(Isolate* isolate,
139 const std::string& id, 153 const std::string& id,
140 v8::Handle<Value> module) { 154 v8::Handle<Value> module) {
141 if (id.empty() || module.IsEmpty()) 155 if (id.empty() || module.IsEmpty())
142 return; 156 return;
143 157
144 unsatisfied_dependencies_.erase(id); 158 unsatisfied_dependencies_.erase(id);
145 available_modules_.insert(id); 159 available_modules_.insert(id);
146 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_); 160 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_);
147 modules->Set(StringToSymbol(isolate, id), module); 161 modules->Set(StringToSymbol(isolate, id), module);
162
163 LoadModuleCallbackMap::iterator it = waiting_callbacks_.find(id);
164 if (it == waiting_callbacks_.end())
165 return;
166 LoadModuleCallback callback = it->second;
167 waiting_callbacks_.erase(it);
168 // Should we call the callback asynchronously?
169 callback.Run(module);
148 } 170 }
149 171
150 void ModuleRegistry::Detach(v8::Handle<Context> context) { 172 void ModuleRegistry::Detach(v8::Handle<Context> context) {
151 context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()), 173 context->Global()->SetHiddenValue(GetHiddenValueKey(context->GetIsolate()),
152 v8::Handle<Value>()); 174 v8::Handle<Value>());
153 } 175 }
154 176
155 bool ModuleRegistry::CheckDependencies(PendingModule* pending) { 177 bool ModuleRegistry::CheckDependencies(PendingModule* pending) {
156 size_t num_missing_dependencies = 0; 178 size_t num_missing_dependencies = 0;
157 size_t len = pending->dependencies.size(); 179 size_t len = pending->dependencies.size();
158 for (size_t i = 0; i < len; ++i) { 180 for (size_t i = 0; i < len; ++i) {
159 const std::string& dependency = pending->dependencies[i]; 181 const std::string& dependency = pending->dependencies[i];
160 if (available_modules_.count(dependency)) 182 if (available_modules_.count(dependency))
161 continue; 183 continue;
162 unsatisfied_dependencies_.insert(dependency); 184 unsatisfied_dependencies_.insert(dependency);
163 num_missing_dependencies++; 185 num_missing_dependencies++;
164 } 186 }
165 return num_missing_dependencies == 0; 187 return num_missing_dependencies == 0;
166 } 188 }
167 189
168 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) { 190 void ModuleRegistry::Load(Isolate* isolate, scoped_ptr<PendingModule> pending) {
169 if (!pending->id.empty() && available_modules_.count(pending->id)) 191 if (!pending->id.empty() && available_modules_.count(pending->id))
170 return; // We've already loaded this module. 192 return; // We've already loaded this module.
171 193
172 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_);
173 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size()); 194 uint32_t argc = static_cast<uint32_t>(pending->dependencies.size());
174 std::vector<v8::Handle<Value> > argv(argc); 195 std::vector<v8::Handle<Value> > argv(argc);
175 for (uint32_t i = 0; i < argc; ++i) { 196 for (uint32_t i = 0; i < argc; ++i)
176 v8::Handle<String> key = StringToSymbol(isolate, pending->dependencies[i]); 197 argv[i] = GetModule(isolate, pending->dependencies[i]);
177 DCHECK(modules->HasOwnProperty(key));
178 argv[i] = modules->Get(key);
179 }
180 198
181 v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory); 199 v8::Handle<Value> module = Local<Value>::New(isolate, pending->factory);
182 200
183 v8::Handle<Function> factory; 201 v8::Handle<Function> factory;
184 if (ConvertFromV8(isolate, module, &factory)) { 202 if (ConvertFromV8(isolate, module, &factory)) {
185 PerContextData* data = PerContextData::From(isolate->GetCurrentContext()); 203 PerContextData* data = PerContextData::From(isolate->GetCurrentContext());
186 Runner* runner = data->runner(); 204 Runner* runner = data->runner();
187 module = runner->Call(factory, runner->global(), argc, argv.data()); 205 module = runner->Call(factory, runner->global(), argc, argv.data());
188 if (pending->id.empty()) 206 if (pending->id.empty())
189 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(), 207 ConvertFromV8(isolate, factory->GetScriptOrigin().ResourceName(),
190 &pending->id); 208 &pending->id);
191 } 209 }
192 210
193 RegisterModule(isolate, pending->id, module); 211 RegisterModule(isolate, pending->id, module);
194 } 212 }
195 213
196 bool ModuleRegistry::AttemptToLoad(Isolate* isolate, 214 bool ModuleRegistry::AttemptToLoad(Isolate* isolate,
197 scoped_ptr<PendingModule> pending) { 215 scoped_ptr<PendingModule> pending) {
198 if (!CheckDependencies(pending.get())) { 216 if (!CheckDependencies(pending.get())) {
199 pending_modules_.push_back(pending.release()); 217 pending_modules_.push_back(pending.release());
200 return false; 218 return false;
201 } 219 }
202 Load(isolate, pending.Pass()); 220 Load(isolate, pending.Pass());
203 return true; 221 return true;
204 } 222 }
205 223
224 v8::Handle<v8::Value> ModuleRegistry::GetModule(v8::Isolate* isolate,
225 const std::string& id) {
226 v8::Handle<Object> modules = Local<Object>::New(isolate, modules_);
227 v8::Handle<String> key = StringToSymbol(isolate, id);
228 DCHECK(modules->HasOwnProperty(key));
229 return modules->Get(key);
230 }
231
206 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) { 232 void ModuleRegistry::AttemptToLoadMoreModules(Isolate* isolate) {
207 bool keep_trying = true; 233 bool keep_trying = true;
208 while (keep_trying) { 234 while (keep_trying) {
209 keep_trying = false; 235 keep_trying = false;
210 PendingModuleVector pending_modules; 236 PendingModuleVector pending_modules;
211 pending_modules.swap(pending_modules_); 237 pending_modules.swap(pending_modules_);
212 for (size_t i = 0; i < pending_modules.size(); ++i) { 238 for (size_t i = 0; i < pending_modules.size(); ++i) {
213 scoped_ptr<PendingModule> pending(pending_modules[i]); 239 scoped_ptr<PendingModule> pending(pending_modules[i]);
214 pending_modules[i] = NULL; 240 pending_modules[i] = NULL;
215 if (AttemptToLoad(isolate, pending.Pass())) 241 if (AttemptToLoad(isolate, pending.Pass()))
216 keep_trying = true; 242 keep_trying = true;
217 } 243 }
218 } 244 }
219 } 245 }
220 246
221 } // namespace gin 247 } // namespace gin
OLDNEW
« no previous file with comments | « gin/modules/module_registry.h ('k') | gin/modules/module_runner_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698