sbBaseDeviceVolume.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
32 //------------------------------------------------------------------------------
33 //
34 // Base device volume imported services.
35 //
36 //------------------------------------------------------------------------------
37 
38 // Self imports.
39 #include "sbBaseDeviceVolume.h"
40 
41 // Local imports.
42 #include "sbBaseDevice.h"
43 
44 
45 //------------------------------------------------------------------------------
46 //
47 // Base device volume nsISupports implementation.
48 //
49 //------------------------------------------------------------------------------
50 
52 
53 
54 //------------------------------------------------------------------------------
55 //
56 // Public base device volume services.
57 //
58 //------------------------------------------------------------------------------
59 
60 /* static */ nsresult
62  sbBaseDevice* aDevice)
63 {
64  // Validate arguments.
65  NS_ENSURE_ARG_POINTER(aVolume);
66  NS_ENSURE_ARG_POINTER(aDevice);
67 
68  // Function variables.
69  nsresult rv;
70 
71  // Create and initialize a new base device volume instance.
72  nsRefPtr<sbBaseDeviceVolume> volume = new sbBaseDeviceVolume();
73  NS_ENSURE_TRUE(volume, NS_ERROR_OUT_OF_MEMORY);
74  rv = volume->Initialize(aDevice);
75  NS_ENSURE_SUCCESS(rv, rv);
76 
77  // Return results.
78  volume.forget(aVolume);
79 
80  return NS_OK;
81 }
82 
84 {
85  // Dispose of the volume lock.
86  if (mVolumeLock)
87  nsAutoLock::DestroyLock(mVolumeLock);
88  mVolumeLock = nsnull;
89 }
90 
91 
92 //
93 // Getters/setters.
94 //
95 
96 nsresult
97 sbBaseDeviceVolume::GetGUID(nsAString& aGUID)
98 {
99  nsAutoLock autoVolumeLock(mVolumeLock);
100  aGUID.Assign(mGUID);
101  return NS_OK;
102 }
103 
104 nsresult
105 sbBaseDeviceVolume::SetGUID(const nsAString& aGUID)
106 {
107  nsAutoLock autoVolumeLock(mVolumeLock);
108  mGUID.Assign(aGUID);
109  return NS_OK;
110 }
111 
112 
113 nsresult
115 {
116  NS_ENSURE_ARG_POINTER(aIsMounted);
117  nsAutoLock autoVolumeLock(mVolumeLock);
118  *aIsMounted = mIsMounted;
119  return NS_OK;
120 }
121 
122 nsresult
124 {
125  nsAutoLock autoVolumeLock(mVolumeLock);
126  mIsMounted = aIsMounted;
127  return NS_OK;
128 }
129 
130 
131 nsresult
133 {
134  NS_ENSURE_ARG_POINTER(aRemovable);
135  nsAutoLock autoVolumeLock(mVolumeLock);
136  *aRemovable = mRemovable;
137  return NS_OK;
138 }
139 
140 nsresult
142 {
143  nsAutoLock autoVolumeLock(mVolumeLock);
144  mRemovable = aRemovable;
145  return NS_OK;
146 }
147 
148 
149 nsresult
151 {
152  NS_ENSURE_ARG_POINTER(aDeviceLibrary);
153  nsAutoLock autoVolumeLock(mVolumeLock);
154  NS_IF_ADDREF(*aDeviceLibrary = mDeviceLibrary);
155  return NS_OK;
156 }
157 
158 nsresult
160 {
161  nsresult rv;
162 
163  // Get the current library and its GUID.
164  nsCOMPtr<sbIDeviceLibrary> currentLibrary;
165  nsAutoString currentLibraryGUID;
166  {
167  nsAutoLock autoVolumeLock(mVolumeLock);
168  currentLibrary = mDeviceLibrary;
169  }
170  if (currentLibrary) {
171  rv = currentLibrary->GetGuid(currentLibraryGUID);
172  NS_ENSURE_SUCCESS(rv, rv);
173  }
174 
175  // Remove current device library.
176  if (currentLibrary) {
177  {
178  nsAutoLock autoVolumeLock(mDevice->mVolumeLock);
179  mDevice->mVolumeLibraryGUIDTable.Remove(currentLibraryGUID);
180  }
181  {
182  nsAutoLock autoVolumeLock(mVolumeLock);
183  mDeviceLibrary = nsnull;
184  }
185  }
186 
187  // Set new device library.
188  if (aDeviceLibrary) {
189  nsAutoString libraryGUID;
190  rv = aDeviceLibrary->GetGuid(libraryGUID);
191  NS_ENSURE_SUCCESS(rv, rv);
192  {
193  nsAutoLock autoVolumeLock(mDevice->mVolumeLock);
194  NS_ENSURE_TRUE(mDevice->mVolumeLibraryGUIDTable.Put(libraryGUID, this),
195  NS_ERROR_OUT_OF_MEMORY);
196  }
197  {
198  nsAutoLock autoVolumeLock(mVolumeLock);
199  mDeviceLibrary = aDeviceLibrary;
200  }
201  }
202 
203  return NS_OK;
204 }
205 
206 
207 nsresult
209 {
210  NS_ENSURE_ARG_POINTER(aStatistics);
211  nsAutoLock autoVolumeLock(mVolumeLock);
212  NS_ENSURE_STATE(aStatistics);
213  NS_ADDREF(*aStatistics = mStatistics);
214  return NS_OK;
215 }
216 
217 
218 //------------------------------------------------------------------------------
219 //
220 // Protected base device volume services.
221 //
222 //------------------------------------------------------------------------------
223 
224 nsresult
226 {
227  nsresult rv;
228 
229  // Create the volume lock.
230  mVolumeLock = nsAutoLock::NewLock("sbBaseDeviceVolume::mVolumeLock");
231  NS_ENSURE_TRUE(mVolumeLock, NS_ERROR_OUT_OF_MEMORY);
232 
233  // Set the volume device.
234  mDevice = aDevice;
235 
236  // Get a device statistics instance.
237  rv = sbDeviceStatistics::New(aDevice, getter_AddRefs(mStatistics));
238  NS_ENSURE_SUCCESS(rv, rv);
239 
240  return NS_OK;
241 }
242 
243 
245  mVolumeLock(nsnull),
246  mIsMounted(PR_FALSE),
247  mRemovable(-1)
248 {
249 }
250 
NS_IMPL_THREADSAFE_ISUPPORTS0(sbMetadataCrashTracker)
PRLock * mVolumeLock
Definition: sbBaseDevice.h:873
return NS_OK
NS_DECL_ISUPPORTS static NS_DECL_SBIMEDIALISTENUMERATIONLISTENER nsresult New(class sbBaseDevice *aDevice, sbDeviceStatistics **aDeviceStatistics)
nsresult SetIsMounted(PRBool aIsMounted)
Songbird Base Device Volume Definitions.
nsresult GetStatistics(sbDeviceStatistics **aStatistics)
nsresult GetIsMounted(PRBool *aIsMounted)
nsresult GetGUID(nsAString &aGUID)
nsresult GetRemovable(PRInt32 *aRemovable)
nsresult GetDeviceLibrary(sbIDeviceLibrary **aDeviceLibrary)
nsresult SetRemovable(PRInt32 aRemovable)
nsresult SetGUID(const nsAString &aGUID)
nsresult SetDeviceLibrary(sbIDeviceLibrary *aDeviceLibrary)
virtual nsresult Initialize(sbBaseDevice *aDevice)
nsInterfaceHashtableMT< nsStringHashKey, sbBaseDeviceVolume > mVolumeLibraryGUIDTable
Definition: sbBaseDevice.h:878