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
24 friend class nsSupportsWeakReference
;
26 explicit nsWeakReference(nsSupportsWeakReference
* aReferent
)
27 : nsIWeakReference(aReferent
)
28 // ...I can only be constructed by an |nsSupportsWeakReference|
32 // ...I will only be destroyed by calling |delete| myself.
34 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
36 static_cast<nsSupportsWeakReference
*>(mObject
)->NoticeProxyDestruction();
40 void NoticeReferentDestruction()
41 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
43 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
48 nsresult
nsQueryReferent::operator()(const nsIID
& aIID
, void** aAnswer
) const {
51 if (NS_FAILED(status
= mWeakPtr
->QueryReferent(aIID
, aAnswer
))) {
55 status
= NS_ERROR_NULL_POINTER
;
64 nsIWeakReference
* NS_GetWeakReference(nsISupportsWeakReference
* aInstancePtr
,
65 nsresult
* aErrorPtr
) {
68 nsIWeakReference
* result
= nullptr;
71 status
= aInstancePtr
->GetWeakReference(&result
);
73 status
= NS_ERROR_NULL_POINTER
;
83 nsIWeakReference
* // or else |already_AddRefed<nsIWeakReference>|
84 NS_GetWeakReference(nsISupports
* aInstancePtr
, nsresult
* aErrorPtr
) {
87 nsIWeakReference
* result
= nullptr;
90 nsCOMPtr
<nsISupportsWeakReference
> factoryPtr
=
91 do_QueryInterface(aInstancePtr
, &status
);
93 status
= factoryPtr
->GetWeakReference(&result
);
95 // else, |status| has already been set by |do_QueryInterface|
97 status
= NS_ERROR_NULL_POINTER
;
106 nsresult
nsSupportsWeakReference::GetWeakReference(
107 nsIWeakReference
** aInstancePtr
) {
109 return NS_ERROR_NULL_POINTER
;
113 mProxy
= new nsWeakReference(this);
115 MOZ_WEAKREF_ASSERT_OWNINGTHREAD_DELEGATED(mProxy
);
117 RefPtr
<nsWeakReference
> rval
= mProxy
;
118 rval
.forget(aInstancePtr
);
123 NS_IMPL_ISUPPORTS(nsWeakReference
, nsIWeakReference
)
126 nsWeakReference::QueryReferentFromScript(const nsIID
& aIID
,
127 void** aInstancePtr
) {
128 return QueryReferent(aIID
, aInstancePtr
);
131 nsresult
nsIWeakReference::QueryReferent(const nsIID
& aIID
,
132 void** aInstancePtr
) {
133 MOZ_WEAKREF_ASSERT_OWNINGTHREAD
;
136 return NS_ERROR_NULL_POINTER
;
139 return mObject
->QueryInterface(aIID
, aInstancePtr
);
142 size_t nsWeakReference::SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf
) {
143 return aMallocSizeOf(this);
146 void nsSupportsWeakReference::ClearWeakReferences() {
148 mProxy
->NoticeReferentDestruction();