1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
9 #include "mozilla/Attributes.h"
11 #include "nsWeakReference.h"
15 class nsWeakReference final
: public nsIWeakReference
{
20 // nsIWeakReference...
21 NS_DECL_NSIWEAKREFERENCE
22 size_t SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf
) const override
;
25 friend class nsSupportsWeakReference
;
27 explicit nsWeakReference(nsSupportsWeakReference
* aReferent
)
28 : nsIWeakReference(aReferent
)
29 // ...I can only be constructed by an |nsSupportsWeakReference|
33 // ...I will only be destroyed by calling |delete| myself.
35 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
37 static_cast<nsSupportsWeakReference
*>(mObject
)->NoticeProxyDestruction();
41 void NoticeReferentDestruction()
42 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
44 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
49 nsresult
nsQueryReferent::operator()(const nsIID
& aIID
, void** aAnswer
) const {
52 if (NS_FAILED(status
= mWeakPtr
->QueryReferent(aIID
, aAnswer
))) {
56 status
= NS_ERROR_NULL_POINTER
;
65 nsIWeakReference
* NS_GetWeakReference(nsISupportsWeakReference
* aInstancePtr
,
66 nsresult
* aErrorPtr
) {
69 nsIWeakReference
* result
= nullptr;
72 status
= aInstancePtr
->GetWeakReference(&result
);
74 status
= NS_ERROR_NULL_POINTER
;
84 nsIWeakReference
* // or else |already_AddRefed<nsIWeakReference>|
85 NS_GetWeakReference(nsISupports
* aInstancePtr
, nsresult
* aErrorPtr
) {
88 nsIWeakReference
* result
= nullptr;
91 nsCOMPtr
<nsISupportsWeakReference
> factoryPtr
=
92 do_QueryInterface(aInstancePtr
, &status
);
94 status
= factoryPtr
->GetWeakReference(&result
);
96 // else, |status| has already been set by |do_QueryInterface|
98 status
= NS_ERROR_NULL_POINTER
;
107 nsresult
nsSupportsWeakReference::GetWeakReference(
108 nsIWeakReference
** aInstancePtr
) {
110 return NS_ERROR_NULL_POINTER
;
114 mProxy
= new nsWeakReference(this);
116 MOZ_WEAKREF_ASSERT_OWNINGTHREAD_DELEGATED(mProxy
);
118 *aInstancePtr
= mProxy
;
121 if (!*aInstancePtr
) {
122 status
= NS_ERROR_OUT_OF_MEMORY
;
124 NS_ADDREF(*aInstancePtr
);
131 NS_IMPL_ISUPPORTS(nsWeakReference
, nsIWeakReference
)
134 nsWeakReference::QueryReferentFromScript(const nsIID
& aIID
,
135 void** aInstancePtr
) {
136 return QueryReferent(aIID
, aInstancePtr
);
139 nsresult
nsIWeakReference::QueryReferent(const nsIID
& aIID
,
140 void** aInstancePtr
) {
141 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
144 return NS_ERROR_NULL_POINTER
;
147 return mObject
->QueryInterface(aIID
, aInstancePtr
);
150 size_t nsWeakReference::SizeOfOnlyThis(
151 mozilla::MallocSizeOf aMallocSizeOf
) const {
152 return aMallocSizeOf(this);
155 void nsSupportsWeakReference::ClearWeakReferences() {
157 mProxy
->NoticeReferentDestruction();