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
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.
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"
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"
63 NS_IMPL_ISUPPORTS2(DirectoryProvider
,
64 nsIDirectoryServiceProvider
,
65 nsIDirectoryServiceProvider2
)
68 DirectoryProvider::GetFile(const char *aKey
, bool *aPersist
, nsIFile
* *aResult
)
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
));
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"));
103 return NS_ERROR_FAILURE
;
106 nsDependentCString
leafstr(leafName
);
108 nsCOMPtr
<nsIFile
> parentDir
;
110 rv
= file
->GetParent(getter_AddRefs(parentDir
));
115 rv
= NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR
, getter_AddRefs(parentDir
));
119 rv
= parentDir
->Clone(getter_AddRefs(file
));
123 file
->AppendNative(leafstr
);
127 NS_ADDREF(*aResult
= file
);
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
));
142 rv
= file
->Exists(&exists
);
143 if (NS_FAILED(rv
) || !exists
)
146 array
.AppendObject(file
);
149 // Appends the distribution-specific search engine directories to the
150 // array. The directory structure is as follows:
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.
167 AppendDistroSearchDirs(nsIProperties
* aDirSvc
, nsCOMArray
<nsIFile
> &array
)
169 nsCOMPtr
<nsIFile
> searchPlugins
;
170 nsresult rv
= aDirSvc
->Get(NS_XPCOM_CURRENT_PROCESS_DIR
,
172 getter_AddRefs(searchPlugins
));
175 searchPlugins
->AppendNative(NS_LITERAL_CSTRING("distribution"));
176 searchPlugins
->AppendNative(NS_LITERAL_CSTRING("searchplugins"));
179 rv
= searchPlugins
->Exists(&exists
);
180 if (NS_FAILED(rv
) || !exists
)
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
));
195 nsCOMPtr
<nsIFile
> localePlugins
;
196 rv
= searchPlugins
->Clone(getter_AddRefs(localePlugins
));
200 localePlugins
->AppendNative(NS_LITERAL_CSTRING("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
);
219 // we didn't append the locale dir - try the default one
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
);
239 DirectoryProvider::GetFiles(const char *aKey
, nsISimpleEnumerator
* *aResult
)
243 if (!strcmp(aKey
, NS_APP_SEARCH_DIR_LIST
)) {
244 nsCOMPtr
<nsIProperties
> dirSvc
245 (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID
));
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
);
269 nsCOMPtr
<nsISimpleEnumerator
> list
;
270 rv
= dirSvc
->Get(XRE_EXTENSIONS_DIR_LIST
,
271 NS_GET_IID(nsISimpleEnumerator
), getter_AddRefs(list
));
275 static char const *const kAppendSPlugins
[] = {"searchplugins", nsnull
};
277 nsCOMPtr
<nsISimpleEnumerator
> extEnum
=
278 new AppendingEnumerator(list
, kAppendSPlugins
);
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
)
291 DirectoryProvider::AppendingEnumerator::HasMoreElements(bool *aResult
)
293 *aResult
= mNext
? true : false;
298 DirectoryProvider::AppendingEnumerator::GetNext(nsISupports
* *aResult
)
301 NS_ADDREF(*aResult
= mNext
);
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
));
318 nextbase
->Clone(getter_AddRefs(mNext
));
322 char const *const * i
= mAppendList
;
324 mNext
->AppendNative(nsDependentCString(*i
));
329 rv
= mNext
->Exists(&exists
);
330 if (NS_SUCCEEDED(rv
) && exists
)
339 DirectoryProvider::AppendingEnumerator::AppendingEnumerator
340 (nsISimpleEnumerator
* aBase
,
341 char const *const *aAppendList
) :
343 mAppendList(aAppendList
)
345 // Initialize mNext to begin.
349 } // namespace browser
350 } // namespace mozilla