/src/mozilla-central/browser/components/dirprovider/DirectoryProvider.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include "nsIDirectoryService.h" |
6 | | #include "DirectoryProvider.h" |
7 | | |
8 | | #include "nsIFile.h" |
9 | | #include "nsISimpleEnumerator.h" |
10 | | #include "nsIPrefService.h" |
11 | | #include "nsIPrefBranch.h" |
12 | | |
13 | | #include "nsArrayEnumerator.h" |
14 | | #include "nsEnumeratorUtils.h" |
15 | | #include "nsAppDirectoryServiceDefs.h" |
16 | | #include "nsDirectoryServiceDefs.h" |
17 | | #include "nsCategoryManagerUtils.h" |
18 | | #include "nsComponentManagerUtils.h" |
19 | | #include "nsCOMArray.h" |
20 | | #include "nsDirectoryServiceUtils.h" |
21 | | #include "mozilla/ModuleUtils.h" |
22 | | #include "mozilla/intl/LocaleService.h" |
23 | | #include "nsServiceManagerUtils.h" |
24 | | #include "nsString.h" |
25 | | #include "nsXULAppAPI.h" |
26 | | #include "nsIPrefLocalizedString.h" |
27 | | |
28 | | using mozilla::intl::LocaleService; |
29 | | |
30 | | namespace mozilla { |
31 | | namespace browser { |
32 | | |
33 | | NS_IMPL_ISUPPORTS(DirectoryProvider, |
34 | | nsIDirectoryServiceProvider, |
35 | | nsIDirectoryServiceProvider2) |
36 | | |
37 | | NS_IMETHODIMP |
38 | | DirectoryProvider::GetFile(const char *aKey, bool *aPersist, nsIFile* *aResult) |
39 | 15 | { |
40 | 15 | return NS_ERROR_FAILURE; |
41 | 15 | } |
42 | | |
43 | | // Appends the distribution-specific search engine directories to the |
44 | | // array. The directory structure is as follows: |
45 | | |
46 | | // appdir/ |
47 | | // \- distribution/ |
48 | | // \- searchplugins/ |
49 | | // |- common/ |
50 | | // \- locale/ |
51 | | // |- <locale 1>/ |
52 | | // ... |
53 | | // \- <locale N>/ |
54 | | |
55 | | // common engines are loaded for all locales. If there is no locale |
56 | | // directory for the current locale, there is a pref: |
57 | | // "distribution.searchplugins.defaultLocale" |
58 | | // which specifies a default locale to use. |
59 | | |
60 | | static void |
61 | | AppendDistroSearchDirs(nsIProperties* aDirSvc, nsCOMArray<nsIFile> &array) |
62 | 0 | { |
63 | 0 | nsCOMPtr<nsIFile> searchPlugins; |
64 | 0 | nsresult rv = aDirSvc->Get(XRE_APP_DISTRIBUTION_DIR, |
65 | 0 | NS_GET_IID(nsIFile), |
66 | 0 | getter_AddRefs(searchPlugins)); |
67 | 0 | if (NS_FAILED(rv)) |
68 | 0 | return; |
69 | 0 | searchPlugins->AppendNative(NS_LITERAL_CSTRING("searchplugins")); |
70 | 0 |
|
71 | 0 | bool exists; |
72 | 0 | rv = searchPlugins->Exists(&exists); |
73 | 0 | if (NS_FAILED(rv) || !exists) |
74 | 0 | return; |
75 | 0 | |
76 | 0 | nsCOMPtr<nsIFile> commonPlugins; |
77 | 0 | rv = searchPlugins->Clone(getter_AddRefs(commonPlugins)); |
78 | 0 | if (NS_SUCCEEDED(rv)) { |
79 | 0 | commonPlugins->AppendNative(NS_LITERAL_CSTRING("common")); |
80 | 0 | rv = commonPlugins->Exists(&exists); |
81 | 0 | if (NS_SUCCEEDED(rv) && exists) |
82 | 0 | array.AppendObject(commonPlugins); |
83 | 0 | } |
84 | 0 |
|
85 | 0 | nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID)); |
86 | 0 | if (prefs) { |
87 | 0 |
|
88 | 0 | nsCOMPtr<nsIFile> localePlugins; |
89 | 0 | rv = searchPlugins->Clone(getter_AddRefs(localePlugins)); |
90 | 0 | if (NS_FAILED(rv)) |
91 | 0 | return; |
92 | 0 | |
93 | 0 | localePlugins->AppendNative(NS_LITERAL_CSTRING("locale")); |
94 | 0 |
|
95 | 0 | nsAutoCString defLocale; |
96 | 0 | rv = prefs->GetCharPref("distribution.searchplugins.defaultLocale", |
97 | 0 | defLocale); |
98 | 0 | if (NS_SUCCEEDED(rv)) { |
99 | 0 |
|
100 | 0 | nsCOMPtr<nsIFile> defLocalePlugins; |
101 | 0 | rv = localePlugins->Clone(getter_AddRefs(defLocalePlugins)); |
102 | 0 | if (NS_SUCCEEDED(rv)) { |
103 | 0 |
|
104 | 0 | defLocalePlugins->AppendNative(defLocale); |
105 | 0 | rv = defLocalePlugins->Exists(&exists); |
106 | 0 | if (NS_SUCCEEDED(rv) && exists) { |
107 | 0 | array.AppendObject(defLocalePlugins); |
108 | 0 | return; // all done |
109 | 0 | } |
110 | 0 | } |
111 | 0 | } |
112 | 0 | |
113 | 0 | // we didn't have a defaultLocale, use the user agent locale |
114 | 0 | nsAutoCString locale; |
115 | 0 | LocaleService::GetInstance()->GetAppLocaleAsLangTag(locale); |
116 | 0 |
|
117 | 0 | nsCOMPtr<nsIFile> curLocalePlugins; |
118 | 0 | rv = localePlugins->Clone(getter_AddRefs(curLocalePlugins)); |
119 | 0 | if (NS_SUCCEEDED(rv)) { |
120 | 0 |
|
121 | 0 | curLocalePlugins->AppendNative(locale); |
122 | 0 | rv = curLocalePlugins->Exists(&exists); |
123 | 0 | if (NS_SUCCEEDED(rv) && exists) { |
124 | 0 | array.AppendObject(curLocalePlugins); |
125 | 0 | return; // all done |
126 | 0 | } |
127 | 0 | } |
128 | 0 | } |
129 | 0 | } |
130 | | |
131 | | NS_IMETHODIMP |
132 | | DirectoryProvider::GetFiles(const char *aKey, nsISimpleEnumerator* *aResult) |
133 | 0 | { |
134 | 0 | if (!strcmp(aKey, NS_APP_DISTRIBUTION_SEARCH_DIR_LIST)) { |
135 | 0 | nsCOMPtr<nsIProperties> dirSvc |
136 | 0 | (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID)); |
137 | 0 | if (!dirSvc) |
138 | 0 | return NS_ERROR_FAILURE; |
139 | 0 | |
140 | 0 | nsCOMArray<nsIFile> distroFiles; |
141 | 0 | AppendDistroSearchDirs(dirSvc, distroFiles); |
142 | 0 |
|
143 | 0 | return NS_NewArrayEnumerator(aResult, distroFiles, NS_GET_IID(nsIFile)); |
144 | 0 | } |
145 | 0 |
|
146 | 0 | return NS_ERROR_FAILURE; |
147 | 0 | } |
148 | | |
149 | | NS_IMETHODIMP |
150 | | DirectoryProvider::AppendingEnumerator::HasMoreElements(bool *aResult) |
151 | 0 | { |
152 | 0 | *aResult = mNext ? true : false; |
153 | 0 | return NS_OK; |
154 | 0 | } |
155 | | |
156 | | NS_IMETHODIMP |
157 | | DirectoryProvider::AppendingEnumerator::GetNext(nsISupports* *aResult) |
158 | 0 | { |
159 | 0 | if (aResult) |
160 | 0 | NS_ADDREF(*aResult = mNext); |
161 | 0 |
|
162 | 0 | mNext = nullptr; |
163 | 0 |
|
164 | 0 | nsresult rv; |
165 | 0 |
|
166 | 0 | // Ignore all errors |
167 | 0 |
|
168 | 0 | bool more; |
169 | 0 | while (NS_SUCCEEDED(mBase->HasMoreElements(&more)) && more) { |
170 | 0 | nsCOMPtr<nsISupports> nextbasesupp; |
171 | 0 | mBase->GetNext(getter_AddRefs(nextbasesupp)); |
172 | 0 |
|
173 | 0 | nsCOMPtr<nsIFile> nextbase(do_QueryInterface(nextbasesupp)); |
174 | 0 | if (!nextbase) |
175 | 0 | continue; |
176 | 0 | |
177 | 0 | nextbase->Clone(getter_AddRefs(mNext)); |
178 | 0 | if (!mNext) |
179 | 0 | continue; |
180 | 0 | |
181 | 0 | char const *const * i = mAppendList; |
182 | 0 | while (*i) { |
183 | 0 | mNext->AppendNative(nsDependentCString(*i)); |
184 | 0 | ++i; |
185 | 0 | } |
186 | 0 |
|
187 | 0 | bool exists; |
188 | 0 | rv = mNext->Exists(&exists); |
189 | 0 | if (NS_SUCCEEDED(rv) && exists) |
190 | 0 | break; |
191 | 0 | |
192 | 0 | mNext = nullptr; |
193 | 0 | } |
194 | 0 |
|
195 | 0 | return NS_OK; |
196 | 0 | } |
197 | | |
198 | | DirectoryProvider::AppendingEnumerator::AppendingEnumerator |
199 | | (nsISimpleEnumerator* aBase, |
200 | | char const *const *aAppendList) : |
201 | | mBase(aBase), |
202 | | mAppendList(aAppendList) |
203 | 0 | { |
204 | 0 | // Initialize mNext to begin. |
205 | 0 | GetNext(nullptr); |
206 | 0 | } |
207 | | |
208 | | } // namespace browser |
209 | | } // namespace mozilla |