| 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(ScriptPromiseResolver* resolver) | 48 explicit Callbacks(ScriptPromiseResolver* resolver) |
| 34 : m_resolver(resolver) { } | 49 : m_resolver(resolver) { } |
| 35 ~Callbacks() override { } | 50 ~Callbacks() override { } |
| 36 | 51 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 | 196 |
| 182 private: | 197 private: |
| 183 Persistent<ScriptPromiseResolver> m_resolver; | 198 Persistent<ScriptPromiseResolver> m_resolver; |
| 184 }; | 199 }; |
| 185 | 200 |
| 186 CacheStorage* CacheStorage::create(WeakPtr<GlobalFetch::ScopedFetcher> fetcher,
WebServiceWorkerCacheStorage* webCacheStorage) | 201 CacheStorage* CacheStorage::create(WeakPtr<GlobalFetch::ScopedFetcher> fetcher,
WebServiceWorkerCacheStorage* webCacheStorage) |
| 187 { | 202 { |
| 188 return new CacheStorage(fetcher, adoptPtr(webCacheStorage)); | 203 return new CacheStorage(fetcher, adoptPtr(webCacheStorage)); |
| 189 } | 204 } |
| 190 | 205 |
| 191 ScriptPromise CacheStorage::open(ScriptState* scriptState, const String& cacheNa
me) | 206 ScriptPromise CacheStorage::open(ScriptState* scriptState, const String& cacheNa
me, ExceptionState& exceptionState) |
| 192 { | 207 { |
| 208 if (!commonChecks(scriptState, exceptionState)) |
| 209 return ScriptPromise(); |
| 210 |
| 193 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 211 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 194 const ScriptPromise promise = resolver->promise(); | 212 const ScriptPromise promise = resolver->promise(); |
| 195 | 213 |
| 196 if (m_nameToCacheMap.contains(cacheName)) { | 214 if (m_nameToCacheMap.contains(cacheName)) { |
| 197 Cache* cache = m_nameToCacheMap.find(cacheName)->value; | 215 Cache* cache = m_nameToCacheMap.find(cacheName)->value; |
| 198 resolver->resolve(cache); | 216 resolver->resolve(cache); |
| 199 return promise; | 217 return promise; |
| 200 } | 218 } |
| 201 | 219 |
| 202 if (m_webCacheStorage) | 220 if (m_webCacheStorage) |
| 203 m_webCacheStorage->dispatchOpen(new WithCacheCallbacks(cacheName, this,
resolver), cacheName); | 221 m_webCacheStorage->dispatchOpen(new WithCacheCallbacks(cacheName, this,
resolver), cacheName); |
| 204 else | 222 else |
| 205 resolver->reject(createNoImplementationException()); | 223 resolver->reject(createNoImplementationException()); |
| 206 | 224 |
| 207 return promise; | 225 return promise; |
| 208 } | 226 } |
| 209 | 227 |
| 210 ScriptPromise CacheStorage::has(ScriptState* scriptState, const String& cacheNam
e) | 228 ScriptPromise CacheStorage::has(ScriptState* scriptState, const String& cacheNam
e, ExceptionState& exceptionState) |
| 211 { | 229 { |
| 230 if (!commonChecks(scriptState, exceptionState)) |
| 231 return ScriptPromise(); |
| 232 |
| 212 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 233 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 213 const ScriptPromise promise = resolver->promise(); | 234 const ScriptPromise promise = resolver->promise(); |
| 214 | 235 |
| 215 if (m_nameToCacheMap.contains(cacheName)) { | 236 if (m_nameToCacheMap.contains(cacheName)) { |
| 216 resolver->resolve(true); | 237 resolver->resolve(true); |
| 217 return promise; | 238 return promise; |
| 218 } | 239 } |
| 219 | 240 |
| 220 if (m_webCacheStorage) | 241 if (m_webCacheStorage) |
| 221 m_webCacheStorage->dispatchHas(new Callbacks(resolver), cacheName); | 242 m_webCacheStorage->dispatchHas(new Callbacks(resolver), cacheName); |
| 222 else | 243 else |
| 223 resolver->reject(createNoImplementationException()); | 244 resolver->reject(createNoImplementationException()); |
| 224 | 245 |
| 225 return promise; | 246 return promise; |
| 226 } | 247 } |
| 227 | 248 |
| 228 ScriptPromise CacheStorage::deleteFunction(ScriptState* scriptState, const Strin
g& cacheName) | 249 ScriptPromise CacheStorage::deleteFunction(ScriptState* scriptState, const Strin
g& cacheName, ExceptionState& exceptionState) |
| 229 { | 250 { |
| 251 if (!commonChecks(scriptState, exceptionState)) |
| 252 return ScriptPromise(); |
| 253 |
| 230 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 254 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 231 const ScriptPromise promise = resolver->promise(); | 255 const ScriptPromise promise = resolver->promise(); |
| 232 | 256 |
| 233 if (m_webCacheStorage) | 257 if (m_webCacheStorage) |
| 234 m_webCacheStorage->dispatchDelete(new DeleteCallbacks(cacheName, this, r
esolver), cacheName); | 258 m_webCacheStorage->dispatchDelete(new DeleteCallbacks(cacheName, this, r
esolver), cacheName); |
| 235 else | 259 else |
| 236 resolver->reject(createNoImplementationException()); | 260 resolver->reject(createNoImplementationException()); |
| 237 | 261 |
| 238 return promise; | 262 return promise; |
| 239 } | 263 } |
| 240 | 264 |
| 241 ScriptPromise CacheStorage::keys(ScriptState* scriptState) | 265 ScriptPromise CacheStorage::keys(ScriptState* scriptState, ExceptionState& excep
tionState) |
| 242 { | 266 { |
| 267 if (!commonChecks(scriptState, exceptionState)) |
| 268 return ScriptPromise(); |
| 269 |
| 243 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 270 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 244 const ScriptPromise promise = resolver->promise(); | 271 const ScriptPromise promise = resolver->promise(); |
| 245 | 272 |
| 246 if (m_webCacheStorage) | 273 if (m_webCacheStorage) |
| 247 m_webCacheStorage->dispatchKeys(new KeysCallbacks(resolver)); | 274 m_webCacheStorage->dispatchKeys(new KeysCallbacks(resolver)); |
| 248 else | 275 else |
| 249 resolver->reject(createNoImplementationException()); | 276 resolver->reject(createNoImplementationException()); |
| 250 | 277 |
| 251 return promise; | 278 return promise; |
| 252 } | 279 } |
| 253 | 280 |
| 254 ScriptPromise CacheStorage::match(ScriptState* scriptState, const RequestInfo& r
equest, const CacheQueryOptions& options, ExceptionState& exceptionState) | 281 ScriptPromise CacheStorage::match(ScriptState* scriptState, const RequestInfo& r
equest, const CacheQueryOptions& options, ExceptionState& exceptionState) |
| 255 { | 282 { |
| 256 ASSERT(!request.isNull()); | 283 ASSERT(!request.isNull()); |
| 284 if (!commonChecks(scriptState, exceptionState)) |
| 285 return ScriptPromise(); |
| 257 | 286 |
| 258 if (request.isRequest()) | 287 if (request.isRequest()) |
| 259 return matchImpl(scriptState, request.getAsRequest(), options); | 288 return matchImpl(scriptState, request.getAsRequest(), options); |
| 260 Request* newRequest = Request::create(scriptState, request.getAsUSVString(),
exceptionState); | 289 Request* newRequest = Request::create(scriptState, request.getAsUSVString(),
exceptionState); |
| 261 if (exceptionState.hadException()) | 290 if (exceptionState.hadException()) |
| 262 return ScriptPromise(); | 291 return ScriptPromise(); |
| 263 return matchImpl(scriptState, newRequest, options); | 292 return matchImpl(scriptState, newRequest, options); |
| 264 } | 293 } |
| 265 | 294 |
| 266 ScriptPromise CacheStorage::matchImpl(ScriptState* scriptState, const Request* r
equest, const CacheQueryOptions& options) | 295 ScriptPromise CacheStorage::matchImpl(ScriptState* scriptState, const Request* r
equest, const CacheQueryOptions& options) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 293 { | 322 { |
| 294 m_webCacheStorage.clear(); | 323 m_webCacheStorage.clear(); |
| 295 } | 324 } |
| 296 | 325 |
| 297 DEFINE_TRACE(CacheStorage) | 326 DEFINE_TRACE(CacheStorage) |
| 298 { | 327 { |
| 299 visitor->trace(m_nameToCacheMap); | 328 visitor->trace(m_nameToCacheMap); |
| 300 } | 329 } |
| 301 | 330 |
| 302 } // namespace blink | 331 } // namespace blink |
| OLD | NEW |