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

Side by Side 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: fix compile errors Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/renderer/module_system.h" 5 #include "chrome/renderer/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/renderer/source_map.h"
8 #include "chrome/renderer/static_v8_external_string_resource.h" 9 #include "chrome/renderer/static_v8_external_string_resource.h"
9 #include "grit/renderer_resources.h" 10 #include "grit/renderer_resources.h"
10 #include "ui/base/resource/resource_bundle.h" 11 #include "ui/base/resource/resource_bundle.h"
11 12
12 namespace { 13 namespace {
13 14
14 v8::Handle<v8::String> GetResource(int resourceId) {
15 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
16 return v8::String::NewExternal(new StaticV8ExternalAsciiStringResource(
17 resource_bundle.GetRawDataResource(resourceId)));
18 }
19
20 } // namespace 15 } // namespace
21 16
22 ModuleSystem::ModuleSystem(const std::map<std::string, std::string>* source_map) 17 ModuleSystem::ModuleSystem(SourceMap* source_map)
23 : source_map_(source_map) { 18 : source_map_(source_map),
19 injection_enabled_(true) {
24 RouteFunction("Run", 20 RouteFunction("Run",
25 base::Bind(&ModuleSystem::Run, base::Unretained(this))); 21 base::Bind(&ModuleSystem::Run, base::Unretained(this)));
26 RouteFunction("GetSource", 22 RouteFunction("GetSource",
27 base::Bind(&ModuleSystem::GetSource, base::Unretained(this))); 23 base::Bind(&ModuleSystem::GetSource, base::Unretained(this)));
28 RouteFunction("GetNative", 24 RouteFunction("GetNative",
29 base::Bind(&ModuleSystem::GetNative, base::Unretained(this))); 25 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
30 } 26 }
31 27
32 ModuleSystem::~ModuleSystem() { 28 ModuleSystem::~ModuleSystem() {
33 } 29 }
34 30
35 void ModuleSystem::Require(const std::string& module_name) { 31 void ModuleSystem::Require(const std::string& module_name) {
36 EnsureRequireLoaded(); 32 EnsureRequireLoaded();
37 v8::HandleScope handle_scope; 33 v8::HandleScope handle_scope;
38 v8::Handle<v8::Value> argv[] = { 34 v8::Handle<v8::Value> argv[] = {
39 v8::String::New(module_name.c_str()), 35 v8::String::New(module_name.c_str()),
40 }; 36 };
41 require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv); 37 require_->Call(v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
42 } 38 }
43 39
44 void ModuleSystem::RegisterNativeHandler(const std::string& name, 40 void ModuleSystem::RegisterNativeHandler(const std::string& name,
45 scoped_ptr<NativeHandler> native_handler) { 41 scoped_ptr<NativeHandler> native_handler) {
46 native_handler_map_[name] = 42 native_handler_map_[name] =
47 linked_ptr<NativeHandler>(native_handler.release()); 43 linked_ptr<NativeHandler>(native_handler.release());
48 } 44 }
49 45
46 void ModuleSystem::DisableInjection() {
47 injection_enabled_ = false;
48 }
49
50 void ModuleSystem::EnsureRequireLoaded() { 50 void ModuleSystem::EnsureRequireLoaded() {
51 v8::HandleScope handle_scope; 51 v8::HandleScope handle_scope;
52 if (!require_.IsEmpty()) 52 if (!require_.IsEmpty())
53 return; 53 return;
54 v8::Handle<v8::Object> bootstrap = NewInstance(); 54 v8::Handle<v8::Object> bootstrap = NewInstance();
55 v8::Handle<v8::Value> result = RunString(GetResource(IDR_REQUIRE_JS)); 55 // NOTE v8 takes ownership of the StaticV8ExternalAsciiStringResource.
56 v8::Handle<v8::String> source = v8::String::NewExternal(
57 new StaticV8ExternalAsciiStringResource(GetResource(IDR_REQUIRE_JS)));
58 v8::Handle<v8::Value> result = RunString(source,
59 v8::String::New("require.js"));
56 v8::Handle<v8::Function> require_factory = 60 v8::Handle<v8::Function> require_factory =
57 v8::Handle<v8::Function>::Cast(result); 61 v8::Handle<v8::Function>::Cast(result);
58 CHECK(!require_factory.IsEmpty()) 62 CHECK(!require_factory.IsEmpty())
59 << "require.js should define a function that returns a require() " 63 << "require.js should define a function that returns a require() "
60 << "function"; 64 << "function";
61 v8::Handle<v8::Value> argv[] = { 65 v8::Handle<v8::Value> argv[] = {
62 bootstrap, 66 bootstrap,
63 }; 67 };
64 v8::Handle<v8::Value> require = require_factory->Call( 68 v8::Handle<v8::Value> require = require_factory->Call(
65 v8::Context::GetCurrent()->Global(), arraysize(argv), argv); 69 v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
66 require_ = v8::Persistent<v8::Function>::New( 70 require_ = v8::Persistent<v8::Function>::New(
67 v8::Handle<v8::Function>::Cast(require)); 71 v8::Handle<v8::Function>::Cast(require));
68 } 72 }
69 73
70 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code) { 74 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
75 v8::Handle<v8::String> name) {
71 v8::HandleScope handle_scope; 76 v8::HandleScope handle_scope;
72 return handle_scope.Close(v8::Script::New(code)->Run()); 77 return handle_scope.Close(v8::Script::New(code, name)->Run());
73 } 78 }
74 79
75 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) { 80 v8::Handle<v8::Value> ModuleSystem::Run(const v8::Arguments& args) {
76 CHECK_EQ(1, args.Length()); 81 CHECK_EQ(2, args.Length());
82 if (!injection_enabled_)
83 return ThrowException("Injection disabled");
77 v8::HandleScope handle_scope; 84 v8::HandleScope handle_scope;
78 return handle_scope.Close(v8::Script::New(args[0]->ToString())->Run()); 85 return handle_scope.Close(
86 RunString(args[0]->ToString(), args[1]->ToString()));
79 } 87 }
80 88
81 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) { 89 v8::Handle<v8::Value> ModuleSystem::GetSource(const v8::Arguments& args) {
82 CHECK_EQ(1, args.Length()); 90 CHECK_EQ(1, args.Length());
91 if (!injection_enabled_)
92 return ThrowException("Injection disabled");
93
94 v8::HandleScope handle_scope;
83 std::string module_name = *v8::String::AsciiValue(args[0]->ToString()); 95 std::string module_name = *v8::String::AsciiValue(args[0]->ToString());
84 std::map<std::string, std::string>::const_iterator p = 96 if (!source_map_->Contains(module_name))
85 source_map_->find(module_name);
86 if (p == source_map_->end())
87 return v8::Undefined(); 97 return v8::Undefined();
88 return v8::String::New(p->second.c_str()); 98 return handle_scope.Close(source_map_->GetSource(module_name));
89 } 99 }
90 100
91 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) { 101 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
92 CHECK_EQ(1, args.Length()); 102 CHECK_EQ(1, args.Length());
103 if (!injection_enabled_)
104 return ThrowException("Injection disabled");
93 std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); 105 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
94 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 106 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
95 if (i == native_handler_map_.end()) 107 if (i == native_handler_map_.end())
96 return v8::Undefined(); 108 return v8::Undefined();
97 return i->second->NewInstance(); 109 return i->second->NewInstance();
98 } 110 }
111
112 base::StringPiece ModuleSystem::GetResource(int resourceId) {
113 const ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance();
114 return resource_bundle.GetRawDataResource(resourceId);
115 }
116
117 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
118 return v8::ThrowException(v8::String::New(message.c_str()));
119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698