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