1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache/nsApplicationCacheService.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,247 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsDiskCache.h" 1.9 +#include "nsDiskCacheDeviceSQL.h" 1.10 +#include "nsCacheService.h" 1.11 +#include "nsApplicationCacheService.h" 1.12 +#include "nsCRT.h" 1.13 +#include "nsNetUtil.h" 1.14 +#include "nsIObserverService.h" 1.15 +#include "nsILoadContextInfo.h" 1.16 + 1.17 +using namespace mozilla; 1.18 + 1.19 +static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID); 1.20 + 1.21 +//----------------------------------------------------------------------------- 1.22 +// nsApplicationCacheService 1.23 +//----------------------------------------------------------------------------- 1.24 + 1.25 +NS_IMPL_ISUPPORTS(nsApplicationCacheService, nsIApplicationCacheService) 1.26 + 1.27 +nsApplicationCacheService::nsApplicationCacheService() 1.28 +{ 1.29 + nsCOMPtr<nsICacheService> serv = do_GetService(kCacheServiceCID); 1.30 + mCacheService = nsCacheService::GlobalInstance(); 1.31 +} 1.32 + 1.33 +NS_IMETHODIMP 1.34 +nsApplicationCacheService::BuildGroupID(nsIURI *aManifestURL, 1.35 + nsILoadContextInfo *aLoadContextInfo, 1.36 + nsACString &_result) 1.37 +{ 1.38 + nsresult rv; 1.39 + 1.40 + uint32_t appId = NECKO_NO_APP_ID; 1.41 + bool isInBrowserElement = false; 1.42 + 1.43 + if (aLoadContextInfo) { 1.44 + appId = aLoadContextInfo->AppId(); 1.45 + isInBrowserElement = aLoadContextInfo->IsInBrowserElement(); 1.46 + } 1.47 + 1.48 + rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID( 1.49 + aManifestURL, appId, isInBrowserElement, _result); 1.50 + NS_ENSURE_SUCCESS(rv, rv); 1.51 + 1.52 + return NS_OK; 1.53 +} 1.54 + 1.55 +NS_IMETHODIMP 1.56 +nsApplicationCacheService::BuildGroupIDForApp(nsIURI *aManifestURL, 1.57 + uint32_t aAppId, 1.58 + bool aIsInBrowser, 1.59 + nsACString &_result) 1.60 +{ 1.61 + nsresult rv = nsOfflineCacheDevice::BuildApplicationCacheGroupID( 1.62 + aManifestURL, aAppId, aIsInBrowser, _result); 1.63 + NS_ENSURE_SUCCESS(rv, rv); 1.64 + 1.65 + return NS_OK; 1.66 +} 1.67 + 1.68 +NS_IMETHODIMP 1.69 +nsApplicationCacheService::CreateApplicationCache(const nsACString &group, 1.70 + nsIApplicationCache **out) 1.71 +{ 1.72 + if (!mCacheService) 1.73 + return NS_ERROR_UNEXPECTED; 1.74 + 1.75 + nsRefPtr<nsOfflineCacheDevice> device; 1.76 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.77 + NS_ENSURE_SUCCESS(rv, rv); 1.78 + return device->CreateApplicationCache(group, out); 1.79 +} 1.80 + 1.81 +NS_IMETHODIMP 1.82 +nsApplicationCacheService::CreateCustomApplicationCache(const nsACString & group, 1.83 + nsIFile *profileDir, 1.84 + int32_t quota, 1.85 + nsIApplicationCache **out) 1.86 +{ 1.87 + if (!mCacheService) 1.88 + return NS_ERROR_UNEXPECTED; 1.89 + 1.90 + nsRefPtr<nsOfflineCacheDevice> device; 1.91 + nsresult rv = mCacheService->GetCustomOfflineDevice(profileDir, 1.92 + quota, 1.93 + getter_AddRefs(device)); 1.94 + NS_ENSURE_SUCCESS(rv, rv); 1.95 + return device->CreateApplicationCache(group, out); 1.96 +} 1.97 + 1.98 +NS_IMETHODIMP 1.99 +nsApplicationCacheService::GetApplicationCache(const nsACString &clientID, 1.100 + nsIApplicationCache **out) 1.101 +{ 1.102 + if (!mCacheService) 1.103 + return NS_ERROR_UNEXPECTED; 1.104 + 1.105 + nsRefPtr<nsOfflineCacheDevice> device; 1.106 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.107 + NS_ENSURE_SUCCESS(rv, rv); 1.108 + return device->GetApplicationCache(clientID, out); 1.109 +} 1.110 + 1.111 +NS_IMETHODIMP 1.112 +nsApplicationCacheService::GetActiveCache(const nsACString &group, 1.113 + nsIApplicationCache **out) 1.114 +{ 1.115 + if (!mCacheService) 1.116 + return NS_ERROR_UNEXPECTED; 1.117 + 1.118 + nsRefPtr<nsOfflineCacheDevice> device; 1.119 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.120 + NS_ENSURE_SUCCESS(rv, rv); 1.121 + return device->GetActiveCache(group, out); 1.122 +} 1.123 + 1.124 +NS_IMETHODIMP 1.125 +nsApplicationCacheService::DeactivateGroup(const nsACString &group) 1.126 +{ 1.127 + if (!mCacheService) 1.128 + return NS_ERROR_UNEXPECTED; 1.129 + 1.130 + nsRefPtr<nsOfflineCacheDevice> device; 1.131 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.132 + NS_ENSURE_SUCCESS(rv, rv); 1.133 + return device->DeactivateGroup(group); 1.134 +} 1.135 + 1.136 +NS_IMETHODIMP 1.137 +nsApplicationCacheService::ChooseApplicationCache(const nsACString &key, 1.138 + nsILoadContextInfo *aLoadContextInfo, 1.139 + nsIApplicationCache **out) 1.140 +{ 1.141 + if (!mCacheService) 1.142 + return NS_ERROR_UNEXPECTED; 1.143 + 1.144 + nsRefPtr<nsOfflineCacheDevice> device; 1.145 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.146 + NS_ENSURE_SUCCESS(rv, rv); 1.147 + 1.148 + return device->ChooseApplicationCache(key, aLoadContextInfo, out); 1.149 +} 1.150 + 1.151 +NS_IMETHODIMP 1.152 +nsApplicationCacheService::CacheOpportunistically(nsIApplicationCache* cache, 1.153 + const nsACString &key) 1.154 +{ 1.155 + if (!mCacheService) 1.156 + return NS_ERROR_UNEXPECTED; 1.157 + 1.158 + nsRefPtr<nsOfflineCacheDevice> device; 1.159 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.160 + NS_ENSURE_SUCCESS(rv, rv); 1.161 + return device->CacheOpportunistically(cache, key); 1.162 +} 1.163 + 1.164 +NS_IMETHODIMP 1.165 +nsApplicationCacheService::DiscardByAppId(int32_t appID, bool isInBrowser) 1.166 +{ 1.167 + if (!mCacheService) 1.168 + return NS_ERROR_UNEXPECTED; 1.169 + 1.170 + nsRefPtr<nsOfflineCacheDevice> device; 1.171 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.172 + NS_ENSURE_SUCCESS(rv, rv); 1.173 + return device->DiscardByAppId(appID, isInBrowser); 1.174 +} 1.175 + 1.176 +NS_IMETHODIMP 1.177 +nsApplicationCacheService::GetGroups(uint32_t *count, 1.178 + char ***keys) 1.179 +{ 1.180 + if (!mCacheService) 1.181 + return NS_ERROR_UNEXPECTED; 1.182 + 1.183 + nsRefPtr<nsOfflineCacheDevice> device; 1.184 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.185 + NS_ENSURE_SUCCESS(rv, rv); 1.186 + return device->GetGroups(count, keys); 1.187 +} 1.188 + 1.189 +NS_IMETHODIMP 1.190 +nsApplicationCacheService::GetGroupsTimeOrdered(uint32_t *count, 1.191 + char ***keys) 1.192 +{ 1.193 + if (!mCacheService) 1.194 + return NS_ERROR_UNEXPECTED; 1.195 + 1.196 + nsRefPtr<nsOfflineCacheDevice> device; 1.197 + nsresult rv = mCacheService->GetOfflineDevice(getter_AddRefs(device)); 1.198 + NS_ENSURE_SUCCESS(rv, rv); 1.199 + return device->GetGroupsTimeOrdered(count, keys); 1.200 +} 1.201 + 1.202 +//----------------------------------------------------------------------------- 1.203 +// AppCacheClearDataObserver: handles clearing appcache data for app uninstall 1.204 +// and clearing user data events. 1.205 +//----------------------------------------------------------------------------- 1.206 + 1.207 +namespace { 1.208 + 1.209 +class AppCacheClearDataObserver MOZ_FINAL : public nsIObserver { 1.210 +public: 1.211 + NS_DECL_ISUPPORTS 1.212 + 1.213 + // nsIObserver implementation. 1.214 + NS_IMETHODIMP 1.215 + Observe(nsISupports *aSubject, const char *aTopic, const char16_t *aData) 1.216 + { 1.217 + MOZ_ASSERT(!nsCRT::strcmp(aTopic, TOPIC_WEB_APP_CLEAR_DATA)); 1.218 + 1.219 + uint32_t appId = NECKO_UNKNOWN_APP_ID; 1.220 + bool browserOnly = false; 1.221 + nsresult rv = NS_GetAppInfoFromClearDataNotification(aSubject, &appId, 1.222 + &browserOnly); 1.223 + NS_ENSURE_SUCCESS(rv, rv); 1.224 + 1.225 + nsCOMPtr<nsIApplicationCacheService> cacheService = 1.226 + do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); 1.227 + NS_ENSURE_SUCCESS(rv, rv); 1.228 + 1.229 + return cacheService->DiscardByAppId(appId, browserOnly); 1.230 + } 1.231 +}; 1.232 + 1.233 +NS_IMPL_ISUPPORTS(AppCacheClearDataObserver, nsIObserver) 1.234 + 1.235 +} // anonymous namespace 1.236 + 1.237 +// Instantiates and registers AppCacheClearDataObserver for notifications 1.238 +void 1.239 +nsApplicationCacheService::AppClearDataObserverInit() 1.240 +{ 1.241 + nsCOMPtr<nsIObserverService> observerService = 1.242 + do_GetService("@mozilla.org/observer-service;1"); 1.243 + if (observerService) { 1.244 + nsRefPtr<AppCacheClearDataObserver> obs 1.245 + = new AppCacheClearDataObserver(); 1.246 + observerService->AddObserver(obs, TOPIC_WEB_APP_CLEAR_DATA, 1.247 + /*holdsWeak=*/ false); 1.248 + } 1.249 +} 1.250 +