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

Side by Side Diff: chrome/renderer/extensions/module_system.cc

Issue 12313142: Revert 184837 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 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/extensions/module_system.h" 5 #include "chrome/renderer/extensions/module_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "base/stringprintf.h"
10 #include "chrome/common/extensions/extension_messages.h"
11 #include "chrome/renderer/extensions/chrome_v8_context.h"
12 #include "content/public/renderer/render_view.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h" 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSup pression.h"
14 10
15 namespace { 11 namespace {
16 12
17 const char* kModuleSystem = "module_system"; 13 const char* kModuleSystem = "module_system";
18 const char* kModuleName = "module_name"; 14 const char* kModuleName = "module_name";
19 const char* kModuleField = "module_field"; 15 const char* kModuleField = "module_field";
20 const char* kModulesField = "modules"; 16 const char* kModulesField = "modules";
21 17
22 } // namespace 18 } // namespace
23 19
24 namespace extensions { 20 namespace extensions {
25 21
26 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, 22 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context,
27 SourceMap* source_map) 23 SourceMap* source_map)
28 : ObjectBackedNativeHandler(context), 24 : NativeHandler(context->GetIsolate()),
25 context_(v8::Persistent<v8::Context>::New(context->GetIsolate(),
26 context)),
29 source_map_(source_map), 27 source_map_(source_map),
30 natives_enabled_(0), 28 natives_enabled_(0) {
31 is_valid_(true) {
32 RouteFunction("require", 29 RouteFunction("require",
33 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); 30 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
34 RouteFunction("requireNative", 31 RouteFunction("requireNative",
35 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); 32 base::Bind(&ModuleSystem::GetNative, base::Unretained(this)));
36 33
37 v8::Handle<v8::Object> global(context->Global()); 34 v8::Handle<v8::Object> global(context_->Global());
38 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); 35 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
39 global->SetHiddenValue(v8::String::New(kModuleSystem), 36 global->SetHiddenValue(v8::String::New(kModuleSystem),
40 v8::External::New(this)); 37 v8::External::New(this));
41 } 38 }
42 39
43 ModuleSystem::~ModuleSystem() { 40 ModuleSystem::~ModuleSystem() {
44 Invalidate();
45 }
46
47 bool ModuleSystem::Invalidate() {
48 if (!ObjectBackedNativeHandler::Invalidate())
49 return false;
50
51 v8::HandleScope handle_scope; 41 v8::HandleScope handle_scope;
52 // Deleting this value here prevents future lazy field accesses from 42 // Deleting this value here prevents future lazy field accesses from
53 // referencing ModuleSystem after it has been freed. 43 // referencing ModuleSystem after it has been freed.
54 v8_context()->Global()->DeleteHiddenValue(v8::String::New(kModuleSystem)); 44 context_->Global()->DeleteHiddenValue(v8::String::New(kModuleSystem));
55 45 context_.Dispose(context_->GetIsolate());
56 return true;
57 } 46 }
58 47
59 ModuleSystem::NativesEnabledScope::NativesEnabledScope( 48 ModuleSystem::NativesEnabledScope::NativesEnabledScope(
60 ModuleSystem* module_system) 49 ModuleSystem* module_system)
61 : module_system_(module_system) { 50 : module_system_(module_system) {
62 module_system_->natives_enabled_++; 51 module_system_->natives_enabled_++;
63 } 52 }
64 53
65 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { 54 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
66 module_system_->natives_enabled_--; 55 module_system_->natives_enabled_--;
67 CHECK_GE(module_system_->natives_enabled_, 0); 56 CHECK_GE(module_system_->natives_enabled_, 0);
68 } 57 }
69 58
70 // static 59 // static
71 bool ModuleSystem::IsPresentInCurrentContext() { 60 bool ModuleSystem::IsPresentInCurrentContext() {
72 // XXX(kalman): Not sure if this is safe. Investigate.
73 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); 61 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
74 if (global.IsEmpty()) 62 if (global.IsEmpty())
75 return false; 63 return false;
76 v8::Handle<v8::Value> module_system = 64 v8::Handle<v8::Value> module_system =
77 global->GetHiddenValue(v8::String::New(kModuleSystem)); 65 global->GetHiddenValue(v8::String::New(kModuleSystem));
78 return !module_system.IsEmpty() && !module_system->IsUndefined(); 66 return !module_system.IsEmpty() && !module_system->IsUndefined();
79 } 67 }
80 68
81 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { 69 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
82 DumpException(try_catch); 70 DumpException(try_catch);
83 if (exception_handler_.get()) 71 if (exception_handler_.get())
84 exception_handler_->HandleUncaughtException(); 72 exception_handler_->HandleUncaughtException();
85 } 73 }
86 74
87 // static 75 // static
88 std::string ModuleSystem::CreateExceptionString(const v8::TryCatch& try_catch) { 76 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) {
77 v8::HandleScope handle_scope;
78
89 v8::Handle<v8::Message> message(try_catch.Message()); 79 v8::Handle<v8::Message> message(try_catch.Message());
90 if (message.IsEmpty()) { 80 if (message.IsEmpty()) {
91 return "try_catch has no message"; 81 LOG(ERROR) << "try_catch has no message";
82 return;
92 } 83 }
93 84
94 std::string resource_name = "<unknown resource>"; 85 std::string resource_name = "<unknown resource>";
95 if (!message->GetScriptResourceName().IsEmpty()) { 86 if (!message->GetScriptResourceName().IsEmpty()) {
96 resource_name = 87 resource_name =
97 *v8::String::Utf8Value(message->GetScriptResourceName()->ToString()); 88 *v8::String::Utf8Value(message->GetScriptResourceName()->ToString());
98 } 89 }
99 90
100 std::string error_message = "<no error message>"; 91 std::string error_message = "<no error message>";
101 if (!message->Get().IsEmpty()) 92 if (!message->Get().IsEmpty())
102 error_message = *v8::String::Utf8Value(message->Get()); 93 error_message = *v8::String::Utf8Value(message->Get());
103 94
104 return base::StringPrintf("%s:%d: %s",
105 resource_name.c_str(),
106 message->GetLineNumber(),
107 error_message.c_str());
108 }
109
110 // static
111 void ModuleSystem::DumpException(const v8::TryCatch& try_catch) {
112 v8::HandleScope handle_scope;
113
114 std::string stack_trace = "<stack trace unavailable>"; 95 std::string stack_trace = "<stack trace unavailable>";
115 if (!try_catch.StackTrace().IsEmpty()) { 96 if (!try_catch.StackTrace().IsEmpty()) {
116 v8::String::Utf8Value stack_value(try_catch.StackTrace()); 97 v8::String::Utf8Value stack_value(try_catch.StackTrace());
117 if (*stack_value) 98 if (*stack_value)
118 stack_trace.assign(*stack_value, stack_value.length()); 99 stack_trace.assign(*stack_value, stack_value.length());
119 else 100 else
120 stack_trace = "<could not convert stack trace to string>"; 101 stack_trace = "<could not convert stack trace to string>";
121 } 102 }
122 103
123 LOG(ERROR) << CreateExceptionString(try_catch) << "{" << stack_trace << "}"; 104 LOG(ERROR) << "[" << resource_name << "(" << message->GetLineNumber() << ")] "
105 << error_message
106 << "{" << stack_trace << "}";
124 } 107 }
125 108
126 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { 109 void ModuleSystem::Require(const std::string& module_name) {
127 v8::HandleScope handle_scope; 110 v8::HandleScope handle_scope;
128 return handle_scope.Close( 111 RequireForJsInner(v8::String::New(module_name.c_str()));
129 RequireForJsInner(v8::String::New(module_name.c_str())));
130 } 112 }
131 113
132 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { 114 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
133 v8::HandleScope handle_scope; 115 v8::HandleScope handle_scope;
134 v8::Handle<v8::String> module_name = args[0]->ToString(); 116 v8::Handle<v8::String> module_name = args[0]->ToString();
135 return handle_scope.Close(RequireForJsInner(module_name)); 117 return handle_scope.Close(RequireForJsInner(module_name));
136 } 118 }
137 119
138 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( 120 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
139 v8::Handle<v8::String> module_name) { 121 v8::Handle<v8::String> module_name) {
140 CHECK(is_valid_);
141 v8::HandleScope handle_scope; 122 v8::HandleScope handle_scope;
142 v8::Handle<v8::Object> global(v8_context()->Global()); 123 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
143 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast( 124 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(
144 global->GetHiddenValue(v8::String::New(kModulesField)))); 125 global->GetHiddenValue(v8::String::New(kModulesField))));
145 v8::Handle<v8::Value> exports(modules->Get(module_name)); 126 v8::Handle<v8::Value> exports(modules->Get(module_name));
146 if (!exports->IsUndefined()) 127 if (!exports->IsUndefined())
147 return handle_scope.Close(exports); 128 return handle_scope.Close(exports);
148 129
149 v8::Handle<v8::Value> source(GetSource(module_name)); 130 v8::Handle<v8::Value> source(GetSource(module_name));
150 if (source->IsUndefined()) 131 if (source->IsUndefined())
151 return handle_scope.Close(v8::Undefined()); 132 return handle_scope.Close(v8::Undefined());
152 v8::Handle<v8::String> wrapped_source(WrapSource( 133 v8::Handle<v8::String> wrapped_source(WrapSource(
(...skipping 19 matching lines...) Expand all
172 func->Call(global, 3, args); 153 func->Call(global, 3, args);
173 if (try_catch.HasCaught()) { 154 if (try_catch.HasCaught()) {
174 HandleException(try_catch); 155 HandleException(try_catch);
175 return v8::Undefined(); 156 return v8::Undefined();
176 } 157 }
177 } 158 }
178 modules->Set(module_name, exports); 159 modules->Set(module_name, exports);
179 return handle_scope.Close(exports); 160 return handle_scope.Close(exports);
180 } 161 }
181 162
182 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 163 void ModuleSystem::CallModuleMethod(const std::string& module_name,
183 const std::string& module_name, 164 const std::string& method_name) {
184 const std::string& method_name) {
185 std::vector<v8::Handle<v8::Value> > args; 165 std::vector<v8::Handle<v8::Value> > args;
186 return CallModuleMethod(module_name, method_name, &args); 166 CallModuleMethod(module_name, method_name, &args);
187 } 167 }
188 168
189 v8::Local<v8::Value> ModuleSystem::CallModuleMethod( 169 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
190 const std::string& module_name, 170 const std::string& module_name,
191 const std::string& method_name, 171 const std::string& method_name,
192 std::vector<v8::Handle<v8::Value> >* args) { 172 std::vector<v8::Handle<v8::Value> >* args) {
193 v8::HandleScope handle_scope; 173 v8::HandleScope handle_scope;
194 v8::Local<v8::Value> module = 174 v8::Local<v8::Value> module =
195 v8::Local<v8::Value>::New( 175 v8::Local<v8::Value>::New(
196 RequireForJsInner(v8::String::New(module_name.c_str()))); 176 RequireForJsInner(v8::String::New(module_name.c_str())));
197 if (module.IsEmpty() || !module->IsObject()) 177 if (module.IsEmpty() || !module->IsObject())
198 return v8::Local<v8::Value>(); 178 return v8::Local<v8::Value>();
199 v8::Local<v8::Value> value = 179 v8::Local<v8::Value> value =
200 v8::Handle<v8::Object>::Cast(module)->Get( 180 v8::Handle<v8::Object>::Cast(module)->Get(
201 v8::String::New(method_name.c_str())); 181 v8::String::New(method_name.c_str()));
202 if (value.IsEmpty() || !value->IsFunction()) 182 if (value.IsEmpty() || !value->IsFunction())
203 return v8::Local<v8::Value>(); 183 return v8::Local<v8::Value>();
204 v8::Handle<v8::Function> func = 184 v8::Handle<v8::Function> func =
205 v8::Handle<v8::Function>::Cast(value); 185 v8::Handle<v8::Function>::Cast(value);
206 v8::Handle<v8::Object> global(v8_context()->Global()); 186 // TODO(jeremya/koz): refer to context_ here, not the current context.
187 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
207 v8::Local<v8::Value> result; 188 v8::Local<v8::Value> result;
208 { 189 {
209 WebKit::WebScopedMicrotaskSuppression suppression; 190 WebKit::WebScopedMicrotaskSuppression suppression;
210 v8::TryCatch try_catch; 191 v8::TryCatch try_catch;
211 try_catch.SetCaptureMessage(true); 192 try_catch.SetCaptureMessage(true);
212 result = func->Call(global, args->size(), vector_as_array(args)); 193 result = func->Call(global, args->size(), vector_as_array(args));
213 if (try_catch.HasCaught()) 194 if (try_catch.HasCaught())
214 HandleException(try_catch); 195 HandleException(try_catch);
215 } 196 }
216 return handle_scope.Close(result); 197 return handle_scope.Close(result);
217 } 198 }
218 199
219 bool ModuleSystem::HasNativeHandler(const std::string& name) {
220 return native_handler_map_.find(name) != native_handler_map_.end();
221 }
222
223 void ModuleSystem::RegisterNativeHandler(const std::string& name, 200 void ModuleSystem::RegisterNativeHandler(const std::string& name,
224 scoped_ptr<NativeHandler> native_handler) { 201 scoped_ptr<NativeHandler> native_handler) {
225 native_handler_map_[name] = 202 native_handler_map_[name] =
226 linked_ptr<NativeHandler>(native_handler.release()); 203 linked_ptr<NativeHandler>(native_handler.release());
227 } 204 }
228 205
229 void ModuleSystem::OverrideNativeHandler(const std::string& name) { 206 void ModuleSystem::OverrideNativeHandler(const std::string& name) {
230 overridden_native_handlers_.insert(name); 207 overridden_native_handlers_.insert(name);
231 } 208 }
232 209
233 void ModuleSystem::RunString(const std::string& code, const std::string& name) { 210 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
234 v8::HandleScope handle_scope; 211 v8::HandleScope handle_scope;
235 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); 212 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
236 } 213 }
237 214
238 // static 215 // static
239 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
240 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
241 return LazyFieldGetterInner(property,
242 info,
243 &ModuleSystem::RequireNativeFromString);
244 }
245
246 // static
247 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( 216 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
248 v8::Local<v8::String> property, const v8::AccessorInfo& info) { 217 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
249 return LazyFieldGetterInner(property, info, &ModuleSystem::Require);
250 }
251
252 // static
253 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner(
254 v8::Local<v8::String> property,
255 const v8::AccessorInfo& info,
256 GetModuleFunc get_module) {
257 CHECK(!info.Data().IsEmpty()); 218 CHECK(!info.Data().IsEmpty());
258 CHECK(info.Data()->IsObject()); 219 CHECK(info.Data()->IsObject());
259 v8::HandleScope handle_scope; 220 v8::HandleScope handle_scope;
260 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); 221 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
261 v8::Handle<v8::Object> global(parameters->CreationContext()->Global()); 222 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
262 v8::Handle<v8::Value> module_system_value = 223 v8::Handle<v8::Value> module_system_value =
263 global->GetHiddenValue(v8::String::New(kModuleSystem)); 224 global->GetHiddenValue(v8::String::New(kModuleSystem));
264 if (module_system_value->IsUndefined()) { 225 if (module_system_value->IsUndefined()) {
265 // ModuleSystem has been deleted. 226 // ModuleSystem has been deleted.
266 return v8::Undefined(); 227 return v8::Undefined();
267 } 228 }
268 ModuleSystem* module_system = static_cast<ModuleSystem*>( 229 ModuleSystem* module_system = static_cast<ModuleSystem*>(
269 v8::Handle<v8::External>::Cast(module_system_value)->Value()); 230 v8::Handle<v8::External>::Cast(module_system_value)->Value());
270 231
271 std::string name = *v8::String::AsciiValue( 232 v8::Handle<v8::Object> module;
272 parameters->Get(v8::String::New(kModuleName))->ToString()); 233 {
273 234 NativesEnabledScope scope(module_system);
274 // HACK(kalman): Switch to the context of the owner module system while 235 module = v8::Handle<v8::Object>::Cast(module_system->RequireForJsInner(
275 // lazily requiring modules. 236 parameters->Get(v8::String::New(kModuleName))->ToString()));
276 //
277 // It seems to be a common incorrect assumption throughout code that the
278 // current context is the owner context. This makes that assumption true for
279 // at least the period where the JavaScript is first evaluated, which is when
280 // things are most likely to go wrong.
281 v8::Context::Scope context_scope(parameters->CreationContext());
282
283 NativesEnabledScope natives_enabled_scope(module_system);
284
285 v8::TryCatch try_catch;
286 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(
287 (module_system->*get_module)(name));
288 if (try_catch.HasCaught()) {
289 module_system->HandleException(try_catch);
290 return handle_scope.Close(v8::Handle<v8::Value>());
291 } 237 }
292
293 if (module.IsEmpty()) 238 if (module.IsEmpty())
294 return handle_scope.Close(v8::Handle<v8::Value>()); 239 return handle_scope.Close(v8::Handle<v8::Value>());
295 240
296 v8::Handle<v8::String> field = 241 v8::Handle<v8::String> field =
297 parameters->Get(v8::String::New(kModuleField))->ToString(); 242 parameters->Get(v8::String::New(kModuleField))->ToString();
298 243
299 v8::Local<v8::Value> new_field = module->Get(field); 244 return handle_scope.Close(module->Get(field));
300 v8::Handle<v8::Object> object = info.This();
301 // Delete the getter and set this field to |new_field| so the same object is
302 // returned every time a certain API is accessed.
303 if (!new_field->IsUndefined()) {
304 object->Delete(property);
305 object->Set(property, new_field);
306 }
307 return handle_scope.Close(new_field);
308 } 245 }
309 246
310 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, 247 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
311 const std::string& field, 248 const std::string& field,
312 const std::string& module_name, 249 const std::string& module_name,
313 const std::string& module_field) { 250 const std::string& module_field) {
314 SetLazyField(object, field, module_name, module_field,
315 &ModuleSystem::LazyFieldGetter);
316 }
317
318 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
319 const std::string& field,
320 const std::string& module_name,
321 const std::string& module_field,
322 v8::AccessorGetter getter) {
323 v8::HandleScope handle_scope; 251 v8::HandleScope handle_scope;
324 v8::Handle<v8::Object> parameters = v8::Object::New(); 252 v8::Handle<v8::Object> parameters = v8::Object::New();
325 parameters->Set(v8::String::New(kModuleName), 253 parameters->Set(v8::String::New(kModuleName),
326 v8::String::New(module_name.c_str())); 254 v8::String::New(module_name.c_str()));
327 parameters->Set(v8::String::New(kModuleField), 255 parameters->Set(v8::String::New(kModuleField),
328 v8::String::New(module_field.c_str())); 256 v8::String::New(module_field.c_str()));
257
329 object->SetAccessor(v8::String::New(field.c_str()), 258 object->SetAccessor(v8::String::New(field.c_str()),
330 getter, 259 &ModuleSystem::LazyFieldGetter,
331 NULL, 260 NULL,
332 parameters); 261 parameters);
333 } 262 }
334 263
335 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
336 const std::string& field,
337 const std::string& module_name,
338 const std::string& module_field) {
339 SetLazyField(object, field, module_name, module_field,
340 &ModuleSystem::NativeLazyFieldGetter);
341 }
342
343 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, 264 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
344 v8::Handle<v8::String> name) { 265 v8::Handle<v8::String> name) {
345 v8::HandleScope handle_scope; 266 v8::HandleScope handle_scope;
346 WebKit::WebScopedMicrotaskSuppression suppression; 267 WebKit::WebScopedMicrotaskSuppression suppression;
347 v8::Handle<v8::Value> result; 268 v8::Handle<v8::Value> result;
348 v8::TryCatch try_catch; 269 v8::TryCatch try_catch;
349 try_catch.SetCaptureMessage(true); 270 try_catch.SetCaptureMessage(true);
350 v8::Handle<v8::Script> script(v8::Script::New(code, name)); 271 v8::Handle<v8::Script> script(v8::Script::New(code, name));
351 if (try_catch.HasCaught()) { 272 if (try_catch.HasCaught()) {
352 HandleException(try_catch); 273 HandleException(try_catch);
353 return handle_scope.Close(result); 274 return handle_scope.Close(result);
354 } 275 }
355 276
356 result = script->Run(); 277 result = script->Run();
357 if (try_catch.HasCaught()) 278 if (try_catch.HasCaught())
358 HandleException(try_catch); 279 HandleException(try_catch);
359 280
360 return handle_scope.Close(result); 281 return handle_scope.Close(result);
361 } 282 }
362 283
363 v8::Handle<v8::Value> ModuleSystem::GetSource( 284 v8::Handle<v8::Value> ModuleSystem::GetSource(
364 v8::Handle<v8::String> source_name) { 285 v8::Handle<v8::String> source_name) {
365 v8::HandleScope handle_scope; 286 v8::HandleScope handle_scope;
366 std::string module_name = *v8::String::AsciiValue(source_name); 287 std::string module_name = *v8::String::AsciiValue(source_name);
367 if (!source_map_->Contains(module_name)) 288 if (!source_map_->Contains(module_name))
368 return v8::Undefined(); 289 return v8::Undefined();
369 return handle_scope.Close(source_map_->GetSource(module_name)); 290 return handle_scope.Close(source_map_->GetSource(module_name));
370 } 291 }
371 292
372 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { 293 v8::Handle<v8::Value> ModuleSystem::GetNative(const v8::Arguments& args) {
373 CHECK_EQ(1, args.Length()); 294 CHECK_EQ(1, args.Length());
374 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
375 return RequireNativeFromString(native_name);
376 }
377
378 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
379 const std::string& native_name) {
380 if (natives_enabled_ == 0) 295 if (natives_enabled_ == 0)
381 return ThrowException("Natives disabled"); 296 return ThrowException("Natives disabled");
297 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
382 if (overridden_native_handlers_.count(native_name) > 0u) 298 if (overridden_native_handlers_.count(native_name) > 0u)
383 return RequireForJsInner(v8::String::New(native_name.c_str())); 299 return RequireForJs(args);
384 NativeHandlerMap::iterator i = native_handler_map_.find(native_name); 300 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
385 if (i == native_handler_map_.end()) 301 if (i == native_handler_map_.end())
386 return v8::Undefined(); 302 return v8::Undefined();
387 return i->second->NewInstance(); 303 return i->second->NewInstance();
388 } 304 }
389 305
390 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { 306 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
391 v8::HandleScope handle_scope; 307 v8::HandleScope handle_scope;
392 v8::Handle<v8::String> left = v8::String::New( 308 v8::Handle<v8::String> left = v8::String::New(
393 "(function(require, requireNative, exports) {'use strict';"); 309 "(function(require, requireNative, exports) {'use strict';");
394 v8::Handle<v8::String> right = v8::String::New("\n})"); 310 v8::Handle<v8::String> right = v8::String::New("\n})");
395 return handle_scope.Close( 311 return handle_scope.Close(
396 v8::String::Concat(left, v8::String::Concat(source, right))); 312 v8::String::Concat(left, v8::String::Concat(source, right)));
397 } 313 }
398 314
399 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) { 315 v8::Handle<v8::Value> ModuleSystem::ThrowException(const std::string& message) {
400 return v8::ThrowException(v8::String::New(message.c_str())); 316 return v8::ThrowException(v8::String::New(message.c_str()));
401 } 317 }
402 318
403 } // extensions 319 } // extensions
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/module_system.h ('k') | chrome/renderer/extensions/module_system_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698