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: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsAgg_h___ |
michael@0 | 7 | #define nsAgg_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsISupports.h" |
michael@0 | 10 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 11 | |
michael@0 | 12 | |
michael@0 | 13 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 14 | |
michael@0 | 15 | // Put NS_DECL_AGGREGATED or NS_DECL_CYCLE_COLLECTING_AGGREGATED in your class's |
michael@0 | 16 | // declaration. |
michael@0 | 17 | #define NS_DECL_AGGREGATED \ |
michael@0 | 18 | NS_DECL_ISUPPORTS \ |
michael@0 | 19 | NS_DECL_AGGREGATED_HELPER |
michael@0 | 20 | |
michael@0 | 21 | #define NS_DECL_CYCLE_COLLECTING_AGGREGATED \ |
michael@0 | 22 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS \ |
michael@0 | 23 | NS_DECL_AGGREGATED_HELPER |
michael@0 | 24 | |
michael@0 | 25 | #define NS_DECL_AGGREGATED_HELPER \ |
michael@0 | 26 | public: \ |
michael@0 | 27 | \ |
michael@0 | 28 | /** \ |
michael@0 | 29 | * Returns the nsISupports pointer of the inner object (aka the \ |
michael@0 | 30 | * aggregatee). This pointer is really only useful to the outer object \ |
michael@0 | 31 | * (aka the aggregator), which can use it to hold on to the inner \ |
michael@0 | 32 | * object. Anything else wants the nsISupports pointer of the outer \ |
michael@0 | 33 | * object (gotten by QI'ing inner or outer to nsISupports). This method \ |
michael@0 | 34 | * returns a non-addrefed pointer. \ |
michael@0 | 35 | * @return the nsISupports pointer of the inner object \ |
michael@0 | 36 | */ \ |
michael@0 | 37 | nsISupports* InnerObject(void) { return &fAggregated; } \ |
michael@0 | 38 | \ |
michael@0 | 39 | /** \ |
michael@0 | 40 | * Returns true if this object is part of an aggregated object. \ |
michael@0 | 41 | */ \ |
michael@0 | 42 | bool IsPartOfAggregated(void) { return fOuter != InnerObject(); } \ |
michael@0 | 43 | \ |
michael@0 | 44 | private: \ |
michael@0 | 45 | \ |
michael@0 | 46 | /* You must implement this operation instead of the nsISupports */ \ |
michael@0 | 47 | /* methods. */ \ |
michael@0 | 48 | nsresult \ |
michael@0 | 49 | AggregatedQueryInterface(const nsIID& aIID, void** aInstancePtr); \ |
michael@0 | 50 | \ |
michael@0 | 51 | class Internal : public nsISupports { \ |
michael@0 | 52 | public: \ |
michael@0 | 53 | \ |
michael@0 | 54 | Internal() {} \ |
michael@0 | 55 | \ |
michael@0 | 56 | NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr); \ |
michael@0 | 57 | NS_IMETHOD_(MozExternalRefCountType) AddRef(void); \ |
michael@0 | 58 | NS_IMETHOD_(MozExternalRefCountType) Release(void); \ |
michael@0 | 59 | \ |
michael@0 | 60 | NS_DECL_OWNINGTHREAD \ |
michael@0 | 61 | }; \ |
michael@0 | 62 | \ |
michael@0 | 63 | friend class Internal; \ |
michael@0 | 64 | \ |
michael@0 | 65 | nsISupports* fOuter; \ |
michael@0 | 66 | Internal fAggregated; \ |
michael@0 | 67 | \ |
michael@0 | 68 | public: \ |
michael@0 | 69 | |
michael@0 | 70 | #define NS_DECL_AGGREGATED_CYCLE_COLLECTION_CLASS(_class) \ |
michael@0 | 71 | class NS_CYCLE_COLLECTION_INNERCLASS \ |
michael@0 | 72 | : public nsXPCOMCycleCollectionParticipant \ |
michael@0 | 73 | { \ |
michael@0 | 74 | public: \ |
michael@0 | 75 | NS_IMETHOD_(void) Unlink(void *p); \ |
michael@0 | 76 | NS_IMETHOD Traverse(void *p, nsCycleCollectionTraversalCallback &cb); \ |
michael@0 | 77 | NS_IMETHOD_(void) DeleteCycleCollectable(void* p) \ |
michael@0 | 78 | { \ |
michael@0 | 79 | NS_CYCLE_COLLECTION_CLASSNAME(_class):: \ |
michael@0 | 80 | Downcast(static_cast<nsISupports*>(p))->DeleteCycleCollectable(); \ |
michael@0 | 81 | } \ |
michael@0 | 82 | static _class* Downcast(nsISupports* s) \ |
michael@0 | 83 | { \ |
michael@0 | 84 | return (_class*)((char*)(s) - offsetof(_class, fAggregated)); \ |
michael@0 | 85 | } \ |
michael@0 | 86 | static nsISupports* Upcast(_class *p) \ |
michael@0 | 87 | { \ |
michael@0 | 88 | return p->InnerObject(); \ |
michael@0 | 89 | } \ |
michael@0 | 90 | static nsXPCOMCycleCollectionParticipant* GetParticipant() \ |
michael@0 | 91 | { \ |
michael@0 | 92 | return &_class::NS_CYCLE_COLLECTION_INNERNAME; \ |
michael@0 | 93 | } \ |
michael@0 | 94 | }; \ |
michael@0 | 95 | NS_CHECK_FOR_RIGHT_PARTICIPANT_IMPL(_class); \ |
michael@0 | 96 | static NS_CYCLE_COLLECTION_INNERCLASS NS_CYCLE_COLLECTION_INNERNAME; |
michael@0 | 97 | |
michael@0 | 98 | // Put this in your class's constructor: |
michael@0 | 99 | #define NS_INIT_AGGREGATED(outer) \ |
michael@0 | 100 | PR_BEGIN_MACRO \ |
michael@0 | 101 | fOuter = outer ? outer : &fAggregated; \ |
michael@0 | 102 | PR_END_MACRO |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | // Put this in your class's implementation file: |
michael@0 | 106 | #define NS_IMPL_AGGREGATED(_class) \ |
michael@0 | 107 | \ |
michael@0 | 108 | NS_IMPL_AGGREGATED_HELPER(_class) \ |
michael@0 | 109 | \ |
michael@0 | 110 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 111 | _class::Internal::AddRef(void) \ |
michael@0 | 112 | { \ |
michael@0 | 113 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \ |
michael@0 | 114 | MOZ_ASSERT(int32_t(agg->mRefCnt) >= 0, "illegal refcnt"); \ |
michael@0 | 115 | NS_ASSERT_OWNINGTHREAD(_class); \ |
michael@0 | 116 | ++agg->mRefCnt; \ |
michael@0 | 117 | NS_LOG_ADDREF(this, agg->mRefCnt, #_class, sizeof(*this)); \ |
michael@0 | 118 | return agg->mRefCnt; \ |
michael@0 | 119 | } \ |
michael@0 | 120 | \ |
michael@0 | 121 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 122 | _class::Internal::Release(void) \ |
michael@0 | 123 | { \ |
michael@0 | 124 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \ |
michael@0 | 125 | MOZ_ASSERT(int32_t(agg->mRefCnt) > 0, "dup release"); \ |
michael@0 | 126 | NS_ASSERT_OWNINGTHREAD(_class); \ |
michael@0 | 127 | --agg->mRefCnt; \ |
michael@0 | 128 | NS_LOG_RELEASE(this, agg->mRefCnt, #_class); \ |
michael@0 | 129 | if (agg->mRefCnt == 0) { \ |
michael@0 | 130 | agg->mRefCnt = 1; /* stabilize */ \ |
michael@0 | 131 | delete agg; \ |
michael@0 | 132 | return 0; \ |
michael@0 | 133 | } \ |
michael@0 | 134 | return agg->mRefCnt; \ |
michael@0 | 135 | } \ |
michael@0 | 136 | |
michael@0 | 137 | #define NS_IMPL_CYCLE_COLLECTING_AGGREGATED(_class) \ |
michael@0 | 138 | \ |
michael@0 | 139 | NS_IMPL_AGGREGATED_HELPER(_class) \ |
michael@0 | 140 | \ |
michael@0 | 141 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 142 | _class::Internal::AddRef(void) \ |
michael@0 | 143 | { \ |
michael@0 | 144 | _class* agg = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Downcast(this); \ |
michael@0 | 145 | MOZ_ASSERT(int32_t(agg->mRefCnt) >= 0, "illegal refcnt"); \ |
michael@0 | 146 | NS_ASSERT_OWNINGTHREAD_AGGREGATE(agg, _class); \ |
michael@0 | 147 | nsrefcnt count = agg->mRefCnt.incr(this); \ |
michael@0 | 148 | NS_LOG_ADDREF(this, count, #_class, sizeof(*agg)); \ |
michael@0 | 149 | return count; \ |
michael@0 | 150 | } \ |
michael@0 | 151 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 152 | _class::Internal::Release(void) \ |
michael@0 | 153 | { \ |
michael@0 | 154 | _class* agg = NS_CYCLE_COLLECTION_CLASSNAME(_class)::Downcast(this); \ |
michael@0 | 155 | MOZ_ASSERT(int32_t(agg->mRefCnt) > 0, "dup release"); \ |
michael@0 | 156 | NS_ASSERT_OWNINGTHREAD_AGGREGATE(agg, _class); \ |
michael@0 | 157 | nsrefcnt count = agg->mRefCnt.decr(this); \ |
michael@0 | 158 | NS_LOG_RELEASE(this, count, #_class); \ |
michael@0 | 159 | return count; \ |
michael@0 | 160 | } \ |
michael@0 | 161 | NS_IMETHODIMP_(void) \ |
michael@0 | 162 | _class::DeleteCycleCollectable(void) \ |
michael@0 | 163 | { \ |
michael@0 | 164 | delete this; \ |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | #define NS_IMPL_AGGREGATED_HELPER(_class) \ |
michael@0 | 168 | NS_IMETHODIMP \ |
michael@0 | 169 | _class::QueryInterface(const nsIID& aIID, void** aInstancePtr) \ |
michael@0 | 170 | { \ |
michael@0 | 171 | return fOuter->QueryInterface(aIID, aInstancePtr); \ |
michael@0 | 172 | } \ |
michael@0 | 173 | \ |
michael@0 | 174 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 175 | _class::AddRef(void) \ |
michael@0 | 176 | { \ |
michael@0 | 177 | return fOuter->AddRef(); \ |
michael@0 | 178 | } \ |
michael@0 | 179 | \ |
michael@0 | 180 | NS_IMETHODIMP_(MozExternalRefCountType) \ |
michael@0 | 181 | _class::Release(void) \ |
michael@0 | 182 | { \ |
michael@0 | 183 | return fOuter->Release(); \ |
michael@0 | 184 | } \ |
michael@0 | 185 | \ |
michael@0 | 186 | NS_IMETHODIMP \ |
michael@0 | 187 | _class::Internal::QueryInterface(const nsIID& aIID, void** aInstancePtr) \ |
michael@0 | 188 | { \ |
michael@0 | 189 | _class* agg = (_class*)((char*)(this) - offsetof(_class, fAggregated)); \ |
michael@0 | 190 | return agg->AggregatedQueryInterface(aIID, aInstancePtr); \ |
michael@0 | 191 | } \ |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * To make aggregated objects participate in cycle collection we need to enable |
michael@0 | 195 | * the outer object (aggregator) to traverse/unlink the objects held by the |
michael@0 | 196 | * inner object (the aggregatee). We can't just make the inner object QI'able to |
michael@0 | 197 | * NS_CYCLECOLLECTIONPARTICIPANT_IID, we don't want to return the inner object's |
michael@0 | 198 | * nsCycleCollectionParticipant for the outer object (which will happen if the |
michael@0 | 199 | * outer object doesn't participate in cycle collection itself). |
michael@0 | 200 | * NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID enables the outer object to get |
michael@0 | 201 | * the inner objects nsCycleCollectionParticipant. |
michael@0 | 202 | * |
michael@0 | 203 | * There are three cases: |
michael@0 | 204 | * - No aggregation |
michael@0 | 205 | * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will return the inner |
michael@0 | 206 | * object's nsCycleCollectionParticipant. |
michael@0 | 207 | * |
michael@0 | 208 | * - Aggregation and outer object does not participate in cycle collection |
michael@0 | 209 | * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will not return anything. |
michael@0 | 210 | * |
michael@0 | 211 | * - Aggregation and outer object does participate in cycle collection |
michael@0 | 212 | * QI'ing to NS_CYCLECOLLECTIONPARTICIPANT_IID will return the outer |
michael@0 | 213 | * object's nsCycleCollectionParticipant. The outer object's |
michael@0 | 214 | * nsCycleCollectionParticipant can then QI the inner object to |
michael@0 | 215 | * NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID to get the inner object's |
michael@0 | 216 | * nsCycleCollectionParticipant, which it can use to traverse/unlink the |
michael@0 | 217 | * objects reachable from the inner object. |
michael@0 | 218 | */ |
michael@0 | 219 | #define NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID \ |
michael@0 | 220 | { \ |
michael@0 | 221 | 0x32889b7e, \ |
michael@0 | 222 | 0xe4fe, \ |
michael@0 | 223 | 0x43f4, \ |
michael@0 | 224 | { 0x85, 0x31, 0xb5, 0x28, 0x23, 0xa2, 0xe9, 0xfc } \ |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | /** |
michael@0 | 228 | * Just holds the IID so NS_GET_IID works. |
michael@0 | 229 | */ |
michael@0 | 230 | class nsAggregatedCycleCollectionParticipant |
michael@0 | 231 | { |
michael@0 | 232 | public: |
michael@0 | 233 | NS_DECLARE_STATIC_IID_ACCESSOR(NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID) |
michael@0 | 234 | }; |
michael@0 | 235 | |
michael@0 | 236 | NS_DEFINE_STATIC_IID_ACCESSOR(nsAggregatedCycleCollectionParticipant, |
michael@0 | 237 | NS_AGGREGATED_CYCLECOLLECTIONPARTICIPANT_IID) |
michael@0 | 238 | |
michael@0 | 239 | // for use with QI macros in nsISupportsUtils.h: |
michael@0 | 240 | |
michael@0 | 241 | #define NS_INTERFACE_MAP_BEGIN_AGGREGATED(_class) \ |
michael@0 | 242 | NS_IMPL_AGGREGATED_QUERY_HEAD(_class) |
michael@0 | 243 | |
michael@0 | 244 | #define NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_AGGREGATED(_class) \ |
michael@0 | 245 | NS_IMPL_QUERY_CYCLE_COLLECTION(_class) |
michael@0 | 246 | |
michael@0 | 247 | #define NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION_AGGREGATED(_class) \ |
michael@0 | 248 | NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_AGGREGATED(_class) \ |
michael@0 | 249 | NS_INTERFACE_MAP_ENTRY_CYCLE_COLLECTION_ISUPPORTS(_class) |
michael@0 | 250 | |
michael@0 | 251 | #define NS_IMPL_AGGREGATED_QUERY_HEAD(_class) \ |
michael@0 | 252 | nsresult \ |
michael@0 | 253 | _class::AggregatedQueryInterface(REFNSIID aIID, void** aInstancePtr) \ |
michael@0 | 254 | { \ |
michael@0 | 255 | NS_ASSERTION(aInstancePtr, \ |
michael@0 | 256 | "AggregatedQueryInterface requires a non-NULL result ptr!"); \ |
michael@0 | 257 | if ( !aInstancePtr ) \ |
michael@0 | 258 | return NS_ERROR_NULL_POINTER; \ |
michael@0 | 259 | nsISupports* foundInterface; \ |
michael@0 | 260 | if ( aIID.Equals(NS_GET_IID(nsISupports)) ) \ |
michael@0 | 261 | foundInterface = InnerObject(); \ |
michael@0 | 262 | else |
michael@0 | 263 | |
michael@0 | 264 | #define NS_IMPL_AGGREGATED_QUERY_CYCLE_COLLECTION(_class) \ |
michael@0 | 265 | if (aIID.Equals(IsPartOfAggregated() ? \ |
michael@0 | 266 | NS_GET_IID(nsCycleCollectionParticipant) : \ |
michael@0 | 267 | NS_GET_IID(nsAggregatedCycleCollectionParticipant))) \ |
michael@0 | 268 | foundInterface = NS_CYCLE_COLLECTION_PARTICIPANT(_class); \ |
michael@0 | 269 | else |
michael@0 | 270 | |
michael@0 | 271 | #define NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_AGGREGATED(_class) \ |
michael@0 | 272 | NS_IMETHODIMP \ |
michael@0 | 273 | NS_CYCLE_COLLECTION_CLASSNAME(_class)::Traverse \ |
michael@0 | 274 | (void *p, nsCycleCollectionTraversalCallback &cb) \ |
michael@0 | 275 | { \ |
michael@0 | 276 | nsISupports *s = static_cast<nsISupports*>(p); \ |
michael@0 | 277 | MOZ_ASSERT(CheckForRightISupports(s), \ |
michael@0 | 278 | "not the nsISupports pointer we expect"); \ |
michael@0 | 279 | _class *tmp = static_cast<_class*>(Downcast(s)); \ |
michael@0 | 280 | if (!tmp->IsPartOfAggregated()) \ |
michael@0 | 281 | NS_IMPL_CYCLE_COLLECTION_DESCRIBE(_class, tmp->mRefCnt.get()) |
michael@0 | 282 | |
michael@0 | 283 | #define NS_GENERIC_AGGREGATED_CONSTRUCTOR(_InstanceClass) \ |
michael@0 | 284 | static nsresult \ |
michael@0 | 285 | _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ |
michael@0 | 286 | void **aResult) \ |
michael@0 | 287 | { \ |
michael@0 | 288 | *aResult = nullptr; \ |
michael@0 | 289 | if (NS_WARN_IF(aOuter && !aIID.Equals(NS_GET_IID(nsISupports)))) \ |
michael@0 | 290 | return NS_ERROR_INVALID_ARG; \ |
michael@0 | 291 | \ |
michael@0 | 292 | _InstanceClass* inst = new _InstanceClass(aOuter); \ |
michael@0 | 293 | if (!inst) { \ |
michael@0 | 294 | return NS_ERROR_OUT_OF_MEMORY; \ |
michael@0 | 295 | } \ |
michael@0 | 296 | \ |
michael@0 | 297 | nsISupports* inner = inst->InnerObject(); \ |
michael@0 | 298 | nsresult rv = inner->QueryInterface(aIID, aResult); \ |
michael@0 | 299 | if (NS_FAILED(rv)) { \ |
michael@0 | 300 | delete inst; \ |
michael@0 | 301 | } \ |
michael@0 | 302 | \ |
michael@0 | 303 | return rv; \ |
michael@0 | 304 | } \ |
michael@0 | 305 | |
michael@0 | 306 | #define NS_GENERIC_AGGREGATED_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \ |
michael@0 | 307 | static nsresult \ |
michael@0 | 308 | _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ |
michael@0 | 309 | void **aResult) \ |
michael@0 | 310 | { \ |
michael@0 | 311 | *aResult = nullptr; \ |
michael@0 | 312 | if (NS_WARN_IF(aOuter && !aIID.Equals(NS_GET_IID(nsISupports)))) \ |
michael@0 | 313 | return NS_ERROR_INVALID_ARG; \ |
michael@0 | 314 | \ |
michael@0 | 315 | _InstanceClass* inst = new _InstanceClass(aOuter); \ |
michael@0 | 316 | if (!inst) { \ |
michael@0 | 317 | return NS_ERROR_OUT_OF_MEMORY; \ |
michael@0 | 318 | } \ |
michael@0 | 319 | \ |
michael@0 | 320 | nsISupports* inner = inst->InnerObject(); \ |
michael@0 | 321 | NS_ADDREF(inner); \ |
michael@0 | 322 | nsresult rv = inst->_InitMethod(); \ |
michael@0 | 323 | if (NS_SUCCEEDED(rv)) { \ |
michael@0 | 324 | rv = inner->QueryInterface(aIID, aResult); \ |
michael@0 | 325 | } \ |
michael@0 | 326 | NS_RELEASE(inner); \ |
michael@0 | 327 | \ |
michael@0 | 328 | return rv; \ |
michael@0 | 329 | } \ |
michael@0 | 330 | |
michael@0 | 331 | #endif /* nsAgg_h___ */ |