michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set ts=8 sts=4 et sw=4 tw=99: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef xpcObjectHelper_h michael@0: #define xpcObjectHelper_h michael@0: michael@0: // Including 'windows.h' will #define GetClassInfo to something else. michael@0: #ifdef XP_WIN michael@0: #ifdef GetClassInfo michael@0: #undef GetClassInfo michael@0: #endif michael@0: #endif michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIClassInfo.h" michael@0: #include "nsISupports.h" michael@0: #include "nsIXPCScriptable.h" michael@0: #include "nsWrapperCache.h" michael@0: michael@0: class xpcObjectHelper michael@0: { michael@0: public: michael@0: xpcObjectHelper(nsISupports *aObject, nsWrapperCache *aCache = nullptr) michael@0: : mCanonical(nullptr) michael@0: , mObject(aObject) michael@0: , mCache(aCache) michael@0: { michael@0: if (!mCache) { michael@0: if (aObject) michael@0: CallQueryInterface(aObject, &mCache); michael@0: else michael@0: mCache = nullptr; michael@0: } michael@0: } michael@0: michael@0: nsISupports *Object() michael@0: { michael@0: return mObject; michael@0: } michael@0: michael@0: nsISupports *GetCanonical() michael@0: { michael@0: if (!mCanonical) { michael@0: mCanonicalStrong = do_QueryInterface(mObject); michael@0: mCanonical = mCanonicalStrong; michael@0: } michael@0: return mCanonical; michael@0: } michael@0: michael@0: already_AddRefed forgetCanonical() michael@0: { michael@0: MOZ_ASSERT(mCanonical, "Huh, no canonical to forget?"); michael@0: michael@0: if (!mCanonicalStrong) michael@0: mCanonicalStrong = mCanonical; michael@0: mCanonical = nullptr; michael@0: return mCanonicalStrong.forget(); michael@0: } michael@0: michael@0: nsIClassInfo *GetClassInfo() michael@0: { michael@0: if (mXPCClassInfo) michael@0: return mXPCClassInfo; michael@0: if (!mClassInfo) michael@0: mClassInfo = do_QueryInterface(mObject); michael@0: return mClassInfo; michael@0: } michael@0: nsXPCClassInfo *GetXPCClassInfo() michael@0: { michael@0: if (!mXPCClassInfo) { michael@0: CallQueryInterface(mObject, getter_AddRefs(mXPCClassInfo)); michael@0: } michael@0: return mXPCClassInfo; michael@0: } michael@0: michael@0: already_AddRefed forgetXPCClassInfo() michael@0: { michael@0: GetXPCClassInfo(); michael@0: michael@0: return mXPCClassInfo.forget(); michael@0: } michael@0: michael@0: // We assert that we can reach an nsIXPCScriptable somehow. michael@0: uint32_t GetScriptableFlags() michael@0: { michael@0: // Try getting an nsXPCClassInfo - this handles DOM scriptable helpers. michael@0: nsCOMPtr sinfo = GetXPCClassInfo(); michael@0: michael@0: // If that didn't work, try just QI-ing. This handles BackstagePass. michael@0: if (!sinfo) michael@0: sinfo = do_QueryInterface(GetCanonical()); michael@0: michael@0: // We should have something by now. michael@0: MOZ_ASSERT(sinfo); michael@0: michael@0: // Grab the flags. michael@0: return sinfo->GetScriptableFlags(); michael@0: } michael@0: michael@0: nsWrapperCache *GetWrapperCache() michael@0: { michael@0: return mCache; michael@0: } michael@0: michael@0: protected: michael@0: xpcObjectHelper(nsISupports *aObject, nsISupports *aCanonical, michael@0: nsWrapperCache *aCache) michael@0: : mCanonical(aCanonical) michael@0: , mObject(aObject) michael@0: , mCache(aCache) michael@0: { michael@0: if (!mCache && aObject) michael@0: CallQueryInterface(aObject, &mCache); michael@0: } michael@0: michael@0: nsCOMPtr mCanonicalStrong; michael@0: nsISupports* mCanonical; michael@0: michael@0: private: michael@0: xpcObjectHelper(xpcObjectHelper& aOther) MOZ_DELETE; michael@0: michael@0: nsISupports* mObject; michael@0: nsWrapperCache* mCache; michael@0: nsCOMPtr mClassInfo; michael@0: nsRefPtr mXPCClassInfo; michael@0: }; michael@0: michael@0: #endif