OLD | NEW |
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/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( | 95 std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
96 const v8::TryCatch& try_catch) { | 96 const v8::TryCatch& try_catch) { |
97 v8::Handle<v8::Message> message(try_catch.Message()); | 97 v8::Handle<v8::Message> message(try_catch.Message()); |
98 if (message.IsEmpty()) { | 98 if (message.IsEmpty()) { |
99 return "try_catch has no message"; | 99 return "try_catch has no message"; |
100 } | 100 } |
101 | 101 |
102 std::string resource_name = "<unknown resource>"; | 102 std::string resource_name = "<unknown resource>"; |
103 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { | 103 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { |
104 v8::String::Utf8Value resource_name_v8( | 104 v8::String::Utf8Value resource_name_v8( |
105 message->GetScriptOrigin().ResourceName()->ToString()); | 105 message->GetScriptOrigin().ResourceName()); |
106 resource_name.assign(*resource_name_v8, resource_name_v8.length()); | 106 resource_name.assign(*resource_name_v8, resource_name_v8.length()); |
107 } | 107 } |
108 | 108 |
109 std::string error_message = "<no error message>"; | 109 std::string error_message = "<no error message>"; |
110 if (!message->Get().IsEmpty()) { | 110 if (!message->Get().IsEmpty()) { |
111 v8::String::Utf8Value error_message_v8(message->Get()); | 111 v8::String::Utf8Value error_message_v8(message->Get()); |
112 error_message.assign(*error_message_v8, error_message_v8.length()); | 112 error_message.assign(*error_message_v8, error_message_v8.length()); |
113 } | 113 } |
114 | 114 |
115 return base::StringPrintf("%s:%d: %s", | 115 return base::StringPrintf("%s:%d: %s", |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 } | 194 } |
195 | 195 |
196 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { | 196 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
197 v8::EscapableHandleScope handle_scope(GetIsolate()); | 197 v8::EscapableHandleScope handle_scope(GetIsolate()); |
198 return handle_scope.Escape(RequireForJsInner( | 198 return handle_scope.Escape(RequireForJsInner( |
199 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); | 199 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); |
200 } | 200 } |
201 | 201 |
202 void ModuleSystem::RequireForJs( | 202 void ModuleSystem::RequireForJs( |
203 const v8::FunctionCallbackInfo<v8::Value>& args) { | 203 const v8::FunctionCallbackInfo<v8::Value>& args) { |
204 v8::Handle<v8::String> module_name = args[0]->ToString(); | 204 v8::Handle<v8::String> module_name = args[0]->ToString(args.GetIsolate()); |
205 args.GetReturnValue().Set(RequireForJsInner(module_name)); | 205 args.GetReturnValue().Set(RequireForJsInner(module_name)); |
206 } | 206 } |
207 | 207 |
208 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( | 208 v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
209 v8::Handle<v8::String> module_name) { | 209 v8::Handle<v8::String> module_name) { |
210 v8::EscapableHandleScope handle_scope(GetIsolate()); | 210 v8::EscapableHandleScope handle_scope(GetIsolate()); |
211 v8::Context::Scope context_scope(context()->v8_context()); | 211 v8::Context::Scope context_scope(context()->v8_context()); |
212 | 212 |
213 v8::Handle<v8::Object> global(context()->v8_context()->Global()); | 213 v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
214 | 214 |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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::Handle<v8::External>::Cast(module_system_value)->Value()); |
356 | 356 |
357 std::string name = | 357 std::string name = *v8::String::Utf8Value(parameters->Get( |
358 *v8::String::Utf8Value( | 358 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName))); |
359 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), | |
360 kModuleName))->ToString()); | |
361 | 359 |
362 // 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 |
363 // 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. |
364 v8::Context::Scope context_scope(context); | 362 v8::Context::Scope context_scope(context); |
365 NativesEnabledScope natives_enabled_scope(module_system); | 363 NativesEnabledScope natives_enabled_scope(module_system); |
366 | 364 |
367 v8::TryCatch try_catch; | 365 v8::TryCatch try_catch; |
368 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); | 366 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); |
369 if (try_catch.HasCaught()) { | 367 if (try_catch.HasCaught()) { |
370 module_system->HandleException(try_catch); | 368 module_system->HandleException(try_catch); |
371 return; | 369 return; |
372 } | 370 } |
373 if (module_value.IsEmpty() || !module_value->IsObject()) { | 371 if (module_value.IsEmpty() || !module_value->IsObject()) { |
374 // 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. |
375 return; | 373 return; |
376 } | 374 } |
377 | 375 |
378 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); | 376 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); |
379 v8::Handle<v8::String> field = | 377 v8::Handle<v8::String> field = |
380 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) | 378 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) |
381 ->ToString(); | 379 ->ToString(info.GetIsolate()); |
382 | 380 |
383 if (!module->Has(field)) { | 381 if (!module->Has(field)) { |
384 std::string field_str = *v8::String::Utf8Value(field); | 382 std::string field_str = *v8::String::Utf8Value(field); |
385 Fatal(module_system->context_, | 383 Fatal(module_system->context_, |
386 "Lazy require of " + name + "." + field_str + " did not set the " + | 384 "Lazy require of " + name + "." + field_str + " did not set the " + |
387 field_str + " field"); | 385 field_str + " field"); |
388 return; | 386 return; |
389 } | 387 } |
390 | 388 |
391 v8::Local<v8::Value> new_field = module->Get(field); | 389 v8::Local<v8::Value> new_field = module->Get(field); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 v8::EscapableHandleScope handle_scope(GetIsolate()); | 482 v8::EscapableHandleScope handle_scope(GetIsolate()); |
485 if (!source_map_->Contains(module_name)) | 483 if (!source_map_->Contains(module_name)) |
486 return v8::Undefined(GetIsolate()); | 484 return v8::Undefined(GetIsolate()); |
487 return handle_scope.Escape( | 485 return handle_scope.Escape( |
488 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); | 486 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); |
489 } | 487 } |
490 | 488 |
491 void ModuleSystem::RequireNative( | 489 void ModuleSystem::RequireNative( |
492 const v8::FunctionCallbackInfo<v8::Value>& args) { | 490 const v8::FunctionCallbackInfo<v8::Value>& args) { |
493 CHECK_EQ(1, args.Length()); | 491 CHECK_EQ(1, args.Length()); |
494 std::string native_name = *v8::String::Utf8Value(args[0]->ToString()); | 492 std::string native_name = *v8::String::Utf8Value(args[0]); |
495 args.GetReturnValue().Set(RequireNativeFromString(native_name)); | 493 args.GetReturnValue().Set(RequireNativeFromString(native_name)); |
496 } | 494 } |
497 | 495 |
498 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( | 496 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
499 const std::string& native_name) { | 497 const std::string& native_name) { |
500 if (natives_enabled_ == 0) { | 498 if (natives_enabled_ == 0) { |
501 // HACK: if in test throw exception so that we can test the natives-disabled | 499 // HACK: if in test throw exception so that we can test the natives-disabled |
502 // logic; however, under normal circumstances, this is programmer error so | 500 // logic; however, under normal circumstances, this is programmer error so |
503 // we could crash. | 501 // we could crash. |
504 if (exception_handler_) { | 502 if (exception_handler_) { |
(...skipping 14 matching lines...) Expand all Loading... |
519 Fatal(context_, | 517 Fatal(context_, |
520 "Couldn't find native for requireNative(" + native_name + ")"); | 518 "Couldn't find native for requireNative(" + native_name + ")"); |
521 return v8::Undefined(GetIsolate()); | 519 return v8::Undefined(GetIsolate()); |
522 } | 520 } |
523 return i->second->NewInstance(); | 521 return i->second->NewInstance(); |
524 } | 522 } |
525 | 523 |
526 void ModuleSystem::RequireAsync( | 524 void ModuleSystem::RequireAsync( |
527 const v8::FunctionCallbackInfo<v8::Value>& args) { | 525 const v8::FunctionCallbackInfo<v8::Value>& args) { |
528 CHECK_EQ(1, args.Length()); | 526 CHECK_EQ(1, args.Length()); |
529 std::string module_name = *v8::String::Utf8Value(args[0]->ToString()); | 527 std::string module_name = *v8::String::Utf8Value(args[0]); |
530 v8::Handle<v8::Promise::Resolver> resolver( | 528 v8::Handle<v8::Promise::Resolver> resolver( |
531 v8::Promise::Resolver::New(GetIsolate())); | 529 v8::Promise::Resolver::New(GetIsolate())); |
532 args.GetReturnValue().Set(resolver->GetPromise()); | 530 args.GetReturnValue().Set(resolver->GetPromise()); |
533 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > persistent_resolver( | 531 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > persistent_resolver( |
534 new v8::UniquePersistent<v8::Promise::Resolver>(GetIsolate(), resolver)); | 532 new v8::UniquePersistent<v8::Promise::Resolver>(GetIsolate(), resolver)); |
535 gin::ModuleRegistry* module_registry = | 533 gin::ModuleRegistry* module_registry = |
536 gin::ModuleRegistry::From(context_->v8_context()); | 534 gin::ModuleRegistry::From(context_->v8_context()); |
537 if (!module_registry) { | 535 if (!module_registry) { |
538 Warn(GetIsolate(), "Extension view no longer exists"); | 536 Warn(GetIsolate(), "Extension view no longer exists"); |
539 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( | 537 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 v8::Handle<v8::Value> value) { | 670 v8::Handle<v8::Value> value) { |
673 if (!is_valid()) | 671 if (!is_valid()) |
674 return; | 672 return; |
675 v8::HandleScope handle_scope(GetIsolate()); | 673 v8::HandleScope handle_scope(GetIsolate()); |
676 v8::Handle<v8::Promise::Resolver> resolver_local( | 674 v8::Handle<v8::Promise::Resolver> resolver_local( |
677 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); | 675 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); |
678 resolver_local->Resolve(value); | 676 resolver_local->Resolve(value); |
679 } | 677 } |
680 | 678 |
681 } // namespace extensions | 679 } // namespace extensions |
OLD | NEW |