Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / base / nsWeakReference.cpp
blob958ef08451b3f0f43c118e5e8e15c8b32cbd72f9
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
23 private:
24 friend class nsSupportsWeakReference;
26 explicit nsWeakReference(nsSupportsWeakReference* aReferent)
27 : nsIWeakReference(aReferent)
28 // ...I can only be constructed by an |nsSupportsWeakReference|
31 ~nsWeakReference()
32 // ...I will only be destroyed by calling |delete| myself.
34 MOZ_WEAKREF_ASSERT_OWNINGTHREAD;
35 if (mObject) {
36 static_cast<nsSupportsWeakReference*>(mObject)->NoticeProxyDestruction();
40 void NoticeReferentDestruction()
41 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
43 MOZ_WEAKREF_ASSERT_OWNINGTHREAD;
44 mObject = nullptr;
48 nsresult nsQueryReferent::operator()(const nsIID& aIID, void** aAnswer) const {
49 nsresult status;
50 if (mWeakPtr) {
51 if (NS_FAILED(status = mWeakPtr->QueryReferent(aIID, aAnswer))) {
52 *aAnswer = 0;
54 } else {
55 status = NS_ERROR_NULL_POINTER;
58 if (mErrorPtr) {
59 *mErrorPtr = status;
61 return status;
64 nsIWeakReference* NS_GetWeakReference(nsISupportsWeakReference* aInstancePtr,
65 nsresult* aErrorPtr) {
66 nsresult status;
68 nsIWeakReference* result = nullptr;
70 if (aInstancePtr) {
71 status = aInstancePtr->GetWeakReference(&result);
72 } else {
73 status = NS_ERROR_NULL_POINTER;
76 if (aErrorPtr) {
77 *aErrorPtr = status;
80 return result;
83 nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
84 NS_GetWeakReference(nsISupports* aInstancePtr, nsresult* aErrorPtr) {
85 nsresult status;
87 nsIWeakReference* result = nullptr;
89 if (aInstancePtr) {
90 nsCOMPtr<nsISupportsWeakReference> factoryPtr =
91 do_QueryInterface(aInstancePtr, &status);
92 if (factoryPtr) {
93 status = factoryPtr->GetWeakReference(&result);
95 // else, |status| has already been set by |do_QueryInterface|
96 } else {
97 status = NS_ERROR_NULL_POINTER;
100 if (aErrorPtr) {
101 *aErrorPtr = status;
103 return result;
106 nsresult nsSupportsWeakReference::GetWeakReference(
107 nsIWeakReference** aInstancePtr) {
108 if (!aInstancePtr) {
109 return NS_ERROR_NULL_POINTER;
112 if (!mProxy) {
113 mProxy = new nsWeakReference(this);
114 } else {
115 MOZ_WEAKREF_ASSERT_OWNINGTHREAD_DELEGATED(mProxy);
117 RefPtr<nsWeakReference> rval = mProxy;
118 rval.forget(aInstancePtr);
120 return NS_OK;
123 NS_IMPL_ISUPPORTS(nsWeakReference, nsIWeakReference)
125 NS_IMETHODIMP
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;
135 if (!mObject) {
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() {
147 if (mProxy) {
148 mProxy->NoticeReferentDestruction();
149 mProxy = nullptr;