netwerk/cache/nsApplicationCacheService.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:228584734a3d
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 "nsNetUtil.h"
11 #include "nsIObserverService.h"
12 #include "nsILoadContextInfo.h"
13
14 using namespace mozilla;
15
16 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
17
18 //-----------------------------------------------------------------------------
19 // nsApplicationCacheService
20 //-----------------------------------------------------------------------------
21
22 NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService)
23
24 nsApplicationCacheService::nsApplicationCacheService()
25 {
26 nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
27 mCacheService = nsCacheService::GlobalInstance();
28 }
29
30 NS_IMETHODIMP
31 nsApplicationCacheService::BuildGroupID(nsIURI *aManifestURL,
32 nsILoadContextInfo *aLoadContextInfo,
33 nsACString &_result)
34 {
35 nsresult rv;
36
37 uint32_t appId = NECKO_NO_APP_ID;
38 bool isInBrowserElement = false;
39
40 if (aLoadContextInfo) {
41 appId = aLoadContextInfo->AppId();
42 isInBrowserElement = aLoadContextInfo->IsInBrowserElement();
43 }
44
45 rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
46 aManifestURL, appId, isInBrowserElement, _result);
47 NS_ENSURE_SUCCESS(rv, rv);
48
49 return NS_OK;
50 }
51
52 NS_IMETHODIMP
53 nsApplicationCacheService::BuildGroupIDForApp(nsIURI *aManifestURL,
54 uint32_t aAppId,
55 bool aIsInBrowser,
56 nsACString &_result)
57 {
58 nsresult rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
59 aManifestURL, aAppId, aIsInBrowser, _result);
60 NS_ENSURE_SUCCESS(rv, rv);
61
62 return NS_OK;
63 }
64
65 NS_IMETHODIMP
66 nsApplicationCacheService::CreateApplicationCache(const nsACString &group,
67 nsIApplicationCache **out)
68 {
69 if (!mCacheService)
70 return NS_ERROR_UNEXPECTED;
71
72 nsRefPtr<nsOfflineCacheDevice> device;
73 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
74 NS_ENSURE_SUCCESS(rv, rv);
75 return device->CreateApplicationCache(group, out);
76 }
77
78 NS_IMETHODIMP
79 nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group,
80 nsIFile *profileDir,
81 int32_t quota,
82 nsIApplicationCache **out)
83 {
84 if (!mCacheService)
85 return NS_ERROR_UNEXPECTED;
86
87 nsRefPtr<nsOfflineCacheDevice> device;
88 nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir,
89 quota,
90 getter_AddRefs(device));
91 NS_ENSURE_SUCCESS(rv, rv);
92 return device->CreateApplicationCache(group, out);
93 }
94
95 NS_IMETHODIMP
96 nsApplicationCacheService::GetApplicationCache(const nsACString &clientID,
97 nsIApplicationCache **out)
98 {
99 if (!mCacheService)
100 return NS_ERROR_UNEXPECTED;
101
102 nsRefPtr<nsOfflineCacheDevice> device;
103 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
104 NS_ENSURE_SUCCESS(rv, rv);
105 return device->GetApplicationCache(clientID, out);
106 }
107
108 NS_IMETHODIMP
109 nsApplicationCacheService::GetActiveCache(const nsACString &group,
110 nsIApplicationCache **out)
111 {
112 if (!mCacheService)
113 return NS_ERROR_UNEXPECTED;
114
115 nsRefPtr<nsOfflineCacheDevice> device;
116 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
117 NS_ENSURE_SUCCESS(rv, rv);
118 return device->GetActiveCache(group, out);
119 }
120
121 NS_IMETHODIMP
122 nsApplicationCacheService::DeactivateGroup(const nsACString &group)
123 {
124 if (!mCacheService)
125 return NS_ERROR_UNEXPECTED;
126
127 nsRefPtr<nsOfflineCacheDevice> device;
128 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
129 NS_ENSURE_SUCCESS(rv, rv);
130 return device->DeactivateGroup(group);
131 }
132
133 NS_IMETHODIMP
134 nsApplicationCacheService::ChooseApplicationCache(const nsACString &key,
135 nsILoadContextInfo *aLoadContextInfo,
136 nsIApplicationCache **out)
137 {
138 if (!mCacheService)
139 return NS_ERROR_UNEXPECTED;
140
141 nsRefPtr<nsOfflineCacheDevice> device;
142 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
143 NS_ENSURE_SUCCESS(rv, rv);
144
145 return device->ChooseApplicationCache(key, aLoadContextInfo, out);
146 }
147
148 NS_IMETHODIMP
149 nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache,
150 const nsACString &key)
151 {
152 if (!mCacheService)
153 return NS_ERROR_UNEXPECTED;
154
155 nsRefPtr<nsOfflineCacheDevice> device;
156 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
157 NS_ENSURE_SUCCESS(rv, rv);
158 return device->CacheOpportunistically(cache, key);
159 }
160
161 NS_IMETHODIMP
162 nsApplicationCacheService::DiscardByAppId(int32_t appID, bool isInBrowser)
163 {
164 if (!mCacheService)
165 return NS_ERROR_UNEXPECTED;
166
167 nsRefPtr<nsOfflineCacheDevice> device;
168 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
169 NS_ENSURE_SUCCESS(rv, rv);
170 return device->DiscardByAppId(appID, isInBrowser);
171 }
172
173 NS_IMETHODIMP
174 nsApplicationCacheService::GetGroups(uint32_t *count,
175 char ***keys)
176 {
177 if (!mCacheService)
178 return NS_ERROR_UNEXPECTED;
179
180 nsRefPtr<nsOfflineCacheDevice> device;
181 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
182 NS_ENSURE_SUCCESS(rv, rv);
183 return device->GetGroups(count, keys);
184 }
185
186 NS_IMETHODIMP
187 nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count,
188 char ***keys)
189 {
190 if (!mCacheService)
191 return NS_ERROR_UNEXPECTED;
192
193 nsRefPtr<nsOfflineCacheDevice> device;
194 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
195 NS_ENSURE_SUCCESS(rv, rv);
196 return device->GetGroupsTimeOrdered(count, keys);
197 }
198
199 //-----------------------------------------------------------------------------
200 // AppCacheClearDataObserver: handles clearing appcache data for app uninstall
201 // and clearing user data events.
202 //-----------------------------------------------------------------------------
203
204 namespace {
205
206 class AppCacheClearDataObserver MOZ_FINAL : public nsIObserver {
207 public:
208 NS_DECL_ISUPPORTS
209
210 // nsIObserver implementation.
211 NS_IMETHODIMP
212 Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData)
213 {
214 MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA));
215
216 uint32_t appId = NECKO_UNKNOWN_APP_ID;
217 bool browserOnly = false;
218 nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId,
219 &browserOnly);
220 NS_ENSURE_SUCCESS(rv, rv);
221
222 nsCOMPtr<nsIApplicationCacheService> cacheService =
223 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
224 NS_ENSURE_SUCCESS(rv, rv);
225
226 return cacheService->DiscardByAppId(appId, browserOnly);
227 }
228 };
229
230 NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver)
231
232 } // anonymous namespace
233
234 // Instantiates and registers AppCacheClearDataObserver for notifications
235 void
236 nsApplicationCacheService::AppClearDataObserverInit()
237 {
238 nsCOMPtr<nsIObserverService> observerService =
239 do_GetService("@mozilla.org/observer-service;1");
240 if (observerService) {
241 nsRefPtr<AppCacheClearDataObserver> obs
242 = new AppCacheClearDataObserver();
243 observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA,
244 /*holdsWeak=*/ false);
245 }
246 }
247

mercurial