/src/mozilla-central/netwerk/cache/nsApplicationCacheService.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 file, |
3 | | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include "nsDiskCache.h" |
6 | | #include "nsDiskCacheDeviceSQL.h" |
7 | | #include "nsCacheService.h" |
8 | | #include "nsApplicationCacheService.h" |
9 | | #include "nsCRT.h" |
10 | | #include "nsNetCID.h" |
11 | | #include "nsNetUtil.h" |
12 | | #include "nsIObserverService.h" |
13 | | #include "nsIPrincipal.h" |
14 | | #include "mozilla/LoadContextInfo.h" |
15 | | |
16 | | using namespace mozilla; |
17 | | |
18 | | static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID); |
19 | | |
20 | | //----------------------------------------------------------------------------- |
21 | | // nsApplicationCacheService |
22 | | //----------------------------------------------------------------------------- |
23 | | |
24 | | NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService) |
25 | | |
26 | | nsApplicationCacheService::nsApplicationCacheService() |
27 | 0 | { |
28 | 0 | nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID); |
29 | 0 | mCacheService = nsCacheService::GlobalInstance(); |
30 | 0 | } |
31 | | |
32 | | NS_IMETHODIMP |
33 | | nsApplicationCacheService::BuildGroupIDForInfo( |
34 | | nsIURI *aManifestURL, |
35 | | nsILoadContextInfo *aLoadContextInfo, |
36 | | nsACString &_result) |
37 | 0 | { |
38 | 0 | nsresult rv; |
39 | 0 |
|
40 | 0 | nsAutoCString originSuffix; |
41 | 0 | if (aLoadContextInfo) { |
42 | 0 | aLoadContextInfo->OriginAttributesPtr()->CreateSuffix(originSuffix); |
43 | 0 | } |
44 | 0 |
|
45 | 0 | rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID( |
46 | 0 | aManifestURL, originSuffix, _result); |
47 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
48 | 0 |
|
49 | 0 | return NS_OK; |
50 | 0 | } |
51 | | |
52 | | NS_IMETHODIMP |
53 | | nsApplicationCacheService::BuildGroupIDForSuffix( |
54 | | nsIURI *aManifestURL, |
55 | | nsACString const &aOriginSuffix, |
56 | | nsACString &_result) |
57 | 0 | { |
58 | 0 | nsresult rv; |
59 | 0 |
|
60 | 0 | rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID( |
61 | 0 | aManifestURL, aOriginSuffix, _result); |
62 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
63 | 0 |
|
64 | 0 | return NS_OK; |
65 | 0 | } |
66 | | |
67 | | NS_IMETHODIMP |
68 | | nsApplicationCacheService::CreateApplicationCache(const nsACString &group, |
69 | | nsIApplicationCache **out) |
70 | 0 | { |
71 | 0 | if (!mCacheService) |
72 | 0 | return NS_ERROR_UNEXPECTED; |
73 | 0 | |
74 | 0 | RefPtr<nsOfflineCacheDevice> device; |
75 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
76 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
77 | 0 | return device->CreateApplicationCache(group, out); |
78 | 0 | } |
79 | | |
80 | | NS_IMETHODIMP |
81 | | nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group, |
82 | | nsIFile *profileDir, |
83 | | int32_t quota, |
84 | | nsIApplicationCache **out) |
85 | 0 | { |
86 | 0 | if (!mCacheService) |
87 | 0 | return NS_ERROR_UNEXPECTED; |
88 | 0 | |
89 | 0 | RefPtr<nsOfflineCacheDevice> device; |
90 | 0 | nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir, |
91 | 0 | quota, |
92 | 0 | getter_AddRefs(device)); |
93 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
94 | 0 | return device->CreateApplicationCache(group, out); |
95 | 0 | } |
96 | | |
97 | | NS_IMETHODIMP |
98 | | nsApplicationCacheService::GetApplicationCache(const nsACString &clientID, |
99 | | nsIApplicationCache **out) |
100 | 0 | { |
101 | 0 | if (!mCacheService) |
102 | 0 | return NS_ERROR_UNEXPECTED; |
103 | 0 | |
104 | 0 | RefPtr<nsOfflineCacheDevice> device; |
105 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
106 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
107 | 0 | return device->GetApplicationCache(clientID, out); |
108 | 0 | } |
109 | | |
110 | | NS_IMETHODIMP |
111 | | nsApplicationCacheService::GetActiveCache(const nsACString &group, |
112 | | nsIApplicationCache **out) |
113 | 0 | { |
114 | 0 | if (!mCacheService) |
115 | 0 | return NS_ERROR_UNEXPECTED; |
116 | 0 | |
117 | 0 | RefPtr<nsOfflineCacheDevice> device; |
118 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
119 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
120 | 0 | return device->GetActiveCache(group, out); |
121 | 0 | } |
122 | | |
123 | | NS_IMETHODIMP |
124 | | nsApplicationCacheService::DeactivateGroup(const nsACString &group) |
125 | 0 | { |
126 | 0 | if (!mCacheService) |
127 | 0 | return NS_ERROR_UNEXPECTED; |
128 | 0 | |
129 | 0 | RefPtr<nsOfflineCacheDevice> device; |
130 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
131 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
132 | 0 | return device->DeactivateGroup(group); |
133 | 0 | } |
134 | | |
135 | | NS_IMETHODIMP |
136 | | nsApplicationCacheService::ChooseApplicationCache(const nsACString &key, |
137 | | nsILoadContextInfo *aLoadContextInfo, |
138 | | nsIApplicationCache **out) |
139 | 0 | { |
140 | 0 | if (!mCacheService) |
141 | 0 | return NS_ERROR_UNEXPECTED; |
142 | 0 | |
143 | 0 | RefPtr<nsOfflineCacheDevice> device; |
144 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
145 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
146 | 0 |
|
147 | 0 | return device->ChooseApplicationCache(key, aLoadContextInfo, out); |
148 | 0 | } |
149 | | |
150 | | NS_IMETHODIMP |
151 | | nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache, |
152 | | const nsACString &key) |
153 | 0 | { |
154 | 0 | if (!mCacheService) |
155 | 0 | return NS_ERROR_UNEXPECTED; |
156 | 0 | |
157 | 0 | RefPtr<nsOfflineCacheDevice> device; |
158 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
159 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
160 | 0 | return device->CacheOpportunistically(cache, key); |
161 | 0 | } |
162 | | |
163 | | NS_IMETHODIMP |
164 | | nsApplicationCacheService::Evict(nsILoadContextInfo *aInfo) |
165 | 0 | { |
166 | 0 | if (!mCacheService) |
167 | 0 | return NS_ERROR_UNEXPECTED; |
168 | 0 | |
169 | 0 | RefPtr<nsOfflineCacheDevice> device; |
170 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
171 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
172 | 0 | return device->Evict(aInfo); |
173 | 0 | } |
174 | | |
175 | | NS_IMETHODIMP |
176 | | nsApplicationCacheService::EvictMatchingOriginAttributes(nsAString const &aPattern) |
177 | 0 | { |
178 | 0 | if (!mCacheService) |
179 | 0 | return NS_ERROR_UNEXPECTED; |
180 | 0 | |
181 | 0 | RefPtr<nsOfflineCacheDevice> device; |
182 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
183 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
184 | 0 |
|
185 | 0 | mozilla::OriginAttributesPattern pattern; |
186 | 0 | if (!pattern.Init(aPattern)) { |
187 | 0 | NS_ERROR("Could not parse OriginAttributesPattern JSON in clear-origin-attributes-data notification"); |
188 | 0 | return NS_ERROR_FAILURE; |
189 | 0 | } |
190 | 0 |
|
191 | 0 | return device->Evict(pattern); |
192 | 0 | } |
193 | | |
194 | | NS_IMETHODIMP |
195 | | nsApplicationCacheService::GetGroups(uint32_t *count, |
196 | | char ***keys) |
197 | 0 | { |
198 | 0 | if (!mCacheService) |
199 | 0 | return NS_ERROR_UNEXPECTED; |
200 | 0 | |
201 | 0 | RefPtr<nsOfflineCacheDevice> device; |
202 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
203 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
204 | 0 | return device->GetGroups(count, keys); |
205 | 0 | } |
206 | | |
207 | | NS_IMETHODIMP |
208 | | nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count, |
209 | | char ***keys) |
210 | 0 | { |
211 | 0 | if (!mCacheService) |
212 | 0 | return NS_ERROR_UNEXPECTED; |
213 | 0 | |
214 | 0 | RefPtr<nsOfflineCacheDevice> device; |
215 | 0 | nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); |
216 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
217 | 0 | return device->GetGroupsTimeOrdered(count, keys); |
218 | 0 | } |
219 | | |
220 | | //----------------------------------------------------------------------------- |
221 | | // AppCacheClearDataObserver: handles clearing appcache data for app uninstall |
222 | | // and clearing user data events. |
223 | | //----------------------------------------------------------------------------- |
224 | | |
225 | | namespace { |
226 | | |
227 | | class AppCacheClearDataObserver final : public nsIObserver { |
228 | | public: |
229 | | NS_DECL_ISUPPORTS |
230 | | |
231 | | // nsIObserver implementation. |
232 | | NS_IMETHOD |
233 | | Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData) override |
234 | 0 | { |
235 | 0 | MOZ_ASSERT(!nsCRT::strcmp(aTopic, "clear-origin-attributes-data")); |
236 | 0 |
|
237 | 0 | nsresult rv; |
238 | 0 |
|
239 | 0 | nsCOMPtr<nsIApplicationCacheService> cacheService = |
240 | 0 | do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); |
241 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
242 | 0 |
|
243 | 0 | return cacheService->EvictMatchingOriginAttributes(nsDependentString(aData)); |
244 | 0 | } |
245 | | |
246 | | private: |
247 | | ~AppCacheClearDataObserver() = default; |
248 | | }; |
249 | | |
250 | | NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver) |
251 | | |
252 | | } // namespace |
253 | | |
254 | | // Instantiates and registers AppCacheClearDataObserver for notifications |
255 | | void |
256 | | nsApplicationCacheService::AppClearDataObserverInit() |
257 | 3 | { |
258 | 3 | nsCOMPtr<nsIObserverService> observerService = services::GetObserverService(); |
259 | 3 | if (observerService) { |
260 | 3 | RefPtr<AppCacheClearDataObserver> obs = new AppCacheClearDataObserver(); |
261 | 3 | observerService->AddObserver(obs, "clear-origin-attributes-data", /*ownsWeak=*/ false); |
262 | 3 | } |
263 | 3 | } |