OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "modules/cachestorage/CacheStorage.h" | 6 #include "modules/cachestorage/CacheStorage.h" |
7 | 7 |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
12 #include "modules/cachestorage/CacheStorageError.h" | 12 #include "modules/cachestorage/CacheStorageError.h" |
13 #include "modules/fetch/Request.h" | 13 #include "modules/fetch/Request.h" |
14 #include "modules/fetch/Response.h" | 14 #include "modules/fetch/Response.h" |
15 #include "public/platform/WebServiceWorkerCacheError.h" | 15 #include "public/platform/WebServiceWorkerCacheError.h" |
16 #include "public/platform/WebServiceWorkerCacheStorage.h" | 16 #include "public/platform/WebServiceWorkerCacheStorage.h" |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 DOMException* createNoImplementationException() | 22 DOMException* createNoImplementationException() |
23 { | 23 { |
24 return DOMException::create(NotSupportedError, "No CacheStorage implementati
on provided."); | 24 return DOMException::create(NotSupportedError, "No CacheStorage implementati
on provided."); |
25 } | 25 } |
26 | 26 |
| 27 bool commonChecks(ScriptState* scriptState, ExceptionState& exceptionState) |
| 28 { |
| 29 ExecutionContext* executionContext = scriptState->executionContext(); |
| 30 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
| 31 if (!executionContext) |
| 32 return false; |
| 33 |
| 34 String errorMessage; |
| 35 if (!executionContext->isPrivilegedContext(errorMessage)) { |
| 36 exceptionState.throwSecurityError(errorMessage); |
| 37 return false; |
| 38 } |
| 39 return true; |
| 40 } |
| 41 |
27 } | 42 } |
28 | 43 |
29 // FIXME: Consider using CallbackPromiseAdapter. | 44 // FIXME: Consider using CallbackPromiseAdapter. |
30 class CacheStorage::Callbacks final : public WebServiceWorkerCacheStorage::Cache
StorageCallbacks { | 45 class CacheStorage::Callbacks final : public WebServiceWorkerCacheStorage::Cache
StorageCallbacks { |
31 WTF_MAKE_NONCOPYABLE(Callbacks); | 46 WTF_MAKE_NONCOPYABLE(Callbacks); |
32 public: | 47 public: |
33 explicit Callbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) | 48 explicit Callbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver) |
34 : m_resolver(resolver) { } | 49 : m_resolver(resolver) { } |
35 ~Callbacks() override { } | 50 ~Callbacks() override { } |
36 | 51 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 { | 191 { |
177 OwnPtr<WebServiceWorkerCacheError> reason = adoptPtr(rawReason); | 192 OwnPtr<WebServiceWorkerCacheError> reason = adoptPtr(rawReason); |
178 m_resolver->reject(CacheStorageError::createException(*reason)); | 193 m_resolver->reject(CacheStorageError::createException(*reason)); |
179 m_resolver.clear(); | 194 m_resolver.clear(); |
180 } | 195 } |
181 | 196 |
182 private: | 197 private: |
183 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; | 198 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; |
184 }; | 199 }; |
185 | 200 |
| 201 // static |
| 202 bool CacheStorage::canAccessCacheStorage(ExecutionContext* executionContext, Str
ing* errorMessage) |
| 203 { |
| 204 String ignored; |
| 205 if (!errorMessage) |
| 206 errorMessage = &ignored; |
| 207 return executionContext->isPrivilegedContext(*errorMessage); |
| 208 } |
| 209 |
| 210 // static |
186 CacheStorage* CacheStorage::create(WeakPtr<GlobalFetch::ScopedFetcher> fetcher,
WebServiceWorkerCacheStorage* webCacheStorage) | 211 CacheStorage* CacheStorage::create(WeakPtr<GlobalFetch::ScopedFetcher> fetcher,
WebServiceWorkerCacheStorage* webCacheStorage) |
187 { | 212 { |
188 return new CacheStorage(fetcher, adoptPtr(webCacheStorage)); | 213 return new CacheStorage(fetcher, adoptPtr(webCacheStorage)); |
189 } | 214 } |
190 | 215 |
191 ScriptPromise CacheStorage::open(ScriptState* scriptState, const String& cacheNa
me) | 216 ScriptPromise CacheStorage::open(ScriptState* scriptState, const String& cacheNa
me, ExceptionState& exceptionState) |
192 { | 217 { |
| 218 if (!commonChecks(scriptState, exceptionState)) |
| 219 return ScriptPromise(); |
| 220 |
193 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 221 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
194 const ScriptPromise promise = resolver->promise(); | 222 const ScriptPromise promise = resolver->promise(); |
195 | 223 |
196 if (m_nameToCacheMap.contains(cacheName)) { | 224 if (m_nameToCacheMap.contains(cacheName)) { |
197 Cache* cache = m_nameToCacheMap.find(cacheName)->value; | 225 Cache* cache = m_nameToCacheMap.find(cacheName)->value; |
198 resolver->resolve(cache); | 226 resolver->resolve(cache); |
199 return promise; | 227 return promise; |
200 } | 228 } |
201 | 229 |
202 if (m_webCacheStorage) | 230 if (m_webCacheStorage) |
203 m_webCacheStorage->dispatchOpen(new WithCacheCallbacks(cacheName, this,
resolver), cacheName); | 231 m_webCacheStorage->dispatchOpen(new WithCacheCallbacks(cacheName, this,
resolver), cacheName); |
204 else | 232 else |
205 resolver->reject(createNoImplementationException()); | 233 resolver->reject(createNoImplementationException()); |
206 | 234 |
207 return promise; | 235 return promise; |
208 } | 236 } |
209 | 237 |
210 ScriptPromise CacheStorage::has(ScriptState* scriptState, const String& cacheNam
e) | 238 ScriptPromise CacheStorage::has(ScriptState* scriptState, const String& cacheNam
e, ExceptionState& exceptionState) |
211 { | 239 { |
| 240 if (!commonChecks(scriptState, exceptionState)) |
| 241 return ScriptPromise(); |
| 242 |
212 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 243 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
213 const ScriptPromise promise = resolver->promise(); | 244 const ScriptPromise promise = resolver->promise(); |
214 | 245 |
215 if (m_nameToCacheMap.contains(cacheName)) { | 246 if (m_nameToCacheMap.contains(cacheName)) { |
216 resolver->resolve(true); | 247 resolver->resolve(true); |
217 return promise; | 248 return promise; |
218 } | 249 } |
219 | 250 |
220 if (m_webCacheStorage) | 251 if (m_webCacheStorage) |
221 m_webCacheStorage->dispatchHas(new Callbacks(resolver), cacheName); | 252 m_webCacheStorage->dispatchHas(new Callbacks(resolver), cacheName); |
222 else | 253 else |
223 resolver->reject(createNoImplementationException()); | 254 resolver->reject(createNoImplementationException()); |
224 | 255 |
225 return promise; | 256 return promise; |
226 } | 257 } |
227 | 258 |
228 ScriptPromise CacheStorage::deleteFunction(ScriptState* scriptState, const Strin
g& cacheName) | 259 ScriptPromise CacheStorage::deleteFunction(ScriptState* scriptState, const Strin
g& cacheName, ExceptionState& exceptionState) |
229 { | 260 { |
| 261 if (!commonChecks(scriptState, exceptionState)) |
| 262 return ScriptPromise(); |
| 263 |
230 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 264 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
231 const ScriptPromise promise = resolver->promise(); | 265 const ScriptPromise promise = resolver->promise(); |
232 | 266 |
233 if (m_webCacheStorage) | 267 if (m_webCacheStorage) |
234 m_webCacheStorage->dispatchDelete(new DeleteCallbacks(cacheName, this, r
esolver), cacheName); | 268 m_webCacheStorage->dispatchDelete(new DeleteCallbacks(cacheName, this, r
esolver), cacheName); |
235 else | 269 else |
236 resolver->reject(createNoImplementationException()); | 270 resolver->reject(createNoImplementationException()); |
237 | 271 |
238 return promise; | 272 return promise; |
239 } | 273 } |
240 | 274 |
241 ScriptPromise CacheStorage::keys(ScriptState* scriptState) | 275 ScriptPromise CacheStorage::keys(ScriptState* scriptState, ExceptionState& excep
tionState) |
242 { | 276 { |
| 277 if (!commonChecks(scriptState, exceptionState)) |
| 278 return ScriptPromise(); |
| 279 |
243 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 280 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
244 const ScriptPromise promise = resolver->promise(); | 281 const ScriptPromise promise = resolver->promise(); |
245 | 282 |
246 if (m_webCacheStorage) | 283 if (m_webCacheStorage) |
247 m_webCacheStorage->dispatchKeys(new KeysCallbacks(resolver)); | 284 m_webCacheStorage->dispatchKeys(new KeysCallbacks(resolver)); |
248 else | 285 else |
249 resolver->reject(createNoImplementationException()); | 286 resolver->reject(createNoImplementationException()); |
250 | 287 |
251 return promise; | 288 return promise; |
252 } | 289 } |
253 | 290 |
254 ScriptPromise CacheStorage::match(ScriptState* scriptState, const RequestInfo& r
equest, const CacheQueryOptions& options, ExceptionState& exceptionState) | 291 ScriptPromise CacheStorage::match(ScriptState* scriptState, const RequestInfo& r
equest, const CacheQueryOptions& options, ExceptionState& exceptionState) |
255 { | 292 { |
256 ASSERT(!request.isNull()); | 293 ASSERT(!request.isNull()); |
| 294 if (!commonChecks(scriptState, exceptionState)) |
| 295 return ScriptPromise(); |
257 | 296 |
258 if (request.isRequest()) | 297 if (request.isRequest()) |
259 return matchImpl(scriptState, request.getAsRequest(), options); | 298 return matchImpl(scriptState, request.getAsRequest(), options); |
260 Request* newRequest = Request::create(scriptState, request.getAsUSVString(),
exceptionState); | 299 Request* newRequest = Request::create(scriptState, request.getAsUSVString(),
exceptionState); |
261 if (exceptionState.hadException()) | 300 if (exceptionState.hadException()) |
262 return ScriptPromise(); | 301 return ScriptPromise(); |
263 return matchImpl(scriptState, newRequest, options); | 302 return matchImpl(scriptState, newRequest, options); |
264 } | 303 } |
265 | 304 |
266 ScriptPromise CacheStorage::matchImpl(ScriptState* scriptState, const Request* r
equest, const CacheQueryOptions& options) | 305 ScriptPromise CacheStorage::matchImpl(ScriptState* scriptState, const Request* r
equest, const CacheQueryOptions& options) |
(...skipping 26 matching lines...) Expand all Loading... |
293 { | 332 { |
294 m_webCacheStorage.clear(); | 333 m_webCacheStorage.clear(); |
295 } | 334 } |
296 | 335 |
297 DEFINE_TRACE(CacheStorage) | 336 DEFINE_TRACE(CacheStorage) |
298 { | 337 { |
299 visitor->trace(m_nameToCacheMap); | 338 visitor->trace(m_nameToCacheMap); |
300 } | 339 } |
301 | 340 |
302 } // namespace blink | 341 } // namespace blink |
OLD | NEW |