blob: bcb229ed9dd01628ea1be6ff831298e17930b32a [file] [log] [blame]
[email protected]58e10452012-02-22 03:34:451// 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.
4
[email protected]3c6babd2012-08-28 03:17:295#include "chrome/renderer/extensions/module_system.h"
[email protected]58e10452012-02-22 03:34:456
7#include "base/bind.h"
[email protected]ad6aa8f92013-06-22 15:34:168#include "base/command_line.h"
[email protected]68e63ea12013-06-05 05:00:549#include "base/debug/trace_event.h"
[email protected]a714e462013-01-26 06:33:1010#include "base/stl_util.h"
[email protected]a19a16d82013-06-11 17:45:1211#include "base/strings/string_util.h"
12#include "base/strings/stringprintf.h"
[email protected]ad6aa8f92013-06-22 15:34:1613#include "chrome/common/chrome_switches.h"
[email protected]4f1633f2013-03-09 14:26:2414#include "chrome/common/extensions/extension_messages.h"
15#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]d2663612013-03-17 09:25:5616#include "chrome/renderer/extensions/console.h"
[email protected]295890bd2013-06-15 10:52:4517#include "chrome/renderer/extensions/safe_builtins.h"
[email protected]4f1633f2013-03-09 14:26:2418#include "content/public/renderer/render_view.h"
[email protected]2255a9332013-06-17 05:12:3119#include "third_party/WebKit/public/web/WebFrame.h"
20#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
[email protected]58e10452012-02-22 03:34:4521
[email protected]95ee77da2013-03-19 21:11:1122namespace extensions {
23
[email protected]58e10452012-02-22 03:34:4524namespace {
25
[email protected]561ddce2012-03-22 01:18:5526const char* kModuleSystem = "module_system";
27const char* kModuleName = "module_name";
28const char* kModuleField = "module_field";
[email protected]bad9a5f2012-04-13 19:16:5429const char* kModulesField = "modules";
[email protected]cc0457712012-03-21 03:56:3830
[email protected]ad6aa8f92013-06-22 15:34:1631// Prepends |extension_id| if it's non-empty to |message|.
32std::string PrependExtensionID(const std::string& extension_id,
33 const std::string& message) {
34 std::string with_extension_id;
35 if (!extension_id.empty()) {
36 with_extension_id += "(";
37 with_extension_id += extension_id;
38 with_extension_id += ") ";
39 }
40 with_extension_id += message;
41 return with_extension_id;
42}
43
44void Fatal(const std::string& extension_id, const std::string& message) {
45 // Only crash web pages in dev channel.
46 // Always crash extension processes, or when in single process mode (since
47 // typically it's used to debug renderer crashes).
48 bool is_fatal = false;
49 const CommandLine* command_line = CommandLine::ForCurrentProcess();
50 if (command_line->HasSwitch(switches::kExtensionProcess) ||
51 command_line->HasSwitch(switches::kSingleProcess)) {
52 is_fatal = true;
53 } else {
54 // <= dev means dev, canary, and trunk.
55 is_fatal = Feature::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV;
56 }
57 std::string with_extension_id = PrependExtensionID(extension_id, message);
58 if (is_fatal)
59 console::Fatal(v8::Context::GetCalling(), with_extension_id);
60 else
61 console::Error(v8::Context::GetCalling(), with_extension_id);
62}
63
64void Warn(const std::string& extension_id, const std::string& message) {
65 console::Warn(v8::Context::GetCalling(),
66 PrependExtensionID(extension_id, message));
67}
68
[email protected]68e63ea12013-06-05 05:00:5469// Default exception handler which logs the exception.
70class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler {
71 public:
[email protected]ad6aa8f92013-06-22 15:34:1672 explicit DefaultExceptionHandler(const std::string& extension_id)
73 : extension_id_(extension_id) {}
74
[email protected]68e63ea12013-06-05 05:00:5475 // Fatally dumps the debug info from |try_catch| to the console.
76 // Make sure this is never used for exceptions that originate in external
77 // code!
78 virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE {
79 v8::HandleScope handle_scope;
80 std::string stack_trace = "<stack trace unavailable>";
81 if (!try_catch.StackTrace().IsEmpty()) {
82 v8::String::Utf8Value stack_value(try_catch.StackTrace());
83 if (*stack_value)
84 stack_trace.assign(*stack_value, stack_value.length());
85 else
86 stack_trace = "<could not convert stack trace to string>";
87 }
[email protected]ad6aa8f92013-06-22 15:34:1688 Fatal(extension_id_,
89 CreateExceptionString(try_catch) + "{" + stack_trace + "}");
[email protected]68e63ea12013-06-05 05:00:5490 }
[email protected]ad6aa8f92013-06-22 15:34:1691
92 private:
93 std::string extension_id_;
[email protected]68e63ea12013-06-05 05:00:5494};
95
96} // namespace
97
98std::string ModuleSystem::ExceptionHandler::CreateExceptionString(
99 const v8::TryCatch& try_catch) {
[email protected]95ee77da2013-03-19 21:11:11100 v8::Handle<v8::Message> message(try_catch.Message());
101 if (message.IsEmpty()) {
102 return "try_catch has no message";
103 }
[email protected]58e10452012-02-22 03:34:45104
[email protected]95ee77da2013-03-19 21:11:11105 std::string resource_name = "<unknown resource>";
106 if (!message->GetScriptResourceName().IsEmpty()) {
107 v8::String::Utf8Value resource_name_v8(
108 message->GetScriptResourceName()->ToString());
109 resource_name.assign(*resource_name_v8, resource_name_v8.length());
110 }
111
112 std::string error_message = "<no error message>";
113 if (!message->Get().IsEmpty()) {
114 v8::String::Utf8Value error_message_v8(message->Get());
115 error_message.assign(*error_message_v8, error_message_v8.length());
116 }
117
118 return base::StringPrintf("%s:%d: %s",
119 resource_name.c_str(),
120 message->GetLineNumber(),
121 error_message.c_str());
122}
123
[email protected]9a598442013-06-04 16:39:12124ModuleSystem::ModuleSystem(ChromeV8Context* context,
[email protected]bad9a5f2012-04-13 19:16:54125 SourceMap* source_map)
[email protected]4f1633f2013-03-09 14:26:24126 : ObjectBackedNativeHandler(context),
[email protected]68e63ea12013-06-05 05:00:54127 context_(context),
[email protected]bad9a5f2012-04-13 19:16:54128 source_map_(source_map),
[email protected]68e63ea12013-06-05 05:00:54129 natives_enabled_(0),
[email protected]ad6aa8f92013-06-22 15:34:16130 exception_handler_(
131 new DefaultExceptionHandler(context->GetExtensionID())) {
[email protected]ecde1912012-03-16 06:25:31132 RouteFunction("require",
133 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
134 RouteFunction("requireNative",
[email protected]4f1633f2013-03-09 14:26:24135 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
[email protected]ecde1912012-03-16 06:25:31136
[email protected]9a598442013-06-04 16:39:12137 v8::Handle<v8::Object> global(context->v8_context()->Global());
[email protected]bad9a5f2012-04-13 19:16:54138 global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New());
139 global->SetHiddenValue(v8::String::New(kModuleSystem),
140 v8::External::New(this));
[email protected]58e10452012-02-22 03:34:45141}
142
143ModuleSystem::~ModuleSystem() {
[email protected]4f1633f2013-03-09 14:26:24144 Invalidate();
145}
146
147void ModuleSystem::Invalidate() {
148 if (!is_valid())
149 return;
[email protected]079532242013-03-12 06:01:10150
151 // Clear the module system properties from the global context. It's polite,
152 // and we use this as a signal in lazy handlers that we no longer exist.
153 {
154 v8::HandleScope scope;
[email protected]9a598442013-06-04 16:39:12155 v8::Handle<v8::Object> global = context()->v8_context()->Global();
[email protected]079532242013-03-12 06:01:10156 global->DeleteHiddenValue(v8::String::New(kModulesField));
157 global->DeleteHiddenValue(v8::String::New(kModuleSystem));
158 }
159
160 // Invalidate all of the successfully required handlers we own.
[email protected]4f1633f2013-03-09 14:26:24161 for (NativeHandlerMap::iterator it = native_handler_map_.begin();
162 it != native_handler_map_.end(); ++it) {
163 it->second->Invalidate();
164 }
[email protected]079532242013-03-12 06:01:10165
[email protected]4f1633f2013-03-09 14:26:24166 ObjectBackedNativeHandler::Invalidate();
[email protected]58e10452012-02-22 03:34:45167}
168
[email protected]b6aad81c2012-03-27 08:45:15169ModuleSystem::NativesEnabledScope::NativesEnabledScope(
170 ModuleSystem* module_system)
171 : module_system_(module_system) {
172 module_system_->natives_enabled_++;
173}
174
175ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
176 module_system_->natives_enabled_--;
177 CHECK_GE(module_system_->natives_enabled_, 0);
178}
179
[email protected]144114942012-12-04 07:23:23180void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
[email protected]68e63ea12013-06-05 05:00:54181 exception_handler_->HandleUncaughtException(try_catch);
[email protected]1c6189f2012-05-18 06:45:52182}
183
[email protected]4f1633f2013-03-09 14:26:24184v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
[email protected]58e10452012-02-22 03:34:45185 v8::HandleScope handle_scope;
[email protected]4f1633f2013-03-09 14:26:24186 return handle_scope.Close(
187 RequireForJsInner(v8::String::New(module_name.c_str())));
[email protected]ecde1912012-03-16 06:25:31188}
189
[email protected]d8c5fbb2013-06-14 11:35:25190void ModuleSystem::RequireForJs(
191 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]ecde1912012-03-16 06:25:31192 v8::Handle<v8::String> module_name = args[0]->ToString();
[email protected]d8c5fbb2013-06-14 11:35:25193 args.GetReturnValue().Set(RequireForJsInner(module_name));
[email protected]ecde1912012-03-16 06:25:31194}
195
196v8::Handle<v8::Value> ModuleSystem::RequireForJsInner(
197 v8::Handle<v8::String> module_name) {
198 v8::HandleScope handle_scope;
[email protected]9a598442013-06-04 16:39:12199 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11200
[email protected]9a598442013-06-04 16:39:12201 v8::Handle<v8::Object> global(context()->v8_context()->Global());
[email protected]079532242013-03-12 06:01:10202
203 // The module system might have been deleted. This can happen if a different
204 // context keeps a reference to us, but our frame is destroyed (e.g.
205 // background page keeps reference to chrome object in a closed popup).
206 v8::Handle<v8::Value> modules_value =
207 global->GetHiddenValue(v8::String::New(kModulesField));
[email protected]d2663612013-03-17 09:25:56208 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
[email protected]ad6aa8f92013-06-22 15:34:16209 Warn(context_->GetExtensionID(), "Extension view no longer exists");
[email protected]95ee77da2013-03-19 21:11:11210 return v8::Undefined();
[email protected]d2663612013-03-17 09:25:56211 }
[email protected]079532242013-03-12 06:01:10212
213 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
[email protected]ecde1912012-03-16 06:25:31214 v8::Handle<v8::Value> exports(modules->Get(module_name));
215 if (!exports->IsUndefined())
216 return handle_scope.Close(exports);
[email protected]7c95c1a82012-04-17 02:11:33217
[email protected]d2663612013-03-17 09:25:56218 std::string module_name_str = *v8::String::AsciiValue(module_name);
219 v8::Handle<v8::Value> source(GetSource(module_name_str));
[email protected]95ee77da2013-03-19 21:11:11220 if (source.IsEmpty() || source->IsUndefined()) {
[email protected]ad6aa8f92013-06-22 15:34:16221 Fatal(context_->GetExtensionID(),
222 "No source for require(" + module_name_str + ")");
[email protected]95ee77da2013-03-19 21:11:11223 return v8::Undefined();
224 }
[email protected]ecde1912012-03-16 06:25:31225 v8::Handle<v8::String> wrapped_source(WrapSource(
226 v8::Handle<v8::String>::Cast(source)));
[email protected]95ee77da2013-03-19 21:11:11227 // Modules are wrapped in (function(){...}) so they always return functions.
228 v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name);
229 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
[email protected]ad6aa8f92013-06-22 15:34:16230 Fatal(context_->GetExtensionID(),
231 "Bad source for require(" + module_name_str + ")");
[email protected]95ee77da2013-03-19 21:11:11232 return v8::Undefined();
233 }
234
235 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value);
[email protected]7c95c1a82012-04-17 02:11:33236
[email protected]ecde1912012-03-16 06:25:31237 exports = v8::Object::New();
238 v8::Handle<v8::Object> natives(NewInstance());
[email protected]31bbfd72013-06-22 02:35:54239 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues
240
[email protected]295890bd2013-06-15 10:52:45241 // These must match the argument order in WrapSource.
[email protected]ecde1912012-03-16 06:25:31242 v8::Handle<v8::Value> args[] = {
[email protected]295890bd2013-06-15 10:52:45243 // CommonJS.
[email protected]ecde1912012-03-16 06:25:31244 natives->Get(v8::String::NewSymbol("require")),
245 natives->Get(v8::String::NewSymbol("requireNative")),
246 exports,
[email protected]295890bd2013-06-15 10:52:45247 // Each safe builtin. Keep in order with the arguments in WrapSource.
248 context_->safe_builtins()->GetArray(),
249 context_->safe_builtins()->GetFunction(),
[email protected]53229732013-06-21 02:56:02250 context_->safe_builtins()->GetJSON(),
[email protected]295890bd2013-06-15 10:52:45251 context_->safe_builtins()->GetObjekt(),
[email protected]31bbfd72013-06-22 02:35:54252 context_->safe_builtins()->GetRegExp(),
253 context_->safe_builtins()->GetString(),
[email protected]58e10452012-02-22 03:34:45254 };
[email protected]11d032ba2012-04-09 20:39:51255 {
[email protected]452b36f2012-07-12 05:27:54256 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54257 try_catch.SetCaptureMessage(true);
[email protected]68e63ea12013-06-05 05:00:54258 context_->CallFunction(func, arraysize(args), args);
[email protected]452b36f2012-07-12 05:27:54259 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23260 HandleException(try_catch);
[email protected]452b36f2012-07-12 05:27:54261 return v8::Undefined();
262 }
[email protected]11d032ba2012-04-09 20:39:51263 }
[email protected]ecde1912012-03-16 06:25:31264 modules->Set(module_name, exports);
265 return handle_scope.Close(exports);
[email protected]58e10452012-02-22 03:34:45266}
267
[email protected]4f1633f2013-03-09 14:26:24268v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
269 const std::string& module_name,
270 const std::string& method_name) {
[email protected]68e63ea12013-06-05 05:00:54271 v8::HandleScope handle_scope;
272 v8::Handle<v8::Value> no_args;
273 return CallModuleMethod(module_name, method_name, 0, &no_args);
[email protected]a714e462013-01-26 06:33:10274}
275
276v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
277 const std::string& module_name,
278 const std::string& method_name,
279 std::vector<v8::Handle<v8::Value> >* args) {
[email protected]68e63ea12013-06-05 05:00:54280 return CallModuleMethod(
281 module_name, method_name, args->size(), vector_as_array(args));
282}
283
284v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
285 const std::string& module_name,
286 const std::string& method_name,
287 int argc,
288 v8::Handle<v8::Value> argv[]) {
289 TRACE_EVENT2("v8", "v8.callModuleMethod",
290 "module_name", module_name,
291 "method_name", method_name);
292
[email protected]2bcbf712012-09-05 06:58:49293 v8::HandleScope handle_scope;
[email protected]68e63ea12013-06-05 05:00:54294 v8::Context::Scope context_scope(context()->v8_context());
295
296 v8::Local<v8::Value> module;
297 {
298 NativesEnabledScope natives_enabled(this);
299 module = v8::Local<v8::Value>::New(
300 RequireForJsInner(v8::String::New(module_name.c_str())));
301 }
302
[email protected]95ee77da2013-03-19 21:11:11303 if (module.IsEmpty() || !module->IsObject()) {
[email protected]ad6aa8f92013-06-22 15:34:16304 Fatal(context_->GetExtensionID(),
305 "Failed to get module " + module_name + " to call " + method_name);
[email protected]95ee77da2013-03-19 21:11:11306 return handle_scope.Close(v8::Undefined());
307 }
308
[email protected]2bcbf712012-09-05 06:58:49309 v8::Local<v8::Value> value =
310 v8::Handle<v8::Object>::Cast(module)->Get(
311 v8::String::New(method_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11312 if (value.IsEmpty() || !value->IsFunction()) {
[email protected]ad6aa8f92013-06-22 15:34:16313 Fatal(context_->GetExtensionID(),
314 module_name + "." + method_name + " is not a function");
[email protected]95ee77da2013-03-19 21:11:11315 return handle_scope.Close(v8::Undefined());
316 }
317
[email protected]68e63ea12013-06-05 05:00:54318 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
[email protected]a714e462013-01-26 06:33:10319 v8::Local<v8::Value> result;
[email protected]2bcbf712012-09-05 06:58:49320 {
[email protected]2bcbf712012-09-05 06:58:49321 v8::TryCatch try_catch;
322 try_catch.SetCaptureMessage(true);
[email protected]68e63ea12013-06-05 05:00:54323 result = context_->CallFunction(func, argc, argv);
[email protected]2bcbf712012-09-05 06:58:49324 if (try_catch.HasCaught())
[email protected]144114942012-12-04 07:23:23325 HandleException(try_catch);
[email protected]2bcbf712012-09-05 06:58:49326 }
[email protected]a714e462013-01-26 06:33:10327 return handle_scope.Close(result);
[email protected]2bcbf712012-09-05 06:58:49328}
329
[email protected]58e10452012-02-22 03:34:45330void ModuleSystem::RegisterNativeHandler(const std::string& name,
331 scoped_ptr<NativeHandler> native_handler) {
332 native_handler_map_[name] =
333 linked_ptr<NativeHandler>(native_handler.release());
334}
335
[email protected]95ee77da2013-03-19 21:11:11336void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) {
[email protected]11844fa2012-05-10 00:35:59337 overridden_native_handlers_.insert(name);
338}
339
[email protected]ecde1912012-03-16 06:25:31340void ModuleSystem::RunString(const std::string& code, const std::string& name) {
[email protected]9fb990b2012-03-13 02:09:41341 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31342 RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str()));
[email protected]58e10452012-02-22 03:34:45343}
344
[email protected]cc0457712012-03-21 03:56:38345// static
[email protected]4e008e172013-06-13 20:15:48346void ModuleSystem::NativeLazyFieldGetter(
[email protected]4f1633f2013-03-09 14:26:24347 v8::Local<v8::String> property,
[email protected]4e008e172013-06-13 20:15:48348 const v8::PropertyCallbackInfo<v8::Value>& info) {
349 LazyFieldGetterInner(property,
350 info,
351 &ModuleSystem::RequireNativeFromString);
352}
353
354// static
355void ModuleSystem::LazyFieldGetter(
356 v8::Local<v8::String> property,
357 const v8::PropertyCallbackInfo<v8::Value>& info) {
358 LazyFieldGetterInner(property, info, &ModuleSystem::Require);
359}
360
361// static
362void ModuleSystem::LazyFieldGetterInner(
363 v8::Local<v8::String> property,
364 const v8::PropertyCallbackInfo<v8::Value>& info,
[email protected]4f1633f2013-03-09 14:26:24365 RequireFunction require_function) {
[email protected]561ddce2012-03-22 01:18:55366 CHECK(!info.Data().IsEmpty());
367 CHECK(info.Data()->IsObject());
[email protected]cc0457712012-03-21 03:56:38368 v8::HandleScope handle_scope;
[email protected]561ddce2012-03-22 01:18:55369 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
[email protected]9a598442013-06-04 16:39:12370 // This context should be the same as context()->v8_context().
[email protected]4f1633f2013-03-09 14:26:24371 v8::Handle<v8::Context> context = parameters->CreationContext();
372 v8::Handle<v8::Object> global(context->Global());
[email protected]561ddce2012-03-22 01:18:55373 v8::Handle<v8::Value> module_system_value =
[email protected]bad9a5f2012-04-13 19:16:54374 global->GetHiddenValue(v8::String::New(kModuleSystem));
[email protected]95ee77da2013-03-19 21:11:11375 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
[email protected]bad9a5f2012-04-13 19:16:54376 // ModuleSystem has been deleted.
[email protected]95ee77da2013-03-19 21:11:11377 // TODO(kalman): See comment in header file.
[email protected]ad6aa8f92013-06-22 15:34:16378 Warn("", "Module system has been deleted, does extension view exist?");
[email protected]4e008e172013-06-13 20:15:48379 return;
[email protected]bad9a5f2012-04-13 19:16:54380 }
[email protected]95ee77da2013-03-19 21:11:11381
[email protected]561ddce2012-03-22 01:18:55382 ModuleSystem* module_system = static_cast<ModuleSystem*>(
383 v8::Handle<v8::External>::Cast(module_system_value)->Value());
[email protected]cc0457712012-03-21 03:56:38384
[email protected]4f1633f2013-03-09 14:26:24385 std::string name = *v8::String::AsciiValue(
386 parameters->Get(v8::String::New(kModuleName))->ToString());
387
388 // Switch to our v8 context because we need functions created while running
389 // the require()d module to belong to our context, not the current one.
390 v8::Context::Scope context_scope(context);
391 NativesEnabledScope natives_enabled_scope(module_system);
392
393 v8::TryCatch try_catch;
[email protected]95ee77da2013-03-19 21:11:11394 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
[email protected]4f1633f2013-03-09 14:26:24395 if (try_catch.HasCaught()) {
396 module_system->HandleException(try_catch);
[email protected]4e008e172013-06-13 20:15:48397 return;
[email protected]95ee77da2013-03-19 21:11:11398 }
399 if (module_value.IsEmpty() || !module_value->IsObject()) {
400 // require_function will have already logged this, we don't need to.
[email protected]4e008e172013-06-13 20:15:48401 return;
[email protected]b6aad81c2012-03-27 08:45:15402 }
[email protected]4f1633f2013-03-09 14:26:24403
[email protected]95ee77da2013-03-19 21:11:11404 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
[email protected]561ddce2012-03-22 01:18:55405 v8::Handle<v8::String> field =
406 parameters->Get(v8::String::New(kModuleField))->ToString();
407
[email protected]95ee77da2013-03-19 21:11:11408 if (!module->Has(field)) {
409 std::string field_str = *v8::String::AsciiValue(field);
[email protected]ad6aa8f92013-06-22 15:34:16410 Fatal(module_system->context_->GetExtensionID(),
411 "Lazy require of " + name + "." + field_str + " did not " +
412 "set the " + field_str + " field");
[email protected]4e008e172013-06-13 20:15:48413 return;
[email protected]95ee77da2013-03-19 21:11:11414 }
[email protected]4f1633f2013-03-09 14:26:24415
416 v8::Local<v8::Value> new_field = module->Get(field);
[email protected]95ee77da2013-03-19 21:11:11417 if (try_catch.HasCaught()) {
418 module_system->HandleException(try_catch);
[email protected]4e008e172013-06-13 20:15:48419 return;
[email protected]95ee77da2013-03-19 21:11:11420 }
421
422 // Ok for it to be undefined, among other things it's how bindings signify
423 // that the extension doesn't have permission to use them.
424 CHECK(!new_field.IsEmpty());
425
[email protected]4f1633f2013-03-09 14:26:24426 // Delete the getter and set this field to |new_field| so the same object is
427 // returned every time a certain API is accessed.
[email protected]95ee77da2013-03-19 21:11:11428 v8::Handle<v8::Object> object = info.This();
429 object->Delete(property);
430 object->Set(property, new_field);
[email protected]4e008e172013-06-13 20:15:48431 info.GetReturnValue().Set(new_field);
[email protected]561ddce2012-03-22 01:18:55432}
433
434void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
435 const std::string& field,
436 const std::string& module_name,
437 const std::string& module_field) {
[email protected]4f1633f2013-03-09 14:26:24438 SetLazyField(object, field, module_name, module_field,
439 &ModuleSystem::LazyFieldGetter);
440}
441
442void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
443 const std::string& field,
444 const std::string& module_name,
445 const std::string& module_field,
[email protected]4e008e172013-06-13 20:15:48446 v8::AccessorGetterCallback getter) {
[email protected]561ddce2012-03-22 01:18:55447 v8::HandleScope handle_scope;
448 v8::Handle<v8::Object> parameters = v8::Object::New();
449 parameters->Set(v8::String::New(kModuleName),
450 v8::String::New(module_name.c_str()));
451 parameters->Set(v8::String::New(kModuleField),
452 v8::String::New(module_field.c_str()));
[email protected]561ddce2012-03-22 01:18:55453 object->SetAccessor(v8::String::New(field.c_str()),
[email protected]4f1633f2013-03-09 14:26:24454 getter,
[email protected]561ddce2012-03-22 01:18:55455 NULL,
456 parameters);
[email protected]cc0457712012-03-21 03:56:38457}
[email protected]ecde1912012-03-16 06:25:31458
[email protected]4f1633f2013-03-09 14:26:24459void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
460 const std::string& field,
461 const std::string& module_name,
462 const std::string& module_field) {
463 SetLazyField(object, field, module_name, module_field,
464 &ModuleSystem::NativeLazyFieldGetter);
465}
466
[email protected]ecde1912012-03-16 06:25:31467v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
468 v8::Handle<v8::String> name) {
[email protected]58e10452012-02-22 03:34:45469 v8::HandleScope handle_scope;
[email protected]68e63ea12013-06-05 05:00:54470 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11471
[email protected]11d032ba2012-04-09 20:39:51472 WebKit::WebScopedMicrotaskSuppression suppression;
[email protected]7c95c1a82012-04-17 02:11:33473 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54474 try_catch.SetCaptureMessage(true);
[email protected]7c95c1a82012-04-17 02:11:33475 v8::Handle<v8::Script> script(v8::Script::New(code, name));
476 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23477 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11478 return v8::Undefined();
[email protected]7c95c1a82012-04-17 02:11:33479 }
480
[email protected]95ee77da2013-03-19 21:11:11481 v8::Handle<v8::Value> result = script->Run();
482 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23483 HandleException(try_catch);
[email protected]95ee77da2013-03-19 21:11:11484 return v8::Undefined();
485 }
[email protected]7c95c1a82012-04-17 02:11:33486
487 return handle_scope.Close(result);
[email protected]9e03ce22012-03-13 08:50:05488}
489
[email protected]d2663612013-03-17 09:25:56490v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
[email protected]9e03ce22012-03-13 08:50:05491 v8::HandleScope handle_scope;
[email protected]ecde1912012-03-16 06:25:31492 if (!source_map_->Contains(module_name))
[email protected]58e10452012-02-22 03:34:45493 return v8::Undefined();
[email protected]ecde1912012-03-16 06:25:31494 return handle_scope.Close(source_map_->GetSource(module_name));
[email protected]58e10452012-02-22 03:34:45495}
496
[email protected]d8c5fbb2013-06-14 11:35:25497void ModuleSystem::RequireNative(
498 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]58e10452012-02-22 03:34:45499 CHECK_EQ(1, args.Length());
[email protected]4f1633f2013-03-09 14:26:24500 std::string native_name = *v8::String::AsciiValue(args[0]->ToString());
[email protected]d8c5fbb2013-06-14 11:35:25501 args.GetReturnValue().Set(RequireNativeFromString(native_name));
[email protected]4f1633f2013-03-09 14:26:24502}
503
504v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
505 const std::string& native_name) {
[email protected]95ee77da2013-03-19 21:11:11506 if (natives_enabled_ == 0) {
507 // HACK: if in test throw exception so that we can test the natives-disabled
508 // logic; however, under normal circumstances, this is programmer error so
509 // we could crash.
510 if (exception_handler_)
511 return v8::ThrowException(v8::String::New("Natives disabled"));
[email protected]ad6aa8f92013-06-22 15:34:16512 Fatal(context_->GetExtensionID(),
513 "Natives disabled for requireNative(" + native_name + ")");
[email protected]95ee77da2013-03-19 21:11:11514 return v8::Undefined();
515 }
516
[email protected]11844fa2012-05-10 00:35:59517 if (overridden_native_handlers_.count(native_name) > 0u)
[email protected]4f1633f2013-03-09 14:26:24518 return RequireForJsInner(v8::String::New(native_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11519
[email protected]58e10452012-02-22 03:34:45520 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
[email protected]95ee77da2013-03-19 21:11:11521 if (i == native_handler_map_.end()) {
[email protected]ad6aa8f92013-06-22 15:34:16522 Fatal(context_->GetExtensionID(),
523 "Couldn't find native for requireNative(" + native_name + ")");
[email protected]58e10452012-02-22 03:34:45524 return v8::Undefined();
[email protected]95ee77da2013-03-19 21:11:11525 }
[email protected]58e10452012-02-22 03:34:45526 return i->second->NewInstance();
527}
[email protected]ecde1912012-03-16 06:25:31528
529v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
530 v8::HandleScope handle_scope;
[email protected]295890bd2013-06-15 10:52:45531 // Keep in order with the arguments in RequireForJsInner.
[email protected]b0c739f2013-02-20 09:21:40532 v8::Handle<v8::String> left = v8::String::New(
[email protected]295890bd2013-06-15 10:52:45533 "(function(require, requireNative, exports,"
[email protected]31bbfd72013-06-22 02:35:54534 "$Array, $Function, $JSON, $Object, $RegExp, $String) {"
[email protected]295890bd2013-06-15 10:52:45535 "'use strict';");
[email protected]ecde1912012-03-16 06:25:31536 v8::Handle<v8::String> right = v8::String::New("\n})");
537 return handle_scope.Close(
538 v8::String::Concat(left, v8::String::Concat(source, right)));
539}
540
[email protected]4e008e172013-06-13 20:15:48541} // namespace extensions