Bug 1686838 [wpt PR 27194] - [webcodecs] Deprecate VideoFrame.destroy()., a=testonly
[gecko.git] / xpcom / base / nsWeakReference.cpp
blob6a37c8cb990eb00457408b9253c82eb67fcaec5e
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/. */
7 // nsWeakReference.cpp
9 #include "mozilla/Attributes.h"
11 #include "nsWeakReference.h"
12 #include "nsCOMPtr.h"
13 #include "nsDebug.h"
15 class nsWeakReference final : public nsIWeakReference {
16 public:
17 // nsISupports...
18 NS_DECL_ISUPPORTS
20 // nsIWeakReference...
21 NS_DECL_NSIWEAKREFERENCE
22 size_t SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf) const override;
24 private:
25 friend class nsSupportsWeakReference;
27 explicit nsWeakReference(nsSupportsWeakReference* aReferent)
28 : nsIWeakReference(aReferent)
29 // ...I can only be constructed by an |nsSupportsWeakReference|
32 ~nsWeakReference()
33 // ...I will only be destroyed by calling |delete| myself.
35 MOZ_WEAKREF_ASSERT_OWNINGTHREAD;
36 if (mObject) {
37 static_cast<nsSupportsWeakReference*>(mObject)->NoticeProxyDestruction();
41 void NoticeReferentDestruction()
42 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
44 MOZ_WEAKREF_ASSERT_OWNINGTHREAD;
45 mObject = nullptr;
49 nsresult nsQueryReferent::operator()(const nsIID& aIID, void** aAnswer) const {
50 nsresult status;
51 if (mWeakPtr) {
52 if (NS_FAILED(status = mWeakPtr->QueryReferent(aIID, aAnswer))) {
53 *aAnswer = 0;
55 } else {
56 status = NS_ERROR_NULL_POINTER;
59 if (mErrorPtr) {
60 *mErrorPtr = status;
62 return status;
65 nsIWeakReference* NS_GetWeakReference(nsISupportsWeakReference* aInstancePtr,
66 nsresult* aErrorPtr) {
67 nsresult status;
69 nsIWeakReference* result = nullptr;
71 if (aInstancePtr) {
72 status = aInstancePtr->GetWeakReference(&result);
73 } else {
74 status = NS_ERROR_NULL_POINTER;
77 if (aErrorPtr) {
78 *aErrorPtr = status;
81 return result;
84 nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
85 NS_GetWeakReference(nsISupports* aInstancePtr, nsresult* aErrorPtr) {
86 nsresult status;
88 nsIWeakReference* result = nullptr;
90 if (aInstancePtr) {
91 nsCOMPtr<nsISupportsWeakReference> factoryPtr =
92 do_QueryInterface(aInstancePtr, &status);
93 if (factoryPtr) {
94 status = factoryPtr->GetWeakReference(&result);
96 // else, |status| has already been set by |do_QueryInterface|
97 } else {
98 status = NS_ERROR_NULL_POINTER;
101 if (aErrorPtr) {
102 *aErrorPtr = status;
104 return result;
107 nsresult nsSupportsWeakReference::GetWeakReference(
108 nsIWeakReference** aInstancePtr) {
109 if (!aInstancePtr) {
110 return NS_ERROR_NULL_POINTER;
113 if (!mProxy) {
114 mProxy = new nsWeakReference(this);
115 } else {
116 MOZ_WEAKREF_ASSERT_OWNINGTHREAD_DELEGATED(mProxy);
118 *aInstancePtr = mProxy;
120 nsresult status;
121 if (!*aInstancePtr) {
122 status = NS_ERROR_OUT_OF_MEMORY;
123 } else {
124 NS_ADDREF(*aInstancePtr);
125 status = NS_OK;
128 return status;
131 NS_IMPL_ISUPPORTS(nsWeakReference, nsIWeakReference)
133 NS_IMETHODIMP
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;
143 if (!mObject) {
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() {
156 if (mProxy) {
157 mProxy->NoticeReferentDestruction();
158 mProxy = nullptr;