Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=4 et sw=4 tw=99: */ |
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 | #ifndef xpcObjectHelper_h |
michael@0 | 8 | #define xpcObjectHelper_h |
michael@0 | 9 | |
michael@0 | 10 | // Including 'windows.h' will #define GetClassInfo to something else. |
michael@0 | 11 | #ifdef XP_WIN |
michael@0 | 12 | #ifdef GetClassInfo |
michael@0 | 13 | #undef GetClassInfo |
michael@0 | 14 | #endif |
michael@0 | 15 | #endif |
michael@0 | 16 | |
michael@0 | 17 | #include "mozilla/Attributes.h" |
michael@0 | 18 | #include <stdint.h> |
michael@0 | 19 | #include "nsAutoPtr.h" |
michael@0 | 20 | #include "nsCOMPtr.h" |
michael@0 | 21 | #include "nsIClassInfo.h" |
michael@0 | 22 | #include "nsISupports.h" |
michael@0 | 23 | #include "nsIXPCScriptable.h" |
michael@0 | 24 | #include "nsWrapperCache.h" |
michael@0 | 25 | |
michael@0 | 26 | class xpcObjectHelper |
michael@0 | 27 | { |
michael@0 | 28 | public: |
michael@0 | 29 | xpcObjectHelper(nsISupports *aObject, nsWrapperCache *aCache = nullptr) |
michael@0 | 30 | : mCanonical(nullptr) |
michael@0 | 31 | , mObject(aObject) |
michael@0 | 32 | , mCache(aCache) |
michael@0 | 33 | { |
michael@0 | 34 | if (!mCache) { |
michael@0 | 35 | if (aObject) |
michael@0 | 36 | CallQueryInterface(aObject, &mCache); |
michael@0 | 37 | else |
michael@0 | 38 | mCache = nullptr; |
michael@0 | 39 | } |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | nsISupports *Object() |
michael@0 | 43 | { |
michael@0 | 44 | return mObject; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | nsISupports *GetCanonical() |
michael@0 | 48 | { |
michael@0 | 49 | if (!mCanonical) { |
michael@0 | 50 | mCanonicalStrong = do_QueryInterface(mObject); |
michael@0 | 51 | mCanonical = mCanonicalStrong; |
michael@0 | 52 | } |
michael@0 | 53 | return mCanonical; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | already_AddRefed<nsISupports> forgetCanonical() |
michael@0 | 57 | { |
michael@0 | 58 | MOZ_ASSERT(mCanonical, "Huh, no canonical to forget?"); |
michael@0 | 59 | |
michael@0 | 60 | if (!mCanonicalStrong) |
michael@0 | 61 | mCanonicalStrong = mCanonical; |
michael@0 | 62 | mCanonical = nullptr; |
michael@0 | 63 | return mCanonicalStrong.forget(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | nsIClassInfo *GetClassInfo() |
michael@0 | 67 | { |
michael@0 | 68 | if (mXPCClassInfo) |
michael@0 | 69 | return mXPCClassInfo; |
michael@0 | 70 | if (!mClassInfo) |
michael@0 | 71 | mClassInfo = do_QueryInterface(mObject); |
michael@0 | 72 | return mClassInfo; |
michael@0 | 73 | } |
michael@0 | 74 | nsXPCClassInfo *GetXPCClassInfo() |
michael@0 | 75 | { |
michael@0 | 76 | if (!mXPCClassInfo) { |
michael@0 | 77 | CallQueryInterface(mObject, getter_AddRefs(mXPCClassInfo)); |
michael@0 | 78 | } |
michael@0 | 79 | return mXPCClassInfo; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | already_AddRefed<nsXPCClassInfo> forgetXPCClassInfo() |
michael@0 | 83 | { |
michael@0 | 84 | GetXPCClassInfo(); |
michael@0 | 85 | |
michael@0 | 86 | return mXPCClassInfo.forget(); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | // We assert that we can reach an nsIXPCScriptable somehow. |
michael@0 | 90 | uint32_t GetScriptableFlags() |
michael@0 | 91 | { |
michael@0 | 92 | // Try getting an nsXPCClassInfo - this handles DOM scriptable helpers. |
michael@0 | 93 | nsCOMPtr<nsIXPCScriptable> sinfo = GetXPCClassInfo(); |
michael@0 | 94 | |
michael@0 | 95 | // If that didn't work, try just QI-ing. This handles BackstagePass. |
michael@0 | 96 | if (!sinfo) |
michael@0 | 97 | sinfo = do_QueryInterface(GetCanonical()); |
michael@0 | 98 | |
michael@0 | 99 | // We should have something by now. |
michael@0 | 100 | MOZ_ASSERT(sinfo); |
michael@0 | 101 | |
michael@0 | 102 | // Grab the flags. |
michael@0 | 103 | return sinfo->GetScriptableFlags(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | nsWrapperCache *GetWrapperCache() |
michael@0 | 107 | { |
michael@0 | 108 | return mCache; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | protected: |
michael@0 | 112 | xpcObjectHelper(nsISupports *aObject, nsISupports *aCanonical, |
michael@0 | 113 | nsWrapperCache *aCache) |
michael@0 | 114 | : mCanonical(aCanonical) |
michael@0 | 115 | , mObject(aObject) |
michael@0 | 116 | , mCache(aCache) |
michael@0 | 117 | { |
michael@0 | 118 | if (!mCache && aObject) |
michael@0 | 119 | CallQueryInterface(aObject, &mCache); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | nsCOMPtr<nsISupports> mCanonicalStrong; |
michael@0 | 123 | nsISupports* mCanonical; |
michael@0 | 124 | |
michael@0 | 125 | private: |
michael@0 | 126 | xpcObjectHelper(xpcObjectHelper& aOther) MOZ_DELETE; |
michael@0 | 127 | |
michael@0 | 128 | nsISupports* mObject; |
michael@0 | 129 | nsWrapperCache* mCache; |
michael@0 | 130 | nsCOMPtr<nsIClassInfo> mClassInfo; |
michael@0 | 131 | nsRefPtr<nsXPCClassInfo> mXPCClassInfo; |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | #endif |