Bug 1494162 - Part 45: Lazy load Menu and MenuItem in TabBar. r=pbro
[gecko.git] / netwerk / cache2 / AppCacheStorage.cpp
blob0e56254b2e943f90c9a67b31d91e925d877d9ed6
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
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "CacheLog.h"
6 #include "AppCacheStorage.h"
7 #include "CacheStorageService.h"
9 #include "OldWrappers.h"
11 #include "nsICacheEntryDoomCallback.h"
13 #include "nsCacheService.h"
14 #include "nsIApplicationCache.h"
15 #include "nsIApplicationCacheService.h"
16 #include "nsIURI.h"
17 #include "nsNetCID.h"
18 #include "nsNetUtil.h"
19 #include "nsServiceManagerUtils.h"
20 #include "nsThreadUtils.h"
22 namespace mozilla {
23 namespace net {
25 AppCacheStorage::AppCacheStorage(nsILoadContextInfo* aInfo,
26 nsIApplicationCache* aAppCache)
27 : CacheStorage(aInfo, true /* disk */, false /* lookup app cache */, false /* skip size check */, false /* pin */)
28 , mAppCache(aAppCache)
32 AppCacheStorage::~AppCacheStorage()
34 ProxyReleaseMainThread("AppCacheStorage::mAppCache", mAppCache);
37 NS_IMETHODIMP AppCacheStorage::AsyncOpenURI(nsIURI *aURI,
38 const nsACString & aIdExtension,
39 uint32_t aFlags,
40 nsICacheEntryOpenCallback *aCallback)
42 if (!CacheStorageService::Self())
43 return NS_ERROR_NOT_INITIALIZED;
45 NS_ENSURE_ARG(aURI);
46 NS_ENSURE_ARG(aCallback);
48 nsresult rv;
50 nsCOMPtr<nsIApplicationCache> appCache = mAppCache;
52 if (!appCache) {
53 rv = ChooseApplicationCache(aURI, getter_AddRefs(appCache));
54 NS_ENSURE_SUCCESS(rv, rv);
57 if (!appCache) {
58 LOG(("AppCacheStorage::AsyncOpenURI entry not found in any appcache, giving up"));
59 aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND);
60 return NS_OK;
63 nsCOMPtr<nsIURI> noRefURI;
64 rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI));
65 NS_ENSURE_SUCCESS(rv, rv);
67 nsAutoCString cacheKey;
68 rv = noRefURI->GetAsciiSpec(cacheKey);
69 NS_ENSURE_SUCCESS(rv, rv);
71 // This is the only way how to recognize appcache data by the anonymous
72 // flag. There is no way to switch to e.g. a different session, because
73 // there is just a single session for an appcache version (identified
74 // by the client id).
75 if (LoadInfo()->IsAnonymous()) {
76 cacheKey = NS_LITERAL_CSTRING("anon&") + cacheKey;
79 nsAutoCString scheme;
80 rv = noRefURI->GetScheme(scheme);
81 NS_ENSURE_SUCCESS(rv, rv);
83 RefPtr<_OldCacheLoad> appCacheLoad =
84 new _OldCacheLoad(scheme, cacheKey, aCallback, appCache,
85 LoadInfo(), WriteToDisk(), aFlags);
86 rv = appCacheLoad->Start();
87 NS_ENSURE_SUCCESS(rv, rv);
89 return NS_OK;
92 NS_IMETHODIMP AppCacheStorage::OpenTruncate(nsIURI *aURI, const nsACString & aIdExtension,
93 nsICacheEntry **aCacheEntry)
95 return NS_ERROR_NOT_IMPLEMENTED;
98 NS_IMETHODIMP AppCacheStorage::Exists(nsIURI *aURI, const nsACString & aIdExtension,
99 bool *aResult)
101 *aResult = false;
102 return NS_ERROR_NOT_AVAILABLE;
105 NS_IMETHODIMP AppCacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension,
106 nsICacheEntryDoomCallback* aCallback)
108 if (!CacheStorageService::Self())
109 return NS_ERROR_NOT_INITIALIZED;
111 if (!mAppCache) {
112 return NS_ERROR_NOT_AVAILABLE;
115 RefPtr<_OldStorage> old = new _OldStorage(
116 LoadInfo(), WriteToDisk(), LookupAppCache(), true, mAppCache);
117 return old->AsyncDoomURI(aURI, aIdExtension, aCallback);
120 NS_IMETHODIMP AppCacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback)
122 if (!CacheStorageService::Self())
123 return NS_ERROR_NOT_INITIALIZED;
125 nsresult rv;
127 if (!mAppCache) {
128 // Discard everything under this storage context
129 nsCOMPtr<nsIApplicationCacheService> appCacheService =
130 do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv);
131 NS_ENSURE_SUCCESS(rv, rv);
133 rv = appCacheService->Evict(LoadInfo());
134 NS_ENSURE_SUCCESS(rv, rv);
135 } else {
136 // Discard the group
137 RefPtr<_OldStorage> old = new _OldStorage(
138 LoadInfo(), WriteToDisk(), LookupAppCache(), true, mAppCache);
139 rv = old->AsyncEvictStorage(aCallback);
140 NS_ENSURE_SUCCESS(rv, rv);
142 return NS_OK;
145 if (aCallback)
146 aCallback->OnCacheEntryDoomed(NS_OK);
148 return NS_OK;
151 NS_IMETHODIMP AppCacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor,
152 bool aVisitEntries)
154 if (!CacheStorageService::Self())
155 return NS_ERROR_NOT_INITIALIZED;
157 LOG(("AppCacheStorage::AsyncVisitStorage [this=%p, cb=%p]", this, aVisitor));
159 nsresult rv;
161 nsCOMPtr<nsICacheService> serv =
162 do_GetService(NS_CACHESERVICE_CONTRACTID, &rv);
163 NS_ENSURE_SUCCESS(rv, rv);
165 RefPtr<_OldVisitCallbackWrapper> cb = new _OldVisitCallbackWrapper(
166 "offline", aVisitor, aVisitEntries, LoadInfo());
167 rv = nsCacheService::GlobalInstance()->VisitEntriesInternal(cb);
168 NS_ENSURE_SUCCESS(rv, rv);
170 return NS_OK;
173 NS_IMETHODIMP AppCacheStorage::GetCacheIndexEntryAttrs(nsIURI *aURI,
174 const nsACString &aIdExtension,
175 bool *aHasAltData,
176 uint32_t *aSizeInKB)
178 return NS_ERROR_NOT_IMPLEMENTED;
181 } // namespace net
182 } // namespace mozilla