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

Unified Diff: chrome/renderer/extensions/module_system.cc

Issue 11571014: Lazy load chrome.* APIs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments/TODOs Created 7 years, 11 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
Index: chrome/renderer/extensions/module_system.cc
diff --git a/chrome/renderer/extensions/module_system.cc b/chrome/renderer/extensions/module_system.cc
index 69b2c78fddf1b215350b554030c2f7bd7149c871..d64cca76fffd1e184f205adf8a9eff5ca7dc881a 100644
--- a/chrome/renderer/extensions/module_system.cc
+++ b/chrome/renderer/extensions/module_system.cc
@@ -103,9 +103,10 @@ void ModuleSystem::DumpException(const v8::TryCatch& try_catch) {
<< "{" << stack_trace << "}";
}
-void ModuleSystem::Require(const std::string& module_name) {
+v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
v8::HandleScope handle_scope;
- RequireForJsInner(v8::String::New(module_name.c_str()));
+ return handle_scope.Close(
+ RequireForJsInner(v8::String::New(module_name.c_str())));
}
v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
@@ -168,20 +169,33 @@ void ModuleSystem::CallModuleMethod(const std::string& module_name,
v8::Local<v8::Value> value =
v8::Handle<v8::Object>::Cast(module)->Get(
v8::String::New(method_name.c_str()));
+ CallModuleMethod(value, 0, NULL);
+}
+
+v8::Handle<v8::Value> ModuleSystem::CallModuleMethod(
+ v8::Local<v8::Value> value,
+ int argc,
+ v8::Handle<v8::Value> argv[]) {
if (value.IsEmpty() || !value->IsFunction())
- return;
+ return v8::Handle<v8::Value>();
v8::Handle<v8::Function> func =
v8::Handle<v8::Function>::Cast(value);
// TODO(jeremya/koz): refer to context_ here, not the current context.
v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
+ v8::Handle<v8::Value> ret;
{
WebKit::WebScopedMicrotaskSuppression suppression;
v8::TryCatch try_catch;
try_catch.SetCaptureMessage(true);
- func->Call(global, 0, NULL);
+ ret = func->Call(global, argc, argv);
if (try_catch.HasCaught())
HandleException(try_catch);
}
+ return ret;
+}
+
+bool ModuleSystem::HasNativeHandler(const std::string& name) {
+ return native_handler_map_.find(name) != native_handler_map_.end();
}
void ModuleSystem::RegisterNativeHandler(const std::string& name,
@@ -199,9 +213,42 @@ void ModuleSystem::RunString(const std::string& code, const std::string& name) {
RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
}
+v8::Handle<v8::Object> ModuleSystem::GetModule(
+ v8::Handle<v8::Object> parameters) {
+ NativesEnabledScope scope(this);
+ v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
+ RequireForJsInner(
+ parameters->Get(v8::String::New(kModuleName))->ToString()));
+ return module;
+}
+
+v8::Handle<v8::Object> ModuleSystem::GetNativeModule(
+ v8::Handle<v8::Object> parameters) {
+ NativesEnabledScope scope(this);
+ std::string name = *v8::String::AsciiValue(
+ parameters->Get(v8::String::New(kModuleName))->ToString());
+ v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
+ GetNativeFromString(name));
+ return module;
+}
+
+// static
+v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
+ v8::Local<v8::String> property, const v8::AccessorInfo& info) {
+ return BaseLazyFieldGetter(property, info, &ModuleSystem::GetNativeModule);
+}
+
// static
v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
v8::Local<v8::String> property, const v8::AccessorInfo& info) {
+ return BaseLazyFieldGetter(property, info, &ModuleSystem::GetModule);
+}
+
+// static
+v8::Handle<v8::Value> ModuleSystem::BaseLazyFieldGetter(
+ v8::Local<v8::String> property,
+ const v8::AccessorInfo& info,
+ GetModuleFunc get_module) {
CHECK(!info.Data().IsEmpty());
CHECK(info.Data()->IsObject());
v8::HandleScope handle_scope;
@@ -216,12 +263,7 @@ v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
ModuleSystem* module_system = static_cast<ModuleSystem*>(
v8::Handle<v8::External>::Cast(module_system_value)->Value());
- v8::Handle<v8::Object> module;
- {
- NativesEnabledScope scope(module_system);
- module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner(
- parameters->Get(v8::String::New(kModuleName))->ToString()));
- }
+ v8::Handle<v8::Object> module = (module_system->*get_module)(parameters);
if (module.IsEmpty())
return handle_scope.Close(v8::Handle<v8::Value>());
@@ -235,6 +277,15 @@ void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
const std::string& field,
const std::string& module_name,
const std::string& module_field) {
+ SetLazyField(object, field, module_name, module_field,
+ &ModuleSystem::LazyFieldGetter);
+}
+
+void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
+ const std::string& field,
+ const std::string& module_name,
+ const std::string& module_field,
+ v8::AccessorGetter getter) {
v8::HandleScope handle_scope;
v8::Handle<v8::Object> parameters = v8::Object::New();
parameters->Set(v8::String::New(kModuleName),
@@ -243,11 +294,19 @@ void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
v8::String::New(module_field.c_str()));
object->SetAccessor(v8::String::New(field.c_str()),
- &ModuleSystem::LazyFieldGetter,
+ getter,
NULL,
parameters);
}
+void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
+ const std::string& field,
+ const std::string& module_name,
+ const std::string& module_field) {
+ SetLazyField(object, field, module_name, module_field,
+ &ModuleSystem::NativeLazyFieldGetter);
+}
+
v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
v8::Handle<v8::String> name) {
v8::HandleScope handle_scope;
@@ -279,11 +338,16 @@ v8::Handle<v8::Value> ModuleSystem::GetSource(
v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
CHECK_EQ(1, args.Length());
+ std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
+ return GetNativeFromString(native_name);
+}
+
+v8::Handle<v8::Value> ModuleSystem::GetNativeFromString(
+ const std::string& native_name) {
if (natives_enabled_ == 0)
return ThrowException("Natives disabled");
- std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
if (overridden_native_handlers_.count(native_name) > 0u)
- return RequireForJs(args);
+ return RequireForJsInner(v8::String::New(native_name.c_str()));
NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
if (i == native_handler_map_.end())
return v8::Undefined();

Powered by Google App Engine
This is Rietveld 408576698