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