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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 524 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 const std::string& field, | 535 const std::string& field, |
536 const std::string& module_name, | 536 const std::string& module_name, |
537 const std::string& module_field) { | 537 const std::string& module_field) { |
538 SetLazyField(object, | 538 SetLazyField(object, |
539 field, | 539 field, |
540 module_name, | 540 module_name, |
541 module_field, | 541 module_field, |
542 &ModuleSystem::NativeLazyFieldGetter); | 542 &ModuleSystem::NativeLazyFieldGetter); |
543 } | 543 } |
544 | 544 |
| 545 void ModuleSystem::OnNativeBindingCreated( |
| 546 const std::string& api_name, |
| 547 v8::Local<v8::Value> api_bridge_value) { |
| 548 v8::HandleScope scope(GetIsolate()); |
| 549 if (source_map_->Contains(api_name)) { |
| 550 NativesEnabledScope enabled(this); |
| 551 LoadModuleWithNativeAPIBridge(api_name, api_bridge_value); |
| 552 } |
| 553 } |
| 554 |
545 v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code, | 555 v8::Local<v8::Value> ModuleSystem::RunString(v8::Local<v8::String> code, |
546 v8::Local<v8::String> name) { | 556 v8::Local<v8::String> name) { |
547 return context_->RunScript( | 557 return context_->RunScript( |
548 name, code, base::Bind(&ExceptionHandler::HandleUncaughtException, | 558 name, code, base::Bind(&ExceptionHandler::HandleUncaughtException, |
549 base::Unretained(exception_handler_.get()))); | 559 base::Unretained(exception_handler_.get()))); |
550 } | 560 } |
551 | 561 |
552 void ModuleSystem::RequireNative( | 562 void ModuleSystem::RequireNative( |
553 const v8::FunctionCallbackInfo<v8::Value>& args) { | 563 const v8::FunctionCallbackInfo<v8::Value>& args) { |
554 CHECK_EQ(1, args.Length()); | 564 CHECK_EQ(1, args.Length()); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 if (module_registry->available_modules().count(module_name) == 0) | 625 if (module_registry->available_modules().count(module_name) == 0) |
616 LoadModule(module_name); | 626 LoadModule(module_name); |
617 } | 627 } |
618 | 628 |
619 v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) { | 629 v8::Local<v8::String> ModuleSystem::WrapSource(v8::Local<v8::String> source) { |
620 v8::EscapableHandleScope handle_scope(GetIsolate()); | 630 v8::EscapableHandleScope handle_scope(GetIsolate()); |
621 // Keep in order with the arguments in RequireForJsInner. | 631 // Keep in order with the arguments in RequireForJsInner. |
622 v8::Local<v8::String> left = ToV8StringUnsafe( | 632 v8::Local<v8::String> left = ToV8StringUnsafe( |
623 GetIsolate(), | 633 GetIsolate(), |
624 "(function(define, require, requireNative, requireAsync, exports, " | 634 "(function(define, require, requireNative, requireAsync, exports, " |
625 "console, privates," | 635 "console, privates, apiBridge," |
626 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" | 636 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" |
627 "'use strict';"); | 637 "'use strict';"); |
628 v8::Local<v8::String> right = ToV8StringUnsafe(GetIsolate(), "\n})"); | 638 v8::Local<v8::String> right = ToV8StringUnsafe(GetIsolate(), "\n})"); |
629 return handle_scope.Escape(v8::Local<v8::String>( | 639 return handle_scope.Escape(v8::Local<v8::String>( |
630 v8::String::Concat(left, v8::String::Concat(source, right)))); | 640 v8::String::Concat(left, v8::String::Concat(source, right)))); |
631 } | 641 } |
632 | 642 |
633 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { | 643 void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
634 CHECK_EQ(1, args.Length()); | 644 CHECK_EQ(1, args.Length()); |
635 if (!args[0]->IsObject() || args[0]->IsNull()) { | 645 if (!args[0]->IsObject() || args[0]->IsNull()) { |
(...skipping 17 matching lines...) Expand all Loading... |
653 v8::Maybe<bool> maybe = | 663 v8::Maybe<bool> maybe = |
654 privates.As<v8::Object>()->SetPrototype(context()->v8_context(), | 664 privates.As<v8::Object>()->SetPrototype(context()->v8_context(), |
655 v8::Null(args.GetIsolate())); | 665 v8::Null(args.GetIsolate())); |
656 CHECK(maybe.IsJust() && maybe.FromJust()); | 666 CHECK(maybe.IsJust() && maybe.FromJust()); |
657 SetPrivate(obj, "privates", privates); | 667 SetPrivate(obj, "privates", privates); |
658 } | 668 } |
659 args.GetReturnValue().Set(privates); | 669 args.GetReturnValue().Set(privates); |
660 } | 670 } |
661 | 671 |
662 v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { | 672 v8::Local<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
| 673 return LoadModuleWithNativeAPIBridge(module_name, |
| 674 v8::Undefined(GetIsolate())); |
| 675 } |
| 676 |
| 677 v8::Local<v8::Value> ModuleSystem::LoadModuleWithNativeAPIBridge( |
| 678 const std::string& module_name, |
| 679 v8::Local<v8::Value> api_bridge) { |
663 v8::EscapableHandleScope handle_scope(GetIsolate()); | 680 v8::EscapableHandleScope handle_scope(GetIsolate()); |
664 v8::Local<v8::Context> v8_context = context()->v8_context(); | 681 v8::Local<v8::Context> v8_context = context()->v8_context(); |
665 v8::Context::Scope context_scope(v8_context); | 682 v8::Context::Scope context_scope(v8_context); |
666 | 683 |
667 v8::Local<v8::String> source = | 684 v8::Local<v8::String> source = |
668 source_map_->GetSource(GetIsolate(), module_name); | 685 source_map_->GetSource(GetIsolate(), module_name); |
669 if (source.IsEmpty()) { | 686 if (source.IsEmpty()) { |
670 Fatal(context_, "No source for require(" + module_name + ")"); | 687 Fatal(context_, "No source for require(" + module_name + ")"); |
671 return v8::Undefined(GetIsolate()); | 688 return v8::Undefined(GetIsolate()); |
672 } | 689 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 v8::NewStringType::kInternalized), | 739 v8::NewStringType::kInternalized), |
723 GetPropertyUnsafe(v8_context, natives, "requireNative", | 740 GetPropertyUnsafe(v8_context, natives, "requireNative", |
724 v8::NewStringType::kInternalized), | 741 v8::NewStringType::kInternalized), |
725 GetPropertyUnsafe(v8_context, natives, "requireAsync", | 742 GetPropertyUnsafe(v8_context, natives, "requireAsync", |
726 v8::NewStringType::kInternalized), | 743 v8::NewStringType::kInternalized), |
727 exports, | 744 exports, |
728 // Libraries that we magically expose to every module. | 745 // Libraries that we magically expose to every module. |
729 console::AsV8Object(GetIsolate()), | 746 console::AsV8Object(GetIsolate()), |
730 GetPropertyUnsafe(v8_context, natives, "privates", | 747 GetPropertyUnsafe(v8_context, natives, "privates", |
731 v8::NewStringType::kInternalized), | 748 v8::NewStringType::kInternalized), |
| 749 api_bridge, // exposed as apiBridge. |
732 // Each safe builtin. Keep in order with the arguments in WrapSource. | 750 // Each safe builtin. Keep in order with the arguments in WrapSource. |
733 context_->safe_builtins()->GetArray(), | 751 context_->safe_builtins()->GetArray(), |
734 context_->safe_builtins()->GetFunction(), | 752 context_->safe_builtins()->GetFunction(), |
735 context_->safe_builtins()->GetJSON(), | 753 context_->safe_builtins()->GetJSON(), |
736 context_->safe_builtins()->GetObjekt(), | 754 context_->safe_builtins()->GetObjekt(), |
737 context_->safe_builtins()->GetRegExp(), | 755 context_->safe_builtins()->GetRegExp(), |
738 context_->safe_builtins()->GetString(), | 756 context_->safe_builtins()->GetString(), |
739 context_->safe_builtins()->GetError(), | 757 context_->safe_builtins()->GetError(), |
740 }; | 758 }; |
741 { | 759 { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 !value->IsFunction()) { | 837 !value->IsFunction()) { |
820 Fatal(context_, module_name + "." + method_name + " is not a function"); | 838 Fatal(context_, module_name + "." + method_name + " is not a function"); |
821 return function; | 839 return function; |
822 } | 840 } |
823 | 841 |
824 function = v8::Local<v8::Function>::Cast(value); | 842 function = v8::Local<v8::Function>::Cast(value); |
825 return function; | 843 return function; |
826 } | 844 } |
827 | 845 |
828 } // namespace extensions | 846 } // namespace extensions |
OLD | NEW |