[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 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] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 5 | #include "extensions/renderer/module_system.h" |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 8 | #include "base/command_line.h" |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 9 | #include "base/debug/trace_event.h" |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 10 | #include "base/stl_util.h" |
[email protected] | a19a16d8 | 2013-06-11 17:45:12 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
| 12 | #include "base/strings/stringprintf.h" |
sammc | fb8875c | 2014-10-28 11:51:04 | [diff] [blame^] | 13 | #include "content/public/renderer/render_frame.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 14 | #include "content/public/renderer/render_view.h" |
thestig | b012bc3d | 2014-09-18 22:57:13 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 16 | #include "extensions/common/extensions_client.h" |
| 17 | #include "extensions/renderer/console.h" |
| 18 | #include "extensions/renderer/safe_builtins.h" |
| 19 | #include "extensions/renderer/script_context.h" |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 20 | #include "gin/modules/module_registry.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 21 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 22 | #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 23 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 24 | namespace extensions { |
| 25 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 26 | namespace { |
| 27 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 28 | const char* kModuleSystem = "module_system"; |
| 29 | const char* kModuleName = "module_name"; |
| 30 | const char* kModuleField = "module_field"; |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 31 | const char* kModulesField = "modules"; |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 32 | |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 33 | // Logs a fatal error for the calling context, with some added metadata about |
| 34 | // the context: |
| 35 | // - Its type (blessed, unblessed, etc). |
| 36 | // - Whether it's valid. |
| 37 | // - The extension ID, if one exists. |
| 38 | // |
[email protected] | d84ecbf | 2013-07-30 22:48:16 | [diff] [blame] | 39 | // This will only actual be fatal in in dev/canary, since in too many cases |
| 40 | // we're at the mercy of the extension or web page's environment. They can mess |
| 41 | // up our JS in unexpected ways. Hopefully dev/canary channel will pick up such |
| 42 | // problems, but given the wider variety on stable/beta it's impossible to know. |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 43 | void Fatal(ScriptContext* context, const std::string& message) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 44 | // Prepend some context metadata. |
| 45 | std::string full_message = "("; |
| 46 | if (!context->is_valid()) |
| 47 | full_message += "Invalid "; |
| 48 | full_message += context->GetContextTypeDescription(); |
| 49 | full_message += " context"; |
| 50 | if (context->extension()) { |
| 51 | full_message += " for "; |
| 52 | full_message += context->extension()->id(); |
| 53 | } |
| 54 | full_message += ") "; |
| 55 | full_message += message; |
| 56 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 57 | if (ExtensionsClient::Get()->ShouldSuppressFatalErrors()) |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 58 | console::Error(context->isolate()->GetCallingContext(), full_message); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 59 | else |
| 60 | console::Fatal(context->isolate()->GetCallingContext(), full_message); |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 61 | } |
| 62 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 63 | void Warn(v8::Isolate* isolate, const std::string& message) { |
| 64 | console::Warn(isolate->GetCallingContext(), message); |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 65 | } |
| 66 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 67 | // Default exception handler which logs the exception. |
| 68 | class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
| 69 | public: |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 70 | explicit DefaultExceptionHandler(ScriptContext* context) |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 71 | : context_(context) {} |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 72 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 73 | // Fatally dumps the debug info from |try_catch| to the console. |
| 74 | // Make sure this is never used for exceptions that originate in external |
| 75 | // code! |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 76 | void HandleUncaughtException(const v8::TryCatch& try_catch) override { |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 77 | v8::HandleScope handle_scope(context_->isolate()); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 78 | std::string stack_trace = "<stack trace unavailable>"; |
| 79 | if (!try_catch.StackTrace().IsEmpty()) { |
| 80 | v8::String::Utf8Value stack_value(try_catch.StackTrace()); |
| 81 | if (*stack_value) |
| 82 | stack_trace.assign(*stack_value, stack_value.length()); |
| 83 | else |
| 84 | stack_trace = "<could not convert stack trace to string>"; |
| 85 | } |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 86 | Fatal(context_, CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 87 | } |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 88 | |
| 89 | private: |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 90 | ScriptContext* context_; |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 91 | }; |
| 92 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 93 | } // namespace |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 94 | |
| 95 | std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
| 96 | const v8::TryCatch& try_catch) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 97 | v8::Handle<v8::Message> message(try_catch.Message()); |
| 98 | if (message.IsEmpty()) { |
| 99 | return "try_catch has no message"; |
| 100 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 101 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 102 | std::string resource_name = "<unknown resource>"; |
[email protected] | bce20ba | 2014-06-27 18:18:45 | [diff] [blame] | 103 | if (!message->GetScriptOrigin().ResourceName().IsEmpty()) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 104 | v8::String::Utf8Value resource_name_v8( |
[email protected] | bce20ba | 2014-06-27 18:18:45 | [diff] [blame] | 105 | message->GetScriptOrigin().ResourceName()->ToString()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 106 | resource_name.assign(*resource_name_v8, resource_name_v8.length()); |
| 107 | } |
| 108 | |
| 109 | std::string error_message = "<no error message>"; |
| 110 | if (!message->Get().IsEmpty()) { |
| 111 | v8::String::Utf8Value error_message_v8(message->Get()); |
| 112 | error_message.assign(*error_message_v8, error_message_v8.length()); |
| 113 | } |
| 114 | |
| 115 | return base::StringPrintf("%s:%d: %s", |
| 116 | resource_name.c_str(), |
| 117 | message->GetLineNumber(), |
| 118 | error_message.c_str()); |
| 119 | } |
| 120 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 121 | ModuleSystem::ModuleSystem(ScriptContext* context, SourceMap* source_map) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 122 | : ObjectBackedNativeHandler(context), |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 123 | context_(context), |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 124 | source_map_(source_map), |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 125 | natives_enabled_(0), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 126 | exception_handler_(new DefaultExceptionHandler(context)), |
| 127 | weak_factory_(this) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 128 | RouteFunction( |
| 129 | "require", |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 130 | base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 131 | RouteFunction( |
| 132 | "requireNative", |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 133 | base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 134 | RouteFunction( |
| 135 | "requireAsync", |
| 136 | base::Bind(&ModuleSystem::RequireAsync, base::Unretained(this))); |
[email protected] | ca55729 | 2013-12-11 08:44:27 | [diff] [blame] | 137 | RouteFunction("privates", |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 138 | base::Bind(&ModuleSystem::Private, base::Unretained(this))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 139 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 140 | v8::Handle<v8::Object> global(context->v8_context()->Global()); |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 141 | v8::Isolate* isolate = context->isolate(); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 142 | global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModulesField), |
| 143 | v8::Object::New(isolate)); |
| 144 | global->SetHiddenValue(v8::String::NewFromUtf8(isolate, kModuleSystem), |
| 145 | v8::External::New(isolate, this)); |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 146 | |
| 147 | gin::ModuleRegistry::From(context->v8_context())->AddObserver(this); |
sammc | fb8875c | 2014-10-28 11:51:04 | [diff] [blame^] | 148 | if (context_->GetRenderFrame()) { |
| 149 | context_->GetRenderFrame()->EnsureMojoBuiltinsAreAvailable( |
| 150 | context->isolate(), context->v8_context()); |
| 151 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 154 | ModuleSystem::~ModuleSystem() { Invalidate(); } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 155 | |
| 156 | void ModuleSystem::Invalidate() { |
| 157 | if (!is_valid()) |
| 158 | return; |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 159 | |
| 160 | // Clear the module system properties from the global context. It's polite, |
| 161 | // and we use this as a signal in lazy handlers that we no longer exist. |
| 162 | { |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 163 | v8::HandleScope scope(GetIsolate()); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 164 | v8::Handle<v8::Object> global = context()->v8_context()->Global(); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 165 | global->DeleteHiddenValue( |
| 166 | v8::String::NewFromUtf8(GetIsolate(), kModulesField)); |
| 167 | global->DeleteHiddenValue( |
| 168 | v8::String::NewFromUtf8(GetIsolate(), kModuleSystem)); |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Invalidate all of the successfully required handlers we own. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 172 | for (NativeHandlerMap::iterator it = native_handler_map_.begin(); |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 173 | it != native_handler_map_.end(); |
| 174 | ++it) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 175 | it->second->Invalidate(); |
| 176 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 177 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 178 | ObjectBackedNativeHandler::Invalidate(); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 181 | ModuleSystem::NativesEnabledScope::NativesEnabledScope( |
| 182 | ModuleSystem* module_system) |
| 183 | : module_system_(module_system) { |
| 184 | module_system_->natives_enabled_++; |
| 185 | } |
| 186 | |
| 187 | ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 188 | module_system_->natives_enabled_--; |
| 189 | CHECK_GE(module_system_->natives_enabled_, 0); |
| 190 | } |
| 191 | |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 192 | void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 193 | exception_handler_->HandleUncaughtException(try_catch); |
[email protected] | 1c6189f | 2012-05-18 06:45:52 | [diff] [blame] | 194 | } |
| 195 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 196 | v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 197 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 198 | return handle_scope.Escape(RequireForJsInner( |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 199 | v8::String::NewFromUtf8(GetIsolate(), module_name.c_str()))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 202 | void ModuleSystem::RequireForJs( |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 203 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 204 | v8::Handle<v8::String> module_name = args[0]->ToString(); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 205 | args.GetReturnValue().Set(RequireForJsInner(module_name)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 206 | } |
| 207 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 208 | v8::Local<v8::Value> ModuleSystem::RequireForJsInner( |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 209 | v8::Handle<v8::String> module_name) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 210 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 211 | v8::Context::Scope context_scope(context()->v8_context()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 212 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 213 | v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 214 | |
| 215 | // The module system might have been deleted. This can happen if a different |
| 216 | // context keeps a reference to us, but our frame is destroyed (e.g. |
| 217 | // background page keeps reference to chrome object in a closed popup). |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 218 | v8::Handle<v8::Value> modules_value = global->GetHiddenValue( |
| 219 | v8::String::NewFromUtf8(GetIsolate(), kModulesField)); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 220 | if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 221 | Warn(GetIsolate(), "Extension view no longer exists"); |
| 222 | return v8::Undefined(GetIsolate()); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 223 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 224 | |
| 225 | v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 226 | v8::Local<v8::Value> exports(modules->Get(module_name)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 227 | if (!exports->IsUndefined()) |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 228 | return handle_scope.Escape(exports); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 229 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 230 | exports = LoadModule(*v8::String::Utf8Value(module_name)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 231 | modules->Set(module_name, exports); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 232 | return handle_scope.Escape(exports); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 235 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 236 | const std::string& module_name, |
| 237 | const std::string& method_name) { |
[email protected] | f80685c3 | 2014-07-26 19:48:04 | [diff] [blame] | 238 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 239 | v8::Handle<v8::Value> no_args; |
[email protected] | f80685c3 | 2014-07-26 19:48:04 | [diff] [blame] | 240 | return handle_scope.Escape( |
| 241 | CallModuleMethod(module_name, method_name, 0, &no_args)); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 245 | const std::string& module_name, |
| 246 | const std::string& method_name, |
| 247 | std::vector<v8::Handle<v8::Value> >* args) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 248 | return CallModuleMethod( |
| 249 | module_name, method_name, args->size(), vector_as_array(args)); |
| 250 | } |
| 251 | |
| 252 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 253 | const std::string& module_name, |
| 254 | const std::string& method_name, |
| 255 | int argc, |
| 256 | v8::Handle<v8::Value> argv[]) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 257 | TRACE_EVENT2("v8", |
| 258 | "v8.callModuleMethod", |
| 259 | "module_name", |
| 260 | module_name, |
| 261 | "method_name", |
| 262 | method_name); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 263 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 264 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 265 | v8::Context::Scope context_scope(context()->v8_context()); |
| 266 | |
| 267 | v8::Local<v8::Value> module; |
| 268 | { |
| 269 | NativesEnabledScope natives_enabled(this); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 270 | module = RequireForJsInner( |
| 271 | v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 274 | if (module.IsEmpty() || !module->IsObject()) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 275 | Fatal(context_, |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 276 | "Failed to get module " + module_name + " to call " + method_name); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 277 | return handle_scope.Escape( |
| 278 | v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 281 | v8::Local<v8::Value> value = v8::Handle<v8::Object>::Cast(module)->Get( |
| 282 | v8::String::NewFromUtf8(GetIsolate(), method_name.c_str())); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 283 | if (value.IsEmpty() || !value->IsFunction()) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 284 | Fatal(context_, module_name + "." + method_name + " is not a function"); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 285 | return handle_scope.Escape( |
| 286 | v8::Local<v8::Primitive>(v8::Undefined(GetIsolate()))); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 287 | } |
| 288 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 289 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 290 | v8::Local<v8::Value> result; |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 291 | { |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 292 | v8::TryCatch try_catch; |
| 293 | try_catch.SetCaptureMessage(true); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 294 | result = context_->CallFunction(func, argc, argv); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 295 | if (try_catch.HasCaught()) |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 296 | HandleException(try_catch); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 297 | } |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 298 | return handle_scope.Escape(result); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 301 | void ModuleSystem::RegisterNativeHandler( |
| 302 | const std::string& name, |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 303 | scoped_ptr<NativeHandler> native_handler) { |
| 304 | native_handler_map_[name] = |
| 305 | linked_ptr<NativeHandler>(native_handler.release()); |
| 306 | } |
| 307 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 308 | void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 309 | overridden_native_handlers_.insert(name); |
| 310 | } |
| 311 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 312 | void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 313 | v8::HandleScope handle_scope(GetIsolate()); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 314 | RunString(v8::String::NewFromUtf8(GetIsolate(), code.c_str()), |
| 315 | v8::String::NewFromUtf8(GetIsolate(), name.c_str())); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 316 | } |
| 317 | |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 318 | // static |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 319 | void ModuleSystem::NativeLazyFieldGetter( |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 320 | v8::Local<v8::String> property, |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 321 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 322 | LazyFieldGetterInner(property, info, &ModuleSystem::RequireNativeFromString); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // static |
| 326 | void 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 |
| 333 | void ModuleSystem::LazyFieldGetterInner( |
| 334 | v8::Local<v8::String> property, |
| 335 | const v8::PropertyCallbackInfo<v8::Value>& info, |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 336 | RequireFunction require_function) { |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 337 | CHECK(!info.Data().IsEmpty()); |
| 338 | CHECK(info.Data()->IsObject()); |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 339 | v8::HandleScope handle_scope(info.GetIsolate()); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 340 | v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 341 | // This context should be the same as context()->v8_context(). |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 342 | v8::Handle<v8::Context> context = parameters->CreationContext(); |
| 343 | v8::Handle<v8::Object> global(context->Global()); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 344 | v8::Handle<v8::Value> module_system_value = global->GetHiddenValue( |
| 345 | v8::String::NewFromUtf8(info.GetIsolate(), kModuleSystem)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 346 | if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 347 | // ModuleSystem has been deleted. |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 348 | // TODO(kalman): See comment in header file. |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 349 | Warn(info.GetIsolate(), |
| 350 | "Module system has been deleted, does extension view exist?"); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 351 | return; |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 352 | } |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 353 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 354 | ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 355 | v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 356 | |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 357 | std::string name = |
| 358 | *v8::String::Utf8Value( |
| 359 | parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), |
| 360 | kModuleName))->ToString()); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 361 | |
| 362 | // Switch to our v8 context because we need functions created while running |
| 363 | // the require()d module to belong to our context, not the current one. |
| 364 | v8::Context::Scope context_scope(context); |
| 365 | NativesEnabledScope natives_enabled_scope(module_system); |
| 366 | |
| 367 | v8::TryCatch try_catch; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 368 | v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 369 | if (try_catch.HasCaught()) { |
| 370 | module_system->HandleException(try_catch); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 371 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 372 | } |
| 373 | if (module_value.IsEmpty() || !module_value->IsObject()) { |
| 374 | // require_function will have already logged this, we don't need to. |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 375 | return; |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 376 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 377 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 378 | v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 379 | v8::Handle<v8::String> field = |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 380 | parameters->Get(v8::String::NewFromUtf8(info.GetIsolate(), kModuleField)) |
| 381 | ->ToString(); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 382 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 383 | if (!module->Has(field)) { |
[email protected] | d7cd1b0 | 2013-09-10 00:22:16 | [diff] [blame] | 384 | std::string field_str = *v8::String::Utf8Value(field); |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 385 | Fatal(module_system->context_, |
| 386 | "Lazy require of " + name + "." + field_str + " did not set the " + |
| 387 | field_str + " field"); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 388 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 389 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 390 | |
| 391 | v8::Local<v8::Value> new_field = module->Get(field); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 392 | if (try_catch.HasCaught()) { |
| 393 | module_system->HandleException(try_catch); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 394 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Ok for it to be undefined, among other things it's how bindings signify |
| 398 | // that the extension doesn't have permission to use them. |
| 399 | CHECK(!new_field.IsEmpty()); |
| 400 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 401 | // Delete the getter and set this field to |new_field| so the same object is |
| 402 | // returned every time a certain API is accessed. |
[email protected] | 80b0c8c | 2014-04-22 15:00:43 | [diff] [blame] | 403 | v8::Handle<v8::Value> val = info.This(); |
| 404 | if (val->IsObject()) { |
| 405 | v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(val); |
| 406 | object->Delete(property); |
| 407 | object->Set(property, new_field); |
| 408 | } else { |
| 409 | NOTREACHED(); |
| 410 | } |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 411 | info.GetReturnValue().Set(new_field); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 415 | const std::string& field, |
| 416 | const std::string& module_name, |
| 417 | const std::string& module_field) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 418 | SetLazyField( |
| 419 | object, field, module_name, module_field, &ModuleSystem::LazyFieldGetter); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 423 | const std::string& field, |
| 424 | const std::string& module_name, |
| 425 | const std::string& module_field, |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 426 | v8::AccessorGetterCallback getter) { |
[email protected] | dc3d06e | 2013-09-06 12:21:03 | [diff] [blame] | 427 | v8::HandleScope handle_scope(GetIsolate()); |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 428 | v8::Handle<v8::Object> parameters = v8::Object::New(GetIsolate()); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 429 | parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleName), |
| 430 | v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
| 431 | parameters->Set(v8::String::NewFromUtf8(GetIsolate(), kModuleField), |
| 432 | v8::String::NewFromUtf8(GetIsolate(), module_field.c_str())); |
| 433 | object->SetAccessor(v8::String::NewFromUtf8(GetIsolate(), field.c_str()), |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 434 | getter, |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 435 | NULL, |
| 436 | parameters); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 437 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 438 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 439 | void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, |
| 440 | const std::string& field, |
| 441 | const std::string& module_name, |
| 442 | const std::string& module_field) { |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 443 | SetLazyField(object, |
| 444 | field, |
| 445 | module_name, |
| 446 | module_field, |
| 447 | &ModuleSystem::NativeLazyFieldGetter); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 450 | v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 451 | v8::Handle<v8::String> name) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 452 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 453 | v8::Context::Scope context_scope(context()->v8_context()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 454 | |
[email protected] | d7cd1b0 | 2013-09-10 00:22:16 | [diff] [blame] | 455 | // Prepend extensions:: to |name| so that internal code can be differentiated |
| 456 | // from external code in stack traces. This has no effect on behaviour. |
[email protected] | f55c90ee6 | 2014-04-12 00:50:03 | [diff] [blame] | 457 | std::string internal_name = |
| 458 | base::StringPrintf("extensions::%s", *v8::String::Utf8Value(name)); |
[email protected] | d7cd1b0 | 2013-09-10 00:22:16 | [diff] [blame] | 459 | |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 460 | blink::WebScopedMicrotaskSuppression suppression; |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 461 | v8::TryCatch try_catch; |
[email protected] | 56a4bf83 | 2012-07-13 04:26:54 | [diff] [blame] | 462 | try_catch.SetCaptureMessage(true); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 463 | v8::Handle<v8::Script> script( |
[email protected] | 74af28ea | 2014-02-27 18:53:29 | [diff] [blame] | 464 | v8::Script::Compile(code, |
| 465 | v8::String::NewFromUtf8(GetIsolate(), |
| 466 | internal_name.c_str(), |
| 467 | v8::String::kNormalString, |
| 468 | internal_name.size()))); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 469 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 470 | HandleException(try_catch); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 471 | return v8::Undefined(GetIsolate()); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 472 | } |
| 473 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 474 | v8::Local<v8::Value> result = script->Run(); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 475 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 476 | HandleException(try_catch); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 477 | return v8::Undefined(GetIsolate()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 478 | } |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 479 | |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 480 | return handle_scope.Escape(result); |
[email protected] | 9e03ce2 | 2012-03-13 08:50:05 | [diff] [blame] | 481 | } |
| 482 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 483 | v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 484 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 485 | if (!source_map_->Contains(module_name)) |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 486 | return v8::Undefined(GetIsolate()); |
| 487 | return handle_scope.Escape( |
| 488 | v8::Local<v8::Value>(source_map_->GetSource(GetIsolate(), module_name))); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 489 | } |
| 490 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 491 | void ModuleSystem::RequireNative( |
| 492 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 493 | CHECK_EQ(1, args.Length()); |
[email protected] | d7cd1b0 | 2013-09-10 00:22:16 | [diff] [blame] | 494 | std::string native_name = *v8::String::Utf8Value(args[0]->ToString()); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 495 | args.GetReturnValue().Set(RequireNativeFromString(native_name)); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
| 499 | const std::string& native_name) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 500 | if (natives_enabled_ == 0) { |
| 501 | // HACK: if in test throw exception so that we can test the natives-disabled |
| 502 | // logic; however, under normal circumstances, this is programmer error so |
| 503 | // we could crash. |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 504 | if (exception_handler_) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 505 | return GetIsolate()->ThrowException( |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 506 | v8::String::NewFromUtf8(GetIsolate(), "Natives disabled")); |
| 507 | } |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 508 | Fatal(context_, "Natives disabled for requireNative(" + native_name + ")"); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 509 | return v8::Undefined(GetIsolate()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 510 | } |
| 511 | |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 512 | if (overridden_native_handlers_.count(native_name) > 0u) { |
| 513 | return RequireForJsInner( |
| 514 | v8::String::NewFromUtf8(GetIsolate(), native_name.c_str())); |
| 515 | } |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 516 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 517 | NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 518 | if (i == native_handler_map_.end()) { |
[email protected] | 662c48b | 2013-07-12 03:50:52 | [diff] [blame] | 519 | Fatal(context_, |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame] | 520 | "Couldn't find native for requireNative(" + native_name + ")"); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 521 | return v8::Undefined(GetIsolate()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 522 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 523 | return i->second->NewInstance(); |
| 524 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 525 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 526 | void ModuleSystem::RequireAsync( |
| 527 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 528 | CHECK_EQ(1, args.Length()); |
| 529 | std::string module_name = *v8::String::Utf8Value(args[0]->ToString()); |
| 530 | v8::Handle<v8::Promise::Resolver> resolver( |
| 531 | v8::Promise::Resolver::New(GetIsolate())); |
| 532 | args.GetReturnValue().Set(resolver->GetPromise()); |
| 533 | scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > persistent_resolver( |
| 534 | new v8::UniquePersistent<v8::Promise::Resolver>(GetIsolate(), resolver)); |
| 535 | gin::ModuleRegistry* module_registry = |
| 536 | gin::ModuleRegistry::From(context_->v8_context()); |
| 537 | if (!module_registry) { |
| 538 | Warn(GetIsolate(), "Extension view no longer exists"); |
| 539 | resolver->Reject(v8::Exception::Error(v8::String::NewFromUtf8( |
| 540 | GetIsolate(), "Extension view no longer exists"))); |
| 541 | return; |
| 542 | } |
| 543 | module_registry->LoadModule(GetIsolate(), |
| 544 | module_name, |
| 545 | base::Bind(&ModuleSystem::OnModuleLoaded, |
| 546 | weak_factory_.GetWeakPtr(), |
| 547 | base::Passed(&persistent_resolver))); |
| 548 | if (module_registry->available_modules().count(module_name) == 0) |
| 549 | LoadModule(module_name); |
| 550 | } |
| 551 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 552 | v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 553 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 554 | // Keep in order with the arguments in RequireForJsInner. |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 555 | v8::Handle<v8::String> left = v8::String::NewFromUtf8( |
| 556 | GetIsolate(), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 557 | "(function(define, require, requireNative, requireAsync, exports, " |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 558 | "console, privates," |
[email protected] | 0d775c7 | 2014-08-22 13:06:26 | [diff] [blame] | 559 | "$Array, $Function, $JSON, $Object, $RegExp, $String, $Error) {" |
[email protected] | 2a35687 | 2014-02-21 23:18:52 | [diff] [blame] | 560 | "'use strict';"); |
[email protected] | 9c47471e | 2013-11-28 14:41:21 | [diff] [blame] | 561 | v8::Handle<v8::String> right = v8::String::NewFromUtf8(GetIsolate(), "\n})"); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 562 | return handle_scope.Escape(v8::Local<v8::String>( |
| 563 | v8::String::Concat(left, v8::String::Concat(source, right)))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 564 | } |
| 565 | |
[email protected] | ca55729 | 2013-12-11 08:44:27 | [diff] [blame] | 566 | void ModuleSystem::Private(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 567 | CHECK_EQ(1, args.Length()); |
| 568 | CHECK(args[0]->IsObject()); |
kalman | ad8ddd0 | 2014-10-15 00:20:06 | [diff] [blame] | 569 | CHECK(!args[0]->IsNull()); |
[email protected] | ca55729 | 2013-12-11 08:44:27 | [diff] [blame] | 570 | v8::Local<v8::Object> obj = args[0].As<v8::Object>(); |
| 571 | v8::Local<v8::String> privates_key = |
| 572 | v8::String::NewFromUtf8(GetIsolate(), "privates"); |
| 573 | v8::Local<v8::Value> privates = obj->GetHiddenValue(privates_key); |
| 574 | if (privates.IsEmpty()) { |
[email protected] | 505ffde | 2013-12-19 15:47:13 | [diff] [blame] | 575 | privates = v8::Object::New(args.GetIsolate()); |
kalman | ad8ddd0 | 2014-10-15 00:20:06 | [diff] [blame] | 576 | if (privates.IsEmpty()) { |
| 577 | GetIsolate()->ThrowException( |
| 578 | v8::String::NewFromUtf8(GetIsolate(), "Failed to create privates")); |
| 579 | return; |
| 580 | } |
[email protected] | ca55729 | 2013-12-11 08:44:27 | [diff] [blame] | 581 | obj->SetHiddenValue(privates_key, privates); |
| 582 | } |
| 583 | args.GetReturnValue().Set(privates); |
| 584 | } |
| 585 | |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 586 | v8::Handle<v8::Value> ModuleSystem::LoadModule(const std::string& module_name) { |
| 587 | v8::EscapableHandleScope handle_scope(GetIsolate()); |
| 588 | v8::Context::Scope context_scope(context()->v8_context()); |
| 589 | |
| 590 | v8::Handle<v8::Value> source(GetSource(module_name)); |
| 591 | if (source.IsEmpty() || source->IsUndefined()) { |
| 592 | Fatal(context_, "No source for require(" + module_name + ")"); |
| 593 | return v8::Undefined(GetIsolate()); |
| 594 | } |
| 595 | v8::Handle<v8::String> wrapped_source( |
| 596 | WrapSource(v8::Handle<v8::String>::Cast(source))); |
| 597 | // Modules are wrapped in (function(){...}) so they always return functions. |
| 598 | v8::Handle<v8::Value> func_as_value = |
| 599 | RunString(wrapped_source, |
| 600 | v8::String::NewFromUtf8(GetIsolate(), module_name.c_str())); |
| 601 | if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
| 602 | Fatal(context_, "Bad source for require(" + module_name + ")"); |
| 603 | return v8::Undefined(GetIsolate()); |
| 604 | } |
| 605 | |
| 606 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
| 607 | |
| 608 | v8::Handle<v8::Object> define_object = v8::Object::New(GetIsolate()); |
| 609 | gin::ModuleRegistry::InstallGlobals(GetIsolate(), define_object); |
| 610 | |
| 611 | v8::Local<v8::Value> exports = v8::Object::New(GetIsolate()); |
| 612 | v8::Handle<v8::Object> natives(NewInstance()); |
| 613 | CHECK(!natives.IsEmpty()); // this can happen if v8 has issues |
| 614 | |
| 615 | // These must match the argument order in WrapSource. |
| 616 | v8::Handle<v8::Value> args[] = { |
| 617 | // AMD. |
| 618 | define_object->Get(v8::String::NewFromUtf8(GetIsolate(), "define")), |
| 619 | // CommonJS. |
| 620 | natives->Get(v8::String::NewFromUtf8( |
| 621 | GetIsolate(), "require", v8::String::kInternalizedString)), |
| 622 | natives->Get(v8::String::NewFromUtf8( |
| 623 | GetIsolate(), "requireNative", v8::String::kInternalizedString)), |
| 624 | natives->Get(v8::String::NewFromUtf8( |
| 625 | GetIsolate(), "requireAsync", v8::String::kInternalizedString)), |
| 626 | exports, |
| 627 | // Libraries that we magically expose to every module. |
| 628 | console::AsV8Object(), |
| 629 | natives->Get(v8::String::NewFromUtf8( |
| 630 | GetIsolate(), "privates", v8::String::kInternalizedString)), |
| 631 | // Each safe builtin. Keep in order with the arguments in WrapSource. |
| 632 | context_->safe_builtins()->GetArray(), |
| 633 | context_->safe_builtins()->GetFunction(), |
| 634 | context_->safe_builtins()->GetJSON(), |
| 635 | context_->safe_builtins()->GetObjekt(), |
| 636 | context_->safe_builtins()->GetRegExp(), |
| 637 | context_->safe_builtins()->GetString(), |
[email protected] | 0d775c7 | 2014-08-22 13:06:26 | [diff] [blame] | 638 | context_->safe_builtins()->GetError(), |
[email protected] | d9f51dad | 2014-07-09 05:39:38 | [diff] [blame] | 639 | }; |
| 640 | { |
| 641 | v8::TryCatch try_catch; |
| 642 | try_catch.SetCaptureMessage(true); |
| 643 | context_->CallFunction(func, arraysize(args), args); |
| 644 | if (try_catch.HasCaught()) { |
| 645 | HandleException(try_catch); |
| 646 | return v8::Undefined(GetIsolate()); |
| 647 | } |
| 648 | } |
| 649 | return handle_scope.Escape(exports); |
| 650 | } |
| 651 | |
| 652 | void ModuleSystem::OnDidAddPendingModule( |
| 653 | const std::string& id, |
| 654 | const std::vector<std::string>& dependencies) { |
| 655 | if (!source_map_->Contains(id)) |
| 656 | return; |
| 657 | |
| 658 | gin::ModuleRegistry* registry = |
| 659 | gin::ModuleRegistry::From(context_->v8_context()); |
| 660 | DCHECK(registry); |
| 661 | for (std::vector<std::string>::const_iterator it = dependencies.begin(); |
| 662 | it != dependencies.end(); |
| 663 | ++it) { |
| 664 | if (registry->available_modules().count(*it) == 0) |
| 665 | LoadModule(*it); |
| 666 | } |
| 667 | registry->AttemptToLoadMoreModules(GetIsolate()); |
| 668 | } |
| 669 | |
| 670 | void ModuleSystem::OnModuleLoaded( |
| 671 | scoped_ptr<v8::UniquePersistent<v8::Promise::Resolver> > resolver, |
| 672 | v8::Handle<v8::Value> value) { |
| 673 | if (!is_valid()) |
| 674 | return; |
| 675 | v8::HandleScope handle_scope(GetIsolate()); |
| 676 | v8::Handle<v8::Promise::Resolver> resolver_local( |
| 677 | v8::Local<v8::Promise::Resolver>::New(GetIsolate(), *resolver)); |
| 678 | resolver_local->Resolve(value); |
| 679 | } |
| 680 | |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 681 | } // namespace extensions |