sbDeviceContent.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #include "sbDeviceContent.h"
28 
29 #include <sbIDeviceLibrary.h>
30 
31 #include <nsArrayEnumerator.h>
32 #include <nsComponentManagerUtils.h>
33 #include <nsISimpleEnumerator.h>
34 #include <nsServiceManagerUtils.h>
35 
37 
38 
42 #ifdef PR_LOGGING
43 static PRLogModuleInfo* gDeviceContentLog = nsnull;
44 #define TRACE(args) PR_LOG(gDeviceContentLog, PR_LOG_DEBUG, args)
45 #define LOG(args) PR_LOG(gDeviceContentLog, PR_LOG_WARN, args)
46 #else
47 #define TRACE(args) /* nothing */
48 #define LOG(args) /* nothing */
49 #endif /* PR_LOGGING */
50 
51 sbDeviceContent::sbDeviceContent()
52 : mDeviceLibrariesMonitor(nsnull)
53 {
54 #ifdef PR_LOGGING
55  if (!gDeviceContentLog) {
56  gDeviceContentLog = PR_NewLogModule("sbDeviceContent");
57  }
58 #endif
59  TRACE(("DeviceContent[0x%.8x] - Constructed", this));
60 }
61 
62 sbDeviceContent::~sbDeviceContent()
63 {
64  Finalize();
65 
66  if(mDeviceLibrariesMonitor) {
67  nsAutoMonitor::DestroyMonitor(mDeviceLibrariesMonitor);
68  }
69  TRACE(("DeviceContent[0x%.8x] - Destructed", this));
70 }
71 
73 {
74  return new sbDeviceContent;
75 }
76 
77 NS_IMETHODIMP
78 sbDeviceContent::Initialize()
79 {
80  mDeviceLibrariesMonitor = nsAutoMonitor::NewMonitor("sbDeviceContent::mDeviceLibrariesMonitor");
81  NS_ENSURE_TRUE(mDeviceLibrariesMonitor, NS_ERROR_OUT_OF_MEMORY);
82 
83  nsresult rv;
84  mDeviceLibraries =
85  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  return NS_OK;
89 }
90 
91 NS_IMETHODIMP
92 sbDeviceContent::Finalize()
93 {
94  nsresult rv;
95 
96  // Finalize and remove all device libraries.
97  if (mDeviceLibraries) {
98  PRUint32 length;
99  rv = mDeviceLibraries->GetLength(&length);
100  if (NS_SUCCEEDED(rv)) {
101  for (PRUint32 i = 0; i < length; i++) {
102  nsCOMPtr<sbIDeviceLibrary> library;
103  rv = mDeviceLibraries->QueryElementAt(i,
104  NS_GET_IID(sbIDeviceLibrary),
105  getter_AddRefs(library));
106  if (NS_SUCCEEDED(rv))
107  library->Finalize();
108  }
109  }
110  mDeviceLibraries->Clear();
111  }
112 
113  return NS_OK;
114 }
115 
116 NS_IMETHODIMP
117 sbDeviceContent::GetLibraries(nsIArray** aLibraries)
118 {
119  NS_ENSURE_ARG_POINTER(aLibraries);
120 
121  nsAutoMonitor mon(mDeviceLibrariesMonitor);
122  *aLibraries = mDeviceLibraries;
123  NS_ADDREF(*aLibraries);
124  return NS_OK;
125 }
126 
127 NS_IMETHODIMP
128 sbDeviceContent::GetLibraryFactory(sbILibraryFactory** aLibraryFactory)
129 {
130  return NS_ERROR_NOT_IMPLEMENTED;
131 }
132 
133 NS_IMETHODIMP
134 sbDeviceContent::AddLibrary(sbIDeviceLibrary* aLibrary)
135 {
136  NS_ENSURE_ARG_POINTER(aLibrary);
137 
138  nsresult rv;
139  nsAutoMonitor mon(mDeviceLibrariesMonitor);
140  PRUint32 existingIndex;
141  rv = FindLibrary(aLibrary, &existingIndex);
142  if (NS_FAILED(rv)) {
143  rv = mDeviceLibraries->AppendElement(aLibrary, false);
144  NS_ENSURE_SUCCESS(rv, rv);
145  }
146 
147  return NS_OK;
148 }
149 
150 NS_IMETHODIMP
151 sbDeviceContent::RemoveLibrary(sbIDeviceLibrary* aLibrary)
152 {
153  NS_ENSURE_ARG_POINTER(aLibrary);
154  PRUint32 itemIndex;
155 
156  nsresult rv;
157  rv = FindLibrary(aLibrary, &itemIndex);
158  if (NS_FAILED(rv)) {
159  return rv;
160  }
161 
162  nsAutoMonitor mon(mDeviceLibrariesMonitor);
163  rv = mDeviceLibraries->RemoveElementAt(itemIndex);
164  NS_ENSURE_SUCCESS(rv, rv);
165 
166  return NS_OK;
167 }
168 
169 nsresult
170 sbDeviceContent::FindLibrary(sbIDeviceLibrary* aLibrary, PRUint32* _retval)
171 {
172  NS_ENSURE_ARG_POINTER(aLibrary);
173  NS_ENSURE_ARG_POINTER(_retval);
174 
175  nsAutoMonitor mon(mDeviceLibrariesMonitor);
176  nsresult rv;
177  PRUint32 index;
178  rv = mDeviceLibraries->IndexOf(0, aLibrary, &index);
179  if (rv == NS_ERROR_FAILURE) {
180  // yes, not found throws a generic error. sigh.
181  return NS_ERROR_NOT_AVAILABLE;
182  }
183  NS_ENSURE_SUCCESS(rv, rv);
184 
185  *_retval = (PRUint32)index;
186  return NS_OK;
187 }
188 
return NS_OK
Factory for new library instances.
#define TRACE(args)
[UNIMPLEMENTED UNTIL AFTER 0.3]
NS_IMPL_THREADSAFE_ISUPPORTS1(sbDeviceCapsCompatibility, sbIDeviceCapsCompatibility) sbDeviceCapsCompatibility
NS_DECL_ISUPPORTS static NS_DECL_SBIDEVICECONTENT sbDeviceContent * New()
_getSelectedPageStyle s i