dom/bindings/CallbackObject.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 /**
michael@0 8 * A common base class for representing WebIDL callback function and
michael@0 9 * callback interface types in C++.
michael@0 10 *
michael@0 11 * This class implements common functionality like lifetime
michael@0 12 * management, initialization with the JS object, and setup of the
michael@0 13 * call environment. Subclasses are responsible for providing methods
michael@0 14 * that do the call into JS as needed.
michael@0 15 */
michael@0 16
michael@0 17 #ifndef mozilla_dom_CallbackObject_h
michael@0 18 #define mozilla_dom_CallbackObject_h
michael@0 19
michael@0 20 #include "nsISupports.h"
michael@0 21 #include "nsISupportsImpl.h"
michael@0 22 #include "nsCycleCollectionParticipant.h"
michael@0 23 #include "jswrapper.h"
michael@0 24 #include "mozilla/Assertions.h"
michael@0 25 #include "mozilla/ErrorResult.h"
michael@0 26 #include "mozilla/HoldDropJSObjects.h"
michael@0 27 #include "mozilla/MemoryReporting.h"
michael@0 28 #include "mozilla/dom/ScriptSettings.h"
michael@0 29 #include "nsContentUtils.h"
michael@0 30 #include "nsWrapperCache.h"
michael@0 31 #include "nsJSEnvironment.h"
michael@0 32 #include "xpcpublic.h"
michael@0 33
michael@0 34 namespace mozilla {
michael@0 35 namespace dom {
michael@0 36
michael@0 37 #define DOM_CALLBACKOBJECT_IID \
michael@0 38 { 0xbe74c190, 0x6d76, 0x4991, \
michael@0 39 { 0x84, 0xb9, 0x65, 0x06, 0x99, 0xe6, 0x93, 0x2b } }
michael@0 40
michael@0 41 class CallbackObject : public nsISupports
michael@0 42 {
michael@0 43 public:
michael@0 44 NS_DECLARE_STATIC_IID_ACCESSOR(DOM_CALLBACKOBJECT_IID)
michael@0 45
michael@0 46 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 47 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CallbackObject)
michael@0 48
michael@0 49 // The caller may pass a global object which will act as an override for the
michael@0 50 // incumbent script settings object when the callback is invoked (overriding
michael@0 51 // the entry point computed from aCallback). If no override is required, the
michael@0 52 // caller should pass null.
michael@0 53 explicit CallbackObject(JS::Handle<JSObject*> aCallback, nsIGlobalObject *aIncumbentGlobal)
michael@0 54 {
michael@0 55 Init(aCallback, aIncumbentGlobal);
michael@0 56 }
michael@0 57
michael@0 58 virtual ~CallbackObject()
michael@0 59 {
michael@0 60 DropJSObjects();
michael@0 61 }
michael@0 62
michael@0 63 JS::Handle<JSObject*> Callback() const
michael@0 64 {
michael@0 65 JS::ExposeObjectToActiveJS(mCallback);
michael@0 66 return CallbackPreserveColor();
michael@0 67 }
michael@0 68
michael@0 69 /*
michael@0 70 * This getter does not change the color of the JSObject meaning that the
michael@0 71 * object returned is not guaranteed to be kept alive past the next CC.
michael@0 72 *
michael@0 73 * This should only be called if you are certain that the return value won't
michael@0 74 * be passed into a JS API function and that it won't be stored without being
michael@0 75 * rooted (or otherwise signaling the stored value to the CC).
michael@0 76 */
michael@0 77 JS::Handle<JSObject*> CallbackPreserveColor() const
michael@0 78 {
michael@0 79 // Calling fromMarkedLocation() is safe because we trace our mCallback, and
michael@0 80 // because the value of mCallback cannot change after if has been set.
michael@0 81 return JS::Handle<JSObject*>::fromMarkedLocation(mCallback.address());
michael@0 82 }
michael@0 83
michael@0 84 nsIGlobalObject* IncumbentGlobalOrNull() const
michael@0 85 {
michael@0 86 return mIncumbentGlobal;
michael@0 87 }
michael@0 88
michael@0 89 enum ExceptionHandling {
michael@0 90 // Report any exception and don't throw it to the caller code.
michael@0 91 eReportExceptions,
michael@0 92 // Throw an exception to the caller code if the thrown exception is a
michael@0 93 // binding object for a DOMError from the caller's scope, otherwise report
michael@0 94 // it.
michael@0 95 eRethrowContentExceptions,
michael@0 96 // Throw any exception to the caller code.
michael@0 97 eRethrowExceptions
michael@0 98 };
michael@0 99
michael@0 100 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
michael@0 101 {
michael@0 102 return aMallocSizeOf(this);
michael@0 103 }
michael@0 104
michael@0 105 protected:
michael@0 106 explicit CallbackObject(CallbackObject* aCallbackObject)
michael@0 107 {
michael@0 108 Init(aCallbackObject->mCallback, aCallbackObject->mIncumbentGlobal);
michael@0 109 }
michael@0 110
michael@0 111 bool operator==(const CallbackObject& aOther) const
michael@0 112 {
michael@0 113 JSObject* thisObj =
michael@0 114 js::UncheckedUnwrap(CallbackPreserveColor());
michael@0 115 JSObject* otherObj =
michael@0 116 js::UncheckedUnwrap(aOther.CallbackPreserveColor());
michael@0 117 return thisObj == otherObj;
michael@0 118 }
michael@0 119
michael@0 120 private:
michael@0 121 inline void Init(JSObject* aCallback, nsIGlobalObject* aIncumbentGlobal)
michael@0 122 {
michael@0 123 MOZ_ASSERT(aCallback && !mCallback);
michael@0 124 // Set script objects before we hold, on the off chance that a GC could
michael@0 125 // somehow happen in there... (which would be pretty odd, granted).
michael@0 126 mCallback = aCallback;
michael@0 127 if (aIncumbentGlobal) {
michael@0 128 mIncumbentGlobal = aIncumbentGlobal;
michael@0 129 mIncumbentJSGlobal = aIncumbentGlobal->GetGlobalJSObject();
michael@0 130 }
michael@0 131 mozilla::HoldJSObjects(this);
michael@0 132 }
michael@0 133
michael@0 134 CallbackObject(const CallbackObject&) MOZ_DELETE;
michael@0 135 CallbackObject& operator =(const CallbackObject&) MOZ_DELETE;
michael@0 136
michael@0 137 protected:
michael@0 138 void DropJSObjects()
michael@0 139 {
michael@0 140 MOZ_ASSERT_IF(mIncumbentJSGlobal, mCallback);
michael@0 141 if (mCallback) {
michael@0 142 mCallback = nullptr;
michael@0 143 mIncumbentJSGlobal = nullptr;
michael@0 144 mozilla::DropJSObjects(this);
michael@0 145 }
michael@0 146 }
michael@0 147
michael@0 148 JS::Heap<JSObject*> mCallback;
michael@0 149 // Ideally, we'd just hold a reference to the nsIGlobalObject, since that's
michael@0 150 // what we need to pass to AutoIncumbentScript. Unfortunately, that doesn't
michael@0 151 // hold the actual JS global alive. So we maintain an additional pointer to
michael@0 152 // the JS global itself so that we can trace it.
michael@0 153 //
michael@0 154 // At some point we should consider trying to make native globals hold their
michael@0 155 // scripted global alive, at which point we can get rid of the duplication
michael@0 156 // here.
michael@0 157 nsCOMPtr<nsIGlobalObject> mIncumbentGlobal;
michael@0 158 JS::TenuredHeap<JSObject*> mIncumbentJSGlobal;
michael@0 159
michael@0 160 class MOZ_STACK_CLASS CallSetup
michael@0 161 {
michael@0 162 /**
michael@0 163 * A class that performs whatever setup we need to safely make a
michael@0 164 * call while this class is on the stack, After the constructor
michael@0 165 * returns, the call is safe to make if GetContext() returns
michael@0 166 * non-null.
michael@0 167 */
michael@0 168 public:
michael@0 169 // If aExceptionHandling == eRethrowContentExceptions then aCompartment
michael@0 170 // needs to be set to the compartment in which exceptions will be rethrown.
michael@0 171 CallSetup(CallbackObject* aCallback, ErrorResult& aRv,
michael@0 172 ExceptionHandling aExceptionHandling,
michael@0 173 JSCompartment* aCompartment = nullptr,
michael@0 174 bool aIsJSImplementedWebIDL = false);
michael@0 175 ~CallSetup();
michael@0 176
michael@0 177 JSContext* GetContext() const
michael@0 178 {
michael@0 179 return mCx;
michael@0 180 }
michael@0 181
michael@0 182 private:
michael@0 183 // We better not get copy-constructed
michael@0 184 CallSetup(const CallSetup&) MOZ_DELETE;
michael@0 185
michael@0 186 bool ShouldRethrowException(JS::Handle<JS::Value> aException);
michael@0 187
michael@0 188 // Members which can go away whenever
michael@0 189 JSContext* mCx;
michael@0 190
michael@0 191 // Caller's compartment. This will only have a sensible value if
michael@0 192 // mExceptionHandling == eRethrowContentExceptions.
michael@0 193 JSCompartment* mCompartment;
michael@0 194
michael@0 195 // And now members whose construction/destruction order we need to control.
michael@0 196 Maybe<AutoEntryScript> mAutoEntryScript;
michael@0 197 Maybe<AutoIncumbentScript> mAutoIncumbentScript;
michael@0 198
michael@0 199 // Constructed the rooter within the scope of mCxPusher above, so that it's
michael@0 200 // always within a request during its lifetime.
michael@0 201 Maybe<JS::Rooted<JSObject*> > mRootedCallable;
michael@0 202
michael@0 203 // Can't construct a JSAutoCompartment without a JSContext either. Also,
michael@0 204 // Put mAc after mAutoEntryScript so that we exit the compartment before
michael@0 205 // we pop the JSContext. Though in practice we'll often manually order
michael@0 206 // those two things.
michael@0 207 Maybe<JSAutoCompartment> mAc;
michael@0 208
michael@0 209 // An ErrorResult to possibly re-throw exceptions on and whether
michael@0 210 // we should re-throw them.
michael@0 211 ErrorResult& mErrorResult;
michael@0 212 const ExceptionHandling mExceptionHandling;
michael@0 213 JS::ContextOptions mSavedJSContextOptions;
michael@0 214 const bool mIsMainThread;
michael@0 215 };
michael@0 216 };
michael@0 217
michael@0 218 template<class WebIDLCallbackT, class XPCOMCallbackT>
michael@0 219 class CallbackObjectHolder;
michael@0 220
michael@0 221 template<class T, class U>
michael@0 222 void ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField);
michael@0 223
michael@0 224 class CallbackObjectHolderBase
michael@0 225 {
michael@0 226 protected:
michael@0 227 // Returns null on all failures
michael@0 228 already_AddRefed<nsISupports> ToXPCOMCallback(CallbackObject* aCallback,
michael@0 229 const nsIID& aIID) const;
michael@0 230 };
michael@0 231
michael@0 232 template<class WebIDLCallbackT, class XPCOMCallbackT>
michael@0 233 class CallbackObjectHolder : CallbackObjectHolderBase
michael@0 234 {
michael@0 235 /**
michael@0 236 * A class which stores either a WebIDLCallbackT* or an XPCOMCallbackT*. Both
michael@0 237 * types must inherit from nsISupports. The pointer that's stored can be
michael@0 238 * null.
michael@0 239 *
michael@0 240 * When storing a WebIDLCallbackT*, mPtrBits is set to the pointer value.
michael@0 241 * When storing an XPCOMCallbackT*, mPtrBits is the pointer value with low bit
michael@0 242 * set.
michael@0 243 */
michael@0 244 public:
michael@0 245 explicit CallbackObjectHolder(WebIDLCallbackT* aCallback)
michael@0 246 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback))
michael@0 247 {
michael@0 248 NS_IF_ADDREF(aCallback);
michael@0 249 }
michael@0 250
michael@0 251 explicit CallbackObjectHolder(XPCOMCallbackT* aCallback)
michael@0 252 : mPtrBits(reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag)
michael@0 253 {
michael@0 254 NS_IF_ADDREF(aCallback);
michael@0 255 }
michael@0 256
michael@0 257 explicit CallbackObjectHolder(const CallbackObjectHolder& aOther)
michael@0 258 : mPtrBits(aOther.mPtrBits)
michael@0 259 {
michael@0 260 NS_IF_ADDREF(GetISupports());
michael@0 261 }
michael@0 262
michael@0 263 CallbackObjectHolder()
michael@0 264 : mPtrBits(0)
michael@0 265 {}
michael@0 266
michael@0 267 ~CallbackObjectHolder()
michael@0 268 {
michael@0 269 UnlinkSelf();
michael@0 270 }
michael@0 271
michael@0 272 void operator=(WebIDLCallbackT* aCallback)
michael@0 273 {
michael@0 274 UnlinkSelf();
michael@0 275 mPtrBits = reinterpret_cast<uintptr_t>(aCallback);
michael@0 276 NS_IF_ADDREF(aCallback);
michael@0 277 }
michael@0 278
michael@0 279 void operator=(XPCOMCallbackT* aCallback)
michael@0 280 {
michael@0 281 UnlinkSelf();
michael@0 282 mPtrBits = reinterpret_cast<uintptr_t>(aCallback) | XPCOMCallbackFlag;
michael@0 283 NS_IF_ADDREF(aCallback);
michael@0 284 }
michael@0 285
michael@0 286 void operator=(const CallbackObjectHolder& aOther)
michael@0 287 {
michael@0 288 UnlinkSelf();
michael@0 289 mPtrBits = aOther.mPtrBits;
michael@0 290 NS_IF_ADDREF(GetISupports());
michael@0 291 }
michael@0 292
michael@0 293 nsISupports* GetISupports() const
michael@0 294 {
michael@0 295 return reinterpret_cast<nsISupports*>(mPtrBits & ~XPCOMCallbackFlag);
michael@0 296 }
michael@0 297
michael@0 298 // Boolean conversion operator so people can use this in boolean tests
michael@0 299 operator bool() const
michael@0 300 {
michael@0 301 return GetISupports();
michael@0 302 }
michael@0 303
michael@0 304 // Even if HasWebIDLCallback returns true, GetWebIDLCallback() might still
michael@0 305 // return null.
michael@0 306 bool HasWebIDLCallback() const
michael@0 307 {
michael@0 308 return !(mPtrBits & XPCOMCallbackFlag);
michael@0 309 }
michael@0 310
michael@0 311 WebIDLCallbackT* GetWebIDLCallback() const
michael@0 312 {
michael@0 313 MOZ_ASSERT(HasWebIDLCallback());
michael@0 314 return reinterpret_cast<WebIDLCallbackT*>(mPtrBits);
michael@0 315 }
michael@0 316
michael@0 317 XPCOMCallbackT* GetXPCOMCallback() const
michael@0 318 {
michael@0 319 MOZ_ASSERT(!HasWebIDLCallback());
michael@0 320 return reinterpret_cast<XPCOMCallbackT*>(mPtrBits & ~XPCOMCallbackFlag);
michael@0 321 }
michael@0 322
michael@0 323 bool operator==(WebIDLCallbackT* aOtherCallback) const
michael@0 324 {
michael@0 325 if (!aOtherCallback) {
michael@0 326 // If other is null, then we must be null to be equal.
michael@0 327 return !GetISupports();
michael@0 328 }
michael@0 329
michael@0 330 if (!HasWebIDLCallback() || !GetWebIDLCallback()) {
michael@0 331 // If other is non-null, then we can't be equal if we have a
michael@0 332 // non-WebIDL callback or a null callback.
michael@0 333 return false;
michael@0 334 }
michael@0 335
michael@0 336 return *GetWebIDLCallback() == *aOtherCallback;
michael@0 337 }
michael@0 338
michael@0 339 bool operator==(XPCOMCallbackT* aOtherCallback) const
michael@0 340 {
michael@0 341 return (!aOtherCallback && !GetISupports()) ||
michael@0 342 (!HasWebIDLCallback() && GetXPCOMCallback() == aOtherCallback);
michael@0 343 }
michael@0 344
michael@0 345 bool operator==(const CallbackObjectHolder& aOtherCallback) const
michael@0 346 {
michael@0 347 if (aOtherCallback.HasWebIDLCallback()) {
michael@0 348 return *this == aOtherCallback.GetWebIDLCallback();
michael@0 349 }
michael@0 350
michael@0 351 return *this == aOtherCallback.GetXPCOMCallback();
michael@0 352 }
michael@0 353
michael@0 354 // Try to return an XPCOMCallbackT version of this object.
michael@0 355 already_AddRefed<XPCOMCallbackT> ToXPCOMCallback() const
michael@0 356 {
michael@0 357 if (!HasWebIDLCallback()) {
michael@0 358 nsRefPtr<XPCOMCallbackT> callback = GetXPCOMCallback();
michael@0 359 return callback.forget();
michael@0 360 }
michael@0 361
michael@0 362 nsCOMPtr<nsISupports> supp =
michael@0 363 CallbackObjectHolderBase::ToXPCOMCallback(GetWebIDLCallback(),
michael@0 364 NS_GET_TEMPLATE_IID(XPCOMCallbackT));
michael@0 365 // ToXPCOMCallback already did the right QI for us.
michael@0 366 return supp.forget().downcast<XPCOMCallbackT>();
michael@0 367 }
michael@0 368
michael@0 369 // Try to return a WebIDLCallbackT version of this object.
michael@0 370 already_AddRefed<WebIDLCallbackT> ToWebIDLCallback() const
michael@0 371 {
michael@0 372 if (HasWebIDLCallback()) {
michael@0 373 nsRefPtr<WebIDLCallbackT> callback = GetWebIDLCallback();
michael@0 374 return callback.forget();
michael@0 375 }
michael@0 376 return nullptr;
michael@0 377 }
michael@0 378
michael@0 379 private:
michael@0 380 static const uintptr_t XPCOMCallbackFlag = 1u;
michael@0 381
michael@0 382 friend void
michael@0 383 ImplCycleCollectionUnlink<WebIDLCallbackT,
michael@0 384 XPCOMCallbackT>(CallbackObjectHolder& aField);
michael@0 385
michael@0 386 void UnlinkSelf()
michael@0 387 {
michael@0 388 // NS_IF_RELEASE because we might have been unlinked before
michael@0 389 nsISupports* ptr = GetISupports();
michael@0 390 NS_IF_RELEASE(ptr);
michael@0 391 mPtrBits = 0;
michael@0 392 }
michael@0 393
michael@0 394 uintptr_t mPtrBits;
michael@0 395 };
michael@0 396
michael@0 397 NS_DEFINE_STATIC_IID_ACCESSOR(CallbackObject, DOM_CALLBACKOBJECT_IID)
michael@0 398
michael@0 399 template<class T, class U>
michael@0 400 inline void
michael@0 401 ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
michael@0 402 CallbackObjectHolder<T, U>& aField,
michael@0 403 const char* aName,
michael@0 404 uint32_t aFlags = 0)
michael@0 405 {
michael@0 406 CycleCollectionNoteChild(aCallback, aField.GetISupports(), aName, aFlags);
michael@0 407 }
michael@0 408
michael@0 409 template<class T, class U>
michael@0 410 void
michael@0 411 ImplCycleCollectionUnlink(CallbackObjectHolder<T, U>& aField)
michael@0 412 {
michael@0 413 aField.UnlinkSelf();
michael@0 414 }
michael@0 415
michael@0 416 } // namespace dom
michael@0 417 } // namespace mozilla
michael@0 418
michael@0 419 #endif // mozilla_dom_CallbackObject_h

mercurial