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/. */
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"
18 #include "nsNetUtil.h"
19 #include "nsServiceManagerUtils.h"
20 #include "nsThreadUtils.h"
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
,
40 nsICacheEntryOpenCallback
*aCallback
)
42 if (!CacheStorageService::Self())
43 return NS_ERROR_NOT_INITIALIZED
;
46 NS_ENSURE_ARG(aCallback
);
50 nsCOMPtr
<nsIApplicationCache
> appCache
= mAppCache
;
53 rv
= ChooseApplicationCache(aURI
, getter_AddRefs(appCache
));
54 NS_ENSURE_SUCCESS(rv
, rv
);
58 LOG(("AppCacheStorage::AsyncOpenURI entry not found in any appcache, giving up"));
59 aCallback
->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_CACHE_KEY_NOT_FOUND
);
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
75 if (LoadInfo()->IsAnonymous()) {
76 cacheKey
= NS_LITERAL_CSTRING("anon&") + cacheKey
;
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
);
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
,
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
;
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
;
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
);
137 RefPtr
<_OldStorage
> old
= new _OldStorage(
138 LoadInfo(), WriteToDisk(), LookupAppCache(), true, mAppCache
);
139 rv
= old
->AsyncEvictStorage(aCallback
);
140 NS_ENSURE_SUCCESS(rv
, rv
);
146 aCallback
->OnCacheEntryDoomed(NS_OK
);
151 NS_IMETHODIMP
AppCacheStorage::AsyncVisitStorage(nsICacheStorageVisitor
* aVisitor
,
154 if (!CacheStorageService::Self())
155 return NS_ERROR_NOT_INITIALIZED
;
157 LOG(("AppCacheStorage::AsyncVisitStorage [this=%p, cb=%p]", this, aVisitor
));
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
);
173 NS_IMETHODIMP
AppCacheStorage::GetCacheIndexEntryAttrs(nsIURI
*aURI
,
174 const nsACString
&aIdExtension
,
178 return NS_ERROR_NOT_IMPLEMENTED
;
182 } // namespace mozilla