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

Unified Diff: chrome/renderer/module_system.cc

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: base files missing? Created 8 years, 10 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/module_system.cc
diff --git a/chrome/renderer/module_system.cc b/chrome/renderer/module_system.cc
index 6c685f3b888badf51fe9aa789329176e9150b840..3fce324afaf47e5c26ce912d973e663bdf18a869 100644
--- a/chrome/renderer/module_system.cc
+++ b/chrome/renderer/module_system.cc
@@ -11,18 +11,11 @@
namespace {
-v8::Handle<v8::String> GetResource(int resourceId) {
- const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
- return v8::String::NewExternal(new StaticV8ExternalAsciiStringResource(
- resource_bundle.GetRawDataResource(resourceId)));
-}
-
} // namespace
-ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map)
- : source_map_(source_map) {
- RouteFunction("Run",
- base::Bind(&ModuleSystem::Run, base::Unretained(this)));
+ModuleSystem::ModuleSystem(SourceMap* source_map)
+ : source_map_(source_map),
+ natives_enabled_(true) {
RouteFunction("GetSource",
base::Bind(&ModuleSystem::GetSource, base::Unretained(this)));
RouteFunction("GetNative",
@@ -52,7 +45,11 @@ void ModuleSystem::EnsureRequireLoaded() {
if (!require_.IsEmpty())
return;
v8::Handle<v8::Object> bootstrap = NewInstance();
- v8::Handle<v8::Value> result = RunString(GetResource(IDR_REQUIRE_JS));
+ // NOTE v8 takes ownership of the StaticV8ExternalAsciiStringResource.
+ v8::Handle<v8::String> source = v8::String::NewExternal(
+ new StaticV8ExternalAsciiStringResource(GetResource(IDR_REQUIRE_JS)));
+ v8::Handle<v8::Value> result = RunString(source,
+ v8::String::New("require.js"));
v8::Handle<v8::Function> require_factory =
v8::Handle<v8::Function>::Cast(result);
CHECK(!require_factory.IsEmpty())
@@ -67,32 +64,38 @@ void ModuleSystem::EnsureRequireLoaded() {
v8::Handle<v8::Function>::Cast(require));
}
-v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) {
- v8::HandleScope handle_scope;
- return handle_scope.Close(v8::Script::New(code)->Run());
-}
-
-v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) {
- CHECK_EQ(1, args.Length());
+v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
+ v8::Handle<v8::String> name) {
v8::HandleScope handle_scope;
- return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run());
+ return handle_scope.Close(v8::Script::New(code, name)->Run());
}
v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) {
CHECK_EQ(1, args.Length());
+
+ v8::HandleScope handle_scope;
std::string module_name = *v8::String::AsciiValue(args[0]->ToString());
- std::map<std::string, std::string>::const_iterator p =
- source_map_->find(module_name);
- if (p == source_map_->end())
+ if (!source_map_->Contains(module_name))
return v8::Undefined();
- return v8::String::New(p->second.c_str());
+ return handle_scope.Close(source_map_->GetSource(module_name));
}
v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
CHECK_EQ(1, args.Length());
+ if (!natives_enabled_)
+ return ThrowException("Natives disabled");
std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
if (i == native_handler_map_.end())
return v8::Undefined();
return i->second->NewInstance();
}
+
+base::StringPiece ModuleSystem::GetResource(int resourceId) {
+ const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
+ return resource_bundle.GetRawDataResource(resourceId);
+}
+
+v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
+ return v8::ThrowException(v8::String::New(message.c_str()));
+}

Powered by Google App Engine
This is Rietveld 408576698