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"
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()
35 nsApplicationCacheService::BuildGroupID(nsIURI
*aManifestURL
,
36 nsILoadContextInfo
*aLoadContextInfo
,
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
);
57 nsApplicationCacheService::BuildGroupIDForApp(nsIURI
*aManifestURL
,
62 nsresult rv
= nsOfflineCacheDevice::BuildApplicationCacheGroupID(
63 aManifestURL
, aAppId
, aIsInBrowser
, _result
);
64 NS_ENSURE_SUCCESS(rv
, rv
);
70 nsApplicationCacheService::CreateApplicationCache(const nsACString
&group
,
71 nsIApplicationCache
**out
)
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
);
83 nsApplicationCacheService::CreateCustomApplicationCache(const nsACString
& group
,
86 nsIApplicationCache
**out
)
89 return NS_ERROR_UNEXPECTED
;
91 nsRefPtr
<nsOfflineCacheDevice
> device
;
92 nsresult rv
= mCacheService
->GetCustomOfflineDevice(profileDir
,
94 getter_AddRefs(device
));
95 NS_ENSURE_SUCCESS(rv
, rv
);
96 return device
->CreateApplicationCache(group
, out
);
100 nsApplicationCacheService::GetApplicationCache(const nsACString
&clientID
,
101 nsIApplicationCache
**out
)
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
);
113 nsApplicationCacheService::GetActiveCache(const nsACString
&group
,
114 nsIApplicationCache
**out
)
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
);
126 nsApplicationCacheService::DeactivateGroup(const nsACString
&group
)
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
);
138 nsApplicationCacheService::ChooseApplicationCache(const nsACString
&key
,
139 nsILoadContextInfo
*aLoadContextInfo
,
140 nsIApplicationCache
**out
)
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
);
153 nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache
* cache
,
154 const nsACString
&key
)
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
);
166 nsApplicationCacheService::DiscardByAppId(int32_t appID
, bool isInBrowser
)
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
);
178 nsApplicationCacheService::GetGroups(uint32_t *count
,
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
);
191 nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count
,
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 //-----------------------------------------------------------------------------
210 class AppCacheClearDataObserver MOZ_FINAL
: public nsIObserver
{
214 // nsIObserver implementation.
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
,
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
);
234 ~AppCacheClearDataObserver() {}
237 NS_IMPL_ISUPPORTS(AppCacheClearDataObserver
, nsIObserver
)
239 } // anonymous namespace
241 // Instantiates and registers AppCacheClearDataObserver for notifications
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);