sbWeakReference.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #include "sbWeakReference.h"
26 #include "nsCOMPtr.h"
27 
28 class sbWeakReference : public nsIWeakReference
29 {
30 public:
32  NS_DECL_NSIWEAKREFERENCE
33 
34 private:
36 
38  : mReferentLock(nsnull)
39  , mReferent(referent) {
40  // ...I can only be constructed by an |sbSupportsWeakReference|
41  mReferentLock = nsAutoLock::NewLock("sbWeakReference::mReferentLock");
42  NS_WARN_IF_FALSE(mReferentLock, "Failed to create lock.");
43  }
44 
45  ~sbWeakReference() {
46  // ...I will only be destroyed by calling |delete| myself.
47  if (mReferent) {
48  mReferent->NoticeProxyDestruction();
49  }
50 
51  if (mReferentLock) {
52  nsAutoLock::DestroyLock(mReferentLock);
53  }
54  }
55 
56  void NoticeReferentDestruction() {
57  NS_ENSURE_TRUE(mReferentLock, /*void*/);
58  nsAutoLock lock(mReferentLock);
59  // ...called (only) by an |sbSupportsWeakReference| from _its_ dtor.
60  mReferent = nsnull;
61  }
62 
63  PRLock *mReferentLock;
64  sbSupportsWeakReference* mReferent;
65 };
66 
67 NS_COM_GLUE nsresult
68 sbSupportsWeakReference::GetWeakReference(nsIWeakReference** aInstancePtr)
69 {
70  NS_ENSURE_ARG_POINTER(aInstancePtr);
71  NS_ENSURE_TRUE(mProxyLock, NS_ERROR_NOT_INITIALIZED);
72 
73  nsAutoLock lock(mProxyLock);
74 
75  if (!mProxy) {
76  mProxy = new sbWeakReference(this);
77  }
78 
79  *aInstancePtr = mProxy;
80 
81  if ( !*aInstancePtr ) {
82  return NS_ERROR_OUT_OF_MEMORY;
83  }
84 
85  NS_ADDREF(*aInstancePtr);
86 
87  return NS_OK;
88 }
89 
91 
92 NS_IMETHODIMP
93 sbWeakReference::QueryReferent(const nsIID& aIID, void** aInstancePtr)
94 {
95  NS_ENSURE_TRUE(mReferentLock, NS_ERROR_NOT_INITIALIZED);
96 
97  nsAutoLock lock(mReferentLock);
98  return mReferent ?
99  mReferent->QueryInterface(aIID, aInstancePtr) : NS_ERROR_NULL_POINTER;
100 }
101 
102 void
104 {
105  NS_ENSURE_TRUE(mProxyLock, /*void*/);
106 
107  nsAutoLock lock(mProxyLock);
108 
109  if (mProxy) {
110  mProxy->NoticeReferentDestruction();
111  mProxy = nsnull;
112  }
113 }
return NS_OK
friend class sbWeakReference
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility