1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Scott Collins <scc@netscape.com>
25 : * Pierre Phaneuf <pp@ludusdesign.com>
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : // nsWeakReference.cpp
42 :
43 : #include "mozilla/Attributes.h"
44 :
45 : #include "nsWeakReference.h"
46 : #include "nsCOMPtr.h"
47 :
48 : class nsWeakReference MOZ_FINAL : public nsIWeakReference
49 : {
50 : public:
51 : // nsISupports...
52 : NS_DECL_ISUPPORTS
53 :
54 : // nsIWeakReference...
55 : NS_DECL_NSIWEAKREFERENCE
56 :
57 : private:
58 : friend class nsSupportsWeakReference;
59 :
60 0 : nsWeakReference( nsSupportsWeakReference* referent )
61 0 : : mReferent(referent)
62 : // ...I can only be constructed by an |nsSupportsWeakReference|
63 : {
64 : // nothing else to do here
65 0 : }
66 :
67 0 : ~nsWeakReference()
68 : // ...I will only be destroyed by calling |delete| myself.
69 0 : {
70 0 : if ( mReferent )
71 0 : mReferent->NoticeProxyDestruction();
72 0 : }
73 :
74 : void
75 0 : NoticeReferentDestruction()
76 : // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
77 : {
78 0 : mReferent = 0;
79 0 : }
80 :
81 : nsSupportsWeakReference* mReferent;
82 : };
83 :
84 : nsresult
85 0 : nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
86 : {
87 : nsresult status;
88 0 : if ( mWeakPtr )
89 : {
90 0 : if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
91 0 : *answer = 0;
92 : }
93 : else
94 0 : status = NS_ERROR_NULL_POINTER;
95 :
96 0 : if ( mErrorPtr )
97 0 : *mErrorPtr = status;
98 0 : return status;
99 : }
100 :
101 : NS_COM_GLUE nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
102 0 : NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
103 : {
104 : nsresult status;
105 :
106 0 : nsIWeakReference* result = nsnull;
107 :
108 0 : if ( aInstancePtr )
109 : {
110 0 : nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
111 0 : NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
112 0 : if ( factoryPtr )
113 : {
114 0 : status = factoryPtr->GetWeakReference(&result);
115 : }
116 : // else, |status| has already been set by |do_QueryInterface|
117 : }
118 : else
119 0 : status = NS_ERROR_NULL_POINTER;
120 :
121 0 : if ( aErrorPtr )
122 0 : *aErrorPtr = status;
123 0 : return result;
124 : }
125 :
126 : NS_COM_GLUE nsresult
127 0 : nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
128 : {
129 0 : if ( !aInstancePtr )
130 0 : return NS_ERROR_NULL_POINTER;
131 :
132 0 : if ( !mProxy )
133 0 : mProxy = new nsWeakReference(this);
134 0 : *aInstancePtr = mProxy;
135 :
136 : nsresult status;
137 0 : if ( !*aInstancePtr )
138 0 : status = NS_ERROR_OUT_OF_MEMORY;
139 : else
140 : {
141 0 : NS_ADDREF(*aInstancePtr);
142 0 : status = NS_OK;
143 : }
144 :
145 0 : return status;
146 : }
147 :
148 0 : NS_IMPL_ISUPPORTS1(nsWeakReference, nsIWeakReference)
149 :
150 : NS_IMETHODIMP
151 0 : nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
152 : {
153 0 : return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
154 : }
155 :
156 : void
157 0 : nsSupportsWeakReference::ClearWeakReferences()
158 : {
159 0 : if ( mProxy )
160 : {
161 0 : mProxy->NoticeReferentDestruction();
162 0 : mProxy = 0;
163 : }
164 0 : }
165 :
|