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