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, v8::Local<v8::Value>()); | |
jbroman
2016/12/16 19:00:50
Any reason to pass an empty local here and then ov
Devlin
2016/12/16 20:31:17
Works for me. Done.
| |
674 } | |
675 | |
676 v8::Local<v8::Value> ModuleSystem::LoadModuleWithNativeAPIBridge( | |
677 const std::string& module_name, | |
678 v8::Local<v8::Value> api_bridge) { | |
663 v8::EscapableHandleScope handle_scope(GetIsolate()); | 679 v8::EscapableHandleScope handle_scope(GetIsolate()); |
664 v8::Local<v8::Context> v8_context = context()->v8_context(); | 680 v8::Local<v8::Context> v8_context = context()->v8_context(); |
665 v8::Context::Scope context_scope(v8_context); | 681 v8::Context::Scope context_scope(v8_context); |
666 | 682 |
667 v8::Local<v8::String> source = | 683 v8::Local<v8::String> source = |
668 source_map_->GetSource(GetIsolate(), module_name); | 684 source_map_->GetSource(GetIsolate(), module_name); |
669 if (source.IsEmpty()) { | 685 if (source.IsEmpty()) { |
670 Fatal(context_, "No source for require(" + module_name + ")"); | 686 Fatal(context_, "No source for require(" + module_name + ")"); |
671 return v8::Undefined(GetIsolate()); | 687 return v8::Undefined(GetIsolate()); |
672 } | 688 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
706 NOTREACHED(); | 722 NOTREACHED(); |
707 return v8::Undefined(GetIsolate()); | 723 return v8::Undefined(GetIsolate()); |
708 } | 724 } |
709 | 725 |
710 exports->DefineOwnProperty(v8_context, v8_key, function, v8::ReadOnly) | 726 exports->DefineOwnProperty(v8_context, v8_key, function, v8::ReadOnly) |
711 .FromJust(); | 727 .FromJust(); |
712 | 728 |
713 v8::Local<v8::Object> natives(NewInstance()); | 729 v8::Local<v8::Object> natives(NewInstance()); |
714 CHECK(!natives.IsEmpty()); // this can fail if v8 has issues | 730 CHECK(!natives.IsEmpty()); // this can fail if v8 has issues |
715 | 731 |
732 if (api_bridge.IsEmpty()) | |
733 api_bridge = v8::Undefined(v8_context->GetIsolate()); | |
734 | |
716 // These must match the argument order in WrapSource. | 735 // These must match the argument order in WrapSource. |
717 v8::Local<v8::Value> args[] = { | 736 v8::Local<v8::Value> args[] = { |
718 // AMD. | 737 // AMD. |
719 GetPropertyUnsafe(v8_context, define_object, "define"), | 738 GetPropertyUnsafe(v8_context, define_object, "define"), |
720 // CommonJS. | 739 // CommonJS. |
721 GetPropertyUnsafe(v8_context, natives, "require", | 740 GetPropertyUnsafe(v8_context, natives, "require", |
722 v8::NewStringType::kInternalized), | 741 v8::NewStringType::kInternalized), |
723 GetPropertyUnsafe(v8_context, natives, "requireNative", | 742 GetPropertyUnsafe(v8_context, natives, "requireNative", |
724 v8::NewStringType::kInternalized), | 743 v8::NewStringType::kInternalized), |
725 GetPropertyUnsafe(v8_context, natives, "requireAsync", | 744 GetPropertyUnsafe(v8_context, natives, "requireAsync", |
726 v8::NewStringType::kInternalized), | 745 v8::NewStringType::kInternalized), |
727 exports, | 746 exports, |
728 // Libraries that we magically expose to every module. | 747 // Libraries that we magically expose to every module. |
729 console::AsV8Object(GetIsolate()), | 748 console::AsV8Object(GetIsolate()), |
730 GetPropertyUnsafe(v8_context, natives, "privates", | 749 GetPropertyUnsafe(v8_context, natives, "privates", |
731 v8::NewStringType::kInternalized), | 750 v8::NewStringType::kInternalized), |
732 // Each safe builtin. Keep in order with the arguments in WrapSource. | 751 // Each safe builtin. Keep in order with the arguments in WrapSource. |
733 context_->safe_builtins()->GetArray(), | 752 api_bridge, context_->safe_builtins()->GetArray(), |
734 context_->safe_builtins()->GetFunction(), | 753 context_->safe_builtins()->GetFunction(), |
735 context_->safe_builtins()->GetJSON(), | 754 context_->safe_builtins()->GetJSON(), |
736 context_->safe_builtins()->GetObjekt(), | 755 context_->safe_builtins()->GetObjekt(), |
737 context_->safe_builtins()->GetRegExp(), | 756 context_->safe_builtins()->GetRegExp(), |
738 context_->safe_builtins()->GetString(), | 757 context_->safe_builtins()->GetString(), |
739 context_->safe_builtins()->GetError(), | 758 context_->safe_builtins()->GetError(), |
740 }; | 759 }; |
741 { | 760 { |
742 v8::TryCatch try_catch(GetIsolate()); | 761 v8::TryCatch try_catch(GetIsolate()); |
743 try_catch.SetCaptureMessage(true); | 762 try_catch.SetCaptureMessage(true); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
819 !value->IsFunction()) { | 838 !value->IsFunction()) { |
820 Fatal(context_, module_name + "." + method_name + " is not a function"); | 839 Fatal(context_, module_name + "." + method_name + " is not a function"); |
821 return function; | 840 return function; |
822 } | 841 } |
823 | 842 |
824 function = v8::Local<v8::Function>::Cast(value); | 843 function = v8::Local<v8::Function>::Cast(value); |
825 return function; | 844 return function; |
826 } | 845 } |
827 | 846 |
828 } // namespace extensions | 847 } // namespace extensions |
OLD | NEW |