/src/mozilla-central/netwerk/cache2/CacheStorage.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
4 | | |
5 | | #include "CacheLog.h" |
6 | | #include "CacheStorage.h" |
7 | | #include "CacheStorageService.h" |
8 | | #include "CacheEntry.h" |
9 | | #include "CacheObserver.h" |
10 | | |
11 | | #include "OldWrappers.h" |
12 | | |
13 | | #include "nsICacheEntryDoomCallback.h" |
14 | | |
15 | | #include "nsIApplicationCache.h" |
16 | | #include "nsIApplicationCacheService.h" |
17 | | #include "nsIURI.h" |
18 | | #include "nsNetCID.h" |
19 | | #include "nsNetUtil.h" |
20 | | #include "nsServiceManagerUtils.h" |
21 | | |
22 | | namespace mozilla { |
23 | | namespace net { |
24 | | |
25 | | NS_IMPL_ISUPPORTS(CacheStorage, nsICacheStorage) |
26 | | |
27 | | CacheStorage::CacheStorage(nsILoadContextInfo* aInfo, |
28 | | bool aAllowDisk, |
29 | | bool aLookupAppCache, |
30 | | bool aSkipSizeCheck, |
31 | | bool aPinning) |
32 | | : mLoadContextInfo(GetLoadContextInfo(aInfo)) |
33 | | , mWriteToDisk(aAllowDisk) |
34 | | , mLookupAppCache(aLookupAppCache) |
35 | | , mSkipSizeCheck(aSkipSizeCheck) |
36 | | , mPinning(aPinning) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | NS_IMETHODIMP CacheStorage::AsyncOpenURI(nsIURI *aURI, |
41 | | const nsACString & aIdExtension, |
42 | | uint32_t aFlags, |
43 | | nsICacheEntryOpenCallback *aCallback) |
44 | 0 | { |
45 | 0 | if (!CacheStorageService::Self()) |
46 | 0 | return NS_ERROR_NOT_INITIALIZED; |
47 | 0 | |
48 | 0 | if (MOZ_UNLIKELY(!CacheObserver::UseDiskCache()) && mWriteToDisk && |
49 | 0 | !(aFlags & OPEN_INTERCEPTED)) { |
50 | 0 | aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE); |
51 | 0 | return NS_OK; |
52 | 0 | } |
53 | 0 | |
54 | 0 | if (MOZ_UNLIKELY(!CacheObserver::UseMemoryCache()) && !mWriteToDisk && |
55 | 0 | !(aFlags & OPEN_INTERCEPTED)) { |
56 | 0 | aCallback->OnCacheEntryAvailable(nullptr, false, nullptr, NS_ERROR_NOT_AVAILABLE); |
57 | 0 | return NS_OK; |
58 | 0 | } |
59 | 0 | |
60 | 0 | NS_ENSURE_ARG(aURI); |
61 | 0 | NS_ENSURE_ARG(aCallback); |
62 | 0 |
|
63 | 0 | nsresult rv; |
64 | 0 |
|
65 | 0 | bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE; |
66 | 0 |
|
67 | 0 | nsCOMPtr<nsIURI> noRefURI; |
68 | 0 | rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI)); |
69 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
70 | 0 |
|
71 | 0 | nsAutoCString asciiSpec; |
72 | 0 | rv = noRefURI->GetAsciiSpec(asciiSpec); |
73 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
74 | 0 |
|
75 | 0 | nsCOMPtr<nsIApplicationCache> appCache; |
76 | 0 | if (LookupAppCache()) { |
77 | 0 | rv = ChooseApplicationCache(noRefURI, getter_AddRefs(appCache)); |
78 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
79 | 0 |
|
80 | 0 | if (appCache) { |
81 | 0 | // From a chosen appcache open only as readonly |
82 | 0 | aFlags &= ~nsICacheStorage::OPEN_TRUNCATE; |
83 | 0 | } |
84 | 0 | } |
85 | 0 |
|
86 | 0 | if (appCache) { |
87 | 0 | nsAutoCString scheme; |
88 | 0 | rv = noRefURI->GetScheme(scheme); |
89 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
90 | 0 |
|
91 | 0 | RefPtr<_OldCacheLoad> appCacheLoad = |
92 | 0 | new _OldCacheLoad(scheme, asciiSpec, aCallback, appCache, |
93 | 0 | LoadInfo(), WriteToDisk(), aFlags); |
94 | 0 | rv = appCacheLoad->Start(); |
95 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
96 | 0 |
|
97 | 0 | LOG(("CacheStorage::AsyncOpenURI loading from appcache")); |
98 | 0 | return NS_OK; |
99 | 0 | } |
100 | 0 | |
101 | 0 | RefPtr<CacheEntryHandle> entry; |
102 | 0 | rv = CacheStorageService::Self()->AddStorageEntry( |
103 | 0 | this, asciiSpec, aIdExtension, |
104 | 0 | truncate, // replace any existing one? |
105 | 0 | getter_AddRefs(entry)); |
106 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
107 | 0 |
|
108 | 0 | // May invoke the callback synchronously |
109 | 0 | entry->Entry()->AsyncOpen(aCallback, aFlags); |
110 | 0 |
|
111 | 0 | return NS_OK; |
112 | 0 | } |
113 | | |
114 | | |
115 | | NS_IMETHODIMP CacheStorage::OpenTruncate(nsIURI *aURI, const nsACString & aIdExtension, |
116 | | nsICacheEntry **aCacheEntry) |
117 | 0 | { |
118 | 0 | if (!CacheStorageService::Self()) |
119 | 0 | return NS_ERROR_NOT_INITIALIZED; |
120 | 0 | |
121 | 0 | nsresult rv; |
122 | 0 |
|
123 | 0 | nsCOMPtr<nsIURI> noRefURI; |
124 | 0 | rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI)); |
125 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
126 | 0 |
|
127 | 0 | nsAutoCString asciiSpec; |
128 | 0 | rv = noRefURI->GetAsciiSpec(asciiSpec); |
129 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
130 | 0 |
|
131 | 0 | RefPtr<CacheEntryHandle> handle; |
132 | 0 | rv = CacheStorageService::Self()->AddStorageEntry( |
133 | 0 | this, asciiSpec, aIdExtension, |
134 | 0 | true, // replace any existing one |
135 | 0 | getter_AddRefs(handle)); |
136 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
137 | 0 |
|
138 | 0 | // Just open w/o callback, similar to nsICacheEntry.recreate(). |
139 | 0 | handle->Entry()->AsyncOpen(nullptr, OPEN_TRUNCATE); |
140 | 0 |
|
141 | 0 | // Return a write handler, consumer is supposed to fill in the entry. |
142 | 0 | RefPtr<CacheEntryHandle> writeHandle = handle->Entry()->NewWriteHandle(); |
143 | 0 | writeHandle.forget(aCacheEntry); |
144 | 0 |
|
145 | 0 | return NS_OK; |
146 | 0 | } |
147 | | |
148 | | NS_IMETHODIMP CacheStorage::Exists(nsIURI *aURI, const nsACString & aIdExtension, |
149 | | bool *aResult) |
150 | 0 | { |
151 | 0 | NS_ENSURE_ARG(aURI); |
152 | 0 | NS_ENSURE_ARG(aResult); |
153 | 0 |
|
154 | 0 | if (!CacheStorageService::Self()) |
155 | 0 | return NS_ERROR_NOT_INITIALIZED; |
156 | 0 | |
157 | 0 | nsresult rv; |
158 | 0 |
|
159 | 0 | nsCOMPtr<nsIURI> noRefURI; |
160 | 0 | rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI)); |
161 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
162 | 0 |
|
163 | 0 | nsAutoCString asciiSpec; |
164 | 0 | rv = noRefURI->GetAsciiSpec(asciiSpec); |
165 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
166 | 0 |
|
167 | 0 | return CacheStorageService::Self()->CheckStorageEntry( |
168 | 0 | this, asciiSpec, aIdExtension, aResult); |
169 | 0 | } |
170 | | |
171 | | NS_IMETHODIMP |
172 | | CacheStorage::GetCacheIndexEntryAttrs(nsIURI *aURI, |
173 | | const nsACString &aIdExtension, |
174 | | bool *aHasAltData, |
175 | | uint32_t *aSizeInKB) |
176 | 0 | { |
177 | 0 | NS_ENSURE_ARG(aURI); |
178 | 0 | NS_ENSURE_ARG(aHasAltData); |
179 | 0 | NS_ENSURE_ARG(aSizeInKB); |
180 | 0 | if (!CacheStorageService::Self()) { |
181 | 0 | return NS_ERROR_NOT_INITIALIZED; |
182 | 0 | } |
183 | 0 | |
184 | 0 | nsresult rv; |
185 | 0 |
|
186 | 0 | nsCOMPtr<nsIURI> noRefURI; |
187 | 0 | rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI)); |
188 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
189 | 0 |
|
190 | 0 | nsAutoCString asciiSpec; |
191 | 0 | rv = noRefURI->GetAsciiSpec(asciiSpec); |
192 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
193 | 0 |
|
194 | 0 | return CacheStorageService::Self()->GetCacheIndexEntryAttrs( |
195 | 0 | this, asciiSpec, aIdExtension, aHasAltData, aSizeInKB); |
196 | 0 | } |
197 | | |
198 | | NS_IMETHODIMP CacheStorage::AsyncDoomURI(nsIURI *aURI, const nsACString & aIdExtension, |
199 | | nsICacheEntryDoomCallback* aCallback) |
200 | 0 | { |
201 | 0 | if (!CacheStorageService::Self()) |
202 | 0 | return NS_ERROR_NOT_INITIALIZED; |
203 | 0 | |
204 | 0 | nsresult rv; |
205 | 0 |
|
206 | 0 | nsCOMPtr<nsIURI> noRefURI; |
207 | 0 | rv = NS_GetURIWithoutRef(aURI, getter_AddRefs(noRefURI)); |
208 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
209 | 0 |
|
210 | 0 | nsAutoCString asciiSpec; |
211 | 0 | rv = noRefURI->GetAsciiSpec(asciiSpec); |
212 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
213 | 0 |
|
214 | 0 | rv = CacheStorageService::Self()->DoomStorageEntry( |
215 | 0 | this, asciiSpec, aIdExtension, aCallback); |
216 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
217 | 0 |
|
218 | 0 | return NS_OK; |
219 | 0 | } |
220 | | |
221 | | NS_IMETHODIMP CacheStorage::AsyncEvictStorage(nsICacheEntryDoomCallback* aCallback) |
222 | 0 | { |
223 | 0 | if (!CacheStorageService::Self()) |
224 | 0 | return NS_ERROR_NOT_INITIALIZED; |
225 | 0 | |
226 | 0 | nsresult rv = CacheStorageService::Self()->DoomStorageEntries( |
227 | 0 | this, aCallback); |
228 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
229 | 0 |
|
230 | 0 | return NS_OK; |
231 | 0 | } |
232 | | |
233 | | NS_IMETHODIMP CacheStorage::AsyncVisitStorage(nsICacheStorageVisitor* aVisitor, |
234 | | bool aVisitEntries) |
235 | 0 | { |
236 | 0 | LOG(("CacheStorage::AsyncVisitStorage [this=%p, cb=%p, disk=%d]", this, aVisitor, (bool)mWriteToDisk)); |
237 | 0 | if (!CacheStorageService::Self()) |
238 | 0 | return NS_ERROR_NOT_INITIALIZED; |
239 | 0 | |
240 | 0 | nsresult rv = CacheStorageService::Self()->WalkStorageEntries( |
241 | 0 | this, aVisitEntries, aVisitor); |
242 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
243 | 0 |
|
244 | 0 | return NS_OK; |
245 | 0 | } |
246 | | |
247 | | // Internal |
248 | | |
249 | | nsresult CacheStorage::ChooseApplicationCache(nsIURI* aURI, |
250 | | nsIApplicationCache** aCache) |
251 | 0 | { |
252 | 0 | nsresult rv; |
253 | 0 |
|
254 | 0 | nsCOMPtr<nsIApplicationCacheService> appCacheService = |
255 | 0 | do_GetService(NS_APPLICATIONCACHESERVICE_CONTRACTID, &rv); |
256 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
257 | 0 |
|
258 | 0 | nsAutoCString cacheKey; |
259 | 0 | rv = aURI->GetAsciiSpec(cacheKey); |
260 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
261 | 0 |
|
262 | 0 | rv = appCacheService->ChooseApplicationCache(cacheKey, LoadInfo(), aCache); |
263 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
264 | 0 |
|
265 | 0 | return NS_OK; |
266 | 0 | } |
267 | | |
268 | | } // namespace net |
269 | | } // namespace mozilla |