blob: 6a120749f4107499b322fc42f998c9f83528a703 [file] [log] [blame]
[email protected]f55c90ee62014-04-12 00:50:031// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]58e10452012-02-22 03:34:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f55c90ee62014-04-12 00:50:035#include "extensions/renderer/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"
kalmanb0c1c502015-04-15 00:25:069#include "base/logging.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"
primiano7182d7b2015-01-30 18:02:0313#include "base/trace_event/trace_event.h"
sammcfb8875c2014-10-28 11:51:0414#include "content/public/renderer/render_frame.h"
[email protected]4f1633f2013-03-09 14:26:2415#include "content/public/renderer/render_view.h"
thestigb012bc3d2014-09-18 22:57:1316#include "extensions/common/extension.h"
[email protected]f55c90ee62014-04-12 00:50:0317#include "extensions/common/extensions_client.h"
18#include "extensions/renderer/console.h"
19#include "extensions/renderer/safe_builtins.h"
20#include "extensions/renderer/script_context.h"
[email protected]d9f51dad2014-07-09 05:39:3821#include "gin/modules/module_registry.h"
[email protected]2255a9332013-06-17 05:12:3122#include "third_party/WebKit/public/web/WebFrame.h"
23#include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h"
[email protected]58e10452012-02-22 03:34:4524
[email protected]95ee77da2013-03-19 21:11:1125namespace extensions {
26
[email protected]58e10452012-02-22 03:34:4527namespace {
28
[email protected]561ddce2012-03-22 01:18:5529const char* kModuleSystem = "module_system";
30const char* kModuleName = "module_name";
31const char* kModuleField = "module_field";
[email protected]bad9a5f2012-04-13 19:16:5432const char* kModulesField = "modules";
[email protected]cc0457712012-03-21 03:56:3833
[email protected]662c48b2013-07-12 03:50:5234// Logs a fatal error for the calling context, with some added metadata about
35// the context:
36// - Its type (blessed, unblessed, etc).
37// - Whether it's valid.
38// - The extension ID, if one exists.
39//
[email protected]d84ecbf2013-07-30 22:48:1640// This will only actual be fatal in in dev/canary, since in too many cases
41// we're at the mercy of the extension or web page's environment. They can mess
42// up our JS in unexpected ways. Hopefully dev/canary channel will pick up such
43// problems, but given the wider variety on stable/beta it's impossible to know.
[email protected]f55c90ee62014-04-12 00:50:0344void Fatal(ScriptContext* context, const std::string& message) {
[email protected]662c48b2013-07-12 03:50:5245 // Prepend some context metadata.
46 std::string full_message = "(";
47 if (!context->is_valid())
48 full_message += "Invalid ";
49 full_message += context->GetContextTypeDescription();
50 full_message += " context";
51 if (context->extension()) {
52 full_message += " for ";
53 full_message += context->extension()->id();
54 }
55 full_message += ") ";
56 full_message += message;
57
[email protected]f55c90ee62014-04-12 00:50:0358 if (ExtensionsClient::Get()->ShouldSuppressFatalErrors())
[email protected]95c6b3012013-12-02 14:30:3159 console::Error(context->isolate()->GetCallingContext(), full_message);
[email protected]f55c90ee62014-04-12 00:50:0360 else
61 console::Fatal(context->isolate()->GetCallingContext(), full_message);
[email protected]ad6aa8f92013-06-22 15:34:1662}
63
[email protected]95c6b3012013-12-02 14:30:3164void Warn(v8::Isolate* isolate, const std::string& message) {
65 console::Warn(isolate->GetCallingContext(), message);
[email protected]ad6aa8f92013-06-22 15:34:1666}
67
[email protected]68e63ea12013-06-05 05:00:5468// Default exception handler which logs the exception.
69class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler {
70 public:
[email protected]f55c90ee62014-04-12 00:50:0371 explicit DefaultExceptionHandler(ScriptContext* context)
[email protected]662c48b2013-07-12 03:50:5272 : context_(context) {}
[email protected]ad6aa8f92013-06-22 15:34:1673
[email protected]68e63ea12013-06-05 05:00:5474 // Fatally dumps the debug info from |try_catch| to the console.
75 // Make sure this is never used for exceptions that originate in external
76 // code!
dcheng9168b2f2014-10-21 12:38:2477 void HandleUncaughtException(const v8::TryCatch& try_catch) override {
[email protected]dc3d06e2013-09-06 12:21:0378 v8::HandleScope handle_scope(context_->isolate());
[email protected]68e63ea12013-06-05 05:00:5479 std::string stack_trace = "<stack trace unavailable>";
80 if (!try_catch.StackTrace().IsEmpty()) {
81 v8::String::Utf8Value stack_value(try_catch.StackTrace());
82 if (*stack_value)
83 stack_trace.assign(*stack_value, stack_value.length());
84 else
85 stack_trace = "<could not convert stack trace to string>";
86 }
[email protected]662c48b2013-07-12 03:50:5287 Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}");
[email protected]68e63ea12013-06-05 05:00:5488 }
[email protected]ad6aa8f92013-06-22 15:34:1689
90 private:
[email protected]f55c90ee62014-04-12 00:50:0391 ScriptContext* context_;
[email protected]68e63ea12013-06-05 05:00:5492};
93
[email protected]f55c90ee62014-04-12 00:50:0394} // namespace
[email protected]68e63ea12013-06-05 05:00:5495
96std::string ModuleSystem::ExceptionHandler::CreateExceptionString(
97 const v8::TryCatch& try_catch) {
[email protected]95ee77da2013-03-19 21:11:1198 v8::Handle<v8::Message> message(try_catch.Message());
99 if (message.IsEmpty()) {
100 return "try_catch has no message";
101 }
[email protected]58e10452012-02-22 03:34:45102
[email protected]95ee77da2013-03-19 21:11:11103 std::string resource_name = "<unknown resource>";
[email protected]bce20ba2014-06-27 18:18:45104 if (!message->GetScriptOrigin().ResourceName().IsEmpty()) {
[email protected]95ee77da2013-03-19 21:11:11105 v8::String::Utf8Value resource_name_v8(
dcarneya261b772014-11-20 17:55:07106 message->GetScriptOrigin().ResourceName());
[email protected]95ee77da2013-03-19 21:11:11107 resource_name.assign(*resource_name_v8, resource_name_v8.length());
108 }
109
110 std::string error_message = "<no error message>";
111 if (!message->Get().IsEmpty()) {
112 v8::String::Utf8Value error_message_v8(message->Get());
113 error_message.assign(*error_message_v8, error_message_v8.length());
114 }
115
116 return base::StringPrintf("%s:%d: %s",
117 resource_name.c_str(),
118 message->GetLineNumber(),
119 error_message.c_str());
120}
121
[email protected]f55c90ee62014-04-12 00:50:03122ModuleSystem::ModuleSystem(ScriptContext* context, SourceMap* source_map)
[email protected]4f1633f2013-03-09 14:26:24123 : ObjectBackedNativeHandler(context),
[email protected]68e63ea12013-06-05 05:00:54124 context_(context),
[email protected]bad9a5f2012-04-13 19:16:54125 source_map_(source_map),
[email protected]68e63ea12013-06-05 05:00:54126 natives_enabled_(0),
[email protected]d9f51dad2014-07-09 05:39:38127 exception_handler_(new DefaultExceptionHandler(context)),
128 weak_factory_(this) {
[email protected]f55c90ee62014-04-12 00:50:03129 RouteFunction(
130 "require",
[email protected]ecde1912012-03-16 06:25:31131 base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this)));
[email protected]f55c90ee62014-04-12 00:50:03132 RouteFunction(
133 "requireNative",
[email protected]4f1633f2013-03-09 14:26:24134 base::Bind(&ModuleSystem::RequireNative, base::Unretained(this)));
[email protected]d9f51dad2014-07-09 05:39:38135 RouteFunction(
136 "requireAsync",
137 base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this)));
[email protected]ca557292013-12-11 08:44:27138 RouteFunction("privates",
[email protected]f55c90ee62014-04-12 00:50:03139 base::Bind(&ModuleSystem::Private, base::Unretained(this)));
[email protected]ecde1912012-03-16 06:25:31140
[email protected]9a598442013-06-04 16:39:12141 v8::Handle<v8::Object> global(context->v8_context()->Global());
[email protected]505ffde2013-12-19 15:47:13142 v8::Isolate* isolate = context->isolate();
[email protected]f55c90ee62014-04-12 00:50:03143 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField),
144 v8::Object::New(isolate));
145 global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem),
146 v8::External::New(isolate, this));
[email protected]d9f51dad2014-07-09 05:39:38147
148 gin::ModuleRegistry::From(context->v8_context())->AddObserver(this);
sammcfb8875c2014-10-28 11:51:04149 if (context_->GetRenderFrame()) {
150 context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable(
151 context->isolate(), context->v8_context());
152 }
[email protected]58e10452012-02-22 03:34:45153}
154
kalmanb0c1c502015-04-15 00:25:06155ModuleSystem::~ModuleSystem() {
156}
[email protected]4f1633f2013-03-09 14:26:24157
158void ModuleSystem::Invalidate() {
[email protected]079532242013-03-12 06:01:10159 // Clear the module system properties from the global context. It's polite,
160 // and we use this as a signal in lazy handlers that we no longer exist.
161 {
[email protected]dc3d06e2013-09-06 12:21:03162 v8::HandleScope scope(GetIsolate());
[email protected]9a598442013-06-04 16:39:12163 v8::Handle<v8::Object> global = context()->v8_context()->Global();
[email protected]9c47471e2013-11-28 14:41:21164 global->DeleteHiddenValue(
165 v8::String::NewFromUtf8(GetIsolate(), kModulesField));
166 global->DeleteHiddenValue(
167 v8::String::NewFromUtf8(GetIsolate(), kModuleSystem));
[email protected]079532242013-03-12 06:01:10168 }
169
kalmanb0c1c502015-04-15 00:25:06170 // Invalidate all active and clobbered NativeHandlers we own.
171 for (const auto& handler : native_handler_map_)
172 handler.second->Invalidate();
173 for (const auto& clobbered_handler : clobbered_native_handlers_)
174 clobbered_handler->Invalidate();
[email protected]079532242013-03-12 06:01:10175
[email protected]4f1633f2013-03-09 14:26:24176 ObjectBackedNativeHandler::Invalidate();
[email protected]58e10452012-02-22 03:34:45177}
178
[email protected]b6aad81c2012-03-27 08:45:15179ModuleSystem::NativesEnabledScope::NativesEnabledScope(
180 ModuleSystem* module_system)
181 : module_system_(module_system) {
182 module_system_->natives_enabled_++;
183}
184
185ModuleSystem::NativesEnabledScope::~NativesEnabledScope() {
186 module_system_->natives_enabled_--;
187 CHECK_GE(module_system_->natives_enabled_, 0);
188}
189
[email protected]144114942012-12-04 07:23:23190void ModuleSystem::HandleException(const v8::TryCatch& try_catch) {
[email protected]68e63ea12013-06-05 05:00:54191 exception_handler_->HandleUncaughtException(try_catch);
[email protected]1c6189f2012-05-18 06:45:52192}
193
[email protected]4f1633f2013-03-09 14:26:24194v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) {
[email protected]95c6b3012013-12-02 14:30:31195 v8::EscapableHandleScope handle_scope(GetIsolate());
196 return handle_scope.Escape(RequireForJsInner(
[email protected]9c47471e2013-11-28 14:41:21197 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())));
[email protected]ecde1912012-03-16 06:25:31198}
199
[email protected]d8c5fbb2013-06-14 11:35:25200void ModuleSystem::RequireForJs(
[email protected]f55c90ee62014-04-12 00:50:03201 const v8::FunctionCallbackInfo<v8::Value>& args) {
dcarneya261b772014-11-20 17:55:07202 v8::Handle<v8::String> module_name = args[0]->ToString(args.GetIsolate());
[email protected]d8c5fbb2013-06-14 11:35:25203 args.GetReturnValue().Set(RequireForJsInner(module_name));
[email protected]ecde1912012-03-16 06:25:31204}
205
[email protected]95c6b3012013-12-02 14:30:31206v8::Local<v8::Value> ModuleSystem::RequireForJsInner(
[email protected]ecde1912012-03-16 06:25:31207 v8::Handle<v8::String> module_name) {
[email protected]95c6b3012013-12-02 14:30:31208 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]9a598442013-06-04 16:39:12209 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11210
[email protected]9a598442013-06-04 16:39:12211 v8::Handle<v8::Object> global(context()->v8_context()->Global());
[email protected]079532242013-03-12 06:01:10212
213 // The module system might have been deleted. This can happen if a different
214 // context keeps a reference to us, but our frame is destroyed (e.g.
215 // background page keeps reference to chrome object in a closed popup).
[email protected]9c47471e2013-11-28 14:41:21216 v8::Handle<v8::Value> modules_value = global->GetHiddenValue(
217 v8::String::NewFromUtf8(GetIsolate(), kModulesField));
[email protected]d2663612013-03-17 09:25:56218 if (modules_value.IsEmpty() || modules_value->IsUndefined()) {
[email protected]95c6b3012013-12-02 14:30:31219 Warn(GetIsolate(), "Extension view no longer exists");
220 return v8::Undefined(GetIsolate());
[email protected]d2663612013-03-17 09:25:56221 }
[email protected]079532242013-03-12 06:01:10222
223 v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value));
[email protected]95c6b3012013-12-02 14:30:31224 v8::Local<v8::Value> exports(modules->Get(module_name));
[email protected]ecde1912012-03-16 06:25:31225 if (!exports->IsUndefined())
[email protected]95c6b3012013-12-02 14:30:31226 return handle_scope.Escape(exports);
[email protected]7c95c1a82012-04-17 02:11:33227
[email protected]d9f51dad2014-07-09 05:39:38228 exports = LoadModule(*v8::String::Utf8Value(module_name));
[email protected]ecde1912012-03-16 06:25:31229 modules->Set(module_name, exports);
[email protected]95c6b3012013-12-02 14:30:31230 return handle_scope.Escape(exports);
[email protected]58e10452012-02-22 03:34:45231}
232
[email protected]4f1633f2013-03-09 14:26:24233v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
234 const std::string& module_name,
235 const std::string& method_name) {
[email protected]f80685c32014-07-26 19:48:04236 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]68e63ea12013-06-05 05:00:54237 v8::Handle<v8::Value> no_args;
[email protected]f80685c32014-07-26 19:48:04238 return handle_scope.Escape(
239 CallModuleMethod(module_name, method_name, 0, &no_args));
[email protected]a714e462013-01-26 06:33:10240}
241
242v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
243 const std::string& module_name,
244 const std::string& method_name,
245 std::vector<v8::Handle<v8::Value> >* args) {
[email protected]68e63ea12013-06-05 05:00:54246 return CallModuleMethod(
247 module_name, method_name, args->size(), vector_as_array(args));
248}
249
250v8::Local<v8::Value> ModuleSystem::CallModuleMethod(
251 const std::string& module_name,
252 const std::string& method_name,
253 int argc,
254 v8::Handle<v8::Value> argv[]) {
[email protected]f55c90ee62014-04-12 00:50:03255 TRACE_EVENT2("v8",
256 "v8.callModuleMethod",
257 "module_name",
258 module_name,
259 "method_name",
260 method_name);
[email protected]68e63ea12013-06-05 05:00:54261
[email protected]95c6b3012013-12-02 14:30:31262 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]68e63ea12013-06-05 05:00:54263 v8::Context::Scope context_scope(context()->v8_context());
264
265 v8::Local<v8::Value> module;
266 {
267 NativesEnabledScope natives_enabled(this);
[email protected]9c47471e2013-11-28 14:41:21268 module = RequireForJsInner(
269 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
[email protected]68e63ea12013-06-05 05:00:54270 }
271
[email protected]95ee77da2013-03-19 21:11:11272 if (module.IsEmpty() || !module->IsObject()) {
[email protected]662c48b2013-07-12 03:50:52273 Fatal(context_,
[email protected]ad6aa8f92013-06-22 15:34:16274 "Failed to get module " + module_name + " to call " + method_name);
[email protected]95c6b3012013-12-02 14:30:31275 return handle_scope.Escape(
276 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())));
[email protected]95ee77da2013-03-19 21:11:11277 }
278
[email protected]f55c90ee62014-04-12 00:50:03279 v8::Local<v8::Value> value = v8::Handle<v8::Object>::Cast(module)->Get(
280 v8::String::NewFromUtf8(GetIsolate(), method_name.c_str()));
[email protected]95ee77da2013-03-19 21:11:11281 if (value.IsEmpty() || !value->IsFunction()) {
[email protected]662c48b2013-07-12 03:50:52282 Fatal(context_, module_name + "." + method_name + " is not a function");
[email protected]95c6b3012013-12-02 14:30:31283 return handle_scope.Escape(
284 v8::Local<v8::Primitive>(v8::Undefined(GetIsolate())));
[email protected]95ee77da2013-03-19 21:11:11285 }
286
[email protected]68e63ea12013-06-05 05:00:54287 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value);
[email protected]a714e462013-01-26 06:33:10288 v8::Local<v8::Value> result;
[email protected]2bcbf712012-09-05 06:58:49289 {
[email protected]2bcbf712012-09-05 06:58:49290 v8::TryCatch try_catch;
291 try_catch.SetCaptureMessage(true);
[email protected]68e63ea12013-06-05 05:00:54292 result = context_->CallFunction(func, argc, argv);
[email protected]2bcbf712012-09-05 06:58:49293 if (try_catch.HasCaught())
[email protected]144114942012-12-04 07:23:23294 HandleException(try_catch);
[email protected]2bcbf712012-09-05 06:58:49295 }
[email protected]95c6b3012013-12-02 14:30:31296 return handle_scope.Escape(result);
[email protected]2bcbf712012-09-05 06:58:49297}
298
[email protected]f55c90ee62014-04-12 00:50:03299void ModuleSystem::RegisterNativeHandler(
300 const std::string& name,
[email protected]58e10452012-02-22 03:34:45301 scoped_ptr<NativeHandler> native_handler) {
kalmanb0c1c502015-04-15 00:25:06302 ClobberExistingNativeHandler(name);
[email protected]58e10452012-02-22 03:34:45303 native_handler_map_[name] =
304 linked_ptr<NativeHandler>(native_handler.release());
305}
306
[email protected]95ee77da2013-03-19 21:11:11307void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) {
kalmanb0c1c502015-04-15 00:25:06308 ClobberExistingNativeHandler(name);
[email protected]11844fa2012-05-10 00:35:59309 overridden_native_handlers_.insert(name);
310}
311
[email protected]ecde1912012-03-16 06:25:31312void ModuleSystem::RunString(const std::string& code, const std::string& name) {
[email protected]dc3d06e2013-09-06 12:21:03313 v8::HandleScope handle_scope(GetIsolate());
[email protected]9c47471e2013-11-28 14:41:21314 RunString(v8::String::NewFromUtf8(GetIsolate(), code.c_str()),
315 v8::String::NewFromUtf8(GetIsolate(), name.c_str()));
[email protected]58e10452012-02-22 03:34:45316}
317
[email protected]cc0457712012-03-21 03:56:38318// static
[email protected]4e008e172013-06-13 20:15:48319void ModuleSystem::NativeLazyFieldGetter(
[email protected]4f1633f2013-03-09 14:26:24320 v8::Local<v8::String> property,
[email protected]4e008e172013-06-13 20:15:48321 const v8::PropertyCallbackInfo<v8::Value>& info) {
[email protected]f55c90ee62014-04-12 00:50:03322 LazyFieldGetterInner(property, info, &ModuleSystem::RequireNativeFromString);
[email protected]4e008e172013-06-13 20:15:48323}
324
325// static
326void ModuleSystem::LazyFieldGetter(
327 v8::Local<v8::String> property,
328 const v8::PropertyCallbackInfo<v8::Value>& info) {
329 LazyFieldGetterInner(property, info, &ModuleSystem::Require);
330}
331
332// static
333void ModuleSystem::LazyFieldGetterInner(
334 v8::Local<v8::String> property,
335 const v8::PropertyCallbackInfo<v8::Value>& info,
[email protected]4f1633f2013-03-09 14:26:24336 RequireFunction require_function) {
[email protected]561ddce2012-03-22 01:18:55337 CHECK(!info.Data().IsEmpty());
338 CHECK(info.Data()->IsObject());
[email protected]dc3d06e2013-09-06 12:21:03339 v8::HandleScope handle_scope(info.GetIsolate());
[email protected]561ddce2012-03-22 01:18:55340 v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data());
[email protected]9a598442013-06-04 16:39:12341 // This context should be the same as context()->v8_context().
[email protected]4f1633f2013-03-09 14:26:24342 v8::Handle<v8::Context> context = parameters->CreationContext();
343 v8::Handle<v8::Object> global(context->Global());
[email protected]9c47471e2013-11-28 14:41:21344 v8::Handle<v8::Value> module_system_value = global->GetHiddenValue(
345 v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem));
[email protected]95ee77da2013-03-19 21:11:11346 if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) {
[email protected]bad9a5f2012-04-13 19:16:54347 // ModuleSystem has been deleted.
[email protected]95ee77da2013-03-19 21:11:11348 // TODO(kalman): See comment in header file.
[email protected]95c6b3012013-12-02 14:30:31349 Warn(info.GetIsolate(),
350 "Module system has been deleted, does extension view exist?");
[email protected]4e008e172013-06-13 20:15:48351 return;
[email protected]bad9a5f2012-04-13 19:16:54352 }
[email protected]95ee77da2013-03-19 21:11:11353
[email protected]561ddce2012-03-22 01:18:55354 ModuleSystem* module_system = static_cast<ModuleSystem*>(
355 v8::Handle<v8::External>::Cast(module_system_value)->Value());
[email protected]cc0457712012-03-21 03:56:38356
dcarneya261b772014-11-20 17:55:07357 std::string name = *v8::String::Utf8Value(parameters->Get(
358 v8::String::NewFromUtf8(info.GetIsolate(), kModuleName)));
[email protected]4f1633f2013-03-09 14:26:24359
360 // Switch to our v8 context because we need functions created while running
361 // the require()d module to belong to our context, not the current one.
362 v8::Context::Scope context_scope(context);
363 NativesEnabledScope natives_enabled_scope(module_system);
364
365 v8::TryCatch try_catch;
[email protected]95ee77da2013-03-19 21:11:11366 v8::Handle<v8::Value> module_value = (module_system->*require_function)(name);
[email protected]4f1633f2013-03-09 14:26:24367 if (try_catch.HasCaught()) {
368 module_system->HandleException(try_catch);
[email protected]4e008e172013-06-13 20:15:48369 return;
[email protected]95ee77da2013-03-19 21:11:11370 }
371 if (module_value.IsEmpty() || !module_value->IsObject()) {
372 // require_function will have already logged this, we don't need to.
[email protected]4e008e172013-06-13 20:15:48373 return;
[email protected]b6aad81c2012-03-27 08:45:15374 }
[email protected]4f1633f2013-03-09 14:26:24375
[email protected]95ee77da2013-03-19 21:11:11376 v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value);
[email protected]561ddce2012-03-22 01:18:55377 v8::Handle<v8::String> field =
[email protected]9c47471e2013-11-28 14:41:21378 parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField))
dcarneya261b772014-11-20 17:55:07379 ->ToString(info.GetIsolate());
[email protected]561ddce2012-03-22 01:18:55380
[email protected]95ee77da2013-03-19 21:11:11381 if (!module->Has(field)) {
[email protected]d7cd1b02013-09-10 00:22:16382 std::string field_str = *v8::String::Utf8Value(field);
[email protected]662c48b2013-07-12 03:50:52383 Fatal(module_system->context_,
384 "Lazy require of " + name + "." + field_str + " did not set the " +
385 field_str + " field");
[email protected]4e008e172013-06-13 20:15:48386 return;
[email protected]95ee77da2013-03-19 21:11:11387 }
[email protected]4f1633f2013-03-09 14:26:24388
389 v8::Local<v8::Value> new_field = module->Get(field);
[email protected]95ee77da2013-03-19 21:11:11390 if (try_catch.HasCaught()) {
391 module_system->HandleException(try_catch);
[email protected]4e008e172013-06-13 20:15:48392 return;
[email protected]95ee77da2013-03-19 21:11:11393 }
394
395 // Ok for it to be undefined, among other things it's how bindings signify
396 // that the extension doesn't have permission to use them.
397 CHECK(!new_field.IsEmpty());
398
[email protected]4f1633f2013-03-09 14:26:24399 // Delete the getter and set this field to |new_field| so the same object is
400 // returned every time a certain API is accessed.
[email protected]80b0c8c2014-04-22 15:00:43401 v8::Handle<v8::Value> val = info.This();
402 if (val->IsObject()) {
403 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(val);
404 object->Delete(property);
405 object->Set(property, new_field);
406 } else {
407 NOTREACHED();
408 }
[email protected]4e008e172013-06-13 20:15:48409 info.GetReturnValue().Set(new_field);
[email protected]561ddce2012-03-22 01:18:55410}
411
412void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
413 const std::string& field,
414 const std::string& module_name,
415 const std::string& module_field) {
[email protected]f55c90ee62014-04-12 00:50:03416 SetLazyField(
417 object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter);
[email protected]4f1633f2013-03-09 14:26:24418}
419
420void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object,
421 const std::string& field,
422 const std::string& module_name,
423 const std::string& module_field,
[email protected]4e008e172013-06-13 20:15:48424 v8::AccessorGetterCallback getter) {
[email protected]dc3d06e2013-09-06 12:21:03425 v8::HandleScope handle_scope(GetIsolate());
[email protected]505ffde2013-12-19 15:47:13426 v8::Handle<v8::Object> parameters = v8::Object::New(GetIsolate());
[email protected]9c47471e2013-11-28 14:41:21427 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName),
428 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
429 parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField),
430 v8::String::NewFromUtf8(GetIsolate(), module_field.c_str()));
431 object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()),
[email protected]4f1633f2013-03-09 14:26:24432 getter,
[email protected]561ddce2012-03-22 01:18:55433 NULL,
434 parameters);
[email protected]cc0457712012-03-21 03:56:38435}
[email protected]ecde1912012-03-16 06:25:31436
[email protected]4f1633f2013-03-09 14:26:24437void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object,
438 const std::string& field,
439 const std::string& module_name,
440 const std::string& module_field) {
[email protected]f55c90ee62014-04-12 00:50:03441 SetLazyField(object,
442 field,
443 module_name,
444 module_field,
445 &ModuleSystem::NativeLazyFieldGetter);
[email protected]4f1633f2013-03-09 14:26:24446}
447
[email protected]ecde1912012-03-16 06:25:31448v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code,
449 v8::Handle<v8::String> name) {
[email protected]95c6b3012013-12-02 14:30:31450 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]68e63ea12013-06-05 05:00:54451 v8::Context::Scope context_scope(context()->v8_context());
[email protected]95ee77da2013-03-19 21:11:11452
[email protected]d7cd1b02013-09-10 00:22:16453 // Prepend extensions:: to |name| so that internal code can be differentiated
454 // from external code in stack traces. This has no effect on behaviour.
[email protected]f55c90ee62014-04-12 00:50:03455 std::string internal_name =
456 base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name));
[email protected]d7cd1b02013-09-10 00:22:16457
[email protected]a1221aea2013-11-07 01:31:30458 blink::WebScopedMicrotaskSuppression suppression;
[email protected]7c95c1a82012-04-17 02:11:33459 v8::TryCatch try_catch;
[email protected]56a4bf832012-07-13 04:26:54460 try_catch.SetCaptureMessage(true);
[email protected]9c47471e2013-11-28 14:41:21461 v8::Handle<v8::Script> script(
[email protected]74af28ea2014-02-27 18:53:29462 v8::Script::Compile(code,
463 v8::String::NewFromUtf8(GetIsolate(),
464 internal_name.c_str(),
465 v8::String::kNormalString,
466 internal_name.size())));
[email protected]7c95c1a82012-04-17 02:11:33467 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23468 HandleException(try_catch);
[email protected]95c6b3012013-12-02 14:30:31469 return v8::Undefined(GetIsolate());
[email protected]7c95c1a82012-04-17 02:11:33470 }
471
[email protected]95c6b3012013-12-02 14:30:31472 v8::Local<v8::Value> result = script->Run();
[email protected]95ee77da2013-03-19 21:11:11473 if (try_catch.HasCaught()) {
[email protected]144114942012-12-04 07:23:23474 HandleException(try_catch);
[email protected]95c6b3012013-12-02 14:30:31475 return v8::Undefined(GetIsolate());
[email protected]95ee77da2013-03-19 21:11:11476 }
[email protected]7c95c1a82012-04-17 02:11:33477
[email protected]95c6b3012013-12-02 14:30:31478 return handle_scope.Escape(result);
[email protected]9e03ce22012-03-13 08:50:05479}
480
[email protected]d2663612013-03-17 09:25:56481v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) {
[email protected]95c6b3012013-12-02 14:30:31482 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]ecde1912012-03-16 06:25:31483 if (!source_map_->Contains(module_name))
[email protected]95c6b3012013-12-02 14:30:31484 return v8::Undefined(GetIsolate());
485 return handle_scope.Escape(
486 v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name)));
[email protected]58e10452012-02-22 03:34:45487}
488
[email protected]d8c5fbb2013-06-14 11:35:25489void ModuleSystem::RequireNative(
490 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]58e10452012-02-22 03:34:45491 CHECK_EQ(1, args.Length());
dcarneya261b772014-11-20 17:55:07492 std::string native_name = *v8::String::Utf8Value(args[0]);
[email protected]d8c5fbb2013-06-14 11:35:25493 args.GetReturnValue().Set(RequireNativeFromString(native_name));
[email protected]4f1633f2013-03-09 14:26:24494}
495
496v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString(
497 const std::string& native_name) {
[email protected]95ee77da2013-03-19 21:11:11498 if (natives_enabled_ == 0) {
499 // HACK: if in test throw exception so that we can test the natives-disabled
500 // logic; however, under normal circumstances, this is programmer error so
501 // we could crash.
[email protected]9c47471e2013-11-28 14:41:21502 if (exception_handler_) {
[email protected]95c6b3012013-12-02 14:30:31503 return GetIsolate()->ThrowException(
[email protected]9c47471e2013-11-28 14:41:21504 v8::String::NewFromUtf8(GetIsolate(), "Natives disabled"));
505 }
[email protected]662c48b2013-07-12 03:50:52506 Fatal(context_, "Natives disabled for requireNative(" + native_name + ")");
[email protected]95c6b3012013-12-02 14:30:31507 return v8::Undefined(GetIsolate());
[email protected]95ee77da2013-03-19 21:11:11508 }
509
[email protected]9c47471e2013-11-28 14:41:21510 if (overridden_native_handlers_.count(native_name) > 0u) {
511 return RequireForJsInner(
512 v8::String::NewFromUtf8(GetIsolate(), native_name.c_str()));
513 }
[email protected]95ee77da2013-03-19 21:11:11514
[email protected]58e10452012-02-22 03:34:45515 NativeHandlerMap::iterator i = native_handler_map_.find(native_name);
[email protected]95ee77da2013-03-19 21:11:11516 if (i == native_handler_map_.end()) {
[email protected]662c48b2013-07-12 03:50:52517 Fatal(context_,
[email protected]ad6aa8f92013-06-22 15:34:16518 "Couldn't find native for requireNative(" + native_name + ")");
[email protected]95c6b3012013-12-02 14:30:31519 return v8::Undefined(GetIsolate());
[email protected]95ee77da2013-03-19 21:11:11520 }
[email protected]58e10452012-02-22 03:34:45521 return i->second->NewInstance();
522}
[email protected]ecde1912012-03-16 06:25:31523
[email protected]d9f51dad2014-07-09 05:39:38524void ModuleSystem::RequireAsync(
525 const v8::FunctionCallbackInfo<v8::Value>& args) {
526 CHECK_EQ(1, args.Length());
dcarneya261b772014-11-20 17:55:07527 std::string module_name = *v8::String::Utf8Value(args[0]);
[email protected]d9f51dad2014-07-09 05:39:38528 v8::Handle<v8::Promise::Resolver> resolver(
529 v8::Promise::Resolver::New(GetIsolate()));
530 args.GetReturnValue().Set(resolver->GetPromise());
kalman83292b82015-03-12 16:40:25531 scoped_ptr<v8::Global<v8::Promise::Resolver>> global_resolver(
532 new v8::Global<v8::Promise::Resolver>(GetIsolate(), resolver));
[email protected]d9f51dad2014-07-09 05:39:38533 gin::ModuleRegistry* module_registry =
534 gin::ModuleRegistry::From(context_->v8_context());
535 if (!module_registry) {
536 Warn(GetIsolate(), "Extension view no longer exists");
537 resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8(
538 GetIsolate(), "Extension view no longer exists")));
539 return;
540 }
kalman83292b82015-03-12 16:40:25541 module_registry->LoadModule(
542 GetIsolate(), module_name,
543 base::Bind(&ModuleSystem::OnModuleLoaded, weak_factory_.GetWeakPtr(),
544 base::Passed(&global_resolver)));
[email protected]d9f51dad2014-07-09 05:39:38545 if (module_registry->available_modules().count(module_name) == 0)
546 LoadModule(module_name);
547}
548
[email protected]ecde1912012-03-16 06:25:31549v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) {
[email protected]95c6b3012013-12-02 14:30:31550 v8::EscapableHandleScope handle_scope(GetIsolate());
[email protected]295890bd2013-06-15 10:52:45551 // Keep in order with the arguments in RequireForJsInner.
[email protected]2a356872014-02-21 23:18:52552 v8::Handle<v8::String> left = v8::String::NewFromUtf8(
553 GetIsolate(),
[email protected]d9f51dad2014-07-09 05:39:38554 "(function(define, require, requireNative, requireAsync, exports, "
[email protected]2a356872014-02-21 23:18:52555 "console, privates,"
[email protected]0d775c72014-08-22 13:06:26556 "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {"
[email protected]2a356872014-02-21 23:18:52557 "'use strict';");
[email protected]9c47471e2013-11-28 14:41:21558 v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})");
[email protected]95c6b3012013-12-02 14:30:31559 return handle_scope.Escape(v8::Local<v8::String>(
560 v8::String::Concat(left, v8::String::Concat(source, right))));
[email protected]ecde1912012-03-16 06:25:31561}
562
[email protected]ca557292013-12-11 08:44:27563void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) {
564 CHECK_EQ(1, args.Length());
rob5c641ed2014-12-09 00:24:57565 if (!args[0]->IsObject() || args[0]->IsNull()) {
566 GetIsolate()->ThrowException(
567 v8::Exception::TypeError(v8::String::NewFromUtf8(GetIsolate(),
568 args[0]->IsUndefined()
569 ? "Method called without a valid receiver (this). "
570 "Did you forget to call .bind()?"
571 : "Invalid invocation: receiver is not an object!")));
572 return;
573 }
[email protected]ca557292013-12-11 08:44:27574 v8::Local<v8::Object> obj = args[0].As<v8::Object>();
575 v8::Local<v8::String> privates_key =
576 v8::String::NewFromUtf8(GetIsolate(), "privates");
577 v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key);
578 if (privates.IsEmpty()) {
[email protected]505ffde2013-12-19 15:47:13579 privates = v8::Object::New(args.GetIsolate());
kalmanad8ddd02014-10-15 00:20:06580 if (privates.IsEmpty()) {
581 GetIsolate()->ThrowException(
582 v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates"));
583 return;
584 }
[email protected]ca557292013-12-11 08:44:27585 obj->SetHiddenValue(privates_key, privates);
586 }
587 args.GetReturnValue().Set(privates);
588}
589
[email protected]d9f51dad2014-07-09 05:39:38590v8::Handle<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) {
591 v8::EscapableHandleScope handle_scope(GetIsolate());
592 v8::Context::Scope context_scope(context()->v8_context());
593
594 v8::Handle<v8::Value> source(GetSource(module_name));
595 if (source.IsEmpty() || source->IsUndefined()) {
596 Fatal(context_, "No source for require(" + module_name + ")");
597 return v8::Undefined(GetIsolate());
598 }
599 v8::Handle<v8::String> wrapped_source(
600 WrapSource(v8::Handle<v8::String>::Cast(source)));
601 // Modules are wrapped in (function(){...}) so they always return functions.
602 v8::Handle<v8::Value> func_as_value =
603 RunString(wrapped_source,
604 v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()));
605 if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) {
606 Fatal(context_, "Bad source for require(" + module_name + ")");
607 return v8::Undefined(GetIsolate());
608 }
609
610 v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value);
611
612 v8::Handle<v8::Object> define_object = v8::Object::New(GetIsolate());
613 gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object);
614
615 v8::Local<v8::Value> exports = v8::Object::New(GetIsolate());
616 v8::Handle<v8::Object> natives(NewInstance());
617 CHECK(!natives.IsEmpty()); // this can happen if v8 has issues
618
619 // These must match the argument order in WrapSource.
620 v8::Handle<v8::Value> args[] = {
621 // AMD.
622 define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")),
623 // CommonJS.
624 natives->Get(v8::String::NewFromUtf8(
625 GetIsolate(), "require", v8::String::kInternalizedString)),
626 natives->Get(v8::String::NewFromUtf8(
627 GetIsolate(), "requireNative", v8::String::kInternalizedString)),
628 natives->Get(v8::String::NewFromUtf8(
629 GetIsolate(), "requireAsync", v8::String::kInternalizedString)),
630 exports,
631 // Libraries that we magically expose to every module.
kalmanfb6f10ac2014-11-06 23:55:35632 console::AsV8Object(GetIsolate()),
[email protected]d9f51dad2014-07-09 05:39:38633 natives->Get(v8::String::NewFromUtf8(
634 GetIsolate(), "privates", v8::String::kInternalizedString)),
635 // Each safe builtin. Keep in order with the arguments in WrapSource.
636 context_->safe_builtins()->GetArray(),
637 context_->safe_builtins()->GetFunction(),
638 context_->safe_builtins()->GetJSON(),
639 context_->safe_builtins()->GetObjekt(),
640 context_->safe_builtins()->GetRegExp(),
641 context_->safe_builtins()->GetString(),
[email protected]0d775c72014-08-22 13:06:26642 context_->safe_builtins()->GetError(),
[email protected]d9f51dad2014-07-09 05:39:38643 };
644 {
645 v8::TryCatch try_catch;
646 try_catch.SetCaptureMessage(true);
647 context_->CallFunction(func, arraysize(args), args);
648 if (try_catch.HasCaught()) {
649 HandleException(try_catch);
650 return v8::Undefined(GetIsolate());
651 }
652 }
653 return handle_scope.Escape(exports);
654}
655
656void ModuleSystem::OnDidAddPendingModule(
657 const std::string& id,
658 const std::vector<std::string>& dependencies) {
sammc2cc226562015-01-14 01:25:06659 bool module_system_managed = source_map_->Contains(id);
[email protected]d9f51dad2014-07-09 05:39:38660
661 gin::ModuleRegistry* registry =
662 gin::ModuleRegistry::From(context_->v8_context());
663 DCHECK(registry);
sammc2cc226562015-01-14 01:25:06664 for (const auto& dependency : dependencies) {
665 // If a dependency is not available, and either the module or this
666 // dependency is managed by ModuleSystem, attempt to load it. Other
667 // gin::ModuleRegistry users (WebUI and users of the mojoPrivate API) are
668 // responsible for loading their module dependencies when required.
669 if (registry->available_modules().count(dependency) == 0 &&
670 (module_system_managed || source_map_->Contains(dependency))) {
671 LoadModule(dependency);
672 }
[email protected]d9f51dad2014-07-09 05:39:38673 }
674 registry->AttemptToLoadMoreModules(GetIsolate());
675}
676
677void ModuleSystem::OnModuleLoaded(
kalman83292b82015-03-12 16:40:25678 scoped_ptr<v8::Global<v8::Promise::Resolver>> resolver,
[email protected]d9f51dad2014-07-09 05:39:38679 v8::Handle<v8::Value> value) {
680 if (!is_valid())
681 return;
682 v8::HandleScope handle_scope(GetIsolate());
683 v8::Handle<v8::Promise::Resolver> resolver_local(
684 v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver));
685 resolver_local->Resolve(value);
686}
687
kalmanb0c1c502015-04-15 00:25:06688void ModuleSystem::ClobberExistingNativeHandler(const std::string& name) {
689 NativeHandlerMap::iterator existing_handler = native_handler_map_.find(name);
690 if (existing_handler != native_handler_map_.end()) {
691 clobbered_native_handlers_.push_back(existing_handler->second);
692 native_handler_map_.erase(existing_handler);
693 }
694}
695
[email protected]4e008e172013-06-13 20:15:48696} // namespace extensions