Bumping manifests a=b2g-bump
[gecko.git] / netwerk / cache / nsApplicationCacheService.cpp
blob600c9c4d6e3222d2d7f46758ae1b6a0a120901a2
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/. */
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"
14 using namespace mozilla;
16 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
18 //-----------------------------------------------------------------------------
19 // nsApplicationCacheService
20 //-----------------------------------------------------------------------------
22 NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService)
24 nsApplicationCacheService::nsApplicationCacheService()
26 nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID);
27 mCacheService = nsCacheService::GlobalInstance();
30 nsApplicationCacheService::~nsApplicationCacheService()
34 NS_IMETHODIMP
35 nsApplicationCacheService::BuildGroupID(nsIURI *aManifestURL,
36 nsILoadContextInfo *aLoadContextInfo,
37 nsACString &_result)
39 nsresult rv;
41 uint32_t appId = NECKO_NO_APP_ID;
42 bool isInBrowserElement = false;
44 if (aLoadContextInfo) {
45 appId = aLoadContextInfo->AppId();
46 isInBrowserElement = aLoadContextInfo->IsInBrowserElement();
49 rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
50 aManifestURL, appId, isInBrowserElement, _result);
51 NS_ENSURE_SUCCESS(rv, rv);
53 return NS_OK;
56 NS_IMETHODIMP
57 nsApplicationCacheService::BuildGroupIDForApp(nsIURI *aManifestURL,
58 uint32_t aAppId,
59 bool aIsInBrowser,
60 nsACString &_result)
62 nsresult rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID(
63 aManifestURL, aAppId, aIsInBrowser, _result);
64 NS_ENSURE_SUCCESS(rv, rv);
66 return NS_OK;
69 NS_IMETHODIMP
70 nsApplicationCacheService::CreateApplicationCache(const nsACString &group,
71 nsIApplicationCache **out)
73 if (!mCacheService)
74 return NS_ERROR_UNEXPECTED;
76 nsRefPtr<nsOfflineCacheDevice> device;
77 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
78 NS_ENSURE_SUCCESS(rv, rv);
79 return device->CreateApplicationCache(group, out);
82 NS_IMETHODIMP
83 nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group,
84 nsIFile *profileDir,
85 int32_t quota,
86 nsIApplicationCache **out)
88 if (!mCacheService)
89 return NS_ERROR_UNEXPECTED;
91 nsRefPtr<nsOfflineCacheDevice> device;
92 nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir,
93 quota,
94 getter_AddRefs(device));
95 NS_ENSURE_SUCCESS(rv, rv);
96 return device->CreateApplicationCache(group, out);
99 NS_IMETHODIMP
100 nsApplicationCacheService::GetApplicationCache(const nsACString &clientID,
101 nsIApplicationCache **out)
103 if (!mCacheService)
104 return NS_ERROR_UNEXPECTED;
106 nsRefPtr<nsOfflineCacheDevice> device;
107 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
108 NS_ENSURE_SUCCESS(rv, rv);
109 return device->GetApplicationCache(clientID, out);
112 NS_IMETHODIMP
113 nsApplicationCacheService::GetActiveCache(const nsACString &group,
114 nsIApplicationCache **out)
116 if (!mCacheService)
117 return NS_ERROR_UNEXPECTED;
119 nsRefPtr<nsOfflineCacheDevice> device;
120 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
121 NS_ENSURE_SUCCESS(rv, rv);
122 return device->GetActiveCache(group, out);
125 NS_IMETHODIMP
126 nsApplicationCacheService::DeactivateGroup(const nsACString &group)
128 if (!mCacheService)
129 return NS_ERROR_UNEXPECTED;
131 nsRefPtr<nsOfflineCacheDevice> device;
132 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
133 NS_ENSURE_SUCCESS(rv, rv);
134 return device->DeactivateGroup(group);
137 NS_IMETHODIMP
138 nsApplicationCacheService::ChooseApplicationCache(const nsACString &key,
139 nsILoadContextInfo *aLoadContextInfo,
140 nsIApplicationCache **out)
142 if (!mCacheService)
143 return NS_ERROR_UNEXPECTED;
145 nsRefPtr<nsOfflineCacheDevice> device;
146 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
147 NS_ENSURE_SUCCESS(rv, rv);
149 return device->ChooseApplicationCache(key, aLoadContextInfo, out);
152 NS_IMETHODIMP
153 nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache,
154 const nsACString &key)
156 if (!mCacheService)
157 return NS_ERROR_UNEXPECTED;
159 nsRefPtr<nsOfflineCacheDevice> device;
160 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
161 NS_ENSURE_SUCCESS(rv, rv);
162 return device->CacheOpportunistically(cache, key);
165 NS_IMETHODIMP
166 nsApplicationCacheService::DiscardByAppId(int32_t appID, bool isInBrowser)
168 if (!mCacheService)
169 return NS_ERROR_UNEXPECTED;
171 nsRefPtr<nsOfflineCacheDevice> device;
172 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
173 NS_ENSURE_SUCCESS(rv, rv);
174 return device->DiscardByAppId(appID, isInBrowser);
177 NS_IMETHODIMP
178 nsApplicationCacheService::GetGroups(uint32_t *count,
179 char ***keys)
181 if (!mCacheService)
182 return NS_ERROR_UNEXPECTED;
184 nsRefPtr<nsOfflineCacheDevice> device;
185 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
186 NS_ENSURE_SUCCESS(rv, rv);
187 return device->GetGroups(count, keys);
190 NS_IMETHODIMP
191 nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count,
192 char ***keys)
194 if (!mCacheService)
195 return NS_ERROR_UNEXPECTED;
197 nsRefPtr<nsOfflineCacheDevice> device;
198 nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device));
199 NS_ENSURE_SUCCESS(rv, rv);
200 return device->GetGroupsTimeOrdered(count, keys);
203 //-----------------------------------------------------------------------------
204 // AppCacheClearDataObserver: handles clearing appcache data for app uninstall
205 // and clearing user data events.
206 //-----------------------------------------------------------------------------
208 namespace {
210 class AppCacheClearDataObserver MOZ_FINAL : public nsIObserver {
211 public:
212 NS_DECL_ISUPPORTS
214 // nsIObserver implementation.
215 NS_IMETHODIMP
216 Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData) MOZ_OVERRIDE
218 MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA));
220 uint32_t appId = NECKO_UNKNOWN_APP_ID;
221 bool browserOnly = false;
222 nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId,
223 &browserOnly);
224 NS_ENSURE_SUCCESS(rv, rv);
226 nsCOMPtr<nsIApplicationCacheService> cacheService =
227 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
228 NS_ENSURE_SUCCESS(rv, rv);
230 return cacheService->DiscardByAppId(appId, browserOnly);
233 private:
234 ~AppCacheClearDataObserver() {}
237 NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver)
239 } // anonymous namespace
241 // Instantiates and registers AppCacheClearDataObserver for notifications
242 void
243 nsApplicationCacheService::AppClearDataObserverInit()
245 nsCOMPtr<nsIObserverService> observerService =
246 do_GetService("@mozilla.org/observer-service;1");
247 if (observerService) {
248 nsRefPtr<AppCacheClearDataObserver> obs
249 = new AppCacheClearDataObserver();
250 observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA,
251 /*holdsWeak=*/ false);