Bumping manifests a=b2g-bump
[gecko.git] / xpcom / glue / nsWeakReference.cpp
blob480b8eebd74678c05d271756f4926c40a0e363b7
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"
14 class nsWeakReference MOZ_FINAL : public nsIWeakReference
16 public:
17 // nsISupports...
18 NS_DECL_ISUPPORTS
20 // nsIWeakReference...
21 NS_DECL_NSIWEAKREFERENCE
22 virtual size_t SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf) const;
24 private:
25 friend class nsSupportsWeakReference;
27 explicit nsWeakReference(nsSupportsWeakReference* aReferent)
28 : mReferent(aReferent)
29 // ...I can only be constructed by an |nsSupportsWeakReference|
33 ~nsWeakReference()
34 // ...I will only be destroyed by calling |delete| myself.
36 if (mReferent) {
37 mReferent->NoticeProxyDestruction();
41 void
42 NoticeReferentDestruction()
43 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
45 mReferent = 0;
48 nsSupportsWeakReference* mReferent;
51 nsresult
52 nsQueryReferent::operator()(const nsIID& aIID, void** aAnswer) const
54 nsresult status;
55 if (mWeakPtr) {
56 if (NS_FAILED(status = mWeakPtr->QueryReferent(aIID, aAnswer))) {
57 *aAnswer = 0;
59 } else {
60 status = NS_ERROR_NULL_POINTER;
63 if (mErrorPtr) {
64 *mErrorPtr = status;
66 return status;
69 nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
70 NS_GetWeakReference(nsISupports* aInstancePtr, nsresult* aErrorPtr)
72 nsresult status;
74 nsIWeakReference* result = nullptr;
76 if (aInstancePtr) {
77 nsCOMPtr<nsISupportsWeakReference> factoryPtr =
78 do_QueryInterface(aInstancePtr, &status);
79 if (factoryPtr) {
80 status = factoryPtr->GetWeakReference(&result);
82 // else, |status| has already been set by |do_QueryInterface|
83 } else {
84 status = NS_ERROR_NULL_POINTER;
87 if (aErrorPtr) {
88 *aErrorPtr = status;
90 return result;
93 nsresult
94 nsSupportsWeakReference::GetWeakReference(nsIWeakReference** aInstancePtr)
96 if (!aInstancePtr) {
97 return NS_ERROR_NULL_POINTER;
100 if (!mProxy) {
101 mProxy = new nsWeakReference(this);
103 *aInstancePtr = mProxy;
105 nsresult status;
106 if (!*aInstancePtr) {
107 status = NS_ERROR_OUT_OF_MEMORY;
108 } else {
109 NS_ADDREF(*aInstancePtr);
110 status = NS_OK;
113 return status;
116 NS_IMPL_ISUPPORTS(nsWeakReference, nsIWeakReference)
118 NS_IMETHODIMP
119 nsWeakReference::QueryReferent(const nsIID& aIID, void** aInstancePtr)
121 return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) :
122 NS_ERROR_NULL_POINTER;
125 size_t
126 nsWeakReference::SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf) const
128 return aMallocSizeOf(this);
131 void
132 nsSupportsWeakReference::ClearWeakReferences()
134 if (mProxy) {
135 mProxy->NoticeReferentDestruction();
136 mProxy = 0;