sbURIUtils.h
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 #ifndef SBURIUTILS_H_
28 #define SBURIUTILS_H_
29 
30 #include <nsIFileURL.h>
31 #include <nsIIOService.h>
32 #include <nsIURI.h>
33 #include <nsNetCID.h>
34 #include <nsNetError.h>
35 #include <nsStringAPI.h>
36 #include <nsThreadUtils.h>
37 
39 
40 static inline nsresult
41 sbGetFileExtensionFromURI(nsIURI* aURI, nsACString& _retval)
42 {
43  NS_ENSURE_ARG_POINTER(aURI);
44 
45  nsCString strExtension;
46 
47  nsCString strPath;
48  nsresult rv = aURI->GetPath(strPath);
49  NS_ENSURE_SUCCESS(rv, rv);
50 
51  if (strPath.IsEmpty())
53 
54  PRInt32 fileExtPosStart = strPath.RFindChar('.');
55  PRInt32 fileExtPosEnd = strPath.RFindChar('?');
56 
57  if(fileExtPosEnd > fileExtPosStart) {
58  strExtension = Substring(strPath, fileExtPosStart, fileExtPosEnd - fileExtPosStart);
59  }
60  else {
61  strExtension = Substring(strPath, fileExtPosStart);
62  }
63 
64  // Strip '.' from the beginning and end, if it exists
65  strExtension.Trim(".");
66 
67  _retval.Assign(strExtension);
68  return NS_OK;
69 }
70 
71 
72 static inline nsresult
74 {
75  NS_ENSURE_ARG_POINTER(aFileURL);
76 
77  // Function variables.
78  nsresult rv;
79 
80  // Touch the file URL scheme to invalidate the file URL cache. Some file URL
81  // changes do not invalidate the cache. For example, changing the file base
82  // name and reading the file URL file object will return a cached file object
83  // that does not contain the new file base name. Reading and writing back the
84  // URI scheme invalidates the cache.
85  nsCAutoString scheme;
86  rv = aFileURL->GetScheme(scheme);
87  NS_ENSURE_SUCCESS(rv, rv);
88  rv = aFileURL->SetScheme(scheme);
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  return NS_OK;
92 }
93 
94 
103 static inline nsresult
104 SB_GetIOService(nsIIOService** aIOService)
105 {
106  nsresult rv;
107 
108  // Get the IO service.
109  nsCOMPtr<nsIIOService> ioService;
110  if (NS_IsMainThread()) {
111  ioService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
112  NS_ENSURE_SUCCESS(rv, rv);
113  }
114  else {
115  ioService = do_ProxiedGetService(NS_IOSERVICE_CONTRACTID, &rv);
116  NS_ENSURE_SUCCESS(rv, rv);
117  }
118 
119  // Return results.
120  ioService.forget(aIOService);
121 
122  return NS_OK;
123 }
124 
125 
138 static inline nsresult
139 SB_NewURI(nsIURI** aURI,
140  const nsACString& aSpec,
141  const char* aCharSet = nsnull,
142  nsIURI* aBaseURI = nsnull)
143 {
144  nsresult rv;
145 
146  // Get the IO service.
147  nsCOMPtr<nsIIOService> ioService;
148  rv = SB_GetIOService(getter_AddRefs(ioService));
149  NS_ENSURE_SUCCESS(rv, rv);
150 
151  // Create the URI.
152  nsCOMPtr<nsIURI> uri;
153  rv = ioService->NewURI(aSpec, aCharSet, aBaseURI, getter_AddRefs(uri));
154  NS_ENSURE_SUCCESS(rv, rv);
155 
156  // Get a main thread URI.
157  nsCOMPtr<nsIURI> mainThreadURI = do_MainThreadQueryInterface(uri, &rv);
158  NS_ENSURE_SUCCESS(rv, rv);
159 
160  // Return results.
161  mainThreadURI.forget(aURI);
162 
163  return NS_OK;
164 }
165 
166 static inline nsresult
167 SB_NewURI(nsIURI** aURI,
168  const nsAString& aSpec,
169  const char* aCharSet = nsnull,
170  nsIURI* aBaseURI = nsnull)
171 {
172  return SB_NewURI(aURI, NS_ConvertUTF16toUTF8(aSpec), aCharSet, aBaseURI);
173 }
174 
175 static inline nsresult
176 SB_NewURI(nsIURI** aURI,
177  const char* aSpec,
178  nsIURI* aBaseURI = nsnull)
179 {
180  return SB_NewURI(aURI, nsDependentCString(aSpec), nsnull, aBaseURI);
181 }
182 
183 #endif /* SBURIUTILS_H_ */
184 
return NS_OK
static nsresult sbGetFileExtensionFromURI(nsIURI *aURI, nsACString &_retval)
Definition: sbURIUtils.h:41
const NS_ERROR_MALFORMED_URI
Definition: test_355473.js:1
var ioService
static nsresult SB_NewURI(nsIURI **aURI, const nsACString &aSpec, const char *aCharSet=nsnull, nsIURI *aBaseURI=nsnull)
Definition: sbURIUtils.h:139
const sbCreateProxiedComponent do_ProxiedGetService(const nsCID &aCID, nsresult *error=0)
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
static nsresult sbInvalidateFileURLCache(nsIFileURL *aFileURL)
Definition: sbURIUtils.h:73
var uri
Definition: FeedWriter.js:1135
static nsresult SB_GetIOService(nsIIOService **aIOService)
Definition: sbURIUtils.h:104
const nsIFileURL