sbHTTPMediaItemDownloader.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 // HTTP media item downloader.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // HTTP media item downloader imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
48 
49 // Local imports.
51 
52 // Songbird imports.
53 #include <sbFileUtils.h>
55 
56 // Mozilla imports.
57 #include <nsAutoPtr.h>
58 #include <nsIChannel.h>
59 #include <nsIIOService.h>
60 #include <nsIPropertyBag2.h>
61 #include <nsIURI.h>
62 
63 
64 //------------------------------------------------------------------------------
65 //
66 // HTTP media item downloader logging services.
67 //
68 //------------------------------------------------------------------------------
69 
77 #include "prlog.h"
78 #ifdef PR_LOGGING
79 static PRLogModuleInfo* gHTTPMediaItemDownloaderLog = nsnull;
80 #define TRACE(args) PR_LOG(gHTTPMediaItemDownloaderLog, PR_LOG_DEBUG, args)
81 #define LOG(args) PR_LOG(gHTTPMediaItemDownloaderLog, PR_LOG_WARN, args)
82 #ifdef __GNUC__
83 #define __FUNCTION__ __PRETTY_FUNCTION__
84 #endif /* __GNUC__ */
85 #else
86 #define TRACE(args) /* nothing */
87 #define LOG(args) /* nothing */
88 #endif /* PR_LOGGING */
89 
90 
91 //------------------------------------------------------------------------------
92 //
93 // HTTP media item downloader nsISupports implementation.
94 //
95 //------------------------------------------------------------------------------
96 
99 
100 
101 //------------------------------------------------------------------------------
102 //
103 // HTTP media item downloader sbIMediaItemDownloader implementation.
104 //
105 // \see sbIMediaItemDownloader
106 //
107 //------------------------------------------------------------------------------
108 
109 //-------------------------------------
110 //
111 // vote
112 //
113 // \see sbIMediaItemDownloader
114 //
115 
116 NS_IMETHODIMP
117 sbHTTPMediaItemDownloader::Vote(sbIMediaItem* aMediaItem,
118  sbILibrary* aTargetLibrary,
119  PRUint32* retval)
120 {
121  TRACE(("%s 0x%08x 0x%08x", __FUNCTION__, aMediaItem, aTargetLibrary));
122 
123  // Validate arguments.
124  NS_ENSURE_ARG_POINTER(aMediaItem);
125  NS_ENSURE_ARG_POINTER(retval);
126 
127  // Function variables.
128  nsresult rv;
129 
130  // Get the content source on the main thread.
131  nsCOMPtr<nsIURI> contentSrc;
132  rv = aMediaItem->GetContentSrc(getter_AddRefs(contentSrc));
133  NS_ENSURE_SUCCESS(rv, rv);
134  nsCOMPtr<nsIURI>
135  mainThreadContentSrc = do_MainThreadQueryInterface(contentSrc, &rv);
136  NS_ENSURE_SUCCESS(rv, rv);
137 
138  // Get the content source scheme.
139  nsCAutoString scheme;
140  rv = mainThreadContentSrc->GetScheme(scheme);
141  NS_ENSURE_SUCCESS(rv, rv);
142 
143  // Check for a scheme match.
144  if (scheme.Equals("http") || scheme.Equals("https"))
146  else
148 
149  return NS_OK;
150 }
151 
152 
153 //-------------------------------------
154 //
155 // getDownloadSize
156 //
157 // \see sbIMediaItemDownloader
158 //
159 
160 NS_IMETHODIMP
161 sbHTTPMediaItemDownloader::GetDownloadSize(sbIMediaItem* aMediaItem,
162  sbILibrary* aTargetLibrary,
163  PRUint64* retval)
164 {
165  TRACE(("%s 0x%08x 0x%08x", __FUNCTION__, aMediaItem, aTargetLibrary));
166 
167  // Validate arguments.
168  NS_ENSURE_ARG_POINTER(aMediaItem);
169 
170  // Function variables.
171  nsresult rv;
172 
173  // Get the content source.
174  nsCOMPtr<nsIURI> contentSrc;
175  rv = aMediaItem->GetContentSrc(getter_AddRefs(contentSrc));
176  NS_ENSURE_SUCCESS(rv, rv);
177 
178  // Get the IO service.
179  nsCOMPtr<nsIIOService>
180  ioService = do_ProxiedGetService("@mozilla.org/network/io-service;1", &rv);
181  NS_ENSURE_SUCCESS(rv, rv);
182 
183  // Make a new channel on the main thread.
184  nsCOMPtr<nsIChannel> channel;
185  rv = ioService->NewChannelFromURI(contentSrc, getter_AddRefs(channel));
186  NS_ENSURE_SUCCESS(rv, rv);
187  nsCOMPtr<nsIChannel>
188  mainThreadChannel = do_MainThreadQueryInterface(channel, &rv);
189  NS_ENSURE_SUCCESS(rv, rv);
190 
191  // Open the channel.
192  nsCOMPtr<nsIInputStream> inputStream;
193  rv = mainThreadChannel->Open(getter_AddRefs(inputStream));
194  NS_ENSURE_SUCCESS(rv, rv);
195  sbAutoInputStream autoInputStream(inputStream);
196 
197  // Get the content length.
198  PRUint64 contentLength = 0;
199  PRBool gotContentLength = PR_FALSE;
200  nsCOMPtr<nsIPropertyBag2>
201  properties = do_MainThreadQueryInterface(mainThreadChannel, &rv);
202  if (NS_SUCCEEDED(rv)) {
203  rv = properties->GetPropertyAsUint64(NS_LITERAL_STRING("content-length"),
204  &contentLength);
205  if (NS_SUCCEEDED(rv))
206  gotContentLength = PR_TRUE;
207  }
208  if (!gotContentLength) {
209  PRInt32 contentLength32;
210  rv = mainThreadChannel->GetContentLength(&contentLength32);
211  NS_ENSURE_SUCCESS(rv, rv);
212  contentLength = contentLength32;
213  }
214 
215  // Return results.
216  *retval = contentLength;
217 
218  return NS_OK;
219 }
220 
221 
222 //-------------------------------------
223 //
224 // createDownloadJob
225 //
226 // \see sbIMediaItemDownloader
227 //
228 
229 NS_IMETHODIMP
230 sbHTTPMediaItemDownloader::CreateDownloadJob
231  (sbIMediaItem* aMediaItem,
232  sbILibrary* aTargetLibrary,
233  sbIMediaItemDownloadJob** retval)
234 {
235  TRACE(("%s 0x%08x 0x%08x", __FUNCTION__, aMediaItem, aTargetLibrary));
236 
237  // Validate arguments.
238  NS_ENSURE_ARG_POINTER(aMediaItem);
239 
240  // Function variables.
241  nsresult rv;
242 
243  // Create an HTTP media item download job.
244  nsRefPtr<sbHTTPMediaItemDownloadJob> job;
245  rv = sbHTTPMediaItemDownloadJob::New(getter_AddRefs(job),
246  aMediaItem,
247  aTargetLibrary);
248  NS_ENSURE_SUCCESS(rv, rv);
249 
250  // Return results.
251  job.forget(retval);
252 
253  return NS_OK;
254 }
255 
256 
257 //------------------------------------------------------------------------------
258 //
259 // HTTP media item downloader public services.
260 //
261 //------------------------------------------------------------------------------
262 
263 // Component registration implementation.
265 
266 
267 //-------------------------------------
268 //
269 // sbHTTPMediaItemDownloader
270 //
271 
273 {
274  // Initialize logging.
275 #ifdef PR_LOGGING
276  if (!gHTTPMediaItemDownloaderLog) {
277  gHTTPMediaItemDownloaderLog =
278  PR_NewLogModule("sbHTTPMediaItemDownloader");
279  }
280 #endif
281 
282  TRACE(("%s[%.8x]", __FUNCTION__, this));
283 }
284 
285 
286 //-------------------------------------
287 //
288 // ~sbHTTPMediaItemDownloader
289 //
290 
292 {
293  TRACE(("%s[%.8x]", __FUNCTION__, this));
294 }
295 
#define SB_MEDIA_ITEM_DOWNLOADER_REGISTERSELF_IMPL(_name)
return NS_OK
NS_IMPL_THREADSAFE_ISUPPORTS1(sbHTTPMediaItemDownloader, sbIMediaItemDownloader) NS_IMETHODIMP sbHTTPMediaItemDownloader
#define TRACE(args)
var ioService
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
BogusChannel prototype contentLength
Media library abstraction.
Definition: sbILibrary.idl:82
HTTP Media Item Download Job Definitions.
static NS_DECL_ISUPPORTS_INHERITED nsresult New(sbHTTPMediaItemDownloadJob **aJob, sbIMediaItem *aMediaItem, sbILibrary *aTargetLibrary=nsnull)
Interface that defines a single item of media in the system.
HTTP Media Item Downloader Definitions.
const unsigned long VOTE_SCHEME