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 "CacheStorage.h"
7 #include "CacheStorageService.h"
8 #include "CacheEntry.h"
9 #include "CacheObserver.h"
11 #include "nsICacheEntryDoomCallback.h"
14 #include "nsNetUtil.h"
16 namespace mozilla::net
{
18 NS_IMPL_ISUPPORTS(CacheStorage
, nsICacheStorage
)
20 CacheStorage::CacheStorage(nsILoadContextInfo
* aInfo
, bool aAllowDisk
,
21 bool aSkipSizeCheck
, bool aPinning
)
22 : mLoadContextInfo(aInfo
? GetLoadContextInfo(aInfo
) : nullptr),
23 mWriteToDisk(aAllowDisk
),
24 mSkipSizeCheck(aSkipSizeCheck
),
27 NS_IMETHODIMP
CacheStorage::AsyncOpenURI(nsIURI
* aURI
,
28 const nsACString
& aIdExtension
,
30 nsICacheEntryOpenCallback
* aCallback
) {
31 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
33 if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk
&&
34 !(aFlags
& OPEN_INTERCEPTED
)) {
35 aCallback
->OnCacheEntryAvailable(nullptr, false, NS_ERROR_NOT_AVAILABLE
);
39 if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk
&&
40 !(aFlags
& OPEN_INTERCEPTED
)) {
41 aCallback
->OnCacheEntryAvailable(nullptr, false, NS_ERROR_NOT_AVAILABLE
);
46 NS_ENSURE_ARG(aCallback
);
50 nsCOMPtr
<nsIURI
> noRefURI
;
51 rv
= NS_GetURIWithoutRef(aURI
, getter_AddRefs(noRefURI
));
52 NS_ENSURE_SUCCESS(rv
, rv
);
54 nsAutoCString asciiSpec
;
55 rv
= noRefURI
->GetAsciiSpec(asciiSpec
);
56 NS_ENSURE_SUCCESS(rv
, rv
);
58 RefPtr
<CacheEntryHandle
> entry
;
59 rv
= CacheStorageService::Self()->AddStorageEntry(
60 this, asciiSpec
, aIdExtension
, aFlags
, getter_AddRefs(entry
));
62 aCallback
->OnCacheEntryAvailable(nullptr, false, rv
);
66 // May invoke the callback synchronously
67 entry
->Entry()->AsyncOpen(aCallback
, aFlags
);
72 NS_IMETHODIMP
CacheStorage::OpenTruncate(nsIURI
* aURI
,
73 const nsACString
& aIdExtension
,
74 nsICacheEntry
** aCacheEntry
) {
75 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
79 nsCOMPtr
<nsIURI
> noRefURI
;
80 rv
= NS_GetURIWithoutRef(aURI
, getter_AddRefs(noRefURI
));
81 NS_ENSURE_SUCCESS(rv
, rv
);
83 nsAutoCString asciiSpec
;
84 rv
= noRefURI
->GetAsciiSpec(asciiSpec
);
85 NS_ENSURE_SUCCESS(rv
, rv
);
87 RefPtr
<CacheEntryHandle
> handle
;
88 rv
= CacheStorageService::Self()->AddStorageEntry(
89 this, asciiSpec
, aIdExtension
,
90 nsICacheStorage::OPEN_TRUNCATE
, // replace any existing one
91 getter_AddRefs(handle
));
92 NS_ENSURE_SUCCESS(rv
, rv
);
94 // Just open w/o callback, similar to nsICacheEntry.recreate().
95 handle
->Entry()->AsyncOpen(nullptr, OPEN_TRUNCATE
);
97 // Return a write handler, consumer is supposed to fill in the entry.
98 RefPtr
<CacheEntryHandle
> writeHandle
= handle
->Entry()->NewWriteHandle();
99 writeHandle
.forget(aCacheEntry
);
104 NS_IMETHODIMP
CacheStorage::Exists(nsIURI
* aURI
, const nsACString
& aIdExtension
,
107 NS_ENSURE_ARG(aResult
);
109 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
113 nsCOMPtr
<nsIURI
> noRefURI
;
114 rv
= NS_GetURIWithoutRef(aURI
, getter_AddRefs(noRefURI
));
115 NS_ENSURE_SUCCESS(rv
, rv
);
117 nsAutoCString asciiSpec
;
118 rv
= noRefURI
->GetAsciiSpec(asciiSpec
);
119 NS_ENSURE_SUCCESS(rv
, rv
);
121 return CacheStorageService::Self()->CheckStorageEntry(this, asciiSpec
,
122 aIdExtension
, aResult
);
126 CacheStorage::GetCacheIndexEntryAttrs(nsIURI
* aURI
,
127 const nsACString
& aIdExtension
,
128 bool* aHasAltData
, uint32_t* aSizeInKB
) {
130 NS_ENSURE_ARG(aHasAltData
);
131 NS_ENSURE_ARG(aSizeInKB
);
132 if (!CacheStorageService::Self()) {
133 return NS_ERROR_NOT_INITIALIZED
;
138 nsCOMPtr
<nsIURI
> noRefURI
;
139 rv
= NS_GetURIWithoutRef(aURI
, getter_AddRefs(noRefURI
));
140 NS_ENSURE_SUCCESS(rv
, rv
);
142 nsAutoCString asciiSpec
;
143 rv
= noRefURI
->GetAsciiSpec(asciiSpec
);
144 NS_ENSURE_SUCCESS(rv
, rv
);
146 return CacheStorageService::Self()->GetCacheIndexEntryAttrs(
147 this, asciiSpec
, aIdExtension
, aHasAltData
, aSizeInKB
);
150 NS_IMETHODIMP
CacheStorage::AsyncDoomURI(nsIURI
* aURI
,
151 const nsACString
& aIdExtension
,
152 nsICacheEntryDoomCallback
* aCallback
) {
153 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
157 nsCOMPtr
<nsIURI
> noRefURI
;
158 rv
= NS_GetURIWithoutRef(aURI
, getter_AddRefs(noRefURI
));
159 NS_ENSURE_SUCCESS(rv
, rv
);
161 nsAutoCString asciiSpec
;
162 rv
= noRefURI
->GetAsciiSpec(asciiSpec
);
163 NS_ENSURE_SUCCESS(rv
, rv
);
165 rv
= CacheStorageService::Self()->DoomStorageEntry(this, asciiSpec
,
166 aIdExtension
, aCallback
);
167 NS_ENSURE_SUCCESS(rv
, rv
);
172 NS_IMETHODIMP
CacheStorage::AsyncEvictStorage(
173 nsICacheEntryDoomCallback
* aCallback
) {
174 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
177 CacheStorageService::Self()->DoomStorageEntries(this, aCallback
);
178 NS_ENSURE_SUCCESS(rv
, rv
);
183 NS_IMETHODIMP
CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor
* aVisitor
,
184 bool aVisitEntries
) {
185 LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this,
186 aVisitor
, (bool)mWriteToDisk
));
187 if (!CacheStorageService::Self()) return NS_ERROR_NOT_INITIALIZED
;
189 nsresult rv
= CacheStorageService::Self()->WalkStorageEntries(
190 this, aVisitEntries
, aVisitor
);
191 NS_ENSURE_SUCCESS(rv
, rv
);
196 } // namespace mozilla::net