[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 5 | #include "chrome/renderer/extensions/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] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 13 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 14 | #include "chrome/common/extensions/extension_messages.h" |
| 15 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 16 | #include "chrome/renderer/extensions/console.h" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 17 | #include "chrome/renderer/extensions/safe_builtins.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 18 | #include "content/public/renderer/render_view.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 19 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 20 | #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 21 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 22 | namespace extensions { |
| 23 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 26 | const char* kModuleSystem = "module_system"; |
| 27 | const char* kModuleName = "module_name"; |
| 28 | const char* kModuleField = "module_field"; |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 29 | const char* kModulesField = "modules"; |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 30 | |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 31 | // Prepends |extension_id| if it's non-empty to |message|. |
| 32 | std::string PrependExtensionID(const std::string& extension_id, |
| 33 | const std::string& message) { |
| 34 | std::string with_extension_id; |
| 35 | if (!extension_id.empty()) { |
| 36 | with_extension_id += "("; |
| 37 | with_extension_id += extension_id; |
| 38 | with_extension_id += ") "; |
| 39 | } |
| 40 | with_extension_id += message; |
| 41 | return with_extension_id; |
| 42 | } |
| 43 | |
| 44 | void Fatal(const std::string& extension_id, const std::string& message) { |
| 45 | // Only crash web pages in dev channel. |
| 46 | // Always crash extension processes, or when in single process mode (since |
| 47 | // typically it's used to debug renderer crashes). |
| 48 | bool is_fatal = false; |
| 49 | const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 50 | if (command_line->HasSwitch(switches::kExtensionProcess) || |
| 51 | command_line->HasSwitch(switches::kSingleProcess)) { |
| 52 | is_fatal = true; |
| 53 | } else { |
| 54 | // <= dev means dev, canary, and trunk. |
| 55 | is_fatal = Feature::GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV; |
| 56 | } |
| 57 | std::string with_extension_id = PrependExtensionID(extension_id, message); |
| 58 | if (is_fatal) |
| 59 | console::Fatal(v8::Context::GetCalling(), with_extension_id); |
| 60 | else |
| 61 | console::Error(v8::Context::GetCalling(), with_extension_id); |
| 62 | } |
| 63 | |
| 64 | void Warn(const std::string& extension_id, const std::string& message) { |
| 65 | console::Warn(v8::Context::GetCalling(), |
| 66 | PrependExtensionID(extension_id, message)); |
| 67 | } |
| 68 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 69 | // Default exception handler which logs the exception. |
| 70 | class DefaultExceptionHandler : public ModuleSystem::ExceptionHandler { |
| 71 | public: |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 72 | explicit DefaultExceptionHandler(const std::string& extension_id) |
| 73 | : extension_id_(extension_id) {} |
| 74 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 75 | // Fatally dumps the debug info from |try_catch| to the console. |
| 76 | // Make sure this is never used for exceptions that originate in external |
| 77 | // code! |
| 78 | virtual void HandleUncaughtException(const v8::TryCatch& try_catch) OVERRIDE { |
| 79 | v8::HandleScope handle_scope; |
| 80 | std::string stack_trace = "<stack trace unavailable>"; |
| 81 | if (!try_catch.StackTrace().IsEmpty()) { |
| 82 | v8::String::Utf8Value stack_value(try_catch.StackTrace()); |
| 83 | if (*stack_value) |
| 84 | stack_trace.assign(*stack_value, stack_value.length()); |
| 85 | else |
| 86 | stack_trace = "<could not convert stack trace to string>"; |
| 87 | } |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 88 | Fatal(extension_id_, |
| 89 | CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 90 | } |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 91 | |
| 92 | private: |
| 93 | std::string extension_id_; |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | std::string ModuleSystem::ExceptionHandler::CreateExceptionString( |
| 99 | const v8::TryCatch& try_catch) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 100 | v8::Handle<v8::Message> message(try_catch.Message()); |
| 101 | if (message.IsEmpty()) { |
| 102 | return "try_catch has no message"; |
| 103 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 104 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 105 | std::string resource_name = "<unknown resource>"; |
| 106 | if (!message->GetScriptResourceName().IsEmpty()) { |
| 107 | v8::String::Utf8Value resource_name_v8( |
| 108 | message->GetScriptResourceName()->ToString()); |
| 109 | resource_name.assign(*resource_name_v8, resource_name_v8.length()); |
| 110 | } |
| 111 | |
| 112 | std::string error_message = "<no error message>"; |
| 113 | if (!message->Get().IsEmpty()) { |
| 114 | v8::String::Utf8Value error_message_v8(message->Get()); |
| 115 | error_message.assign(*error_message_v8, error_message_v8.length()); |
| 116 | } |
| 117 | |
| 118 | return base::StringPrintf("%s:%d: %s", |
| 119 | resource_name.c_str(), |
| 120 | message->GetLineNumber(), |
| 121 | error_message.c_str()); |
| 122 | } |
| 123 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 124 | ModuleSystem::ModuleSystem(ChromeV8Context* context, |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 125 | SourceMap* source_map) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 126 | : ObjectBackedNativeHandler(context), |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 127 | context_(context), |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 128 | source_map_(source_map), |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 129 | natives_enabled_(0), |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 130 | exception_handler_( |
| 131 | new DefaultExceptionHandler(context->GetExtensionID())) { |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 132 | RouteFunction("require", |
| 133 | base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
| 134 | RouteFunction("requireNative", |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 135 | base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 136 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 137 | v8::Handle<v8::Object> global(context->v8_context()->Global()); |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 138 | global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); |
| 139 | global->SetHiddenValue(v8::String::New(kModuleSystem), |
| 140 | v8::External::New(this)); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ModuleSystem::~ModuleSystem() { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 144 | Invalidate(); |
| 145 | } |
| 146 | |
| 147 | void ModuleSystem::Invalidate() { |
| 148 | if (!is_valid()) |
| 149 | return; |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 150 | |
| 151 | // Clear the module system properties from the global context. It's polite, |
| 152 | // and we use this as a signal in lazy handlers that we no longer exist. |
| 153 | { |
| 154 | v8::HandleScope scope; |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 155 | v8::Handle<v8::Object> global = context()->v8_context()->Global(); |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 156 | global->DeleteHiddenValue(v8::String::New(kModulesField)); |
| 157 | global->DeleteHiddenValue(v8::String::New(kModuleSystem)); |
| 158 | } |
| 159 | |
| 160 | // Invalidate all of the successfully required handlers we own. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 161 | for (NativeHandlerMap::iterator it = native_handler_map_.begin(); |
| 162 | it != native_handler_map_.end(); ++it) { |
| 163 | it->second->Invalidate(); |
| 164 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 165 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 166 | ObjectBackedNativeHandler::Invalidate(); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 169 | ModuleSystem::NativesEnabledScope::NativesEnabledScope( |
| 170 | ModuleSystem* module_system) |
| 171 | : module_system_(module_system) { |
| 172 | module_system_->natives_enabled_++; |
| 173 | } |
| 174 | |
| 175 | ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 176 | module_system_->natives_enabled_--; |
| 177 | CHECK_GE(module_system_->natives_enabled_, 0); |
| 178 | } |
| 179 | |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 180 | void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 181 | exception_handler_->HandleUncaughtException(try_catch); |
[email protected] | 1c6189f | 2012-05-18 06:45:52 | [diff] [blame] | 182 | } |
| 183 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 184 | v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 185 | v8::HandleScope handle_scope; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 186 | return handle_scope.Close( |
| 187 | RequireForJsInner(v8::String::New(module_name.c_str()))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 190 | void ModuleSystem::RequireForJs( |
| 191 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 192 | v8::Handle<v8::String> module_name = args[0]->ToString(); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 193 | args.GetReturnValue().Set(RequireForJsInner(module_name)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
| 197 | v8::Handle<v8::String> module_name) { |
| 198 | v8::HandleScope handle_scope; |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 199 | v8::Context::Scope context_scope(context()->v8_context()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 200 | |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 201 | v8::Handle<v8::Object> global(context()->v8_context()->Global()); |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 202 | |
| 203 | // The module system might have been deleted. This can happen if a different |
| 204 | // context keeps a reference to us, but our frame is destroyed (e.g. |
| 205 | // background page keeps reference to chrome object in a closed popup). |
| 206 | v8::Handle<v8::Value> modules_value = |
| 207 | global->GetHiddenValue(v8::String::New(kModulesField)); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 208 | if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 209 | Warn(context_->GetExtensionID(), "Extension view no longer exists"); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 210 | return v8::Undefined(); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 211 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 212 | |
| 213 | v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 214 | v8::Handle<v8::Value> exports(modules->Get(module_name)); |
| 215 | if (!exports->IsUndefined()) |
| 216 | return handle_scope.Close(exports); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 217 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 218 | std::string module_name_str = *v8::String::AsciiValue(module_name); |
| 219 | v8::Handle<v8::Value> source(GetSource(module_name_str)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 220 | if (source.IsEmpty() || source->IsUndefined()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 221 | Fatal(context_->GetExtensionID(), |
| 222 | "No source for require(" + module_name_str + ")"); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 223 | return v8::Undefined(); |
| 224 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 225 | v8::Handle<v8::String> wrapped_source(WrapSource( |
| 226 | v8::Handle<v8::String>::Cast(source))); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 227 | // Modules are wrapped in (function(){...}) so they always return functions. |
| 228 | v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); |
| 229 | if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 230 | Fatal(context_->GetExtensionID(), |
| 231 | "Bad source for require(" + module_name_str + ")"); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 232 | return v8::Undefined(); |
| 233 | } |
| 234 | |
| 235 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 236 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 237 | exports = v8::Object::New(); |
| 238 | v8::Handle<v8::Object> natives(NewInstance()); |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 239 | CHECK(!natives.IsEmpty()); // this can happen if v8 has issues |
| 240 | |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 241 | // These must match the argument order in WrapSource. |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 242 | v8::Handle<v8::Value> args[] = { |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 243 | // CommonJS. |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 244 | natives->Get(v8::String::NewSymbol("require")), |
| 245 | natives->Get(v8::String::NewSymbol("requireNative")), |
| 246 | exports, |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 247 | // Each safe builtin. Keep in order with the arguments in WrapSource. |
| 248 | context_->safe_builtins()->GetArray(), |
| 249 | context_->safe_builtins()->GetFunction(), |
[email protected] | 5322973 | 2013-06-21 02:56:02 | [diff] [blame] | 250 | context_->safe_builtins()->GetJSON(), |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 251 | context_->safe_builtins()->GetObjekt(), |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 252 | context_->safe_builtins()->GetRegExp(), |
| 253 | context_->safe_builtins()->GetString(), |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 254 | }; |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 255 | { |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 256 | v8::TryCatch try_catch; |
[email protected] | 56a4bf83 | 2012-07-13 04:26:54 | [diff] [blame] | 257 | try_catch.SetCaptureMessage(true); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 258 | context_->CallFunction(func, arraysize(args), args); |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 259 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 260 | HandleException(try_catch); |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 261 | return v8::Undefined(); |
| 262 | } |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 263 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 264 | modules->Set(module_name, exports); |
| 265 | return handle_scope.Close(exports); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 266 | } |
| 267 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 268 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 269 | const std::string& module_name, |
| 270 | const std::string& method_name) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 271 | v8::HandleScope handle_scope; |
| 272 | v8::Handle<v8::Value> no_args; |
| 273 | return CallModuleMethod(module_name, method_name, 0, &no_args); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 277 | const std::string& module_name, |
| 278 | const std::string& method_name, |
| 279 | std::vector<v8::Handle<v8::Value> >* args) { |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 280 | return CallModuleMethod( |
| 281 | module_name, method_name, args->size(), vector_as_array(args)); |
| 282 | } |
| 283 | |
| 284 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 285 | const std::string& module_name, |
| 286 | const std::string& method_name, |
| 287 | int argc, |
| 288 | v8::Handle<v8::Value> argv[]) { |
| 289 | TRACE_EVENT2("v8", "v8.callModuleMethod", |
| 290 | "module_name", module_name, |
| 291 | "method_name", method_name); |
| 292 | |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 293 | v8::HandleScope handle_scope; |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 294 | v8::Context::Scope context_scope(context()->v8_context()); |
| 295 | |
| 296 | v8::Local<v8::Value> module; |
| 297 | { |
| 298 | NativesEnabledScope natives_enabled(this); |
| 299 | module = v8::Local<v8::Value>::New( |
| 300 | RequireForJsInner(v8::String::New(module_name.c_str()))); |
| 301 | } |
| 302 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 303 | if (module.IsEmpty() || !module->IsObject()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 304 | Fatal(context_->GetExtensionID(), |
| 305 | "Failed to get module " + module_name + " to call " + method_name); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 306 | return handle_scope.Close(v8::Undefined()); |
| 307 | } |
| 308 | |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 309 | v8::Local<v8::Value> value = |
| 310 | v8::Handle<v8::Object>::Cast(module)->Get( |
| 311 | v8::String::New(method_name.c_str())); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 312 | if (value.IsEmpty() || !value->IsFunction()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 313 | Fatal(context_->GetExtensionID(), |
| 314 | module_name + "." + method_name + " is not a function"); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 315 | return handle_scope.Close(v8::Undefined()); |
| 316 | } |
| 317 | |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 318 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 319 | v8::Local<v8::Value> result; |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 320 | { |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 321 | v8::TryCatch try_catch; |
| 322 | try_catch.SetCaptureMessage(true); |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 323 | result = context_->CallFunction(func, argc, argv); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 324 | if (try_catch.HasCaught()) |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 325 | HandleException(try_catch); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 326 | } |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 327 | return handle_scope.Close(result); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 330 | void ModuleSystem::RegisterNativeHandler(const std::string& name, |
| 331 | scoped_ptr<NativeHandler> native_handler) { |
| 332 | native_handler_map_[name] = |
| 333 | linked_ptr<NativeHandler>(native_handler.release()); |
| 334 | } |
| 335 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 336 | void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 337 | overridden_native_handlers_.insert(name); |
| 338 | } |
| 339 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 340 | void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
[email protected] | 9fb990b | 2012-03-13 02:09:41 | [diff] [blame] | 341 | v8::HandleScope handle_scope; |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 342 | RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 345 | // static |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 346 | void ModuleSystem::NativeLazyFieldGetter( |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 347 | v8::Local<v8::String> property, |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 348 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 349 | LazyFieldGetterInner(property, |
| 350 | info, |
| 351 | &ModuleSystem::RequireNativeFromString); |
| 352 | } |
| 353 | |
| 354 | // static |
| 355 | void ModuleSystem::LazyFieldGetter( |
| 356 | v8::Local<v8::String> property, |
| 357 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 358 | LazyFieldGetterInner(property, info, &ModuleSystem::Require); |
| 359 | } |
| 360 | |
| 361 | // static |
| 362 | void ModuleSystem::LazyFieldGetterInner( |
| 363 | v8::Local<v8::String> property, |
| 364 | const v8::PropertyCallbackInfo<v8::Value>& info, |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 365 | RequireFunction require_function) { |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 366 | CHECK(!info.Data().IsEmpty()); |
| 367 | CHECK(info.Data()->IsObject()); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 368 | v8::HandleScope handle_scope; |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 369 | v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 370 | // This context should be the same as context()->v8_context(). |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 371 | v8::Handle<v8::Context> context = parameters->CreationContext(); |
| 372 | v8::Handle<v8::Object> global(context->Global()); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 373 | v8::Handle<v8::Value> module_system_value = |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 374 | global->GetHiddenValue(v8::String::New(kModuleSystem)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 375 | if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 376 | // ModuleSystem has been deleted. |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 377 | // TODO(kalman): See comment in header file. |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 378 | Warn("", "Module system has been deleted, does extension view exist?"); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 379 | return; |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 380 | } |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 381 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 382 | ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 383 | v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 384 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 385 | std::string name = *v8::String::AsciiValue( |
| 386 | parameters->Get(v8::String::New(kModuleName))->ToString()); |
| 387 | |
| 388 | // Switch to our v8 context because we need functions created while running |
| 389 | // the require()d module to belong to our context, not the current one. |
| 390 | v8::Context::Scope context_scope(context); |
| 391 | NativesEnabledScope natives_enabled_scope(module_system); |
| 392 | |
| 393 | v8::TryCatch try_catch; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 394 | v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 395 | if (try_catch.HasCaught()) { |
| 396 | module_system->HandleException(try_catch); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 397 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 398 | } |
| 399 | if (module_value.IsEmpty() || !module_value->IsObject()) { |
| 400 | // require_function will have already logged this, we don't need to. |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 401 | return; |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 402 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 403 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 404 | v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 405 | v8::Handle<v8::String> field = |
| 406 | parameters->Get(v8::String::New(kModuleField))->ToString(); |
| 407 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 408 | if (!module->Has(field)) { |
| 409 | std::string field_str = *v8::String::AsciiValue(field); |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 410 | Fatal(module_system->context_->GetExtensionID(), |
| 411 | "Lazy require of " + name + "." + field_str + " did not " + |
| 412 | "set the " + field_str + " field"); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 413 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 414 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 415 | |
| 416 | v8::Local<v8::Value> new_field = module->Get(field); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 417 | if (try_catch.HasCaught()) { |
| 418 | module_system->HandleException(try_catch); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 419 | return; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // Ok for it to be undefined, among other things it's how bindings signify |
| 423 | // that the extension doesn't have permission to use them. |
| 424 | CHECK(!new_field.IsEmpty()); |
| 425 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 426 | // Delete the getter and set this field to |new_field| so the same object is |
| 427 | // returned every time a certain API is accessed. |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 428 | v8::Handle<v8::Object> object = info.This(); |
| 429 | object->Delete(property); |
| 430 | object->Set(property, new_field); |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 431 | info.GetReturnValue().Set(new_field); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 435 | const std::string& field, |
| 436 | const std::string& module_name, |
| 437 | const std::string& module_field) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 438 | SetLazyField(object, field, module_name, module_field, |
| 439 | &ModuleSystem::LazyFieldGetter); |
| 440 | } |
| 441 | |
| 442 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 443 | const std::string& field, |
| 444 | const std::string& module_name, |
| 445 | const std::string& module_field, |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 446 | v8::AccessorGetterCallback getter) { |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 447 | v8::HandleScope handle_scope; |
| 448 | v8::Handle<v8::Object> parameters = v8::Object::New(); |
| 449 | parameters->Set(v8::String::New(kModuleName), |
| 450 | v8::String::New(module_name.c_str())); |
| 451 | parameters->Set(v8::String::New(kModuleField), |
| 452 | v8::String::New(module_field.c_str())); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 453 | object->SetAccessor(v8::String::New(field.c_str()), |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 454 | getter, |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 455 | NULL, |
| 456 | parameters); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 457 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 458 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 459 | void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, |
| 460 | const std::string& field, |
| 461 | const std::string& module_name, |
| 462 | const std::string& module_field) { |
| 463 | SetLazyField(object, field, module_name, module_field, |
| 464 | &ModuleSystem::NativeLazyFieldGetter); |
| 465 | } |
| 466 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 467 | v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 468 | v8::Handle<v8::String> name) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 469 | v8::HandleScope handle_scope; |
[email protected] | 68e63ea1 | 2013-06-05 05:00:54 | [diff] [blame] | 470 | v8::Context::Scope context_scope(context()->v8_context()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 471 | |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 472 | WebKit::WebScopedMicrotaskSuppression suppression; |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 473 | v8::TryCatch try_catch; |
[email protected] | 56a4bf83 | 2012-07-13 04:26:54 | [diff] [blame] | 474 | try_catch.SetCaptureMessage(true); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 475 | v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
| 476 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 477 | HandleException(try_catch); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 478 | return v8::Undefined(); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 479 | } |
| 480 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 481 | v8::Handle<v8::Value> result = script->Run(); |
| 482 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 483 | HandleException(try_catch); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 484 | return v8::Undefined(); |
| 485 | } |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 486 | |
| 487 | return handle_scope.Close(result); |
[email protected] | 9e03ce2 | 2012-03-13 08:50:05 | [diff] [blame] | 488 | } |
| 489 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 490 | v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
[email protected] | 9e03ce2 | 2012-03-13 08:50:05 | [diff] [blame] | 491 | v8::HandleScope handle_scope; |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 492 | if (!source_map_->Contains(module_name)) |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 493 | return v8::Undefined(); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 494 | return handle_scope.Close(source_map_->GetSource(module_name)); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 495 | } |
| 496 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 497 | void ModuleSystem::RequireNative( |
| 498 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 499 | CHECK_EQ(1, args.Length()); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 500 | std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 501 | args.GetReturnValue().Set(RequireNativeFromString(native_name)); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
| 505 | const std::string& native_name) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 506 | if (natives_enabled_ == 0) { |
| 507 | // HACK: if in test throw exception so that we can test the natives-disabled |
| 508 | // logic; however, under normal circumstances, this is programmer error so |
| 509 | // we could crash. |
| 510 | if (exception_handler_) |
| 511 | return v8::ThrowException(v8::String::New("Natives disabled")); |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 512 | Fatal(context_->GetExtensionID(), |
| 513 | "Natives disabled for requireNative(" + native_name + ")"); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 514 | return v8::Undefined(); |
| 515 | } |
| 516 | |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 517 | if (overridden_native_handlers_.count(native_name) > 0u) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 518 | return RequireForJsInner(v8::String::New(native_name.c_str())); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 519 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 520 | NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 521 | if (i == native_handler_map_.end()) { |
[email protected] | ad6aa8f9 | 2013-06-22 15:34:16 | [diff] [blame^] | 522 | Fatal(context_->GetExtensionID(), |
| 523 | "Couldn't find native for requireNative(" + native_name + ")"); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 524 | return v8::Undefined(); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 525 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 526 | return i->second->NewInstance(); |
| 527 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 528 | |
| 529 | v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 530 | v8::HandleScope handle_scope; |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 531 | // Keep in order with the arguments in RequireForJsInner. |
[email protected] | b0c739f | 2013-02-20 09:21:40 | [diff] [blame] | 532 | v8::Handle<v8::String> left = v8::String::New( |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 533 | "(function(require, requireNative, exports," |
[email protected] | 31bbfd7 | 2013-06-22 02:35:54 | [diff] [blame] | 534 | "$Array, $Function, $JSON, $Object, $RegExp, $String) {" |
[email protected] | 295890bd | 2013-06-15 10:52:45 | [diff] [blame] | 535 | "'use strict';"); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 536 | v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 537 | return handle_scope.Close( |
| 538 | v8::String::Concat(left, v8::String::Concat(source, right))); |
| 539 | } |
| 540 | |
[email protected] | 4e008e17 | 2013-06-13 20:15:48 | [diff] [blame] | 541 | } // namespace extensions |