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 37018 : nsWeakReference( nsSupportsWeakReference* referent )
61 37018 : : mReferent(referent)
62 : // ...I can only be constructed by an |nsSupportsWeakReference|
63 : {
64 : // nothing else to do here
65 37018 : }
66 :
67 37005 : ~nsWeakReference()
68 : // ...I will only be destroyed by calling |delete| myself.
69 37005 : {
70 37005 : if ( mReferent )
71 23243 : mReferent->NoticeProxyDestruction();
72 37005 : }
73 :
74 : void
75 13773 : NoticeReferentDestruction()
76 : // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
77 : {
78 13773 : mReferent = 0;
79 13773 : }
80 :
81 : nsSupportsWeakReference* mReferent;
82 : };
83 :
84 : nsresult
85 61224 : nsQueryReferent::operator()( const nsIID& aIID, void** answer ) const
86 : {
87 : nsresult status;
88 61224 : if ( mWeakPtr )
89 : {
90 40619 : if ( NS_FAILED(status = mWeakPtr->QueryReferent(aIID, answer)) )
91 305 : *answer = 0;
92 : }
93 : else
94 20605 : status = NS_ERROR_NULL_POINTER;
95 :
96 61224 : if ( mErrorPtr )
97 0 : *mErrorPtr = status;
98 61224 : return status;
99 : }
100 :
101 : NS_COM_GLUE nsIWeakReference* // or else |already_AddRefed<nsIWeakReference>|
102 105681 : NS_GetWeakReference( nsISupports* aInstancePtr, nsresult* aErrorPtr )
103 : {
104 : nsresult status;
105 :
106 105681 : nsIWeakReference* result = nsnull;
107 :
108 105681 : if ( aInstancePtr )
109 : {
110 204946 : nsCOMPtr<nsISupportsWeakReference> factoryPtr = do_QueryInterface(aInstancePtr, &status);
111 102473 : NS_ASSERTION(factoryPtr, "Oops! You're asking for a weak reference to an object that doesn't support that.");
112 102473 : if ( factoryPtr )
113 : {
114 102473 : status = factoryPtr->GetWeakReference(&result);
115 : }
116 : // else, |status| has already been set by |do_QueryInterface|
117 : }
118 : else
119 3208 : status = NS_ERROR_NULL_POINTER;
120 :
121 105681 : if ( aErrorPtr )
122 0 : *aErrorPtr = status;
123 105681 : return result;
124 : }
125 :
126 : NS_COM_GLUE nsresult
127 107026 : nsSupportsWeakReference::GetWeakReference( nsIWeakReference** aInstancePtr )
128 : {
129 107026 : if ( !aInstancePtr )
130 0 : return NS_ERROR_NULL_POINTER;
131 :
132 107026 : if ( !mProxy )
133 37018 : mProxy = new nsWeakReference(this);
134 107026 : *aInstancePtr = mProxy;
135 :
136 : nsresult status;
137 107026 : if ( !*aInstancePtr )
138 0 : status = NS_ERROR_OUT_OF_MEMORY;
139 : else
140 : {
141 107026 : NS_ADDREF(*aInstancePtr);
142 107026 : status = NS_OK;
143 : }
144 :
145 107026 : return status;
146 : }
147 :
148 889758 : NS_IMPL_ISUPPORTS1(nsWeakReference, nsIWeakReference)
149 :
150 : NS_IMETHODIMP
151 564582 : nsWeakReference::QueryReferent( const nsIID& aIID, void** aInstancePtr )
152 : {
153 564582 : return mReferent ? mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
154 : }
155 :
156 : void
157 279149 : nsSupportsWeakReference::ClearWeakReferences()
158 : {
159 279149 : if ( mProxy )
160 : {
161 13773 : mProxy->NoticeReferentDestruction();
162 13773 : mProxy = 0;
163 : }
164 279149 : }
165 :
|