| 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 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 "v8.callModuleMethod", | 307 "v8.callModuleMethod", |
| 308 "module_name", | 308 "module_name", |
| 309 module_name, | 309 module_name, |
| 310 "method_name", | 310 "method_name", |
| 311 method_name); | 311 method_name); |
| 312 | 312 |
| 313 v8::EscapableHandleScope handle_scope(GetIsolate()); | 313 v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 314 v8::Local<v8::Context> v8_context = context()->v8_context(); | 314 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 315 v8::Context::Scope context_scope(v8_context); | 315 v8::Context::Scope context_scope(v8_context); |
| 316 | 316 |
| 317 v8::Local<v8::String> v8_module_name; | 317 v8::Local<v8::Function> function = |
| 318 v8::Local<v8::String> v8_method_name; | 318 GetModuleFunction(module_name, method_name); |
| 319 if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name) || | |
| 320 !ToV8String(GetIsolate(), method_name.c_str(), &v8_method_name)) { | |
| 321 return handle_scope.Escape(v8::Undefined(GetIsolate())); | |
| 322 } | |
| 323 | 319 |
| 324 v8::Local<v8::Value> module; | |
| 325 { | |
| 326 NativesEnabledScope natives_enabled(this); | |
| 327 module = RequireForJsInner(v8_module_name); | |
| 328 } | |
| 329 | |
| 330 if (module.IsEmpty() || !module->IsObject()) { | |
| 331 Fatal(context_, | |
| 332 "Failed to get module " + module_name + " to call " + method_name); | |
| 333 return handle_scope.Escape(v8::Undefined(GetIsolate())); | |
| 334 } | |
| 335 | |
| 336 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module); | |
| 337 v8::Local<v8::Value> value; | |
| 338 if (!GetProperty(v8_context, object, v8_method_name, &value) || | |
| 339 !value->IsFunction()) { | |
| 340 Fatal(context_, module_name + "." + method_name + " is not a function"); | |
| 341 return handle_scope.Escape(v8::Undefined(GetIsolate())); | |
| 342 } | |
| 343 | |
| 344 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value); | |
| 345 v8::Local<v8::Value> result; | 320 v8::Local<v8::Value> result; |
| 346 { | 321 { |
| 347 v8::TryCatch try_catch(GetIsolate()); | 322 v8::TryCatch try_catch(GetIsolate()); |
| 348 try_catch.SetCaptureMessage(true); | 323 try_catch.SetCaptureMessage(true); |
| 349 result = context_->CallFunction(func, argc, argv); | 324 result = context_->CallFunction(function, argc, argv); |
| 350 if (try_catch.HasCaught()) { | 325 if (try_catch.HasCaught()) { |
| 351 HandleException(try_catch); | 326 HandleException(try_catch); |
| 352 result = v8::Undefined(GetIsolate()); | 327 result = v8::Undefined(GetIsolate()); |
| 353 } | 328 } |
| 354 } | 329 } |
| 355 return handle_scope.Escape(result); | 330 return handle_scope.Escape(result); |
| 356 } | 331 } |
| 357 | 332 |
| 333 void ModuleSystem::CallModuleMethodSafe(const std::string& module_name, |
| 334 const std::string& method_name) { |
| 335 v8::HandleScope handle_scope(GetIsolate()); |
| 336 v8::Local<v8::Value> no_args; |
| 337 CallModuleMethodSafe(module_name, method_name, 0, &no_args); |
| 338 } |
| 339 |
| 340 void ModuleSystem::CallModuleMethodSafe( |
| 341 const std::string& module_name, |
| 342 const std::string& method_name, |
| 343 std::vector<v8::Local<v8::Value>>* args) { |
| 344 CallModuleMethodSafe(module_name, method_name, args->size(), args->data()); |
| 345 } |
| 346 |
| 347 void ModuleSystem::CallModuleMethodSafe(const std::string& module_name, |
| 348 const std::string& method_name, |
| 349 int argc, |
| 350 v8::Local<v8::Value> argv[]) { |
| 351 TRACE_EVENT2("v8", "v8.callModuleMethodSafe", "module_name", module_name, |
| 352 "method_name", method_name); |
| 353 |
| 354 v8::HandleScope handle_scope(GetIsolate()); |
| 355 v8::Local<v8::Context> v8_context = context()->v8_context(); |
| 356 v8::Context::Scope context_scope(v8_context); |
| 357 |
| 358 v8::Local<v8::Function> function = |
| 359 GetModuleFunction(module_name, method_name); |
| 360 |
| 361 { |
| 362 v8::TryCatch try_catch(GetIsolate()); |
| 363 try_catch.SetCaptureMessage(true); |
| 364 context_->SafeCallFunction(function, argc, argv); |
| 365 if (try_catch.HasCaught()) |
| 366 HandleException(try_catch); |
| 367 } |
| 368 } |
| 369 |
| 358 void ModuleSystem::RegisterNativeHandler( | 370 void ModuleSystem::RegisterNativeHandler( |
| 359 const std::string& name, | 371 const std::string& name, |
| 360 std::unique_ptr<NativeHandler> native_handler) { | 372 std::unique_ptr<NativeHandler> native_handler) { |
| 361 ClobberExistingNativeHandler(name); | 373 ClobberExistingNativeHandler(name); |
| 362 native_handler_map_[name] = std::move(native_handler); | 374 native_handler_map_[name] = std::move(native_handler); |
| 363 } | 375 } |
| 364 | 376 |
| 365 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { | 377 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
| 366 ClobberExistingNativeHandler(name); | 378 ClobberExistingNativeHandler(name); |
| 367 overridden_native_handlers_.insert(name); | 379 overridden_native_handlers_.insert(name); |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 } | 786 } |
| 775 | 787 |
| 776 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { | 788 void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) { |
| 777 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); | 789 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name); |
| 778 if (existing_handler != native_handler_map_.end()) { | 790 if (existing_handler != native_handler_map_.end()) { |
| 779 clobbered_native_handlers_.push_back(std::move(existing_handler->second)); | 791 clobbered_native_handlers_.push_back(std::move(existing_handler->second)); |
| 780 native_handler_map_.erase(existing_handler); | 792 native_handler_map_.erase(existing_handler); |
| 781 } | 793 } |
| 782 } | 794 } |
| 783 | 795 |
| 796 v8::Local<v8::Function> ModuleSystem::GetModuleFunction( |
| 797 const std::string& module_name, |
| 798 const std::string& method_name) { |
| 799 v8::Local<v8::String> v8_module_name; |
| 800 v8::Local<v8::String> v8_method_name; |
| 801 v8::Local<v8::Function> function; |
| 802 if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name) || |
| 803 !ToV8String(GetIsolate(), method_name.c_str(), &v8_method_name)) { |
| 804 return function; |
| 805 } |
| 806 |
| 807 v8::Local<v8::Value> module; |
| 808 { |
| 809 NativesEnabledScope natives_enabled(this); |
| 810 module = RequireForJsInner(v8_module_name); |
| 811 } |
| 812 |
| 813 if (module.IsEmpty() || !module->IsObject()) { |
| 814 Fatal(context_, |
| 815 "Failed to get module " + module_name + " to call " + method_name); |
| 816 return function; |
| 817 } |
| 818 |
| 819 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module); |
| 820 v8::Local<v8::Value> value; |
| 821 if (!GetProperty(context()->v8_context(), object, v8_method_name, &value) || |
| 822 !value->IsFunction()) { |
| 823 Fatal(context_, module_name + "." + method_name + " is not a function"); |
| 824 return function; |
| 825 } |
| 826 |
| 827 function = v8::Local<v8::Function>::Cast(value); |
| 828 return function; |
| 829 } |
| 830 |
| 784 } // namespace extensions | 831 } // namespace extensions |
| OLD | NEW |