[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] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 8 | #include "base/stl_util.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 9 | #include "base/string_util.h" |
| 10 | #include "base/stringprintf.h" |
| 11 | #include "chrome/common/extensions/extension_messages.h" |
| 12 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 13 | #include "chrome/renderer/extensions/console.h" |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 14 | #include "content/public/renderer/render_view.h" |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedMicrotaskSuppression.h" |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 16 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 17 | namespace extensions { |
| 18 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 19 | namespace { |
| 20 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 21 | const char* kModuleSystem = "module_system"; |
| 22 | const char* kModuleName = "module_name"; |
| 23 | const char* kModuleField = "module_field"; |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 24 | const char* kModulesField = "modules"; |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 25 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 26 | // Formats |try_catch| as a nice string. |
| 27 | std::string CreateExceptionString(const v8::TryCatch& try_catch) { |
| 28 | v8::Handle<v8::Message> message(try_catch.Message()); |
| 29 | if (message.IsEmpty()) { |
| 30 | return "try_catch has no message"; |
| 31 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 32 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 33 | std::string resource_name = "<unknown resource>"; |
| 34 | if (!message->GetScriptResourceName().IsEmpty()) { |
| 35 | v8::String::Utf8Value resource_name_v8( |
| 36 | message->GetScriptResourceName()->ToString()); |
| 37 | resource_name.assign(*resource_name_v8, resource_name_v8.length()); |
| 38 | } |
| 39 | |
| 40 | std::string error_message = "<no error message>"; |
| 41 | if (!message->Get().IsEmpty()) { |
| 42 | v8::String::Utf8Value error_message_v8(message->Get()); |
| 43 | error_message.assign(*error_message_v8, error_message_v8.length()); |
| 44 | } |
| 45 | |
| 46 | return base::StringPrintf("%s:%d: %s", |
| 47 | resource_name.c_str(), |
| 48 | message->GetLineNumber(), |
| 49 | error_message.c_str()); |
| 50 | } |
| 51 | |
| 52 | // Fatally dumps the debug info from |try_catch| to the console. |
| 53 | // Don't use this for logging exceptions that might originate in external code! |
| 54 | void DumpException(const v8::TryCatch& try_catch) { |
| 55 | v8::HandleScope handle_scope; |
| 56 | |
| 57 | std::string stack_trace = "<stack trace unavailable>"; |
| 58 | if (!try_catch.StackTrace().IsEmpty()) { |
| 59 | v8::String::Utf8Value stack_value(try_catch.StackTrace()); |
| 60 | if (*stack_value) |
| 61 | stack_trace.assign(*stack_value, stack_value.length()); |
| 62 | else |
| 63 | stack_trace = "<could not convert stack trace to string>"; |
| 64 | } |
| 65 | |
| 66 | console::Fatal(v8::Context::GetCalling(), |
| 67 | CreateExceptionString(try_catch) + "{" + stack_trace + "}"); |
| 68 | } |
| 69 | |
| 70 | } // namespace |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 71 | |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 72 | ModuleSystem::ModuleSystem(v8::Handle<v8::Context> context, |
| 73 | SourceMap* source_map) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 74 | : ObjectBackedNativeHandler(context), |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 75 | source_map_(source_map), |
[email protected] | e3e77872 | 2013-03-03 23:28:03 | [diff] [blame] | 76 | natives_enabled_(0) { |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 77 | RouteFunction("require", |
| 78 | base::Bind(&ModuleSystem::RequireForJs, base::Unretained(this))); |
| 79 | RouteFunction("requireNative", |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 80 | base::Bind(&ModuleSystem::RequireNative, base::Unretained(this))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 81 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 82 | v8::Handle<v8::Object> global(context->Global()); |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 83 | global->SetHiddenValue(v8::String::New(kModulesField), v8::Object::New()); |
| 84 | global->SetHiddenValue(v8::String::New(kModuleSystem), |
| 85 | v8::External::New(this)); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | ModuleSystem::~ModuleSystem() { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 89 | Invalidate(); |
| 90 | } |
| 91 | |
| 92 | void ModuleSystem::Invalidate() { |
| 93 | if (!is_valid()) |
| 94 | return; |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 95 | |
| 96 | // Clear the module system properties from the global context. It's polite, |
| 97 | // and we use this as a signal in lazy handlers that we no longer exist. |
| 98 | { |
| 99 | v8::HandleScope scope; |
| 100 | v8::Handle<v8::Object> global = v8_context()->Global(); |
| 101 | global->DeleteHiddenValue(v8::String::New(kModulesField)); |
| 102 | global->DeleteHiddenValue(v8::String::New(kModuleSystem)); |
| 103 | } |
| 104 | |
| 105 | // Invalidate all of the successfully required handlers we own. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 106 | for (NativeHandlerMap::iterator it = native_handler_map_.begin(); |
| 107 | it != native_handler_map_.end(); ++it) { |
| 108 | it->second->Invalidate(); |
| 109 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 110 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 111 | ObjectBackedNativeHandler::Invalidate(); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 112 | } |
| 113 | |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 114 | ModuleSystem::NativesEnabledScope::NativesEnabledScope( |
| 115 | ModuleSystem* module_system) |
| 116 | : module_system_(module_system) { |
| 117 | module_system_->natives_enabled_++; |
| 118 | } |
| 119 | |
| 120 | ModuleSystem::NativesEnabledScope::~NativesEnabledScope() { |
| 121 | module_system_->natives_enabled_--; |
| 122 | CHECK_GE(module_system_->natives_enabled_, 0); |
| 123 | } |
| 124 | |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 125 | void ModuleSystem::HandleException(const v8::TryCatch& try_catch) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 126 | if (exception_handler_) |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 127 | exception_handler_->HandleUncaughtException(); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 128 | else |
| 129 | DumpException(try_catch); |
[email protected] | 1c6189f | 2012-05-18 06:45:52 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 132 | v8::Handle<v8::Value> ModuleSystem::Require(const std::string& module_name) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 133 | v8::HandleScope handle_scope; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 134 | return handle_scope.Close( |
| 135 | RequireForJsInner(v8::String::New(module_name.c_str()))); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | v8::Handle<v8::Value> ModuleSystem::RequireForJs(const v8::Arguments& args) { |
| 139 | v8::HandleScope handle_scope; |
| 140 | v8::Handle<v8::String> module_name = args[0]->ToString(); |
| 141 | return handle_scope.Close(RequireForJsInner(module_name)); |
| 142 | } |
| 143 | |
| 144 | v8::Handle<v8::Value> ModuleSystem::RequireForJsInner( |
| 145 | v8::Handle<v8::String> module_name) { |
| 146 | v8::HandleScope handle_scope; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 147 | v8::Context::Scope context_scope(v8_context()); |
| 148 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 149 | v8::Handle<v8::Object> global(v8_context()->Global()); |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 150 | |
| 151 | // The module system might have been deleted. This can happen if a different |
| 152 | // context keeps a reference to us, but our frame is destroyed (e.g. |
| 153 | // background page keeps reference to chrome object in a closed popup). |
| 154 | v8::Handle<v8::Value> modules_value = |
| 155 | global->GetHiddenValue(v8::String::New(kModulesField)); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 156 | if (modules_value.IsEmpty() || modules_value->IsUndefined()) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 157 | console::Warn(v8::Context::GetCalling(), "Extension view no longer exists"); |
| 158 | return v8::Undefined(); |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 159 | } |
[email protected] | 07953224 | 2013-03-12 06:01:10 | [diff] [blame] | 160 | |
| 161 | v8::Handle<v8::Object> modules(v8::Handle<v8::Object>::Cast(modules_value)); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 162 | v8::Handle<v8::Value> exports(modules->Get(module_name)); |
| 163 | if (!exports->IsUndefined()) |
| 164 | return handle_scope.Close(exports); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 165 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 166 | std::string module_name_str = *v8::String::AsciiValue(module_name); |
| 167 | v8::Handle<v8::Value> source(GetSource(module_name_str)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 168 | if (source.IsEmpty() || source->IsUndefined()) { |
| 169 | console::Error(v8::Context::GetCalling(), |
| 170 | "No source for require(" + module_name_str + ")"); |
| 171 | return v8::Undefined(); |
| 172 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 173 | v8::Handle<v8::String> wrapped_source(WrapSource( |
| 174 | v8::Handle<v8::String>::Cast(source))); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 175 | // Modules are wrapped in (function(){...}) so they always return functions. |
| 176 | v8::Handle<v8::Value> func_as_value = RunString(wrapped_source, module_name); |
| 177 | if (func_as_value.IsEmpty() || func_as_value->IsUndefined()) { |
| 178 | console::Error(v8::Context::GetCalling(), |
| 179 | "Bad source for require(" + module_name_str + ")"); |
| 180 | return v8::Undefined(); |
| 181 | } |
| 182 | |
| 183 | v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(func_as_value); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 184 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 185 | exports = v8::Object::New(); |
| 186 | v8::Handle<v8::Object> natives(NewInstance()); |
| 187 | v8::Handle<v8::Value> args[] = { |
| 188 | natives->Get(v8::String::NewSymbol("require")), |
| 189 | natives->Get(v8::String::NewSymbol("requireNative")), |
| 190 | exports, |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 191 | }; |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 192 | { |
| 193 | WebKit::WebScopedMicrotaskSuppression suppression; |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 194 | v8::TryCatch try_catch; |
[email protected] | 56a4bf83 | 2012-07-13 04:26:54 | [diff] [blame] | 195 | try_catch.SetCaptureMessage(true); |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 196 | func->Call(global, 3, args); |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 197 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 198 | HandleException(try_catch); |
[email protected] | 452b36f | 2012-07-12 05:27:54 | [diff] [blame] | 199 | return v8::Undefined(); |
| 200 | } |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 201 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 202 | modules->Set(module_name, exports); |
| 203 | return handle_scope.Close(exports); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 206 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 207 | const std::string& module_name, |
| 208 | const std::string& method_name) { |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 209 | std::vector<v8::Handle<v8::Value> > args; |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 210 | return CallModuleMethod(module_name, method_name, &args); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | v8::Local<v8::Value> ModuleSystem::CallModuleMethod( |
| 214 | const std::string& module_name, |
| 215 | const std::string& method_name, |
| 216 | std::vector<v8::Handle<v8::Value> >* args) { |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 217 | v8::HandleScope handle_scope; |
| 218 | v8::Local<v8::Value> module = |
| 219 | v8::Local<v8::Value>::New( |
| 220 | RequireForJsInner(v8::String::New(module_name.c_str()))); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 221 | if (module.IsEmpty() || !module->IsObject()) { |
| 222 | console::Error( |
| 223 | v8::Context::GetCalling(), |
| 224 | "Failed to get module " + module_name + " to call " + method_name); |
| 225 | return handle_scope.Close(v8::Undefined()); |
| 226 | } |
| 227 | |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 228 | v8::Local<v8::Value> value = |
| 229 | v8::Handle<v8::Object>::Cast(module)->Get( |
| 230 | v8::String::New(method_name.c_str())); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 231 | if (value.IsEmpty() || !value->IsFunction()) { |
| 232 | console::Error(v8::Context::GetCalling(), |
| 233 | module_name + "." + method_name + " is not a function"); |
| 234 | return handle_scope.Close(v8::Undefined()); |
| 235 | } |
| 236 | |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 237 | v8::Handle<v8::Function> func = |
| 238 | v8::Handle<v8::Function>::Cast(value); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 239 | v8::Handle<v8::Object> global(v8_context()->Global()); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 240 | v8::Local<v8::Value> result; |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 241 | { |
| 242 | WebKit::WebScopedMicrotaskSuppression suppression; |
| 243 | v8::TryCatch try_catch; |
| 244 | try_catch.SetCaptureMessage(true); |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 245 | result = func->Call(global, args->size(), vector_as_array(args)); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 246 | if (try_catch.HasCaught()) |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 247 | HandleException(try_catch); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 248 | } |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 249 | return handle_scope.Close(result); |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 252 | void ModuleSystem::RegisterNativeHandler(const std::string& name, |
| 253 | scoped_ptr<NativeHandler> native_handler) { |
| 254 | native_handler_map_[name] = |
| 255 | linked_ptr<NativeHandler>(native_handler.release()); |
| 256 | } |
| 257 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 258 | void ModuleSystem::OverrideNativeHandlerForTest(const std::string& name) { |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 259 | overridden_native_handlers_.insert(name); |
| 260 | } |
| 261 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 262 | void ModuleSystem::RunString(const std::string& code, const std::string& name) { |
[email protected] | 9fb990b | 2012-03-13 02:09:41 | [diff] [blame] | 263 | v8::HandleScope handle_scope; |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 264 | RunString(v8::String::New(code.c_str()), v8::String::New(name.c_str())); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 267 | // static |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 268 | v8::Handle<v8::Value> ModuleSystem::NativeLazyFieldGetter( |
| 269 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
| 270 | return LazyFieldGetterInner(property, |
| 271 | info, |
| 272 | &ModuleSystem::RequireNativeFromString); |
| 273 | } |
| 274 | |
| 275 | // static |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 276 | v8::Handle<v8::Value> ModuleSystem::LazyFieldGetter( |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 277 | v8::Local<v8::String> property, const v8::AccessorInfo& info) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 278 | return LazyFieldGetterInner(property, info, &ModuleSystem::Require); |
| 279 | } |
| 280 | |
| 281 | // static |
| 282 | v8::Handle<v8::Value> ModuleSystem::LazyFieldGetterInner( |
| 283 | v8::Local<v8::String> property, |
| 284 | const v8::AccessorInfo& info, |
| 285 | RequireFunction require_function) { |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 286 | CHECK(!info.Data().IsEmpty()); |
| 287 | CHECK(info.Data()->IsObject()); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 288 | v8::HandleScope handle_scope; |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 289 | v8::Handle<v8::Object> parameters = v8::Handle<v8::Object>::Cast(info.Data()); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 290 | // This context should be the same as v8_context(). |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 291 | v8::Handle<v8::Context> context = parameters->CreationContext(); |
| 292 | v8::Handle<v8::Object> global(context->Global()); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 293 | v8::Handle<v8::Value> module_system_value = |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 294 | global->GetHiddenValue(v8::String::New(kModuleSystem)); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 295 | if (module_system_value.IsEmpty() || !module_system_value->IsExternal()) { |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 296 | // ModuleSystem has been deleted. |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 297 | // TODO(kalman): See comment in header file. |
| 298 | console::Warn(v8::Context::GetCalling(), |
| 299 | "Module system has been deleted, does extension view exist?"); |
[email protected] | bad9a5f | 2012-04-13 19:16:54 | [diff] [blame] | 300 | return v8::Undefined(); |
| 301 | } |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 302 | |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 303 | ModuleSystem* module_system = static_cast<ModuleSystem*>( |
| 304 | v8::Handle<v8::External>::Cast(module_system_value)->Value()); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 305 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 306 | std::string name = *v8::String::AsciiValue( |
| 307 | parameters->Get(v8::String::New(kModuleName))->ToString()); |
| 308 | |
| 309 | // Switch to our v8 context because we need functions created while running |
| 310 | // the require()d module to belong to our context, not the current one. |
| 311 | v8::Context::Scope context_scope(context); |
| 312 | NativesEnabledScope natives_enabled_scope(module_system); |
| 313 | |
| 314 | v8::TryCatch try_catch; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 315 | v8::Handle<v8::Value> module_value = (module_system->*require_function)(name); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 316 | if (try_catch.HasCaught()) { |
| 317 | module_system->HandleException(try_catch); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 318 | return v8::Undefined(); |
| 319 | } |
| 320 | if (module_value.IsEmpty() || !module_value->IsObject()) { |
| 321 | // require_function will have already logged this, we don't need to. |
| 322 | return v8::Undefined(); |
[email protected] | b6aad81c | 2012-03-27 08:45:15 | [diff] [blame] | 323 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 324 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 325 | v8::Handle<v8::Object> module = v8::Handle<v8::Object>::Cast(module_value); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 326 | v8::Handle<v8::String> field = |
| 327 | parameters->Get(v8::String::New(kModuleField))->ToString(); |
| 328 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 329 | if (!module->Has(field)) { |
| 330 | std::string field_str = *v8::String::AsciiValue(field); |
| 331 | console::Fatal(v8::Context::GetCalling(), |
| 332 | "Lazy require of " + name + "." + field_str + " did not " + |
| 333 | "set the " + field_str + " field"); |
| 334 | return v8::Undefined(); |
| 335 | } |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 336 | |
| 337 | v8::Local<v8::Value> new_field = module->Get(field); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 338 | if (try_catch.HasCaught()) { |
| 339 | module_system->HandleException(try_catch); |
| 340 | return v8::Undefined(); |
| 341 | } |
| 342 | |
| 343 | // Ok for it to be undefined, among other things it's how bindings signify |
| 344 | // that the extension doesn't have permission to use them. |
| 345 | CHECK(!new_field.IsEmpty()); |
| 346 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 347 | // Delete the getter and set this field to |new_field| so the same object is |
| 348 | // returned every time a certain API is accessed. |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 349 | v8::Handle<v8::Object> object = info.This(); |
| 350 | object->Delete(property); |
| 351 | object->Set(property, new_field); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 352 | return handle_scope.Close(new_field); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 356 | const std::string& field, |
| 357 | const std::string& module_name, |
| 358 | const std::string& module_field) { |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 359 | SetLazyField(object, field, module_name, module_field, |
| 360 | &ModuleSystem::LazyFieldGetter); |
| 361 | } |
| 362 | |
| 363 | void ModuleSystem::SetLazyField(v8::Handle<v8::Object> object, |
| 364 | const std::string& field, |
| 365 | const std::string& module_name, |
| 366 | const std::string& module_field, |
| 367 | v8::AccessorGetter getter) { |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 368 | v8::HandleScope handle_scope; |
| 369 | v8::Handle<v8::Object> parameters = v8::Object::New(); |
| 370 | parameters->Set(v8::String::New(kModuleName), |
| 371 | v8::String::New(module_name.c_str())); |
| 372 | parameters->Set(v8::String::New(kModuleField), |
| 373 | v8::String::New(module_field.c_str())); |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 374 | object->SetAccessor(v8::String::New(field.c_str()), |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 375 | getter, |
[email protected] | 561ddce | 2012-03-22 01:18:55 | [diff] [blame] | 376 | NULL, |
| 377 | parameters); |
[email protected] | cc045771 | 2012-03-21 03:56:38 | [diff] [blame] | 378 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 379 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 380 | void ModuleSystem::SetNativeLazyField(v8::Handle<v8::Object> object, |
| 381 | const std::string& field, |
| 382 | const std::string& module_name, |
| 383 | const std::string& module_field) { |
| 384 | SetLazyField(object, field, module_name, module_field, |
| 385 | &ModuleSystem::NativeLazyFieldGetter); |
| 386 | } |
| 387 | |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 388 | v8::Handle<v8::Value> ModuleSystem::RunString(v8::Handle<v8::String> code, |
| 389 | v8::Handle<v8::String> name) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 390 | v8::HandleScope handle_scope; |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 391 | |
[email protected] | 11d032ba | 2012-04-09 20:39:51 | [diff] [blame] | 392 | WebKit::WebScopedMicrotaskSuppression suppression; |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 393 | v8::TryCatch try_catch; |
[email protected] | 56a4bf83 | 2012-07-13 04:26:54 | [diff] [blame] | 394 | try_catch.SetCaptureMessage(true); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 395 | v8::Handle<v8::Script> script(v8::Script::New(code, name)); |
| 396 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 397 | HandleException(try_catch); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 398 | return v8::Undefined(); |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 401 | v8::Handle<v8::Value> result = script->Run(); |
| 402 | if (try_catch.HasCaught()) { |
[email protected] | 14411494 | 2012-12-04 07:23:23 | [diff] [blame] | 403 | HandleException(try_catch); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 404 | return v8::Undefined(); |
| 405 | } |
[email protected] | 7c95c1a8 | 2012-04-17 02:11:33 | [diff] [blame] | 406 | |
| 407 | return handle_scope.Close(result); |
[email protected] | 9e03ce2 | 2012-03-13 08:50:05 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | d266361 | 2013-03-17 09:25:56 | [diff] [blame] | 410 | v8::Handle<v8::Value> ModuleSystem::GetSource(const std::string& module_name) { |
[email protected] | 9e03ce2 | 2012-03-13 08:50:05 | [diff] [blame] | 411 | v8::HandleScope handle_scope; |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 412 | if (!source_map_->Contains(module_name)) |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 413 | return v8::Undefined(); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 414 | return handle_scope.Close(source_map_->GetSource(module_name)); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 415 | } |
| 416 | |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 417 | v8::Handle<v8::Value> ModuleSystem::RequireNative(const v8::Arguments& args) { |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 418 | CHECK_EQ(1, args.Length()); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 419 | std::string native_name = *v8::String::AsciiValue(args[0]->ToString()); |
| 420 | return RequireNativeFromString(native_name); |
| 421 | } |
| 422 | |
| 423 | v8::Handle<v8::Value> ModuleSystem::RequireNativeFromString( |
| 424 | const std::string& native_name) { |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 425 | if (natives_enabled_ == 0) { |
| 426 | // HACK: if in test throw exception so that we can test the natives-disabled |
| 427 | // logic; however, under normal circumstances, this is programmer error so |
| 428 | // we could crash. |
| 429 | if (exception_handler_) |
| 430 | return v8::ThrowException(v8::String::New("Natives disabled")); |
| 431 | console::Fatal(v8::Context::GetCalling(), |
| 432 | "Natives disabled for requireNative(" + native_name + ")"); |
| 433 | return v8::Undefined(); |
| 434 | } |
| 435 | |
[email protected] | 11844fa | 2012-05-10 00:35:59 | [diff] [blame] | 436 | if (overridden_native_handlers_.count(native_name) > 0u) |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 437 | return RequireForJsInner(v8::String::New(native_name.c_str())); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 438 | |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 439 | NativeHandlerMap::iterator i = native_handler_map_.find(native_name); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 440 | if (i == native_handler_map_.end()) { |
| 441 | console::Fatal( |
| 442 | v8::Context::GetCalling(), |
| 443 | "Couldn't find native for requireNative(" + native_name + ")"); |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 444 | return v8::Undefined(); |
[email protected] | 95ee77da | 2013-03-19 21:11:11 | [diff] [blame] | 445 | } |
[email protected] | 58e1045 | 2012-02-22 03:34:45 | [diff] [blame] | 446 | return i->second->NewInstance(); |
| 447 | } |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 448 | |
| 449 | v8::Handle<v8::String> ModuleSystem::WrapSource(v8::Handle<v8::String> source) { |
| 450 | v8::HandleScope handle_scope; |
[email protected] | b0c739f | 2013-02-20 09:21:40 | [diff] [blame] | 451 | v8::Handle<v8::String> left = v8::String::New( |
| 452 | "(function(require, requireNative, exports) {'use strict';"); |
[email protected] | ecde191 | 2012-03-16 06:25:31 | [diff] [blame] | 453 | v8::Handle<v8::String> right = v8::String::New("\n})"); |
| 454 | return handle_scope.Close( |
| 455 | v8::String::Concat(left, v8::String::Concat(source, right))); |
| 456 | } |
| 457 | |
[email protected] | 3c6babd | 2012-08-28 03:17:29 | [diff] [blame] | 458 | } // extensions |