Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / xpcom / glue / nsWeakReference.cpp
bloba62d2a3fc56093a3f746f8f1f0ec268ff55581e7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Scott Collins <scc@netscape.com>
25 * Pierre Phaneuf <pp@ludusdesign.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 // nsWeakReference.cpp
43 #include "nsWeakReference.h"
44 #include "nsCOMPtr.h"
46 class nsWeakReference : public nsIWeakReference
48 public:
49 // nsISupports...
50 NS_DECL_ISUPPORTS
52 // nsIWeakReference...
53 NS_DECL_NSIWEAKREFERENCE
55 private:
56 friend class nsSupportsWeakReference;
58 nsWeakReference( nsSupportsWeakReference* referent )
59 : mReferent(referent)
60 // ...I can only be constructed by an |nsSupportsWeakReference|
62 // nothing else to do here
65 ~nsWeakReference()
66 // ...I will only be destroyed by calling |delete| myself.
68 if ( mReferent )
69 mReferent->NoticeProxyDestruction();
72 void
73 NoticeReferentDestruction()
74 // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
76 mReferent = 0;
79 nsSupportsWeakReference* mReferent;
82 nsresult
83 nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
85 nsresult status;
86 if ( mWeakPtr )
88 if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
89 *answer = 0;
91 else
92 status = NS_ERROR_NULL_POINTER;
94 if ( mErrorPtr )
95 *mErrorPtr = status;
96 return status;
99 NS_COM_GLUE nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
100 NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
102 nsresult status;
104 nsIWeakReference* result = nsnull;
106 if ( aInstancePtr )
108 nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
109 NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
110 if ( factoryPtr )
112 status = factoryPtr->GetWeakReference(&result);
114 // else, |status| has already been set by |do_QueryInterface|
116 else
117 status = NS_ERROR_NULL_POINTER;
119 if ( aErrorPtr )
120 *aErrorPtr = status;
121 return result;
124 NS_COM_GLUE nsresult
125 nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
127 if ( !aInstancePtr )
128 return NS_ERROR_NULL_POINTER;
130 if ( !mProxy )
131 mProxy = new nsWeakReference(this);
132 *aInstancePtr = mProxy;
134 nsresult status;
135 if ( !*aInstancePtr )
136 status = NS_ERROR_OUT_OF_MEMORY;
137 else
139 NS_ADDREF(*aInstancePtr);
140 status = NS_OK;
143 return status;
146 NS_IMPL_ISUPPORTS1(nsWeakReference, nsIWeakReference)
148 NS_IMETHODIMP
149 nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
151 return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
154 void
155 nsSupportsWeakReference::ClearWeakReferences()
157 if ( mProxy )
159 mProxy->NoticeReferentDestruction();
160 mProxy = 0;