Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(827)

Unified Diff: extensions/renderer/module_system.cc

Issue 2423043003: [Extensions] Convert some callers of ScriptContext::CallFunction (Closed)
Patch Set: d'oh Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/utils_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/module_system.cc
diff --git a/extensions/renderer/module_system.cc b/extensions/renderer/module_system.cc
index b814350e1c9dddde827d065cb3b09a9aa3fcb324..2fe422a4b871e321c9e12d578ec3c924cb5798f0 100644
--- a/extensions/renderer/module_system.cc
+++ b/extensions/renderer/module_system.cc
@@ -314,39 +314,14 @@ v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
v8::Local<v8::Context> v8_context = context()->v8_context();
v8::Context::Scope context_scope(v8_context);
- v8::Local<v8::String> v8_module_name;
- v8::Local<v8::String> v8_method_name;
- if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name) ||
- !ToV8String(GetIsolate(), method_name.c_str(), &v8_method_name)) {
- return handle_scope.Escape(v8::Undefined(GetIsolate()));
- }
-
- v8::Local<v8::Value> module;
- {
- NativesEnabledScope natives_enabled(this);
- module = RequireForJsInner(v8_module_name);
- }
-
- if (module.IsEmpty() || !module->IsObject()) {
- Fatal(context_,
- "Failed to get module " + module_name + " to call " + method_name);
- return handle_scope.Escape(v8::Undefined(GetIsolate()));
- }
-
- v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module);
- v8::Local<v8::Value> value;
- if (!GetProperty(v8_context, object, v8_method_name, &value) ||
- !value->IsFunction()) {
- Fatal(context_, module_name + "." + method_name + " is not a function");
- return handle_scope.Escape(v8::Undefined(GetIsolate()));
- }
+ v8::Local<v8::Function> function =
+ GetModuleFunction(module_name, method_name);
- v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(value);
v8::Local<v8::Value> result;
{
v8::TryCatch try_catch(GetIsolate());
try_catch.SetCaptureMessage(true);
- result = context_->CallFunction(func, argc, argv);
+ result = context_->CallFunction(function, argc, argv);
if (try_catch.HasCaught()) {
HandleException(try_catch);
result = v8::Undefined(GetIsolate());
@@ -355,6 +330,43 @@ v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
return handle_scope.Escape(result);
}
+void ModuleSystem::CallModuleMethodSafe(const std::string& module_name,
+ const std::string& method_name) {
+ v8::HandleScope handle_scope(GetIsolate());
+ v8::Local<v8::Value> no_args;
+ CallModuleMethodSafe(module_name, method_name, 0, &no_args);
+}
+
+void ModuleSystem::CallModuleMethodSafe(
+ const std::string& module_name,
+ const std::string& method_name,
+ std::vector<v8::Local<v8::Value>>* args) {
+ CallModuleMethodSafe(module_name, method_name, args->size(), args->data());
+}
+
+void ModuleSystem::CallModuleMethodSafe(const std::string& module_name,
+ const std::string& method_name,
+ int argc,
+ v8::Local<v8::Value> argv[]) {
+ TRACE_EVENT2("v8", "v8.callModuleMethodSafe", "module_name", module_name,
+ "method_name", method_name);
+
+ v8::HandleScope handle_scope(GetIsolate());
+ v8::Local<v8::Context> v8_context = context()->v8_context();
+ v8::Context::Scope context_scope(v8_context);
+
+ v8::Local<v8::Function> function =
+ GetModuleFunction(module_name, method_name);
+
+ {
+ v8::TryCatch try_catch(GetIsolate());
+ try_catch.SetCaptureMessage(true);
+ context_->SafeCallFunction(function, argc, argv);
+ if (try_catch.HasCaught())
+ HandleException(try_catch);
+ }
+}
+
void ModuleSystem::RegisterNativeHandler(
const std::string& name,
std::unique_ptr<NativeHandler> native_handler) {
@@ -781,4 +793,39 @@ void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) {
}
}
+v8::Local<v8::Function> ModuleSystem::GetModuleFunction(
+ const std::string& module_name,
+ const std::string& method_name) {
+ v8::Local<v8::String> v8_module_name;
+ v8::Local<v8::String> v8_method_name;
+ v8::Local<v8::Function> function;
+ if (!ToV8String(GetIsolate(), module_name.c_str(), &v8_module_name) ||
+ !ToV8String(GetIsolate(), method_name.c_str(), &v8_method_name)) {
+ return function;
+ }
+
+ v8::Local<v8::Value> module;
+ {
+ NativesEnabledScope natives_enabled(this);
+ module = RequireForJsInner(v8_module_name);
+ }
+
+ if (module.IsEmpty() || !module->IsObject()) {
+ Fatal(context_,
+ "Failed to get module " + module_name + " to call " + method_name);
+ return function;
+ }
+
+ v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(module);
+ v8::Local<v8::Value> value;
+ if (!GetProperty(context()->v8_context(), object, v8_method_name, &value) ||
+ !value->IsFunction()) {
+ Fatal(context_, module_name + "." + method_name + " is not a function");
+ return function;
+ }
+
+ function = v8::Local<v8::Function>::Cast(value);
+ return function;
+}
+
} // namespace extensions
« no previous file with comments | « extensions/renderer/module_system.h ('k') | extensions/renderer/utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698