Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / renderer / extensions / module_system.cc
blob85050a7d6eceeae9be91aad8413bb9023445d07b
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
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/module_system.h"
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "base/string_util.h"
10 #include "base/stringprintf.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/renderer/extensions/chrome_v8_context.h"
13 #include "chrome/renderer/extensions/console.h"
14 #include "content/public/renderer/render_view.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSuppression.h"
17 namespace extensions {
19 namespace {
21 const char* kModuleSystem = "module_system";
22 const char* kModuleName = "module_name";
23 const char* kModuleField = "module_field";
24 const char* kModulesField = "modules";
26 // Formats |try_catch| as a nice string.
27 std::string CreateExceptionString(const v8::TryCatch& try_catch) {
28 v8::Handle<v8::Message> message(try_catch.Message());
29 if (message.IsEmpty()) {
30 return "try_catch has no message";
33 std::string resource_name = "<unknown resource>";
34 if (!message->GetScriptResourceName().IsEmpty()) {
35 v8::String::Utf8Value resource_name_v8(
36 message->GetScriptResourceName()->ToString());
37 resource_name.assign(*resource_name_v8, resource_name_v8.length());
40 std::string error_message = "<no error message>";
41 if (!message->Get().IsEmpty()) {
42 v8::String::Utf8Value error_message_v8(message->Get());
43 error_message.assign(*error_message_v8, error_message_v8.length());
46 return base::StringPrintf("%s:%d: %s",
47 resource_name.c_str(),
48 message->GetLineNumber(),
49 error_message.c_str());
52 // Fatally dumps the debug info from |try_catch| to the console.
53 // Don't use this for logging exceptions that might originate in external code!
54 void DumpException(const v8::TryCatch& try_catch) {
55 v8::HandleScope handle_scope;
57 std::string stack_trace = "<stack trace unavailable>";
58 if (!try_catch.StackTrace().IsEmpty()) {
59 v8::String::Utf8Value stack_value(try_catch.StackTrace());
60 if (*stack_value)
61 stack_trace.assign(*stack_value, stack_value.length());
62 else
63 stack_trace = "<could not convert stack trace to string>";
66 console::Fatal(v8::Context::GetCalling(),
67 CreateExceptionString(try_catch) + "{" + stack_trace + "}");
70 } // namespace
72 ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context,
73 SourceMap* source_map)
74 : ObjectBackedNativeHandler(context),
75 source_map_(source_map),
76 natives_enabled_(0) {
77 RouteFunction("require",
78 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
79 RouteFunction("requireNative",
80 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
82 v8::Handle<v8::Object> global(context->Global());
83 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
84 global->SetHiddenValue(v8::String::New(kModuleSystem),
85 v8::External::New(this));
88 ModuleSystem::~ModuleSystem() {
89 Invalidate();
92 void ModuleSystem::Invalidate() {
93 if (!is_valid())
94 return;
96 // Clear the module system properties from the global context. It's polite,
97 // and we use this as a signal in lazy handlers that we no longer exist.
99 v8::HandleScope scope;
100 v8::Handle<v8::Object> global = v8_context()->Global();
101 global->DeleteHiddenValue(v8::String::New(kModulesField));
102 global->DeleteHiddenValue(v8::String::New(kModuleSystem));
105 // Invalidate all of the successfully required handlers we own.
106 for (NativeHandlerMap::iterator it = native_handler_map_.begin();
107 it != native_handler_map_.end(); ++it) {
108 it->second->Invalidate();
111 ObjectBackedNativeHandler::Invalidate();
114 ModuleSystem::NativesEnabledScope::NativesEnabledScope(
115 ModuleSystem* module_system)
116 : module_system_(module_system) {
117 module_system_->natives_enabled_++;
120 ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
121 module_system_->natives_enabled_--;
122 CHECK_GE(module_system_->natives_enabled_, 0);
125 void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
126 if (exception_handler_)
127 exception_handler_->HandleUncaughtException();
128 else
129 DumpException(try_catch);
132 v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
133 v8::HandleScope handle_scope;
134 return handle_scope.Close(
135 RequireForJsInner(v8::String::New(module_name.c_str())));
138 v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) {
139 v8::HandleScope handle_scope;
140 v8::Handle<v8::String> module_name = args[0]->ToString();
141 return handle_scope.Close(RequireForJsInner(module_name));
144 v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
145 v8::Handle<v8::String> module_name) {
146 v8::HandleScope handle_scope;
147 v8::Context::Scope context_scope(v8_context());
149 v8::Handle<v8::Object> global(v8_context()->Global());
151 // The module system might have been deleted. This can happen if a different
152 // context keeps a reference to us, but our frame is destroyed (e.g.
153 // background page keeps reference to chrome object in a closed popup).
154 v8::Handle<v8::Value> modules_value =
155 global->GetHiddenValue(v8::String::New(kModulesField));
156 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
157 console::Warn(v8::Context::GetCalling(), "Extension view no longer exists");
158 return v8::Undefined();
161 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
162 v8::Handle<v8::Value> exports(modules->Get(module_name));
163 if (!exports->IsUndefined())
164 return handle_scope.Close(exports);
166 std::string module_name_str = *v8::String::AsciiValue(module_name);
167 v8::Handle<v8::Value> source(GetSource(module_name_str));
168 if (source.IsEmpty() || source->IsUndefined()) {
169 console::Error(v8::Context::GetCalling(),
170 "No source for require(" + module_name_str + ")");
171 return v8::Undefined();
173 v8::Handle<v8::String> wrapped_source(WrapSource(
174 v8::Handle<v8::String>::Cast(source)));
175 // Modules are wrapped in (function(){...}) so they always return functions.
176 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name);
177 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
178 console::Error(v8::Context::GetCalling(),
179 "Bad source for require(" + module_name_str + ")");
180 return v8::Undefined();
183 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value);
185 exports = v8::Object::New();
186 v8::Handle<v8::Object> natives(NewInstance());
187 v8::Handle<v8::Value> args[] = {
188 natives->Get(v8::String::NewSymbol("require")),
189 natives->Get(v8::String::NewSymbol("requireNative")),
190 exports,
193 WebKit::WebScopedMicrotaskSuppression suppression;
194 v8::TryCatch try_catch;
195 try_catch.SetCaptureMessage(true);
196 func->Call(global, 3, args);
197 if (try_catch.HasCaught()) {
198 HandleException(try_catch);
199 return v8::Undefined();
202 modules->Set(module_name, exports);
203 return handle_scope.Close(exports);
206 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
207 const std::string& module_name,
208 const std::string& method_name) {
209 std::vector<v8::Handle<v8::Value> > args;
210 return CallModuleMethod(module_name, method_name, &args);
213 v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
214 const std::string& module_name,
215 const std::string& method_name,
216 std::vector<v8::Handle<v8::Value> >* args) {
217 v8::HandleScope handle_scope;
218 v8::Local<v8::Value> module =
219 v8::Local<v8::Value>::New(
220 RequireForJsInner(v8::String::New(module_name.c_str())));
221 if (module.IsEmpty() || !module->IsObject()) {
222 console::Error(
223 v8::Context::GetCalling(),
224 "Failed to get module " + module_name + " to call " + method_name);
225 return handle_scope.Close(v8::Undefined());
228 v8::Local<v8::Value> value =
229 v8::Handle<v8::Object>::Cast(module)->Get(
230 v8::String::New(method_name.c_str()));
231 if (value.IsEmpty() || !value->IsFunction()) {
232 console::Error(v8::Context::GetCalling(),
233 module_name + "." + method_name + " is not a function");
234 return handle_scope.Close(v8::Undefined());
237 v8::Handle<v8::Function> func =
238 v8::Handle<v8::Function>::Cast(value);
239 v8::Handle<v8::Object> global(v8_context()->Global());
240 v8::Local<v8::Value> result;
242 WebKit::WebScopedMicrotaskSuppression suppression;
243 v8::TryCatch try_catch;
244 try_catch.SetCaptureMessage(true);
245 result = func->Call(global, args->size(), vector_as_array(args));
246 if (try_catch.HasCaught())
247 HandleException(try_catch);
249 return handle_scope.Close(result);
252 void ModuleSystem::RegisterNativeHandler(const std::string& name,
253 scoped_ptr<NativeHandler> native_handler) {
254 native_handler_map_[name] =
255 linked_ptr<NativeHandler>(native_handler.release());
258 void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) {
259 overridden_native_handlers_.insert(name);
262 void ModuleSystem::RunString(const std::string& code, const std::string& name) {
263 v8::HandleScope handle_scope;
264 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
267 // static
268 v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter(
269 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
270 return LazyFieldGetterInner(property,
271 info,
272 &ModuleSystem::RequireNativeFromString);
275 // static
276 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter(
277 v8::Local<v8::String> property, const v8::AccessorInfo& info) {
278 return LazyFieldGetterInner(property, info, &ModuleSystem::Require);
281 // static
282 v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner(
283 v8::Local<v8::String> property,
284 const v8::AccessorInfo& info,
285 RequireFunction require_function) {
286 CHECK(!info.Data().IsEmpty());
287 CHECK(info.Data()->IsObject());
288 v8::HandleScope handle_scope;
289 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
290 // This context should be the same as v8_context().
291 v8::Handle<v8::Context> context = parameters->CreationContext();
292 v8::Handle<v8::Object> global(context->Global());
293 v8::Handle<v8::Value> module_system_value =
294 global->GetHiddenValue(v8::String::New(kModuleSystem));
295 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
296 // ModuleSystem has been deleted.
297 // TODO(kalman): See comment in header file.
298 console::Warn(v8::Context::GetCalling(),
299 "Module system has been deleted, does extension view exist?");
300 return v8::Undefined();
303 ModuleSystem* module_system = static_cast<ModuleSystem*>(
304 v8::Handle<v8::External>::Cast(module_system_value)->Value());
306 std::string name = *v8::String::AsciiValue(
307 parameters->Get(v8::String::New(kModuleName))->ToString());
309 // Switch to our v8 context because we need functions created while running
310 // the require()d module to belong to our context, not the current one.
311 v8::Context::Scope context_scope(context);
312 NativesEnabledScope natives_enabled_scope(module_system);
314 v8::TryCatch try_catch;
315 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
316 if (try_catch.HasCaught()) {
317 module_system->HandleException(try_catch);
318 return v8::Undefined();
320 if (module_value.IsEmpty() || !module_value->IsObject()) {
321 // require_function will have already logged this, we don't need to.
322 return v8::Undefined();
325 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
326 v8::Handle<v8::String> field =
327 parameters->Get(v8::String::New(kModuleField))->ToString();
329 if (!module->Has(field)) {
330 std::string field_str = *v8::String::AsciiValue(field);
331 console::Fatal(v8::Context::GetCalling(),
332 "Lazy require of " + name + "." + field_str + " did not " +
333 "set the " + field_str + " field");
334 return v8::Undefined();
337 v8::Local<v8::Value> new_field = module->Get(field);
338 if (try_catch.HasCaught()) {
339 module_system->HandleException(try_catch);
340 return v8::Undefined();
343 // Ok for it to be undefined, among other things it's how bindings signify
344 // that the extension doesn't have permission to use them.
345 CHECK(!new_field.IsEmpty());
347 // Delete the getter and set this field to |new_field| so the same object is
348 // returned every time a certain API is accessed.
349 v8::Handle<v8::Object> object = info.This();
350 object->Delete(property);
351 object->Set(property, new_field);
352 return handle_scope.Close(new_field);
355 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
356 const std::string& field,
357 const std::string& module_name,
358 const std::string& module_field) {
359 SetLazyField(object, field, module_name, module_field,
360 &ModuleSystem::LazyFieldGetter);
363 void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
364 const std::string& field,
365 const std::string& module_name,
366 const std::string& module_field,
367 v8::AccessorGetter getter) {
368 v8::HandleScope handle_scope;
369 v8::Handle<v8::Object> parameters = v8::Object::New();
370 parameters->Set(v8::String::New(kModuleName),
371 v8::String::New(module_name.c_str()));
372 parameters->Set(v8::String::New(kModuleField),
373 v8::String::New(module_field.c_str()));
374 object->SetAccessor(v8::String::New(field.c_str()),
375 getter,
376 NULL,
377 parameters);
380 void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
381 const std::string& field,
382 const std::string& module_name,
383 const std::string& module_field) {
384 SetLazyField(object, field, module_name, module_field,
385 &ModuleSystem::NativeLazyFieldGetter);
388 v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
389 v8::Handle<v8::String> name) {
390 v8::HandleScope handle_scope;
392 WebKit::WebScopedMicrotaskSuppression suppression;
393 v8::TryCatch try_catch;
394 try_catch.SetCaptureMessage(true);
395 v8::Handle<v8::Script> script(v8::Script::New(code, name));
396 if (try_catch.HasCaught()) {
397 HandleException(try_catch);
398 return v8::Undefined();
401 v8::Handle<v8::Value> result = script->Run();
402 if (try_catch.HasCaught()) {
403 HandleException(try_catch);
404 return v8::Undefined();
407 return handle_scope.Close(result);
410 v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
411 v8::HandleScope handle_scope;
412 if (!source_map_->Contains(module_name))
413 return v8::Undefined();
414 return handle_scope.Close(source_map_->GetSource(module_name));
417 v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) {
418 CHECK_EQ(1, args.Length());
419 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
420 return RequireNativeFromString(native_name);
423 v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
424 const std::string& native_name) {
425 if (natives_enabled_ == 0) {
426 // HACK: if in test throw exception so that we can test the natives-disabled
427 // logic; however, under normal circumstances, this is programmer error so
428 // we could crash.
429 if (exception_handler_)
430 return v8::ThrowException(v8::String::New("Natives disabled"));
431 console::Fatal(v8::Context::GetCalling(),
432 "Natives disabled for requireNative(" + native_name + ")");
433 return v8::Undefined();
436 if (overridden_native_handlers_.count(native_name) > 0u)
437 return RequireForJsInner(v8::String::New(native_name.c_str()));
439 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
440 if (i == native_handler_map_.end()) {
441 console::Fatal(
442 v8::Context::GetCalling(),
443 "Couldn't find native for requireNative(" + native_name + ")");
444 return v8::Undefined();
446 return i->second->NewInstance();
449 v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
450 v8::HandleScope handle_scope;
451 v8::Handle<v8::String> left = v8::String::New(
452 "(function(require, requireNative, exports) {'use strict';");
453 v8::Handle<v8::String> right = v8::String::New("\n})");
454 return handle_scope.Close(
455 v8::String::Concat(left, v8::String::Concat(source, right)));
458 } // extensions