Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 sw=2 et tw=78: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /** |
michael@0 | 8 | * This is not a generated file. It contains common utility functions |
michael@0 | 9 | * invoked from the JavaScript code generated from IDL interfaces. |
michael@0 | 10 | * The goal of the utility functions is to cut down on the size of |
michael@0 | 11 | * the generated code itself. |
michael@0 | 12 | */ |
michael@0 | 13 | |
michael@0 | 14 | #include "nsJSUtils.h" |
michael@0 | 15 | #include "jsapi.h" |
michael@0 | 16 | #include "js/OldDebugAPI.h" |
michael@0 | 17 | #include "jsfriendapi.h" |
michael@0 | 18 | #include "nsIScriptContext.h" |
michael@0 | 19 | #include "nsIScriptGlobalObject.h" |
michael@0 | 20 | #include "nsIXPConnect.h" |
michael@0 | 21 | #include "nsCOMPtr.h" |
michael@0 | 22 | #include "nsIScriptSecurityManager.h" |
michael@0 | 23 | #include "nsPIDOMWindow.h" |
michael@0 | 24 | #include "GeckoProfiler.h" |
michael@0 | 25 | #include "nsDOMJSUtils.h" // for GetScriptContextFromJSContext |
michael@0 | 26 | #include "nsJSPrincipals.h" |
michael@0 | 27 | #include "xpcpublic.h" |
michael@0 | 28 | #include "nsContentUtils.h" |
michael@0 | 29 | #include "nsGlobalWindow.h" |
michael@0 | 30 | |
michael@0 | 31 | bool |
michael@0 | 32 | nsJSUtils::GetCallingLocation(JSContext* aContext, const char* *aFilename, |
michael@0 | 33 | uint32_t* aLineno) |
michael@0 | 34 | { |
michael@0 | 35 | JS::AutoFilename filename; |
michael@0 | 36 | unsigned lineno = 0; |
michael@0 | 37 | |
michael@0 | 38 | if (!JS::DescribeScriptedCaller(aContext, &filename, &lineno)) { |
michael@0 | 39 | return false; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | *aFilename = filename.get(); |
michael@0 | 43 | *aLineno = lineno; |
michael@0 | 44 | |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | nsIScriptGlobalObject * |
michael@0 | 49 | nsJSUtils::GetStaticScriptGlobal(JSObject* aObj) |
michael@0 | 50 | { |
michael@0 | 51 | if (!aObj) |
michael@0 | 52 | return nullptr; |
michael@0 | 53 | return xpc::WindowGlobalOrNull(aObj); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | nsIScriptContext * |
michael@0 | 57 | nsJSUtils::GetStaticScriptContext(JSObject* aObj) |
michael@0 | 58 | { |
michael@0 | 59 | nsIScriptGlobalObject *nativeGlobal = GetStaticScriptGlobal(aObj); |
michael@0 | 60 | if (!nativeGlobal) |
michael@0 | 61 | return nullptr; |
michael@0 | 62 | |
michael@0 | 63 | return nativeGlobal->GetScriptContext(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsIScriptGlobalObject * |
michael@0 | 67 | nsJSUtils::GetDynamicScriptGlobal(JSContext* aContext) |
michael@0 | 68 | { |
michael@0 | 69 | nsIScriptContext *scriptCX = GetDynamicScriptContext(aContext); |
michael@0 | 70 | if (!scriptCX) |
michael@0 | 71 | return nullptr; |
michael@0 | 72 | return scriptCX->GetGlobalObject(); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | nsIScriptContext * |
michael@0 | 76 | nsJSUtils::GetDynamicScriptContext(JSContext *aContext) |
michael@0 | 77 | { |
michael@0 | 78 | return GetScriptContextFromJSContext(aContext); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | uint64_t |
michael@0 | 82 | nsJSUtils::GetCurrentlyRunningCodeInnerWindowID(JSContext *aContext) |
michael@0 | 83 | { |
michael@0 | 84 | if (!aContext) |
michael@0 | 85 | return 0; |
michael@0 | 86 | |
michael@0 | 87 | uint64_t innerWindowID = 0; |
michael@0 | 88 | |
michael@0 | 89 | JSObject *jsGlobal = JS::CurrentGlobalOrNull(aContext); |
michael@0 | 90 | if (jsGlobal) { |
michael@0 | 91 | nsIScriptGlobalObject *scriptGlobal = GetStaticScriptGlobal(jsGlobal); |
michael@0 | 92 | if (scriptGlobal) { |
michael@0 | 93 | nsCOMPtr<nsPIDOMWindow> win = do_QueryInterface(scriptGlobal); |
michael@0 | 94 | if (win) |
michael@0 | 95 | innerWindowID = win->WindowID(); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | return innerWindowID; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | void |
michael@0 | 103 | nsJSUtils::ReportPendingException(JSContext *aContext) |
michael@0 | 104 | { |
michael@0 | 105 | if (JS_IsExceptionPending(aContext)) { |
michael@0 | 106 | bool saved = JS_SaveFrameChain(aContext); |
michael@0 | 107 | { |
michael@0 | 108 | // JS_SaveFrameChain set the compartment of aContext to null, so we need |
michael@0 | 109 | // to enter a compartment. The question is, which one? We don't want to |
michael@0 | 110 | // enter the original compartment of aContext (or the compartment of the |
michael@0 | 111 | // current exception on aContext, for that matter) because when we |
michael@0 | 112 | // JS_ReportPendingException the JS engine can try to duck-type the |
michael@0 | 113 | // exception and produce a JSErrorReport. It will then pass that |
michael@0 | 114 | // JSErrorReport to the error reporter on aContext, which might expose |
michael@0 | 115 | // information from it to script via onerror handlers. So it's very |
michael@0 | 116 | // important that the duck typing happen in the same compartment as the |
michael@0 | 117 | // onerror handler. In practice, that's the compartment of the window (or |
michael@0 | 118 | // otherwise default global) of aContext, so use that here. |
michael@0 | 119 | nsIScriptContext* scx = GetScriptContextFromJSContext(aContext); |
michael@0 | 120 | JS::Rooted<JSObject*> scope(aContext); |
michael@0 | 121 | scope = scx ? scx->GetWindowProxy() |
michael@0 | 122 | : js::DefaultObjectForContextOrNull(aContext); |
michael@0 | 123 | if (!scope) { |
michael@0 | 124 | // The SafeJSContext has no default object associated with it. |
michael@0 | 125 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 126 | MOZ_ASSERT(aContext == nsContentUtils::GetSafeJSContext()); |
michael@0 | 127 | scope = xpc::GetSafeJSContextGlobal(); |
michael@0 | 128 | } |
michael@0 | 129 | JSAutoCompartment ac(aContext, scope); |
michael@0 | 130 | JS_ReportPendingException(aContext); |
michael@0 | 131 | } |
michael@0 | 132 | if (saved) { |
michael@0 | 133 | JS_RestoreFrameChain(aContext); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | nsresult |
michael@0 | 139 | nsJSUtils::CompileFunction(JSContext* aCx, |
michael@0 | 140 | JS::Handle<JSObject*> aTarget, |
michael@0 | 141 | JS::CompileOptions& aOptions, |
michael@0 | 142 | const nsACString& aName, |
michael@0 | 143 | uint32_t aArgCount, |
michael@0 | 144 | const char** aArgArray, |
michael@0 | 145 | const nsAString& aBody, |
michael@0 | 146 | JSObject** aFunctionObject) |
michael@0 | 147 | { |
michael@0 | 148 | MOZ_ASSERT(js::GetEnterCompartmentDepth(aCx) > 0); |
michael@0 | 149 | MOZ_ASSERT_IF(aTarget, js::IsObjectInContextCompartment(aTarget, aCx)); |
michael@0 | 150 | MOZ_ASSERT_IF(aOptions.versionSet, aOptions.version != JSVERSION_UNKNOWN); |
michael@0 | 151 | mozilla::DebugOnly<nsIScriptContext*> ctx = GetScriptContextFromJSContext(aCx); |
michael@0 | 152 | MOZ_ASSERT_IF(ctx, ctx->IsContextInitialized()); |
michael@0 | 153 | |
michael@0 | 154 | // Do the junk Gecko is supposed to do before calling into JSAPI. |
michael@0 | 155 | if (aTarget) { |
michael@0 | 156 | JS::ExposeObjectToActiveJS(aTarget); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | // Compile. |
michael@0 | 160 | JSFunction* fun = JS::CompileFunction(aCx, aTarget, aOptions, |
michael@0 | 161 | PromiseFlatCString(aName).get(), |
michael@0 | 162 | aArgCount, aArgArray, |
michael@0 | 163 | PromiseFlatString(aBody).get(), |
michael@0 | 164 | aBody.Length()); |
michael@0 | 165 | if (!fun) { |
michael@0 | 166 | ReportPendingException(aCx); |
michael@0 | 167 | return NS_ERROR_FAILURE; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | *aFunctionObject = JS_GetFunctionObject(fun); |
michael@0 | 171 | return NS_OK; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | nsresult |
michael@0 | 175 | nsJSUtils::EvaluateString(JSContext* aCx, |
michael@0 | 176 | const nsAString& aScript, |
michael@0 | 177 | JS::Handle<JSObject*> aScopeObject, |
michael@0 | 178 | JS::CompileOptions& aCompileOptions, |
michael@0 | 179 | const EvaluateOptions& aEvaluateOptions, |
michael@0 | 180 | JS::MutableHandle<JS::Value> aRetValue, |
michael@0 | 181 | void **aOffThreadToken) |
michael@0 | 182 | { |
michael@0 | 183 | const nsPromiseFlatString& flatScript = PromiseFlatString(aScript); |
michael@0 | 184 | JS::SourceBufferHolder srcBuf(flatScript.get(), aScript.Length(), |
michael@0 | 185 | JS::SourceBufferHolder::NoOwnership); |
michael@0 | 186 | return EvaluateString(aCx, srcBuf, aScopeObject, aCompileOptions, |
michael@0 | 187 | aEvaluateOptions, aRetValue, aOffThreadToken); |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | nsresult |
michael@0 | 191 | nsJSUtils::EvaluateString(JSContext* aCx, |
michael@0 | 192 | JS::SourceBufferHolder& aSrcBuf, |
michael@0 | 193 | JS::Handle<JSObject*> aScopeObject, |
michael@0 | 194 | JS::CompileOptions& aCompileOptions, |
michael@0 | 195 | const EvaluateOptions& aEvaluateOptions, |
michael@0 | 196 | JS::MutableHandle<JS::Value> aRetValue, |
michael@0 | 197 | void **aOffThreadToken) |
michael@0 | 198 | { |
michael@0 | 199 | PROFILER_LABEL("JS", "EvaluateString"); |
michael@0 | 200 | MOZ_ASSERT_IF(aCompileOptions.versionSet, |
michael@0 | 201 | aCompileOptions.version != JSVERSION_UNKNOWN); |
michael@0 | 202 | MOZ_ASSERT_IF(aEvaluateOptions.coerceToString, aEvaluateOptions.needResult); |
michael@0 | 203 | MOZ_ASSERT_IF(!aEvaluateOptions.reportUncaught, aEvaluateOptions.needResult); |
michael@0 | 204 | MOZ_ASSERT(aCx == nsContentUtils::GetCurrentJSContext()); |
michael@0 | 205 | MOZ_ASSERT(aSrcBuf.get()); |
michael@0 | 206 | |
michael@0 | 207 | // Unfortunately, the JS engine actually compiles scripts with a return value |
michael@0 | 208 | // in a different, less efficient way. Furthermore, it can't JIT them in many |
michael@0 | 209 | // cases. So we need to be explicitly told whether the caller cares about the |
michael@0 | 210 | // return value. Callers can do this by calling the other overload of |
michael@0 | 211 | // EvaluateString() which calls this function with aEvaluateOptions.needResult |
michael@0 | 212 | // set to false. |
michael@0 | 213 | aRetValue.setUndefined(); |
michael@0 | 214 | |
michael@0 | 215 | JS::ExposeObjectToActiveJS(aScopeObject); |
michael@0 | 216 | nsAutoMicroTask mt; |
michael@0 | 217 | nsresult rv = NS_OK; |
michael@0 | 218 | |
michael@0 | 219 | bool ok = false; |
michael@0 | 220 | nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); |
michael@0 | 221 | NS_ENSURE_TRUE(ssm->ScriptAllowed(js::GetGlobalForObjectCrossCompartment(aScopeObject)), NS_OK); |
michael@0 | 222 | |
michael@0 | 223 | mozilla::Maybe<AutoDontReportUncaught> dontReport; |
michael@0 | 224 | if (!aEvaluateOptions.reportUncaught) { |
michael@0 | 225 | // We need to prevent AutoLastFrameCheck from reporting and clearing |
michael@0 | 226 | // any pending exceptions. |
michael@0 | 227 | dontReport.construct(aCx); |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | // Scope the JSAutoCompartment so that we can later wrap the return value |
michael@0 | 231 | // into the caller's cx. |
michael@0 | 232 | { |
michael@0 | 233 | JSAutoCompartment ac(aCx, aScopeObject); |
michael@0 | 234 | |
michael@0 | 235 | JS::Rooted<JSObject*> rootedScope(aCx, aScopeObject); |
michael@0 | 236 | if (aOffThreadToken) { |
michael@0 | 237 | JS::Rooted<JSScript*> |
michael@0 | 238 | script(aCx, JS::FinishOffThreadScript(aCx, JS_GetRuntime(aCx), *aOffThreadToken)); |
michael@0 | 239 | *aOffThreadToken = nullptr; // Mark the token as having been finished. |
michael@0 | 240 | if (script) { |
michael@0 | 241 | if (aEvaluateOptions.needResult) { |
michael@0 | 242 | ok = JS_ExecuteScript(aCx, rootedScope, script, aRetValue); |
michael@0 | 243 | } else { |
michael@0 | 244 | ok = JS_ExecuteScript(aCx, rootedScope, script); |
michael@0 | 245 | } |
michael@0 | 246 | } else { |
michael@0 | 247 | ok = false; |
michael@0 | 248 | } |
michael@0 | 249 | } else { |
michael@0 | 250 | if (aEvaluateOptions.needResult) { |
michael@0 | 251 | ok = JS::Evaluate(aCx, rootedScope, aCompileOptions, |
michael@0 | 252 | aSrcBuf, aRetValue); |
michael@0 | 253 | } else { |
michael@0 | 254 | ok = JS::Evaluate(aCx, rootedScope, aCompileOptions, |
michael@0 | 255 | aSrcBuf); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | if (ok && aEvaluateOptions.coerceToString && !aRetValue.isUndefined()) { |
michael@0 | 260 | JS::Rooted<JS::Value> value(aCx, aRetValue); |
michael@0 | 261 | JSString* str = JS::ToString(aCx, value); |
michael@0 | 262 | ok = !!str; |
michael@0 | 263 | aRetValue.set(ok ? JS::StringValue(str) : JS::UndefinedValue()); |
michael@0 | 264 | } |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | if (!ok) { |
michael@0 | 268 | if (aEvaluateOptions.reportUncaught) { |
michael@0 | 269 | ReportPendingException(aCx); |
michael@0 | 270 | if (aEvaluateOptions.needResult) { |
michael@0 | 271 | aRetValue.setUndefined(); |
michael@0 | 272 | } |
michael@0 | 273 | } else { |
michael@0 | 274 | rv = JS_IsExceptionPending(aCx) ? NS_ERROR_FAILURE |
michael@0 | 275 | : NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 276 | JS::Rooted<JS::Value> exn(aCx); |
michael@0 | 277 | JS_GetPendingException(aCx, &exn); |
michael@0 | 278 | if (aEvaluateOptions.needResult) { |
michael@0 | 279 | aRetValue.set(exn); |
michael@0 | 280 | } |
michael@0 | 281 | JS_ClearPendingException(aCx); |
michael@0 | 282 | } |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | // Wrap the return value into whatever compartment aCx was in. |
michael@0 | 286 | if (aEvaluateOptions.needResult) { |
michael@0 | 287 | JS::Rooted<JS::Value> v(aCx, aRetValue); |
michael@0 | 288 | if (!JS_WrapValue(aCx, &v)) { |
michael@0 | 289 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 290 | } |
michael@0 | 291 | aRetValue.set(v); |
michael@0 | 292 | } |
michael@0 | 293 | return rv; |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | nsresult |
michael@0 | 297 | nsJSUtils::EvaluateString(JSContext* aCx, |
michael@0 | 298 | const nsAString& aScript, |
michael@0 | 299 | JS::Handle<JSObject*> aScopeObject, |
michael@0 | 300 | JS::CompileOptions& aCompileOptions, |
michael@0 | 301 | void **aOffThreadToken) |
michael@0 | 302 | { |
michael@0 | 303 | EvaluateOptions options; |
michael@0 | 304 | options.setNeedResult(false); |
michael@0 | 305 | JS::RootedValue unused(aCx); |
michael@0 | 306 | return EvaluateString(aCx, aScript, aScopeObject, aCompileOptions, |
michael@0 | 307 | options, &unused, aOffThreadToken); |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | nsresult |
michael@0 | 311 | nsJSUtils::EvaluateString(JSContext* aCx, |
michael@0 | 312 | JS::SourceBufferHolder& aSrcBuf, |
michael@0 | 313 | JS::Handle<JSObject*> aScopeObject, |
michael@0 | 314 | JS::CompileOptions& aCompileOptions, |
michael@0 | 315 | void **aOffThreadToken) |
michael@0 | 316 | { |
michael@0 | 317 | EvaluateOptions options; |
michael@0 | 318 | options.setNeedResult(false); |
michael@0 | 319 | JS::RootedValue unused(aCx); |
michael@0 | 320 | return EvaluateString(aCx, aSrcBuf, aScopeObject, aCompileOptions, |
michael@0 | 321 | options, &unused, aOffThreadToken); |
michael@0 | 322 | } |
michael@0 | 323 | |
michael@0 | 324 | // |
michael@0 | 325 | // nsDOMJSUtils.h |
michael@0 | 326 | // |
michael@0 | 327 | |
michael@0 | 328 | JSObject* GetDefaultScopeFromJSContext(JSContext *cx) |
michael@0 | 329 | { |
michael@0 | 330 | // DOM JSContexts don't store their default compartment object on |
michael@0 | 331 | // the cx, so in those cases we need to fetch it via the scx |
michael@0 | 332 | // instead. |
michael@0 | 333 | nsIScriptContext *scx = GetScriptContextFromJSContext(cx); |
michael@0 | 334 | if (scx) { |
michael@0 | 335 | return scx->GetWindowProxy(); |
michael@0 | 336 | } |
michael@0 | 337 | return js::DefaultObjectForContextOrNull(cx); |
michael@0 | 338 | } |