1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/CallbackObject.h"
8 #include "mozilla/dom/BindingUtils.h"
9 #include "mozilla/dom/DOMError.h"
10 #include "mozilla/dom/DOMErrorBinding.h"
11 #include "jsfriendapi.h"
12 #include "nsIScriptGlobalObject.h"
13 #include "nsIXPConnect.h"
14 #include "nsIScriptContext.h"
15 #include "nsPIDOMWindow.h"
16 #include "nsJSUtils.h"
17 #include "nsCxPusher.h"
18 #include "nsIScriptSecurityManager.h"
19 #include "xpcprivate.h"
24 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CallbackObject
)
25 NS_INTERFACE_MAP_ENTRY(mozilla::dom::CallbackObject
)
26 NS_INTERFACE_MAP_ENTRY(nsISupports
)
29 NS_IMPL_CYCLE_COLLECTING_ADDREF(CallbackObject
)
30 NS_IMPL_CYCLE_COLLECTING_RELEASE(CallbackObject
)
32 NS_IMPL_CYCLE_COLLECTION_CLASS(CallbackObject
)
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CallbackObject
)
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CallbackObject
)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
40 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CallbackObject
)
41 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCallback
)
42 NS_IMPL_CYCLE_COLLECTION_TRACE_END
44 CallbackObject::CallSetup::CallSetup(JS::Handle
<JSObject
*> aCallback
,
46 ExceptionHandling aExceptionHandling
,
47 JSCompartment
* aCompartment
)
49 , mCompartment(aCompartment
)
51 , mExceptionHandling(aExceptionHandling
)
53 // We need to produce a useful JSContext here. Ideally one that the callback
54 // is in some sense associated with, so that we can sort of treat it as a
55 // "script entry point". Though once we actually have script entry points,
56 // we'll need to do the script entry point bits once we have an actual
59 // First, find the real underlying callback.
60 JSObject
* realCallback
= js::UncheckedUnwrap(aCallback
);
62 // Now get the nsIScriptGlobalObject for this callback.
63 JSContext
* cx
= nullptr;
64 nsIScriptContext
* ctx
= nullptr;
65 nsIScriptGlobalObject
* sgo
= nsJSUtils::GetStaticScriptGlobal(realCallback
);
67 // Make sure that if this is a window it's the current inner, since the
68 // nsIScriptContext and hence JSContext are associated with the outer
69 // window. Which means that if someone holds on to a function from a
70 // now-unloaded document we'd have the new document as the script entry
72 nsCOMPtr
<nsPIDOMWindow
> win
= do_QueryInterface(sgo
);
74 MOZ_ASSERT(win
->IsInnerWindow());
75 nsPIDOMWindow
* outer
= win
->GetOuterWindow();
76 if (!outer
|| win
!= outer
->GetCurrentInnerWindow()) {
77 // Just bail out from here
81 // if not a window at all, just press on
83 ctx
= sgo
->GetContext();
85 // We don't check whether scripts are enabled on ctx, because
86 // CheckFunctionAccess will do that anyway... and because we ignore them
87 // being disabled if the callee is system.
88 cx
= ctx
->GetNativeContext();
93 // We didn't manage to hunt down a script global to work with. Just fall
94 // back on using the safe context.
95 cx
= nsContentUtils::GetSafeJSContext();
98 // Make sure our JSContext is pushed on the stack.
101 // Unmark the callable, and stick it in a Rooted before it can go gray again.
102 // Nothing before us in this function can trigger a CC, so it's safe to wait
103 // until here it do the unmark. This allows us to order the following two
104 // operations _after_ the Push() above, which lets us take advantage of the
105 // JSAutoRequest embedded in the pusher.
107 // We can do this even though we're not in the right compartment yet, because
108 // Rooted<> does not care about compartments.
109 xpc_UnmarkGrayObject(aCallback
);
110 mRootedCallable
.construct(cx
, aCallback
);
112 // Check that it's ok to run this callback at all.
113 // FIXME: Bug 807371: we want a less silly check here.
114 // Make sure to unwrap aCallback before passing it in, because
115 // getting principals from wrappers is silly.
116 nsresult rv
= nsContentUtils::GetSecurityManager()->
117 CheckFunctionAccess(cx
, js::UncheckedUnwrap(aCallback
), nullptr);
120 // Security check failed. We're done here.
124 // Enter the compartment of our callback, so we can actually work with it.
125 mAc
.construct(cx
, aCallback
);
127 // And now we're ready to go.
130 // Make sure the JS engine doesn't report exceptions we want to re-throw
131 if (mExceptionHandling
== eRethrowContentExceptions
||
132 mExceptionHandling
== eRethrowExceptions
) {
133 mSavedJSContextOptions
= JS_GetOptions(cx
);
134 JS_SetOptions(cx
, mSavedJSContextOptions
| JSOPTION_DONT_REPORT_UNCAUGHT
);
139 CallbackObject::CallSetup::ShouldRethrowException(JS::Handle
<JS::Value
> aException
)
141 if (mExceptionHandling
== eRethrowExceptions
) {
145 MOZ_ASSERT(mExceptionHandling
== eRethrowContentExceptions
);
147 // For eRethrowContentExceptions we only want to throw an exception if the
148 // object that was thrown is a DOMError object in the caller compartment
149 // (which we stored in mCompartment).
151 if (!aException
.isObject()) {
155 JS::Rooted
<JSObject
*> obj(mCx
, &aException
.toObject());
156 obj
= js::UncheckedUnwrap(obj
, /* stopAtOuter = */ false);
157 if (js::GetObjectCompartment(obj
) != mCompartment
) {
162 return NS_SUCCEEDED(UNWRAP_OBJECT(DOMError
, mCx
, obj
, domError
));
165 CallbackObject::CallSetup::~CallSetup()
167 // First things first: if we have a JSContext, report any pending
168 // errors on it, unless we were told to re-throw them.
170 bool dealtWithPendingException
= false;
171 if (mExceptionHandling
== eRethrowContentExceptions
||
172 mExceptionHandling
== eRethrowExceptions
) {
173 // Restore the old context options
174 JS_SetOptions(mCx
, mSavedJSContextOptions
);
175 mErrorResult
.MightThrowJSException();
176 if (JS_IsExceptionPending(mCx
)) {
177 JS::Rooted
<JS::Value
> exn(mCx
);
178 if (JS_GetPendingException(mCx
, exn
.address()) &&
179 ShouldRethrowException(exn
)) {
180 mErrorResult
.ThrowJSException(mCx
, exn
);
181 JS_ClearPendingException(mCx
);
182 dealtWithPendingException
= true;
187 if (!dealtWithPendingException
) {
188 // Either we're supposed to report our exceptions, or we're supposed to
189 // re-throw them but we failed to JS_GetPendingException. Either way,
190 // just report the pending exception, if any.
191 nsJSUtils::ReportPendingException(mCx
);
195 // To get our nesting right we have to destroy our JSAutoCompartment first.
196 // But be careful: it might not have been constructed at all!
197 mAc
.destroyIfConstructed();
199 // XXXbz For that matter why do we need to manually call ScriptEvaluated at
200 // all? nsCxPusher::Pop will do that nowadays if !mScriptIsRunning, so the
201 // concerns from bug 295983 don't seem relevant anymore. Do we want to make
202 // sure it's still called when !mScriptIsRunning? I guess play it safe for
203 // now and do what CallEventHandler did, which is call always.
205 // Popping an nsCxPusher is safe even if it never got pushed.
209 already_AddRefed
<nsISupports
>
210 CallbackObjectHolderBase::ToXPCOMCallback(CallbackObject
* aCallback
,
211 const nsIID
& aIID
) const
217 AutoSafeJSContext cx
;
219 JS::Rooted
<JSObject
*> callback(cx
, aCallback
->Callback());
221 JSAutoCompartment
ac(cx
, callback
);
222 nsRefPtr
<nsXPCWrappedJS
> wrappedJS
;
224 nsXPCWrappedJS::GetNewOrUsed(callback
, aIID
,
225 nullptr, getter_AddRefs(wrappedJS
));
226 if (NS_FAILED(rv
) || !wrappedJS
) {
230 nsCOMPtr
<nsISupports
> retval
;
231 rv
= wrappedJS
->QueryInterface(aIID
, getter_AddRefs(retval
));
236 return retval
.forget();
240 } // namespace mozilla