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

Side by Side Diff: extensions/renderer/module_system.cc

Issue 1115563002: extensions/renderer: Use v8::Local instead of v8::Handle. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months 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
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/module_system_test.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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/module_system.h" 5 #include "extensions/renderer/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 } 88 }
89 89
90 private: 90 private:
91 ScriptContext* context_; 91 ScriptContext* context_;
92 }; 92 };
93 93
94 } // namespace 94 } // namespace
95 95
96 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( 96 std::string ModuleSystem::ExceptionHandler::CreateExceptionString(
97 const v8::TryCatch& try_catch) { 97 const v8::TryCatch& try_catch) {
98 v8::Handle<v8::Message> message(try_catch.Message()); 98 v8::Local<v8::Message> message(try_catch.Message());
99 if (message.IsEmpty()) { 99 if (message.IsEmpty()) {
100 return "try_catch has no message"; 100 return "try_catch has no message";
101 } 101 }
102 102
103 std::string resource_name = "<unknown resource>"; 103 std::string resource_name = "<unknown resource>";
104 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { 104 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) {
105 v8::String::Utf8Value resource_name_v8( 105 v8::String::Utf8Value resource_name_v8(
106 message->GetScriptOrigin().ResourceName()); 106 message->GetScriptOrigin().ResourceName());
107 resource_name.assign(*resource_name_v8, resource_name_v8.length()); 107 resource_name.assign(*resource_name_v8, resource_name_v8.length());
108 } 108 }
(...skipping 22 matching lines...) Expand all
131 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); 131 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
132 RouteFunction( 132 RouteFunction(
133 "requireNative", 133 "requireNative",
134 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); 134 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
135 RouteFunction( 135 RouteFunction(
136 "requireAsync", 136 "requireAsync",
137 base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this))); 137 base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this)));
138 RouteFunction("privates", 138 RouteFunction("privates",
139 base::Bind(&ModuleSystem::Private, base::Unretained(this))); 139 base::Bind(&ModuleSystem::Private, base::Unretained(this)));
140 140
141 v8::Handle<v8::Object> global(context->v8_context()->Global()); 141 v8::Local<v8::Object> global(context->v8_context()->Global());
142 v8::Isolate* isolate = context->isolate(); 142 v8::Isolate* isolate = context->isolate();
143 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField), 143 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField),
144 v8::Object::New(isolate)); 144 v8::Object::New(isolate));
145 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem), 145 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem),
146 v8::External::New(isolate, this)); 146 v8::External::New(isolate, this));
147 147
148 gin::ModuleRegistry::From(context->v8_context())->AddObserver(this); 148 gin::ModuleRegistry::From(context->v8_context())->AddObserver(this);
149 if (context_->GetRenderFrame()) { 149 if (context_->GetRenderFrame()) {
150 context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable( 150 context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable(
151 context->isolate(), context->v8_context()); 151 context->isolate(), context->v8_context());
152 } 152 }
153 } 153 }
154 154
155 ModuleSystem::~ModuleSystem() { 155 ModuleSystem::~ModuleSystem() {
156 } 156 }
157 157
158 void ModuleSystem::Invalidate() { 158 void ModuleSystem::Invalidate() {
159 // Clear the module system properties from the global context. It's polite, 159 // Clear the module system properties from the global context. It's polite,
160 // and we use this as a signal in lazy handlers that we no longer exist. 160 // and we use this as a signal in lazy handlers that we no longer exist.
161 { 161 {
162 v8::HandleScope scope(GetIsolate()); 162 v8::HandleScope scope(GetIsolate());
163 v8::Handle<v8::Object> global = context()->v8_context()->Global(); 163 v8::Local<v8::Object> global = context()->v8_context()->Global();
164 global->DeleteHiddenValue( 164 global->DeleteHiddenValue(
165 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); 165 v8::String::NewFromUtf8(GetIsolate(), kModulesField));
166 global->DeleteHiddenValue( 166 global->DeleteHiddenValue(
167 v8::String::NewFromUtf8(GetIsolate(), kModuleSystem)); 167 v8::String::NewFromUtf8(GetIsolate(), kModuleSystem));
168 } 168 }
169 169
170 // Invalidate all active and clobbered NativeHandlers we own. 170 // Invalidate all active and clobbered NativeHandlers we own.
171 for (const auto& handler : native_handler_map_) 171 for (const auto& handler : native_handler_map_)
172 handler.second->Invalidate(); 172 handler.second->Invalidate();
173 for (const auto& clobbered_handler : clobbered_native_handlers_) 173 for (const auto& clobbered_handler : clobbered_native_handlers_)
(...skipping 10 matching lines...) Expand all
184 184
185 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { 185 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
186 module_system_->natives_enabled_--; 186 module_system_->natives_enabled_--;
187 CHECK_GE(module_system_->natives_enabled_, 0); 187 CHECK_GE(module_system_->natives_enabled_, 0);
188 } 188 }
189 189
190 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { 190 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
191 exception_handler_->HandleUncaughtException(try_catch); 191 exception_handler_->HandleUncaughtException(try_catch);
192 } 192 }
193 193
194 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { 194 v8::Local<v8::Value> ModuleSystem::Require(const std::string& module_name) {
195 v8::EscapableHandleScope handle_scope(GetIsolate()); 195 v8::EscapableHandleScope handle_scope(GetIsolate());
196 return handle_scope.Escape(RequireForJsInner( 196 return handle_scope.Escape(RequireForJsInner(
197 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); 197 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())));
198 } 198 }
199 199
200 void ModuleSystem::RequireForJs( 200 void ModuleSystem::RequireForJs(
201 const v8::FunctionCallbackInfo<v8::Value>& args) { 201 const v8::FunctionCallbackInfo<v8::Value>& args) {
202 v8::Handle<v8::String> module_name = args[0]->ToString(args.GetIsolate()); 202 v8::Local<v8::String> module_name = args[0]->ToString(args.GetIsolate());
203 args.GetReturnValue().Set(RequireForJsInner(module_name)); 203 args.GetReturnValue().Set(RequireForJsInner(module_name));
204 } 204 }
205 205
206 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( 206 v8::Local<v8::Value> ModuleSystem::RequireForJsInner(
207 v8::Handle<v8::String> module_name) { 207 v8::Local<v8::String> module_name) {
208 v8::EscapableHandleScope handle_scope(GetIsolate()); 208 v8::EscapableHandleScope handle_scope(GetIsolate());
209 v8::Context::Scope context_scope(context()->v8_context()); 209 v8::Context::Scope context_scope(context()->v8_context());
210 210
211 v8::Handle<v8::Object> global(context()->v8_context()->Global()); 211 v8::Local<v8::Object> global(context()->v8_context()->Global());
212 212
213 // The module system might have been deleted. This can happen if a different 213 // The module system might have been deleted. This can happen if a different
214 // context keeps a reference to us, but our frame is destroyed (e.g. 214 // context keeps a reference to us, but our frame is destroyed (e.g.
215 // background page keeps reference to chrome object in a closed popup). 215 // background page keeps reference to chrome object in a closed popup).
216 v8::Handle<v8::Value> modules_value = global->GetHiddenValue( 216 v8::Local<v8::Value> modules_value = global->GetHiddenValue(
217 v8::String::NewFromUtf8(GetIsolate(), kModulesField)); 217 v8::String::NewFromUtf8(GetIsolate(), kModulesField));
218 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { 218 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
219 Warn(GetIsolate(), "Extension view no longer exists"); 219 Warn(GetIsolate(), "Extension view no longer exists");
220 return v8::Undefined(GetIsolate()); 220 return v8::Undefined(GetIsolate());
221 } 221 }
222 222
223 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); 223 v8::Local<v8::Object> modules(v8::Local<v8::Object>::Cast(modules_value));
224 v8::Local<v8::Value> exports(modules->Get(module_name)); 224 v8::Local<v8::Value> exports(modules->Get(module_name));
225 if (!exports->IsUndefined()) 225 if (!exports->IsUndefined())
226 return handle_scope.Escape(exports); 226 return handle_scope.Escape(exports);
227 227
228 exports = LoadModule(*v8::String::Utf8Value(module_name)); 228 exports = LoadModule(*v8::String::Utf8Value(module_name));
229 modules->Set(module_name, exports); 229 modules->Set(module_name, exports);
230 return handle_scope.Escape(exports); 230 return handle_scope.Escape(exports);
231 } 231 }
232 232
233 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 233 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
234 const std::string& module_name, 234 const std::string& module_name,
235 const std::string& method_name) { 235 const std::string& method_name) {
236 v8::EscapableHandleScope handle_scope(GetIsolate()); 236 v8::EscapableHandleScope handle_scope(GetIsolate());
237 v8::Handle<v8::Value> no_args; 237 v8::Local<v8::Value> no_args;
238 return handle_scope.Escape( 238 return handle_scope.Escape(
239 CallModuleMethod(module_name, method_name, 0, &no_args)); 239 CallModuleMethod(module_name, method_name, 0, &no_args));
240 } 240 }
241 241
242 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 242 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
243 const std::string& module_name, 243 const std::string& module_name,
244 const std::string& method_name, 244 const std::string& method_name,
245 std::vector<v8::Handle<v8::Value> >* args) { 245 std::vector<v8::Local<v8::Value>>* args) {
246 return CallModuleMethod( 246 return CallModuleMethod(
247 module_name, method_name, args->size(), vector_as_array(args)); 247 module_name, method_name, args->size(), vector_as_array(args));
248 } 248 }
249 249
250 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 250 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
251 const std::string& module_name, 251 const std::string& module_name,
252 const std::string& method_name, 252 const std::string& method_name,
253 int argc, 253 int argc,
254 v8::Handle<v8::Value> argv[]) { 254 v8::Local<v8::Value> argv[]) {
255 TRACE_EVENT2("v8", 255 TRACE_EVENT2("v8",
256 "v8.callModuleMethod", 256 "v8.callModuleMethod",
257 "module_name", 257 "module_name",
258 module_name, 258 module_name,
259 "method_name", 259 "method_name",
260 method_name); 260 method_name);
261 261
262 v8::EscapableHandleScope handle_scope(GetIsolate()); 262 v8::EscapableHandleScope handle_scope(GetIsolate());
263 v8::Context::Scope context_scope(context()->v8_context()); 263 v8::Context::Scope context_scope(context()->v8_context());
264 264
265 v8::Local<v8::Value> module; 265 v8::Local<v8::Value> module;
266 { 266 {
267 NativesEnabledScope natives_enabled(this); 267 NativesEnabledScope natives_enabled(this);
268 module = RequireForJsInner( 268 module = RequireForJsInner(
269 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); 269 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
270 } 270 }
271 271
272 if (module.IsEmpty() || !module->IsObject()) { 272 if (module.IsEmpty() || !module->IsObject()) {
273 Fatal(context_, 273 Fatal(context_,
274 "Failed to get module " + module_name + " to call " + method_name); 274 "Failed to get module " + module_name + " to call " + method_name);
275 return handle_scope.Escape( 275 return handle_scope.Escape(
276 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); 276 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())));
277 } 277 }
278 278
279 v8::Local<v8::Value> value = v8::Handle<v8::Object>::Cast(module)->Get( 279 v8::Local<v8::Value> value = v8::Local<v8::Object>::Cast(module)->Get(
280 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); 280 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str()));
281 if (value.IsEmpty() || !value->IsFunction()) { 281 if (value.IsEmpty() || !value->IsFunction()) {
282 Fatal(context_, module_name + "." + method_name + " is not a function"); 282 Fatal(context_, module_name + "." + method_name + " is not a function");
283 return handle_scope.Escape( 283 return handle_scope.Escape(
284 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); 284 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())));
285 } 285 }
286 286
287 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); 287 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value);
288 v8::Local<v8::Value> result; 288 v8::Local<v8::Value> result;
289 { 289 {
290 v8::TryCatch try_catch; 290 v8::TryCatch try_catch;
291 try_catch.SetCaptureMessage(true); 291 try_catch.SetCaptureMessage(true);
292 result = context_->CallFunction(func, argc, argv); 292 result = context_->CallFunction(func, argc, argv);
293 if (try_catch.HasCaught()) 293 if (try_catch.HasCaught())
294 HandleException(try_catch); 294 HandleException(try_catch);
295 } 295 }
296 return handle_scope.Escape(result); 296 return handle_scope.Escape(result);
297 } 297 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 } 330 }
331 331
332 // static 332 // static
333 void ModuleSystem::LazyFieldGetterInner( 333 void ModuleSystem::LazyFieldGetterInner(
334 v8::Local<v8::String> property, 334 v8::Local<v8::String> property,
335 const v8::PropertyCallbackInfo<v8::Value>& info, 335 const v8::PropertyCallbackInfo<v8::Value>& info,
336 RequireFunction require_function) { 336 RequireFunction require_function) {
337 CHECK(!info.Data().IsEmpty()); 337 CHECK(!info.Data().IsEmpty());
338 CHECK(info.Data()->IsObject()); 338 CHECK(info.Data()->IsObject());
339 v8::HandleScope handle_scope(info.GetIsolate()); 339 v8::HandleScope handle_scope(info.GetIsolate());
340 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); 340 v8::Local<v8::Object> parameters = v8::Local<v8::Object>::Cast(info.Data());
341 // This context should be the same as context()->v8_context(). 341 // This context should be the same as context()->v8_context().
342 v8::Handle<v8::Context> context = parameters->CreationContext(); 342 v8::Local<v8::Context> context = parameters->CreationContext();
343 v8::Handle<v8::Object> global(context->Global()); 343 v8::Local<v8::Object> global(context->Global());
344 v8::Handle<v8::Value> module_system_value = global->GetHiddenValue( 344 v8::Local<v8::Value> module_system_value = global->GetHiddenValue(
345 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); 345 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem));
346 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { 346 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
347 // ModuleSystem has been deleted. 347 // ModuleSystem has been deleted.
348 // TODO(kalman): See comment in header file. 348 // TODO(kalman): See comment in header file.
349 Warn(info.GetIsolate(), 349 Warn(info.GetIsolate(),
350 "Module system has been deleted, does extension view exist?"); 350 "Module system has been deleted, does extension view exist?");
351 return; 351 return;
352 } 352 }
353 353
354 ModuleSystem* module_system = static_cast<ModuleSystem*>( 354 ModuleSystem* module_system = static_cast<ModuleSystem*>(
355 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 355 v8::Local<v8::External>::Cast(module_system_value)->Value());
356 356
357 std::string name = *v8::String::Utf8Value(parameters->Get( 357 std::string name = *v8::String::Utf8Value(parameters->Get(
358 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))); 358 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName)));
359 359
360 // Switch to our v8 context because we need functions created while running 360 // Switch to our v8 context because we need functions created while running
361 // the require()d module to belong to our context, not the current one. 361 // the require()d module to belong to our context, not the current one.
362 v8::Context::Scope context_scope(context); 362 v8::Context::Scope context_scope(context);
363 NativesEnabledScope natives_enabled_scope(module_system); 363 NativesEnabledScope natives_enabled_scope(module_system);
364 364
365 v8::TryCatch try_catch; 365 v8::TryCatch try_catch;
366 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); 366 v8::Local<v8::Value> module_value = (module_system->*require_function)(name);
367 if (try_catch.HasCaught()) { 367 if (try_catch.HasCaught()) {
368 module_system->HandleException(try_catch); 368 module_system->HandleException(try_catch);
369 return; 369 return;
370 } 370 }
371 if (module_value.IsEmpty() || !module_value->IsObject()) { 371 if (module_value.IsEmpty() || !module_value->IsObject()) {
372 // require_function will have already logged this, we don't need to. 372 // require_function will have already logged this, we don't need to.
373 return; 373 return;
374 } 374 }
375 375
376 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); 376 v8::Local<v8::Object> module = v8::Local<v8::Object>::Cast(module_value);
377 v8::Handle<v8::String> field = 377 v8::Local<v8::String> field =
378 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) 378 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField))
379 ->ToString(info.GetIsolate()); 379 ->ToString(info.GetIsolate());
380 380
381 if (!module->Has(field)) { 381 if (!module->Has(field)) {
382 std::string field_str = *v8::String::Utf8Value(field); 382 std::string field_str = *v8::String::Utf8Value(field);
383 Fatal(module_system->context_, 383 Fatal(module_system->context_,
384 "Lazy require of " + name + "." + field_str + " did not set the " + 384 "Lazy require of " + name + "." + field_str + " did not set the " +
385 field_str + " field"); 385 field_str + " field");
386 return; 386 return;
387 } 387 }
388 388
389 v8::Local<v8::Value> new_field = module->Get(field); 389 v8::Local<v8::Value> new_field = module->Get(field);
390 if (try_catch.HasCaught()) { 390 if (try_catch.HasCaught()) {
391 module_system->HandleException(try_catch); 391 module_system->HandleException(try_catch);
392 return; 392 return;
393 } 393 }
394 394
395 // Ok for it to be undefined, among other things it's how bindings signify 395 // Ok for it to be undefined, among other things it's how bindings signify
396 // that the extension doesn't have permission to use them. 396 // that the extension doesn't have permission to use them.
397 CHECK(!new_field.IsEmpty()); 397 CHECK(!new_field.IsEmpty());
398 398
399 // Delete the getter and set this field to |new_field| so the same object is 399 // Delete the getter and set this field to |new_field| so the same object is
400 // returned every time a certain API is accessed. 400 // returned every time a certain API is accessed.
401 v8::Handle<v8::Value> val = info.This(); 401 v8::Local<v8::Value> val = info.This();
402 if (val->IsObject()) { 402 if (val->IsObject()) {
403 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(val); 403 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(val);
404 object->Delete(property); 404 object->Delete(property);
405 object->Set(property, new_field); 405 object->Set(property, new_field);
406 } else { 406 } else {
407 NOTREACHED(); 407 NOTREACHED();
408 } 408 }
409 info.GetReturnValue().Set(new_field); 409 info.GetReturnValue().Set(new_field);
410 } 410 }
411 411
412 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 412 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object,
413 const std::string& field, 413 const std::string& field,
414 const std::string& module_name, 414 const std::string& module_name,
415 const std::string& module_field) { 415 const std::string& module_field) {
416 SetLazyField( 416 SetLazyField(
417 object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter); 417 object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter);
418 } 418 }
419 419
420 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 420 void ModuleSystem::SetLazyField(v8::Local<v8::Object> object,
421 const std::string& field, 421 const std::string& field,
422 const std::string& module_name, 422 const std::string& module_name,
423 const std::string& module_field, 423 const std::string& module_field,
424 v8::AccessorGetterCallback getter) { 424 v8::AccessorGetterCallback getter) {
425 v8::HandleScope handle_scope(GetIsolate()); 425 v8::HandleScope handle_scope(GetIsolate());
426 v8::Handle<v8::Object> parameters = v8::Object::New(GetIsolate()); 426 v8::Local<v8::Object> parameters = v8::Object::New(GetIsolate());
427 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName), 427 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName),
428 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); 428 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
429 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField), 429 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField),
430 v8::String::NewFromUtf8(GetIsolate(), module_field.c_str())); 430 v8::String::NewFromUtf8(GetIsolate(), module_field.c_str()));
431 object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()), 431 object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()),
432 getter, 432 getter,
433 NULL, 433 NULL,
434 parameters); 434 parameters);
435 } 435 }
436 436
437 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, 437 void ModuleSystem::SetNativeLazyField(v8::Local<v8::Object> object,
438 const std::string& field, 438 const std::string& field,
439 const std::string& module_name, 439 const std::string& module_name,
440 const std::string& module_field) { 440 const std::string& module_field) {
441 SetLazyField(object, 441 SetLazyField(object,
442 field, 442 field,
443 module_name, 443 module_name,
444 module_field, 444 module_field,
445 &ModuleSystem::NativeLazyFieldGetter); 445 &ModuleSystem::NativeLazyFieldGetter);
446 } 446 }
447 447
448 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 448 v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code,
449 v8::Handle<v8::String> name) { 449 v8::Local<v8::String> name) {
450 v8::EscapableHandleScope handle_scope(GetIsolate()); 450 v8::EscapableHandleScope handle_scope(GetIsolate());
451 v8::Context::Scope context_scope(context()->v8_context()); 451 v8::Context::Scope context_scope(context()->v8_context());
452 452
453 // Prepend extensions:: to |name| so that internal code can be differentiated 453 // Prepend extensions:: to |name| so that internal code can be differentiated
454 // from external code in stack traces. This has no effect on behaviour. 454 // from external code in stack traces. This has no effect on behaviour.
455 std::string internal_name = 455 std::string internal_name =
456 base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name)); 456 base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name));
457 457
458 blink::WebScopedMicrotaskSuppression suppression; 458 blink::WebScopedMicrotaskSuppression suppression;
459 v8::TryCatch try_catch; 459 v8::TryCatch try_catch;
460 try_catch.SetCaptureMessage(true); 460 try_catch.SetCaptureMessage(true);
461 v8::Handle<v8::Script> script( 461 v8::Local<v8::Script> script(v8::Script::Compile(
462 v8::Script::Compile(code, 462 code, v8::String::NewFromUtf8(GetIsolate(), internal_name.c_str(),
463 v8::String::NewFromUtf8(GetIsolate(), 463 v8::String::kNormalString,
464 internal_name.c_str(), 464 internal_name.size())));
465 v8::String::kNormalString,
466 internal_name.size())));
467 if (try_catch.HasCaught()) { 465 if (try_catch.HasCaught()) {
468 HandleException(try_catch); 466 HandleException(try_catch);
469 return v8::Undefined(GetIsolate()); 467 return v8::Undefined(GetIsolate());
470 } 468 }
471 469
472 v8::Local<v8::Value> result = script->Run(); 470 v8::Local<v8::Value> result = script->Run();
473 if (try_catch.HasCaught()) { 471 if (try_catch.HasCaught()) {
474 HandleException(try_catch); 472 HandleException(try_catch);
475 return v8::Undefined(GetIsolate()); 473 return v8::Undefined(GetIsolate());
476 } 474 }
477 475
478 return handle_scope.Escape(result); 476 return handle_scope.Escape(result);
479 } 477 }
480 478
481 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { 479 v8::Local<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
482 v8::EscapableHandleScope handle_scope(GetIsolate()); 480 v8::EscapableHandleScope handle_scope(GetIsolate());
483 if (!source_map_->Contains(module_name)) 481 if (!source_map_->Contains(module_name))
484 return v8::Undefined(GetIsolate()); 482 return v8::Undefined(GetIsolate());
485 return handle_scope.Escape( 483 return handle_scope.Escape(
486 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); 484 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name)));
487 } 485 }
488 486
489 void ModuleSystem::RequireNative( 487 void ModuleSystem::RequireNative(
490 const v8::FunctionCallbackInfo<v8::Value>& args) { 488 const v8::FunctionCallbackInfo<v8::Value>& args) {
491 CHECK_EQ(1, args.Length()); 489 CHECK_EQ(1, args.Length());
492 std::string native_name = *v8::String::Utf8Value(args[0]); 490 std::string native_name = *v8::String::Utf8Value(args[0]);
493 args.GetReturnValue().Set(RequireNativeFromString(native_name)); 491 args.GetReturnValue().Set(RequireNativeFromString(native_name));
494 } 492 }
495 493
496 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( 494 v8::Local<v8::Value> ModuleSystem::RequireNativeFromString(
497 const std::string& native_name) { 495 const std::string& native_name) {
498 if (natives_enabled_ == 0) { 496 if (natives_enabled_ == 0) {
499 // HACK: if in test throw exception so that we can test the natives-disabled 497 // HACK: if in test throw exception so that we can test the natives-disabled
500 // logic; however, under normal circumstances, this is programmer error so 498 // logic; however, under normal circumstances, this is programmer error so
501 // we could crash. 499 // we could crash.
502 if (exception_handler_) { 500 if (exception_handler_) {
503 return GetIsolate()->ThrowException( 501 return GetIsolate()->ThrowException(
504 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); 502 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled"));
505 } 503 }
506 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); 504 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")");
(...skipping 11 matching lines...) Expand all
518 "Couldn't find native for requireNative(" + native_name + ")"); 516 "Couldn't find native for requireNative(" + native_name + ")");
519 return v8::Undefined(GetIsolate()); 517 return v8::Undefined(GetIsolate());
520 } 518 }
521 return i->second->NewInstance(); 519 return i->second->NewInstance();
522 } 520 }
523 521
524 void ModuleSystem::RequireAsync( 522 void ModuleSystem::RequireAsync(
525 const v8::FunctionCallbackInfo<v8::Value>& args) { 523 const v8::FunctionCallbackInfo<v8::Value>& args) {
526 CHECK_EQ(1, args.Length()); 524 CHECK_EQ(1, args.Length());
527 std::string module_name = *v8::String::Utf8Value(args[0]); 525 std::string module_name = *v8::String::Utf8Value(args[0]);
528 v8::Handle<v8::Promise::Resolver> resolver( 526 v8::Local<v8::Promise::Resolver> resolver(
529 v8::Promise::Resolver::New(GetIsolate())); 527 v8::Promise::Resolver::New(GetIsolate()));
530 args.GetReturnValue().Set(resolver->GetPromise()); 528 args.GetReturnValue().Set(resolver->GetPromise());
531 scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver( 529 scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver(
532 new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver)); 530 new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver));
533 gin::ModuleRegistry* module_registry = 531 gin::ModuleRegistry* module_registry =
534 gin::ModuleRegistry::From(context_->v8_context()); 532 gin::ModuleRegistry::From(context_->v8_context());
535 if (!module_registry) { 533 if (!module_registry) {
536 Warn(GetIsolate(), "Extension view no longer exists"); 534 Warn(GetIsolate(), "Extension view no longer exists");
537 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( 535 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8(
538 GetIsolate(), "Extension view no longer exists"))); 536 GetIsolate(), "Extension view no longer exists")));
539 return; 537 return;
540 } 538 }
541 module_registry->LoadModule( 539 module_registry->LoadModule(
542 GetIsolate(), module_name, 540 GetIsolate(), module_name,
543 base::Bind(&ModuleSystem::OnModuleLoaded, weak_factory_.GetWeakPtr(), 541 base::Bind(&ModuleSystem::OnModuleLoaded, weak_factory_.GetWeakPtr(),
544 base::Passed(&global_resolver))); 542 base::Passed(&global_resolver)));
545 if (module_registry->available_modules().count(module_name) == 0) 543 if (module_registry->available_modules().count(module_name) == 0)
546 LoadModule(module_name); 544 LoadModule(module_name);
547 } 545 }
548 546
549 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { 547 v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) {
550 v8::EscapableHandleScope handle_scope(GetIsolate()); 548 v8::EscapableHandleScope handle_scope(GetIsolate());
551 // Keep in order with the arguments in RequireForJsInner. 549 // Keep in order with the arguments in RequireForJsInner.
552 v8::Handle<v8::String> left = v8::String::NewFromUtf8( 550 v8::Local<v8::String> left = v8::String::NewFromUtf8(
553 GetIsolate(), 551 GetIsolate(),
554 "(function(define, require, requireNative, requireAsync, exports, " 552 "(function(define, require, requireNative, requireAsync, exports, "
555 "console, privates," 553 "console, privates,"
556 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" 554 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {"
557 "'use strict';"); 555 "'use strict';");
558 v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); 556 v8::Local<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})");
559 return handle_scope.Escape(v8::Local<v8::String>( 557 return handle_scope.Escape(v8::Local<v8::String>(
560 v8::String::Concat(left, v8::String::Concat(source, right)))); 558 v8::String::Concat(left, v8::String::Concat(source, right))));
561 } 559 }
562 560
563 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { 561 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) {
564 CHECK_EQ(1, args.Length()); 562 CHECK_EQ(1, args.Length());
565 if (!args[0]->IsObject() || args[0]->IsNull()) { 563 if (!args[0]->IsObject() || args[0]->IsNull()) {
566 GetIsolate()->ThrowException( 564 GetIsolate()->ThrowException(
567 v8::Exception::TypeError(v8::String::NewFromUtf8(GetIsolate(), 565 v8::Exception::TypeError(v8::String::NewFromUtf8(GetIsolate(),
568 args[0]->IsUndefined() 566 args[0]->IsUndefined()
(...skipping 11 matching lines...) Expand all
580 if (privates.IsEmpty()) { 578 if (privates.IsEmpty()) {
581 GetIsolate()->ThrowException( 579 GetIsolate()->ThrowException(
582 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); 580 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates"));
583 return; 581 return;
584 } 582 }
585 obj->SetHiddenValue(privates_key, privates); 583 obj->SetHiddenValue(privates_key, privates);
586 } 584 }
587 args.GetReturnValue().Set(privates); 585 args.GetReturnValue().Set(privates);
588 } 586 }
589 587
590 v8::Handle<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { 588 v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) {
591 v8::EscapableHandleScope handle_scope(GetIsolate()); 589 v8::EscapableHandleScope handle_scope(GetIsolate());
592 v8::Context::Scope context_scope(context()->v8_context()); 590 v8::Context::Scope context_scope(context()->v8_context());
593 591
594 v8::Handle<v8::Value> source(GetSource(module_name)); 592 v8::Local<v8::Value> source(GetSource(module_name));
595 if (source.IsEmpty() || source->IsUndefined()) { 593 if (source.IsEmpty() || source->IsUndefined()) {
596 Fatal(context_, "No source for require(" + module_name + ")"); 594 Fatal(context_, "No source for require(" + module_name + ")");
597 return v8::Undefined(GetIsolate()); 595 return v8::Undefined(GetIsolate());
598 } 596 }
599 v8::Handle<v8::String> wrapped_source( 597 v8::Local<v8::String> wrapped_source(
600 WrapSource(v8::Handle<v8::String>::Cast(source))); 598 WrapSource(v8::Local<v8::String>::Cast(source)));
601 // Modules are wrapped in (function(){...}) so they always return functions. 599 // Modules are wrapped in (function(){...}) so they always return functions.
602 v8::Handle<v8::Value> func_as_value = 600 v8::Local<v8::Value> func_as_value =
603 RunString(wrapped_source, 601 RunString(wrapped_source,
604 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); 602 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
605 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { 603 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
606 Fatal(context_, "Bad source for require(" + module_name + ")"); 604 Fatal(context_, "Bad source for require(" + module_name + ")");
607 return v8::Undefined(GetIsolate()); 605 return v8::Undefined(GetIsolate());
608 } 606 }
609 607
610 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); 608 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(func_as_value);
611 609
612 v8::Handle<v8::Object> define_object = v8::Object::New(GetIsolate()); 610 v8::Local<v8::Object> define_object = v8::Object::New(GetIsolate());
613 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); 611 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object);
614 612
615 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); 613 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate());
616 v8::Handle<v8::Object> natives(NewInstance()); 614 v8::Local<v8::Object> natives(NewInstance());
617 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues 615 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues
618 616
619 // These must match the argument order in WrapSource. 617 // These must match the argument order in WrapSource.
620 v8::Handle<v8::Value> args[] = { 618 v8::Local<v8::Value> args[] = {
621 // AMD. 619 // AMD.
622 define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")), 620 define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")),
623 // CommonJS. 621 // CommonJS.
624 natives->Get(v8::String::NewFromUtf8( 622 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "require",
625 GetIsolate(), "require", v8::String::kInternalizedString)), 623 v8::String::kInternalizedString)),
626 natives->Get(v8::String::NewFromUtf8( 624 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireNative",
627 GetIsolate(), "requireNative", v8::String::kInternalizedString)), 625 v8::String::kInternalizedString)),
628 natives->Get(v8::String::NewFromUtf8( 626 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "requireAsync",
629 GetIsolate(), "requireAsync", v8::String::kInternalizedString)), 627 v8::String::kInternalizedString)),
630 exports, 628 exports,
631 // Libraries that we magically expose to every module. 629 // Libraries that we magically expose to every module.
632 console::AsV8Object(GetIsolate()), 630 console::AsV8Object(GetIsolate()),
633 natives->Get(v8::String::NewFromUtf8( 631 natives->Get(v8::String::NewFromUtf8(GetIsolate(), "privates",
634 GetIsolate(), "privates", v8::String::kInternalizedString)), 632 v8::String::kInternalizedString)),
635 // Each safe builtin. Keep in order with the arguments in WrapSource. 633 // Each safe builtin. Keep in order with the arguments in WrapSource.
636 context_->safe_builtins()->GetArray(), 634 context_->safe_builtins()->GetArray(),
637 context_->safe_builtins()->GetFunction(), 635 context_->safe_builtins()->GetFunction(),
638 context_->safe_builtins()->GetJSON(), 636 context_->safe_builtins()->GetJSON(),
639 context_->safe_builtins()->GetObjekt(), 637 context_->safe_builtins()->GetObjekt(),
640 context_->safe_builtins()->GetRegExp(), 638 context_->safe_builtins()->GetRegExp(),
641 context_->safe_builtins()->GetString(), 639 context_->safe_builtins()->GetString(),
642 context_->safe_builtins()->GetError(), 640 context_->safe_builtins()->GetError(),
643 }; 641 };
644 { 642 {
(...skipping 24 matching lines...) Expand all
669 if (registry->available_modules().count(dependency) == 0 && 667 if (registry->available_modules().count(dependency) == 0 &&
670 (module_system_managed || source_map_->Contains(dependency))) { 668 (module_system_managed || source_map_->Contains(dependency))) {
671 LoadModule(dependency); 669 LoadModule(dependency);
672 } 670 }
673 } 671 }
674 registry->AttemptToLoadMoreModules(GetIsolate()); 672 registry->AttemptToLoadMoreModules(GetIsolate());
675 } 673 }
676 674
677 void ModuleSystem::OnModuleLoaded( 675 void ModuleSystem::OnModuleLoaded(
678 scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver, 676 scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver,
679 v8::Handle<v8::Value> value) { 677 v8::Local<v8::Value> value) {
680 if (!is_valid()) 678 if (!is_valid())
681 return; 679 return;
682 v8::HandleScope handle_scope(GetIsolate()); 680 v8::HandleScope handle_scope(GetIsolate());
683 v8::Handle<v8::Promise::Resolver> resolver_local( 681 v8::Local<v8::Promise::Resolver> resolver_local(
684 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); 682 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver));
685 resolver_local->Resolve(value); 683 resolver_local->Resolve(value);
686 } 684 }
687 685
688 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { 686 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) {
689 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); 687 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name);
690 if (existing_handler != native_handler_map_.end()) { 688 if (existing_handler != native_handler_map_.end()) {
691 clobbered_native_handlers_.push_back(existing_handler->second); 689 clobbered_native_handlers_.push_back(existing_handler->second);
692 native_handler_map_.erase(existing_handler); 690 native_handler_map_.erase(existing_handler);
693 } 691 }
694 } 692 }
695 693
696 } // namespace extensions 694 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/module_system_test.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698