sbMediaItemDownloadService.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 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird media item download services.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird media item download service imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
48 
49 // Songbird imports.
50 #include <sbIMediaItemDownloader.h>
51 #include <sbXPCOMUtils.h>
52 
53 // Mozilla imports.
54 #include <nsComponentManagerUtils.h>
55 
56 #include <nsIObserverService.h>
57 
58 
59 //------------------------------------------------------------------------------
60 //
61 // Songbird media item download service logging services.
62 //
63 //------------------------------------------------------------------------------
64 
72 #include "prlog.h"
73 #ifdef PR_LOGGING
74 static PRLogModuleInfo* gMediaItemDownloadServiceLog = nsnull;
75 #define TRACE(args) PR_LOG(gMediaItemDownloadServiceLog, PR_LOG_DEBUG, args)
76 #define LOG(args) PR_LOG(gMediaItemDownloadServiceLog, PR_LOG_WARN, args)
77 #ifdef __GNUC__
78 #define __FUNCTION__ __PRETTY_FUNCTION__
79 #endif /* __GNUC__ */
80 #else
81 #define TRACE(args) /* nothing */
82 #define LOG(args) /* nothing */
83 #endif /* PR_LOGGING */
84 
85 
86 //------------------------------------------------------------------------------
87 //
88 // Songbird media item download service nsISupports implementation.
89 //
90 //------------------------------------------------------------------------------
91 
95 
96 
97 //------------------------------------------------------------------------------
98 //
99 // Songbird media item download service sbIMediaItemDownloadService
100 // implementation.
101 //
102 // \see sbIMediaItemDownloadService
103 //
104 //------------------------------------------------------------------------------
105 
106 //-------------------------------------
107 //
108 // getDownloader
109 //
110 // \see sbIMediaItemDownloadService
111 //
112 
113 NS_IMETHODIMP
114 sbMediaItemDownloadService::GetDownloader
115  (sbIMediaItem* aMediaItem,
116  sbILibrary* aTargetLibrary,
117  sbIMediaItemDownloader** retval)
118 {
119  TRACE(("%s 0x%08x 0x%08x", __FUNCTION__, aMediaItem, aTargetLibrary));
120 
121  // Validate state and arguments.
122  NS_ENSURE_STATE(mInitialized);
123  NS_ENSURE_ARG_POINTER(aMediaItem);
124  NS_ENSURE_ARG_POINTER(retval);
125 
126  // Function variables.
127  nsresult rv;
128 
129  // Search for the downloader with the highest vote.
130  nsCOMPtr<sbIMediaItemDownloader> highestVoteDownloader;
131  PRUint32 highestVote = sbIMediaItemDownloader::VOTE_NONE;
132  PRUint32 downloaderCount = mDownloaderList.Length();
133  for (PRUint32 i = 0; i < downloaderCount; ++i) {
134  // Get the next downloader's vote.
135  sbIMediaItemDownloader* downloader = mDownloaderList[i];
136  PRUint32 vote;
137  rv = downloader->Vote(aMediaItem, aTargetLibrary, &vote);
138  NS_ENSURE_SUCCESS(rv, rv);
139 
140  // Update the highest vote downloader.
141  if (vote > highestVote) {
142  highestVoteDownloader = downloader;
143  highestVote = vote;
144  }
145  }
146 
147  // Return downloader.
148  highestVoteDownloader.forget(retval);
149 
150  return NS_OK;
151 }
152 
153 
154 //------------------------------------------------------------------------------
155 //
156 // Songbird media item download service nsIObserver implementation.
157 //
158 // \see nsIObserver
159 //
160 //------------------------------------------------------------------------------
161 
162 //-------------------------------------
163 //
164 // Observer
165 //
166 // \see nsIObserver
167 //
168 
169 NS_IMETHODIMP
170 sbMediaItemDownloadService::Observe(nsISupports* aSubject,
171  const char* aTopic,
172  const PRUnichar* aData)
173 {
174  if (aData) {
175  TRACE(("%s[%.8x] \"%s\" \"%s\"", __FUNCTION__,
176  this,
177  aTopic,
178  NS_ConvertUTF16toUTF8(aData).get()));
179  }
180  else {
181  TRACE(("%s[%.8x] \"%s\"", __FUNCTION__, this, aTopic));
182  }
183 
184  nsresult rv;
185 
186  // Dispatch processing of event.
187  if (!strcmp(aTopic, "app-startup")) {
188  nsCOMPtr<nsIObserverService> obsSvc = do_GetService(
189  "@mozilla.org/observer-service;1", &rv);
190  NS_ENSURE_SUCCESS(rv, rv);
191 
192  rv = obsSvc->AddObserver(this,
193  "profile-after-change",
194  PR_FALSE);
195  NS_ENSURE_SUCCESS(rv, rv);
196  } else if (!strcmp(aTopic, "profile-after-change")) {
197  // Initialize the component.
198  rv = Initialize();
199  NS_ENSURE_SUCCESS(rv, rv);
200 
201  // Now remove the listener, since it's no longer needed.
202  nsCOMPtr<nsIObserverService> obsSvc = do_GetService(
203  "@mozilla.org/observer-service;1", &rv);
204  NS_ENSURE_SUCCESS(rv, rv);
205 
206  rv = obsSvc->RemoveObserver(this,
207  "profile-after-change");
208  NS_ENSURE_SUCCESS(rv, rv);
209 
210  }
211 
212  return NS_OK;
213 }
214 
215 
216 //------------------------------------------------------------------------------
217 //
218 // Songbird media item download public services.
219 //
220 //------------------------------------------------------------------------------
221 
222 //-------------------------------------
223 //
224 // sbMediaItemDownloadService
225 //
226 
228  mInitialized(PR_FALSE)
229 {
230  // Initialize logging.
231 #ifdef PR_LOGGING
232  if (!gMediaItemDownloadServiceLog) {
233  gMediaItemDownloadServiceLog =
234  PR_NewLogModule("sbMediaItemDownloadService");
235  }
236 #endif
237 
238  TRACE(("%s[%.8x]", __FUNCTION__, this));
239 }
240 
241 
242 //-------------------------------------
243 //
244 // ~sbMediaItemDownloadService
245 //
246 
248 {
249  TRACE(("%s[%.8x]", __FUNCTION__, this));
250 
251  // Finalize the media item download services.
252  Finalize();
253 }
254 
255 
256 //-------------------------------------
257 //
258 // RegisterSelf
259 //
260 
261 /* static */ NS_METHOD
263  (nsIComponentManager* aCompMgr,
264  nsIFile* aPath,
265  const char* aLoaderStr,
266  const char* aType,
267  const nsModuleComponentInfo* aInfo)
268 {
269  nsresult rv;
270 
271  // Get the category manager.
272  nsCOMPtr<nsICategoryManager> categoryManager =
273  do_GetService(NS_CATEGORYMANAGER_CONTRACTID,
274  &rv);
275  NS_ENSURE_SUCCESS(rv, rv);
276 
277  // Add self to the application startup category.
278  rv = categoryManager->AddCategoryEntry
279  ("app-startup",
282  PR_TRUE,
283  PR_TRUE,
284  nsnull);
285  NS_ENSURE_SUCCESS(rv, rv);
286 
287  return NS_OK;
288 }
289 
290 
291 /* static */ NS_METHOD
293  (nsIComponentManager* aCompMgr,
294  nsIFile* aPath,
295  const char* aLoaderStr,
296  const nsModuleComponentInfo* aInfo)
297 {
298  nsresult rv;
299 
300  // Get the category manager.
301  nsCOMPtr<nsICategoryManager> categoryManager =
302  do_GetService(NS_CATEGORYMANAGER_CONTRACTID,
303  &rv);
304  NS_ENSURE_SUCCESS(rv, rv);
305 
306  // Delete self from the application startup category.
307  rv = categoryManager->DeleteCategoryEntry
308  ("app-startup",
310  PR_TRUE);
311  NS_ENSURE_SUCCESS(rv, rv);
312 
313  return NS_OK;
314 }
315 
316 
317 //------------------------------------------------------------------------------
318 //
319 // Songbird media item download private services.
320 //
321 //------------------------------------------------------------------------------
322 
323 //-------------------------------------
324 //
325 // Initialize
326 //
327 
328 nsresult
329 sbMediaItemDownloadService::Initialize()
330 {
331  TRACE(("%s[%.8x]", __FUNCTION__, this));
332 
333  // Function variables.
334  nsresult rv;
335 
336  // Do nothing if already initialized.
337  if (mInitialized)
338  return NS_OK;
339 
340  // Get the service manager.
341  mServiceManager = do_GetService(SB_SERVICE_MANAGER_CONTRACTID, &rv);
342  NS_ENSURE_SUCCESS(rv, rv);
343 
344  // Get the list of media item downloader components.
345  nsTArray<nsCString> entryValueList;
347  entryValueList);
348  NS_ENSURE_SUCCESS(rv, rv);
349 
350  // Add all of the media item downloaders.
351  for (PRUint32 i = 0; i < entryValueList.Length(); ++i) {
352  // Get the next downloader.
353  nsCOMPtr<sbIMediaItemDownloader>
354  downloader = do_CreateInstance(entryValueList[i].get(), &rv);
355  NS_ENSURE_SUCCESS(rv, rv);
356 
357  // Add the downloader.
358  nsCOMPtr<sbIMediaItemDownloader>*
359  appendedElement = mDownloaderList.AppendElement(downloader);
360  NS_ENSURE_TRUE(appendedElement, NS_ERROR_OUT_OF_MEMORY);
361  }
362 
363  // Services are now initialized.
364  mInitialized = PR_TRUE;
365 
366  // Indicate that the media item download services are ready.
367  rv = mServiceManager->SetServiceReady
369  NS_ENSURE_SUCCESS(rv, rv);
370 
371  return NS_OK;
372 }
373 
374 
375 //-------------------------------------
376 //
377 // Finalize
378 //
379 
380 void
381 sbMediaItemDownloadService::Finalize()
382 {
383  TRACE(("%s[%.8x]", __FUNCTION__, this));
384 
385  if (mInitialized) {
386  // Indicate that the media item download services are no longer ready.
387  mServiceManager->SetServiceReady(SB_MEDIA_ITEM_DOWNLOAD_SERVICE_CONTRACTID,
388  PR_FALSE);
389 
390  // Clear the media item downloader list.
391  mDownloaderList.Clear();
392 
393  // No longer initialized.
394  mInitialized = PR_FALSE;
395  }
396 }
397 
NS_IMPL_THREADSAFE_ISUPPORTS2(sbMediaItemDownloadService, sbIMediaItemDownloadService, nsIObserver) NS_IMETHODIMP sbMediaItemDownloadService
return NS_OK
_updateCookies aPath
#define SB_MEDIA_ITEM_DOWNLOADER_CATEGORY
Songbird XPCOM Utilities Definitions.
#define TRACE(args)
#define SB_MEDIA_ITEM_DOWNLOAD_SERVICE_CLASSNAME
static NS_METHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const char *aType, const nsModuleComponentInfo *aInfo)
Songbird Media Item Download Service Definitions.
#define SB_MEDIA_ITEM_DOWNLOAD_SERVICE_CONTRACTID
#define SB_SERVICE_MANAGER_CONTRACTID
Media library abstraction.
Definition: sbILibrary.idl:82
static nsresult SB_GetCategoryEntryValues(const char *aCategory, nsTArray< nsCString > &aEntryValueList)
Definition: sbXPCOMUtils.h:71
Interface that defines a single item of media in the system.
NS_DECL_ISUPPORTS NS_DECL_SBIMEDIAITEMDOWNLOADSERVICE NS_DECL_NSIOBSERVER sbMediaItemDownloadService()
_getSelectedPageStyle s i
static NS_METHOD UnregisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const nsModuleComponentInfo *aInfo)
_updateTextAndScrollDataForFrame aData