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 sts=2 et cindent: */ |
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 | #include "mozilla/dom/CallbackObject.h" |
michael@0 | 8 | #include "mozilla/dom/BindingUtils.h" |
michael@0 | 9 | #include "mozilla/dom/DOMError.h" |
michael@0 | 10 | #include "mozilla/dom/DOMErrorBinding.h" |
michael@0 | 11 | #include "jsfriendapi.h" |
michael@0 | 12 | #include "nsIScriptGlobalObject.h" |
michael@0 | 13 | #include "nsIXPConnect.h" |
michael@0 | 14 | #include "nsIScriptContext.h" |
michael@0 | 15 | #include "nsPIDOMWindow.h" |
michael@0 | 16 | #include "nsJSUtils.h" |
michael@0 | 17 | #include "nsCxPusher.h" |
michael@0 | 18 | #include "nsIScriptSecurityManager.h" |
michael@0 | 19 | #include "xpcprivate.h" |
michael@0 | 20 | #include "WorkerPrivate.h" |
michael@0 | 21 | #include "nsGlobalWindow.h" |
michael@0 | 22 | #include "WorkerScope.h" |
michael@0 | 23 | |
michael@0 | 24 | namespace mozilla { |
michael@0 | 25 | namespace dom { |
michael@0 | 26 | |
michael@0 | 27 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CallbackObject) |
michael@0 | 28 | NS_INTERFACE_MAP_ENTRY(mozilla::dom::CallbackObject) |
michael@0 | 29 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 30 | NS_INTERFACE_MAP_END |
michael@0 | 31 | |
michael@0 | 32 | NS_IMPL_CYCLE_COLLECTING_ADDREF(CallbackObject) |
michael@0 | 33 | NS_IMPL_CYCLE_COLLECTING_RELEASE(CallbackObject) |
michael@0 | 34 | |
michael@0 | 35 | NS_IMPL_CYCLE_COLLECTION_CLASS(CallbackObject) |
michael@0 | 36 | |
michael@0 | 37 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject) |
michael@0 | 38 | tmp->DropJSObjects(); |
michael@0 | 39 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mIncumbentGlobal) |
michael@0 | 40 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 41 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CallbackObject) |
michael@0 | 42 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 43 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIncumbentGlobal) |
michael@0 | 44 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 45 | NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CallbackObject) |
michael@0 | 46 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallback) |
michael@0 | 47 | NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mIncumbentJSGlobal) |
michael@0 | 48 | NS_IMPL_CYCLE_COLLECTION_TRACE_END |
michael@0 | 49 | |
michael@0 | 50 | CallbackObject::CallSetup::CallSetup(CallbackObject* aCallback, |
michael@0 | 51 | ErrorResult& aRv, |
michael@0 | 52 | ExceptionHandling aExceptionHandling, |
michael@0 | 53 | JSCompartment* aCompartment, |
michael@0 | 54 | bool aIsJSImplementedWebIDL) |
michael@0 | 55 | : mCx(nullptr) |
michael@0 | 56 | , mCompartment(aCompartment) |
michael@0 | 57 | , mErrorResult(aRv) |
michael@0 | 58 | , mExceptionHandling(aExceptionHandling) |
michael@0 | 59 | , mIsMainThread(NS_IsMainThread()) |
michael@0 | 60 | { |
michael@0 | 61 | if (mIsMainThread) { |
michael@0 | 62 | nsContentUtils::EnterMicroTask(); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | // Compute the caller's subject principal (if necessary) early, before we |
michael@0 | 66 | // do anything that might perturb the relevant state. |
michael@0 | 67 | nsIPrincipal* webIDLCallerPrincipal = nullptr; |
michael@0 | 68 | if (aIsJSImplementedWebIDL) { |
michael@0 | 69 | webIDLCallerPrincipal = nsContentUtils::GetSubjectPrincipal(); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | // We need to produce a useful JSContext here. Ideally one that the callback |
michael@0 | 73 | // is in some sense associated with, so that we can sort of treat it as a |
michael@0 | 74 | // "script entry point". Though once we actually have script entry points, |
michael@0 | 75 | // we'll need to do the script entry point bits once we have an actual |
michael@0 | 76 | // callable. |
michael@0 | 77 | |
michael@0 | 78 | // First, find the real underlying callback. |
michael@0 | 79 | JSObject* realCallback = js::UncheckedUnwrap(aCallback->CallbackPreserveColor()); |
michael@0 | 80 | JSContext* cx = nullptr; |
michael@0 | 81 | nsIGlobalObject* globalObject = nullptr; |
michael@0 | 82 | |
michael@0 | 83 | { |
michael@0 | 84 | JS::AutoAssertNoGC nogc; |
michael@0 | 85 | if (mIsMainThread) { |
michael@0 | 86 | // Now get the global and JSContext for this callback. |
michael@0 | 87 | nsGlobalWindow* win = xpc::WindowGlobalOrNull(realCallback); |
michael@0 | 88 | if (win) { |
michael@0 | 89 | // Make sure that if this is a window it's the current inner, since the |
michael@0 | 90 | // nsIScriptContext and hence JSContext are associated with the outer |
michael@0 | 91 | // window. Which means that if someone holds on to a function from a |
michael@0 | 92 | // now-unloaded document we'd have the new document as the script entry |
michael@0 | 93 | // point... |
michael@0 | 94 | MOZ_ASSERT(win->IsInnerWindow()); |
michael@0 | 95 | nsPIDOMWindow* outer = win->GetOuterWindow(); |
michael@0 | 96 | if (!outer || win != outer->GetCurrentInnerWindow()) { |
michael@0 | 97 | // Just bail out from here |
michael@0 | 98 | return; |
michael@0 | 99 | } |
michael@0 | 100 | cx = win->GetContext() ? win->GetContext()->GetNativeContext() |
michael@0 | 101 | // This happens - Removing it causes |
michael@0 | 102 | // test_bug293235.xul to go orange. |
michael@0 | 103 | : nsContentUtils::GetSafeJSContext(); |
michael@0 | 104 | globalObject = win; |
michael@0 | 105 | } else { |
michael@0 | 106 | // No DOM Window. Store the global and use the SafeJSContext. |
michael@0 | 107 | JSObject* glob = js::GetGlobalForObjectCrossCompartment(realCallback); |
michael@0 | 108 | globalObject = xpc::GetNativeForGlobal(glob); |
michael@0 | 109 | MOZ_ASSERT(globalObject); |
michael@0 | 110 | cx = nsContentUtils::GetSafeJSContext(); |
michael@0 | 111 | } |
michael@0 | 112 | } else { |
michael@0 | 113 | cx = workers::GetCurrentThreadJSContext(); |
michael@0 | 114 | globalObject = workers::GetCurrentThreadWorkerPrivate()->GlobalScope(); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | // Bail out if there's no useful global. This seems to happen intermittently |
michael@0 | 118 | // on gaia-ui tests, probably because nsInProcessTabChildGlobal is returning |
michael@0 | 119 | // null in some kind of teardown state. |
michael@0 | 120 | if (!globalObject->GetGlobalJSObject()) { |
michael@0 | 121 | return; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | mAutoEntryScript.construct(globalObject, mIsMainThread, cx); |
michael@0 | 125 | mAutoEntryScript.ref().SetWebIDLCallerPrincipal(webIDLCallerPrincipal); |
michael@0 | 126 | nsIGlobalObject* incumbent = aCallback->IncumbentGlobalOrNull(); |
michael@0 | 127 | if (incumbent) { |
michael@0 | 128 | // The callback object traces its incumbent JS global, so in general it |
michael@0 | 129 | // should be alive here. However, it's possible that we could run afoul |
michael@0 | 130 | // of the same IPC global weirdness described above, wherein the |
michael@0 | 131 | // nsIGlobalObject has severed its reference to the JS global. Let's just |
michael@0 | 132 | // be safe here, so that nobody has to waste a day debugging gaia-ui tests. |
michael@0 | 133 | if (!incumbent->GetGlobalJSObject()) { |
michael@0 | 134 | return; |
michael@0 | 135 | } |
michael@0 | 136 | mAutoIncumbentScript.construct(incumbent); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | // Unmark the callable (by invoking Callback() and not the CallbackPreserveColor() |
michael@0 | 140 | // variant), and stick it in a Rooted before it can go gray again. |
michael@0 | 141 | // Nothing before us in this function can trigger a CC, so it's safe to wait |
michael@0 | 142 | // until here it do the unmark. This allows us to order the following two |
michael@0 | 143 | // operations _after_ the Push() above, which lets us take advantage of the |
michael@0 | 144 | // JSAutoRequest embedded in the pusher. |
michael@0 | 145 | // |
michael@0 | 146 | // We can do this even though we're not in the right compartment yet, because |
michael@0 | 147 | // Rooted<> does not care about compartments. |
michael@0 | 148 | mRootedCallable.construct(cx, aCallback->Callback()); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | if (mIsMainThread) { |
michael@0 | 152 | // Check that it's ok to run this callback at all. |
michael@0 | 153 | // Make sure to use realCallback to get the global of the callback object, |
michael@0 | 154 | // not the wrapper. |
michael@0 | 155 | bool allowed = nsContentUtils::GetSecurityManager()-> |
michael@0 | 156 | ScriptAllowed(js::GetGlobalForObjectCrossCompartment(realCallback)); |
michael@0 | 157 | |
michael@0 | 158 | if (!allowed) { |
michael@0 | 159 | return; |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | // Enter the compartment of our callback, so we can actually work with it. |
michael@0 | 164 | // |
michael@0 | 165 | // Note that if the callback is a wrapper, this will not be the same |
michael@0 | 166 | // compartment that we ended up in with mAutoEntryScript above, because the |
michael@0 | 167 | // entry point is based off of the unwrapped callback (realCallback). |
michael@0 | 168 | mAc.construct(cx, mRootedCallable.ref()); |
michael@0 | 169 | |
michael@0 | 170 | // And now we're ready to go. |
michael@0 | 171 | mCx = cx; |
michael@0 | 172 | |
michael@0 | 173 | // Make sure the JS engine doesn't report exceptions we want to re-throw |
michael@0 | 174 | if ((mCompartment && mExceptionHandling == eRethrowContentExceptions) || |
michael@0 | 175 | mExceptionHandling == eRethrowExceptions) { |
michael@0 | 176 | mSavedJSContextOptions = JS::ContextOptionsRef(cx); |
michael@0 | 177 | JS::ContextOptionsRef(cx).setDontReportUncaught(true); |
michael@0 | 178 | } |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | bool |
michael@0 | 182 | CallbackObject::CallSetup::ShouldRethrowException(JS::Handle<JS::Value> aException) |
michael@0 | 183 | { |
michael@0 | 184 | if (mExceptionHandling == eRethrowExceptions) { |
michael@0 | 185 | return true; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | MOZ_ASSERT(mExceptionHandling == eRethrowContentExceptions); |
michael@0 | 189 | |
michael@0 | 190 | // For eRethrowContentExceptions we only want to throw an exception if the |
michael@0 | 191 | // object that was thrown is a DOMError object in the caller compartment |
michael@0 | 192 | // (which we stored in mCompartment). |
michael@0 | 193 | |
michael@0 | 194 | if (!aException.isObject()) { |
michael@0 | 195 | return false; |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | JS::Rooted<JSObject*> obj(mCx, &aException.toObject()); |
michael@0 | 199 | obj = js::UncheckedUnwrap(obj, /* stopAtOuter = */ false); |
michael@0 | 200 | if (js::GetObjectCompartment(obj) != mCompartment) { |
michael@0 | 201 | return false; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | DOMError* domError; |
michael@0 | 205 | return NS_SUCCEEDED(UNWRAP_OBJECT(DOMError, obj, domError)); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | CallbackObject::CallSetup::~CallSetup() |
michael@0 | 209 | { |
michael@0 | 210 | // To get our nesting right we have to destroy our JSAutoCompartment first. |
michael@0 | 211 | // In particular, we want to do this before we try reporting any exceptions, |
michael@0 | 212 | // so we end up reporting them while in the compartment of our entry point, |
michael@0 | 213 | // not whatever cross-compartment wrappper mCallback might be. |
michael@0 | 214 | // Be careful: the JSAutoCompartment might not have been constructed at all! |
michael@0 | 215 | mAc.destroyIfConstructed(); |
michael@0 | 216 | |
michael@0 | 217 | // Now, if we have a JSContext, report any pending errors on it, unless we |
michael@0 | 218 | // were told to re-throw them. |
michael@0 | 219 | if (mCx) { |
michael@0 | 220 | bool dealtWithPendingException = false; |
michael@0 | 221 | if ((mCompartment && mExceptionHandling == eRethrowContentExceptions) || |
michael@0 | 222 | mExceptionHandling == eRethrowExceptions) { |
michael@0 | 223 | // Restore the old context options |
michael@0 | 224 | JS::ContextOptionsRef(mCx) = mSavedJSContextOptions; |
michael@0 | 225 | mErrorResult.MightThrowJSException(); |
michael@0 | 226 | if (JS_IsExceptionPending(mCx)) { |
michael@0 | 227 | JS::Rooted<JS::Value> exn(mCx); |
michael@0 | 228 | if (JS_GetPendingException(mCx, &exn) && |
michael@0 | 229 | ShouldRethrowException(exn)) { |
michael@0 | 230 | mErrorResult.ThrowJSException(mCx, exn); |
michael@0 | 231 | JS_ClearPendingException(mCx); |
michael@0 | 232 | dealtWithPendingException = true; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | } |
michael@0 | 236 | |
michael@0 | 237 | if (!dealtWithPendingException) { |
michael@0 | 238 | // Either we're supposed to report our exceptions, or we're supposed to |
michael@0 | 239 | // re-throw them but we failed to JS_GetPendingException. Either way, |
michael@0 | 240 | // just report the pending exception, if any. |
michael@0 | 241 | // |
michael@0 | 242 | // We don't use nsJSUtils::ReportPendingException here because all it |
michael@0 | 243 | // does at this point is JS_SaveFrameChain and enter a compartment around |
michael@0 | 244 | // a JS_ReportPendingException call. But our mAutoEntryScript should |
michael@0 | 245 | // already do a JS_SaveFrameChain and we are already in the compartment |
michael@0 | 246 | // we want to be in, so all nsJSUtils::ReportPendingException would do is |
michael@0 | 247 | // screw up our compartment, which is exactly what we do not want. |
michael@0 | 248 | // |
michael@0 | 249 | // XXXbz FIXME: bug 979525 means we don't always JS_SaveFrameChain here, |
michael@0 | 250 | // so we need to go ahead and do that. |
michael@0 | 251 | JS::Rooted<JSObject*> oldGlobal(mCx, JS::CurrentGlobalOrNull(mCx)); |
michael@0 | 252 | MOZ_ASSERT(oldGlobal, "How can we not have a global here??"); |
michael@0 | 253 | bool saved = JS_SaveFrameChain(mCx); |
michael@0 | 254 | // Make sure the JSAutoCompartment goes out of scope before the |
michael@0 | 255 | // JS_RestoreFrameChain call! |
michael@0 | 256 | { |
michael@0 | 257 | JSAutoCompartment ac(mCx, oldGlobal); |
michael@0 | 258 | MOZ_ASSERT(!JS::DescribeScriptedCaller(mCx), |
michael@0 | 259 | "Our comment above about JS_SaveFrameChain having been " |
michael@0 | 260 | "called is a lie?"); |
michael@0 | 261 | JS_ReportPendingException(mCx); |
michael@0 | 262 | } |
michael@0 | 263 | if (saved) { |
michael@0 | 264 | JS_RestoreFrameChain(mCx); |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | } |
michael@0 | 268 | |
michael@0 | 269 | mAutoIncumbentScript.destroyIfConstructed(); |
michael@0 | 270 | mAutoEntryScript.destroyIfConstructed(); |
michael@0 | 271 | |
michael@0 | 272 | // It is important that this is the last thing we do, after leaving the |
michael@0 | 273 | // compartment and undoing all our entry/incumbent script changes |
michael@0 | 274 | if (mIsMainThread) { |
michael@0 | 275 | nsContentUtils::LeaveMicroTask(); |
michael@0 | 276 | } |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | already_AddRefed<nsISupports> |
michael@0 | 280 | CallbackObjectHolderBase::ToXPCOMCallback(CallbackObject* aCallback, |
michael@0 | 281 | const nsIID& aIID) const |
michael@0 | 282 | { |
michael@0 | 283 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 284 | if (!aCallback) { |
michael@0 | 285 | return nullptr; |
michael@0 | 286 | } |
michael@0 | 287 | |
michael@0 | 288 | AutoSafeJSContext cx; |
michael@0 | 289 | |
michael@0 | 290 | JS::Rooted<JSObject*> callback(cx, aCallback->Callback()); |
michael@0 | 291 | |
michael@0 | 292 | JSAutoCompartment ac(cx, callback); |
michael@0 | 293 | nsRefPtr<nsXPCWrappedJS> wrappedJS; |
michael@0 | 294 | nsresult rv = |
michael@0 | 295 | nsXPCWrappedJS::GetNewOrUsed(callback, aIID, getter_AddRefs(wrappedJS)); |
michael@0 | 296 | if (NS_FAILED(rv) || !wrappedJS) { |
michael@0 | 297 | return nullptr; |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | nsCOMPtr<nsISupports> retval; |
michael@0 | 301 | rv = wrappedJS->QueryInterface(aIID, getter_AddRefs(retval)); |
michael@0 | 302 | if (NS_FAILED(rv)) { |
michael@0 | 303 | return nullptr; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | return retval.forget(); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | } // namespace dom |
michael@0 | 310 | } // namespace mozilla |