Chromium Code Reviews| 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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { | 220 if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
| 221 Warn(GetIsolate(), "Extension view no longer exists"); | 221 Warn(GetIsolate(), "Extension view no longer exists"); |
| 222 return v8::Undefined(GetIsolate()); | 222 return v8::Undefined(GetIsolate()); |
| 223 } | 223 } |
| 224 | 224 |
| 225 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); | 225 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
| 226 v8::Local<v8::Value> exports(modules->Get(module_name)); | 226 v8::Local<v8::Value> exports(modules->Get(module_name)); |
| 227 if (!exports->IsUndefined()) | 227 if (!exports->IsUndefined()) |
| 228 return handle_scope.Escape(exports); | 228 return handle_scope.Escape(exports); |
| 229 | 229 |
| 230 exports = LoadModule(*v8::String::Utf8Value(module_name)); | 230 std::string module_name_str = *v8::String::Utf8Value(module_name); |
| 231 | |
| 232 v8::Handle<v8::Value> source(GetSource(module_name_str)); | |
| 233 if (source.IsEmpty() || source->IsUndefined()) { | |
| 234 Fatal(context_, "No source for require(" + module_name_str + ")"); | |
| 235 return v8::Undefined(GetIsolate()); | |
| 236 } | |
| 237 | |
| 238 exports = LoadModule(module_name_str, source); | |
| 231 modules->Set(module_name, exports); | 239 modules->Set(module_name, exports); |
| 232 return handle_scope.Escape(exports); | 240 return handle_scope.Escape(exports); |
| 233 } | 241 } |
| 234 | 242 |
| 235 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( | 243 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 236 const std::string& module_name, | 244 const std::string& module_name, |
| 237 const std::string& method_name) { | 245 const std::string& method_name) { |
| 238 v8::EscapableHandleScope handle_scope(GetIsolate()); | 246 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 239 v8::Handle<v8::Value> no_args; | 247 v8::Handle<v8::Value> no_args; |
| 240 return handle_scope.Escape( | 248 return handle_scope.Escape( |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 531 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > persistent_resolver( | 539 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > persistent_resolver( |
| 532 new v8::UniquePersistent<v8::Promise::Resolver>(GetIsolate(), resolver)); | 540 new v8::UniquePersistent<v8::Promise::Resolver>(GetIsolate(), resolver)); |
| 533 gin::ModuleRegistry* module_registry = | 541 gin::ModuleRegistry* module_registry = |
| 534 gin::ModuleRegistry::From(context_->v8_context()); | 542 gin::ModuleRegistry::From(context_->v8_context()); |
| 535 if (!module_registry) { | 543 if (!module_registry) { |
| 536 Warn(GetIsolate(), "Extension view no longer exists"); | 544 Warn(GetIsolate(), "Extension view no longer exists"); |
| 537 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( | 545 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( |
| 538 GetIsolate(), "Extension view no longer exists"))); | 546 GetIsolate(), "Extension view no longer exists"))); |
| 539 return; | 547 return; |
| 540 } | 548 } |
| 549 | |
| 550 v8::Handle<v8::Value> source(GetSource(module_name)); | |
| 551 if ((source.IsEmpty() || source->IsUndefined()) && | |
|
Sam McNally
2015/01/22 07:42:11
I'm not a fan of this. It introduces ordering depe
| |
| 552 !ContainsKey(module_registry->available_modules(), module_name)) { | |
| 553 std::string error_text = "No source for require(" + module_name + ")"; | |
| 554 Fatal(context_, error_text); | |
| 555 resolver->Reject(v8::Exception::Error( | |
| 556 v8::String::NewFromUtf8(GetIsolate(), error_text.c_str()))); | |
| 557 return; | |
| 558 } | |
| 559 | |
| 541 module_registry->LoadModule(GetIsolate(), | 560 module_registry->LoadModule(GetIsolate(), |
| 542 module_name, | 561 module_name, |
| 543 base::Bind(&ModuleSystem::OnModuleLoaded, | 562 base::Bind(&ModuleSystem::OnModuleLoaded, |
| 544 weak_factory_.GetWeakPtr(), | 563 weak_factory_.GetWeakPtr(), |
| 545 base::Passed(&persistent_resolver))); | 564 base::Passed(&persistent_resolver))); |
| 546 if (module_registry->available_modules().count(module_name) == 0) | 565 if (module_registry->available_modules().count(module_name) == 0) |
| 547 LoadModule(module_name); | 566 LoadModule(module_name, source); |
| 548 } | 567 } |
| 549 | 568 |
| 550 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { | 569 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 551 v8::EscapableHandleScope handle_scope(GetIsolate()); | 570 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 552 // Keep in order with the arguments in RequireForJsInner. | 571 // Keep in order with the arguments in RequireForJsInner. |
| 553 v8::Handle<v8::String> left = v8::String::NewFromUtf8( | 572 v8::Handle<v8::String> left = v8::String::NewFromUtf8( |
| 554 GetIsolate(), | 573 GetIsolate(), |
| 555 "(function(define, require, requireNative, requireAsync, exports, " | 574 "(function(define, require, requireNative, requireAsync, exports, " |
| 556 "console, privates," | 575 "console, privates," |
| 557 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" | 576 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 581 if (privates.IsEmpty()) { | 600 if (privates.IsEmpty()) { |
| 582 GetIsolate()->ThrowException( | 601 GetIsolate()->ThrowException( |
| 583 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); | 602 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); |
| 584 return; | 603 return; |
| 585 } | 604 } |
| 586 obj->SetHiddenValue(privates_key, privates); | 605 obj->SetHiddenValue(privates_key, privates); |
| 587 } | 606 } |
| 588 args.GetReturnValue().Set(privates); | 607 args.GetReturnValue().Set(privates); |
| 589 } | 608 } |
| 590 | 609 |
| 591 v8::Handle<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { | 610 v8::Handle<v8::Value> ModuleSystem::LoadModule( |
| 611 const std::string& module_name, | |
| 612 v8::Handle<v8::Value> module_source) { | |
| 592 v8::EscapableHandleScope handle_scope(GetIsolate()); | 613 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 593 v8::Context::Scope context_scope(context()->v8_context()); | 614 v8::Context::Scope context_scope(context()->v8_context()); |
| 594 | 615 |
| 595 v8::Handle<v8::Value> source(GetSource(module_name)); | |
| 596 if (source.IsEmpty() || source->IsUndefined()) { | |
| 597 Fatal(context_, "No source for require(" + module_name + ")"); | |
| 598 return v8::Undefined(GetIsolate()); | |
| 599 } | |
| 600 v8::Handle<v8::String> wrapped_source( | 616 v8::Handle<v8::String> wrapped_source( |
| 601 WrapSource(v8::Handle<v8::String>::Cast(source))); | 617 WrapSource(v8::Handle<v8::String>::Cast(module_source))); |
| 602 // Modules are wrapped in (function(){...}) so they always return functions. | 618 // Modules are wrapped in (function(){...}) so they always return functions. |
| 603 v8::Handle<v8::Value> func_as_value = | 619 v8::Handle<v8::Value> func_as_value = |
| 604 RunString(wrapped_source, | 620 RunString(wrapped_source, |
| 605 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); | 621 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
| 606 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { | 622 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
| 607 Fatal(context_, "Bad source for require(" + module_name + ")"); | 623 Fatal(context_, "Bad source for require(" + module_name + ")"); |
| 608 return v8::Undefined(GetIsolate()); | 624 return v8::Undefined(GetIsolate()); |
| 609 } | 625 } |
| 610 | 626 |
| 611 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); | 627 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 662 gin::ModuleRegistry* registry = | 678 gin::ModuleRegistry* registry = |
| 663 gin::ModuleRegistry::From(context_->v8_context()); | 679 gin::ModuleRegistry::From(context_->v8_context()); |
| 664 DCHECK(registry); | 680 DCHECK(registry); |
| 665 for (const auto& dependency : dependencies) { | 681 for (const auto& dependency : dependencies) { |
| 666 // If a dependency is not available, and either the module or this | 682 // If a dependency is not available, and either the module or this |
| 667 // dependency is managed by ModuleSystem, attempt to load it. Other | 683 // dependency is managed by ModuleSystem, attempt to load it. Other |
| 668 // gin::ModuleRegistry users (WebUI and users of the mojoPrivate API) are | 684 // gin::ModuleRegistry users (WebUI and users of the mojoPrivate API) are |
| 669 // responsible for loading their module dependencies when required. | 685 // responsible for loading their module dependencies when required. |
| 670 if (registry->available_modules().count(dependency) == 0 && | 686 if (registry->available_modules().count(dependency) == 0 && |
| 671 (module_system_managed || source_map_->Contains(dependency))) { | 687 (module_system_managed || source_map_->Contains(dependency))) { |
| 672 LoadModule(dependency); | 688 LoadModule(dependency, GetSource(dependency)); |
| 673 } | 689 } |
| 674 } | 690 } |
| 675 registry->AttemptToLoadMoreModules(GetIsolate()); | 691 registry->AttemptToLoadMoreModules(GetIsolate()); |
| 676 } | 692 } |
| 677 | 693 |
| 678 void ModuleSystem::OnModuleLoaded( | 694 void ModuleSystem::OnModuleLoaded( |
| 679 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver, | 695 scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver, |
| 680 v8::Handle<v8::Value> value) { | 696 v8::Handle<v8::Value> value) { |
| 681 if (!is_valid()) | 697 if (!is_valid()) { |
| 698 LOG(ERROR) << "!is_valid"; | |
| 682 return; | 699 return; |
| 700 } | |
| 683 v8::HandleScope handle_scope(GetIsolate()); | 701 v8::HandleScope handle_scope(GetIsolate()); |
| 684 v8::Handle<v8::Promise::Resolver> resolver_local( | 702 v8::Handle<v8::Promise::Resolver> resolver_local( |
| 685 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); | 703 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); |
| 686 resolver_local->Resolve(value); | 704 resolver_local->Resolve(value); |
| 687 } | 705 } |
| 688 | 706 |
| 689 } // namespace extensions | 707 } // namespace extensions |
| OLD | NEW |