Bug 616542 - Shorten file path length of mochitest; r=ted
[gecko.git] / browser / components / dirprovider / DirectoryProvider.cpp
blob1373adc6c51cbe673f57e511c6d8fbe3cec986a1
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Mozilla Firefox browser.
16 * The Initial Developer of the Original Code is
17 * Benjamin Smedberg <benjamin@smedbergs.us>
19 * Portions created by the Initial Developer are Copyright (C) 2005
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsIDirectoryService.h"
39 #include "DirectoryProvider.h"
41 #include "nsIFile.h"
42 #include "nsISimpleEnumerator.h"
43 #include "nsIPrefService.h"
44 #include "nsIPrefBranch.h"
46 #include "nsArrayEnumerator.h"
47 #include "nsEnumeratorUtils.h"
48 #include "nsBrowserDirectoryServiceDefs.h"
49 #include "nsAppDirectoryServiceDefs.h"
50 #include "nsDirectoryServiceDefs.h"
51 #include "nsCategoryManagerUtils.h"
52 #include "nsComponentManagerUtils.h"
53 #include "nsCOMArray.h"
54 #include "nsDirectoryServiceUtils.h"
55 #include "mozilla/ModuleUtils.h"
56 #include "nsServiceManagerUtils.h"
57 #include "nsStringAPI.h"
58 #include "nsXULAppAPI.h"
60 namespace mozilla {
61 namespace browser {
63 NS_IMPL_ISUPPORTS2(DirectoryProvider,
64 nsIDirectoryServiceProvider,
65 nsIDirectoryServiceProvider2)
67 NS_IMETHODIMP
68 DirectoryProvider::GetFile(const char *aKey, bool *aPersist, nsIFile* *aResult)
70 nsresult rv;
72 *aResult = nsnull;
74 // NOTE: This function can be reentrant through the NS_GetSpecialDirectory
75 // call, so be careful not to cause infinite recursion.
77 nsCOMPtr<nsIFile> file;
79 char const* leafName = nsnull;
81 if (!strcmp(aKey, NS_APP_BOOKMARKS_50_FILE)) {
82 leafName = "bookmarks.html";
84 nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
85 if (prefs) {
86 nsCString path;
87 rv = prefs->GetCharPref("browser.bookmarks.file", getter_Copies(path));
88 if (NS_SUCCEEDED(rv)) {
89 NS_NewNativeLocalFile(path, true, (nsILocalFile**)(nsIFile**) getter_AddRefs(file));
93 else if (!strcmp(aKey, NS_APP_EXISTING_PREF_OVERRIDE)) {
94 rv = NS_GetSpecialDirectory(NS_APP_DEFAULTS_50_DIR,
95 getter_AddRefs(file));
96 NS_ENSURE_SUCCESS(rv, rv);
98 file->AppendNative(NS_LITERAL_CSTRING("existing-profile-defaults.js"));
99 file.swap(*aResult);
100 return NS_OK;
102 else {
103 return NS_ERROR_FAILURE;
106 nsDependentCString leafstr(leafName);
108 nsCOMPtr<nsIFile> parentDir;
109 if (file) {
110 rv = file->GetParent(getter_AddRefs(parentDir));
111 if (NS_FAILED(rv))
112 return rv;
114 else {
115 rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(parentDir));
116 if (NS_FAILED(rv))
117 return rv;
119 rv = parentDir->Clone(getter_AddRefs(file));
120 if (NS_FAILED(rv))
121 return rv;
123 file->AppendNative(leafstr);
126 *aPersist = true;
127 NS_ADDREF(*aResult = file);
129 return NS_OK;
132 static void
133 AppendFileKey(const char *key, nsIProperties* aDirSvc,
134 nsCOMArray<nsIFile> &array)
136 nsCOMPtr<nsIFile> file;
137 nsresult rv = aDirSvc->Get(key, NS_GET_IID(nsIFile), getter_AddRefs(file));
138 if (NS_FAILED(rv))
139 return;
141 bool exists;
142 rv = file->Exists(&exists);
143 if (NS_FAILED(rv) || !exists)
144 return;
146 array.AppendObject(file);
149 // Appends the distribution-specific search engine directories to the
150 // array. The directory structure is as follows:
152 // appdir/
153 // \- distribution/
154 // \- searchplugins/
155 // |- common/
156 // \- locale/
157 // |- <locale 1>/
158 // ...
159 // \- <locale N>/
161 // common engines are loaded for all locales. If there is no locale
162 // directory for the current locale, there is a pref:
163 // "distribution.searchplugins.defaultLocale"
164 // which specifies a default locale to use.
166 static void
167 AppendDistroSearchDirs(nsIProperties* aDirSvc, nsCOMArray<nsIFile> &array)
169 nsCOMPtr<nsIFile> searchPlugins;
170 nsresult rv = aDirSvc->Get(NS_XPCOM_CURRENT_PROCESS_DIR,
171 NS_GET_IID(nsIFile),
172 getter_AddRefs(searchPlugins));
173 if (NS_FAILED(rv))
174 return;
175 searchPlugins->AppendNative(NS_LITERAL_CSTRING("distribution"));
176 searchPlugins->AppendNative(NS_LITERAL_CSTRING("searchplugins"));
178 bool exists;
179 rv = searchPlugins->Exists(&exists);
180 if (NS_FAILED(rv) || !exists)
181 return;
183 nsCOMPtr<nsIFile> commonPlugins;
184 rv = searchPlugins->Clone(getter_AddRefs(commonPlugins));
185 if (NS_SUCCEEDED(rv)) {
186 commonPlugins->AppendNative(NS_LITERAL_CSTRING("common"));
187 rv = commonPlugins->Exists(&exists);
188 if (NS_SUCCEEDED(rv) && exists)
189 array.AppendObject(commonPlugins);
192 nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
193 if (prefs) {
195 nsCOMPtr<nsIFile> localePlugins;
196 rv = searchPlugins->Clone(getter_AddRefs(localePlugins));
197 if (NS_FAILED(rv))
198 return;
200 localePlugins->AppendNative(NS_LITERAL_CSTRING("locale"));
202 nsCString locale;
203 rv = prefs->GetCharPref("general.useragent.locale", getter_Copies(locale));
204 if (NS_SUCCEEDED(rv)) {
206 nsCOMPtr<nsIFile> curLocalePlugins;
207 rv = localePlugins->Clone(getter_AddRefs(curLocalePlugins));
208 if (NS_SUCCEEDED(rv)) {
210 curLocalePlugins->AppendNative(locale);
211 rv = curLocalePlugins->Exists(&exists);
212 if (NS_SUCCEEDED(rv) && exists) {
213 array.AppendObject(curLocalePlugins);
214 return; // all done
219 // we didn't append the locale dir - try the default one
220 nsCString defLocale;
221 rv = prefs->GetCharPref("distribution.searchplugins.defaultLocale",
222 getter_Copies(defLocale));
223 if (NS_SUCCEEDED(rv)) {
225 nsCOMPtr<nsIFile> defLocalePlugins;
226 rv = localePlugins->Clone(getter_AddRefs(defLocalePlugins));
227 if (NS_SUCCEEDED(rv)) {
229 defLocalePlugins->AppendNative(defLocale);
230 rv = defLocalePlugins->Exists(&exists);
231 if (NS_SUCCEEDED(rv) && exists)
232 array.AppendObject(defLocalePlugins);
238 NS_IMETHODIMP
239 DirectoryProvider::GetFiles(const char *aKey, nsISimpleEnumerator* *aResult)
241 nsresult rv;
243 if (!strcmp(aKey, NS_APP_SEARCH_DIR_LIST)) {
244 nsCOMPtr<nsIProperties> dirSvc
245 (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
246 if (!dirSvc)
247 return NS_ERROR_FAILURE;
249 nsCOMArray<nsIFile> baseFiles;
252 * We want to preserve the following order, since the search service loads
253 * engines in first-loaded-wins order.
254 * - extension search plugin locations (prepended below using
255 * NS_NewUnionEnumerator)
256 * - distro search plugin locations
257 * - user search plugin locations (profile)
258 * - app search plugin location (shipped engines)
260 AppendDistroSearchDirs(dirSvc, baseFiles);
261 AppendFileKey(NS_APP_USER_SEARCH_DIR, dirSvc, baseFiles);
262 AppendFileKey(NS_APP_SEARCH_DIR, dirSvc, baseFiles);
264 nsCOMPtr<nsISimpleEnumerator> baseEnum;
265 rv = NS_NewArrayEnumerator(getter_AddRefs(baseEnum), baseFiles);
266 if (NS_FAILED(rv))
267 return rv;
269 nsCOMPtr<nsISimpleEnumerator> list;
270 rv = dirSvc->Get(XRE_EXTENSIONS_DIR_LIST,
271 NS_GET_IID(nsISimpleEnumerator), getter_AddRefs(list));
272 if (NS_FAILED(rv))
273 return rv;
275 static char const *const kAppendSPlugins[] = {"searchplugins", nsnull};
277 nsCOMPtr<nsISimpleEnumerator> extEnum =
278 new AppendingEnumerator(list, kAppendSPlugins);
279 if (!extEnum)
280 return NS_ERROR_OUT_OF_MEMORY;
282 return NS_NewUnionEnumerator(aResult, extEnum, baseEnum);
285 return NS_ERROR_FAILURE;
288 NS_IMPL_ISUPPORTS1(DirectoryProvider::AppendingEnumerator, nsISimpleEnumerator)
290 NS_IMETHODIMP
291 DirectoryProvider::AppendingEnumerator::HasMoreElements(bool *aResult)
293 *aResult = mNext ? true : false;
294 return NS_OK;
297 NS_IMETHODIMP
298 DirectoryProvider::AppendingEnumerator::GetNext(nsISupports* *aResult)
300 if (aResult)
301 NS_ADDREF(*aResult = mNext);
303 mNext = nsnull;
305 nsresult rv;
307 // Ignore all errors
309 bool more;
310 while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) {
311 nsCOMPtr<nsISupports> nextbasesupp;
312 mBase->GetNext(getter_AddRefs(nextbasesupp));
314 nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp));
315 if (!nextbase)
316 continue;
318 nextbase->Clone(getter_AddRefs(mNext));
319 if (!mNext)
320 continue;
322 char const *const * i = mAppendList;
323 while (*i) {
324 mNext->AppendNative(nsDependentCString(*i));
325 ++i;
328 bool exists;
329 rv = mNext->Exists(&exists);
330 if (NS_SUCCEEDED(rv) && exists)
331 break;
333 mNext = nsnull;
336 return NS_OK;
339 DirectoryProvider::AppendingEnumerator::AppendingEnumerator
340 (nsISimpleEnumerator* aBase,
341 char const *const *aAppendList) :
342 mBase(aBase),
343 mAppendList(aAppendList)
345 // Initialize mNext to begin.
346 GetNext(nsnull);
349 } // namespace browser
350 } // namespace mozilla