| 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/Cache.h" | 6 #include "modules/cachestorage/Cache.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
| 12 #include "bindings/core/v8/V8ThrowException.h" | 12 #include "bindings/core/v8/V8ThrowException.h" |
| 13 #include "bindings/modules/v8/V8Response.h" | 13 #include "bindings/modules/v8/V8Response.h" |
| 14 #include "core/dom/DOMException.h" | 14 #include "core/dom/DOMException.h" |
| 15 #include "core/dom/ExceptionCode.h" |
| 15 #include "modules/cachestorage/CacheStorageError.h" | 16 #include "modules/cachestorage/CacheStorageError.h" |
| 16 #include "modules/fetch/BodyStreamBuffer.h" | 17 #include "modules/fetch/BodyStreamBuffer.h" |
| 18 #include "modules/fetch/FetchDataLoader.h" |
| 17 #include "modules/fetch/GlobalFetch.h" | 19 #include "modules/fetch/GlobalFetch.h" |
| 18 #include "modules/fetch/Request.h" | 20 #include "modules/fetch/Request.h" |
| 19 #include "modules/fetch/Response.h" | 21 #include "modules/fetch/Response.h" |
| 20 #include "public/platform/WebServiceWorkerCache.h" | 22 #include "public/platform/WebServiceWorkerCache.h" |
| 21 | 23 |
| 22 namespace blink { | 24 namespace blink { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 // FIXME: Consider using CallbackPromiseAdapter. | 28 // FIXME: Consider using CallbackPromiseAdapter. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 { | 193 { |
| 192 ASSERT(index < m_batchOperations.size()); | 194 ASSERT(index < m_batchOperations.size()); |
| 193 if (m_completed) | 195 if (m_completed) |
| 194 return; | 196 return; |
| 195 m_batchOperations[index] = batchOperation; | 197 m_batchOperations[index] = batchOperation; |
| 196 if (--m_numberOfRemainingOperations != 0) | 198 if (--m_numberOfRemainingOperations != 0) |
| 197 return; | 199 return; |
| 198 m_cache->webCache()->dispatchBatch(new CallbackPromiseAdapter<void, Cach
eStorageError>(m_resolver), m_batchOperations); | 200 m_cache->webCache()->dispatchBatch(new CallbackPromiseAdapter<void, Cach
eStorageError>(m_resolver), m_batchOperations); |
| 199 } | 201 } |
| 200 | 202 |
| 201 void onError(DOMException* exception) | 203 void onError(const String& errorMessage) |
| 202 { | 204 { |
| 203 if (m_completed) | 205 if (m_completed) |
| 204 return; | 206 return; |
| 205 m_completed = true; | 207 m_completed = true; |
| 206 m_resolver->reject(exception); | 208 ScriptState* state = m_resolver->scriptState(); |
| 207 } | 209 ScriptState::Scope scope(state); |
| 208 | 210 m_resolver->reject(V8ThrowException::createTypeError(state->isolate(), e
rrorMessage)); |
| 209 void onError(v8::Local<v8::Value> exception) | |
| 210 { | |
| 211 if (m_completed) | |
| 212 return; | |
| 213 m_completed = true; | |
| 214 m_resolver->reject(exception); | |
| 215 } | 211 } |
| 216 | 212 |
| 217 DEFINE_INLINE_VIRTUAL_TRACE() | 213 DEFINE_INLINE_VIRTUAL_TRACE() |
| 218 { | 214 { |
| 219 visitor->trace(m_cache); | 215 visitor->trace(m_cache); |
| 220 visitor->trace(m_resolver); | 216 visitor->trace(m_resolver); |
| 221 } | 217 } |
| 222 | 218 |
| 223 private: | 219 private: |
| 224 bool m_completed = false; | 220 bool m_completed = false; |
| 225 int m_numberOfRemainingOperations; | 221 int m_numberOfRemainingOperations; |
| 226 Member<Cache> m_cache; | 222 Member<Cache> m_cache; |
| 227 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; | 223 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; |
| 228 Vector<WebServiceWorkerCache::BatchOperation> m_batchOperations; | 224 Vector<WebServiceWorkerCache::BatchOperation> m_batchOperations; |
| 229 }; | 225 }; |
| 230 | 226 |
| 231 class Cache::BlobHandleCallbackForPut final : public BodyStreamBuffer::BlobHandl
eCreatorClient { | 227 class Cache::BlobHandleCallbackForPut final : public GarbageCollectedFinalized<B
lobHandleCallbackForPut>, public FetchDataLoader::Client { |
| 228 USING_GARBAGE_COLLECTED_MIXIN(BlobHandleCallbackForPut); |
| 232 public: | 229 public: |
| 233 BlobHandleCallbackForPut(size_t index, BarrierCallbackForPut* barrierCallbac
k, Request* request, Response* response) | 230 BlobHandleCallbackForPut(size_t index, BarrierCallbackForPut* barrierCallbac
k, Request* request, Response* response) |
| 234 : m_index(index) | 231 : m_index(index) |
| 235 , m_barrierCallback(barrierCallback) | 232 , m_barrierCallback(barrierCallback) |
| 236 { | 233 { |
| 237 request->populateWebServiceWorkerRequest(m_webRequest); | 234 request->populateWebServiceWorkerRequest(m_webRequest); |
| 238 response->populateWebServiceWorkerResponse(m_webResponse); | 235 response->populateWebServiceWorkerResponse(m_webResponse); |
| 239 } | 236 } |
| 240 ~BlobHandleCallbackForPut() override { } | 237 ~BlobHandleCallbackForPut() override { } |
| 241 | 238 |
| 242 void didCreateBlobHandle(PassRefPtr<BlobDataHandle> handle) override | 239 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle> handle) overrid
e |
| 243 { | 240 { |
| 244 WebServiceWorkerCache::BatchOperation batchOperation; | 241 WebServiceWorkerCache::BatchOperation batchOperation; |
| 245 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; | 242 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
| 246 batchOperation.request = m_webRequest; | 243 batchOperation.request = m_webRequest; |
| 247 batchOperation.response = m_webResponse; | 244 batchOperation.response = m_webResponse; |
| 248 batchOperation.response.setBlobDataHandle(handle); | 245 batchOperation.response.setBlobDataHandle(handle); |
| 249 m_barrierCallback->onSuccess(m_index, batchOperation); | 246 m_barrierCallback->onSuccess(m_index, batchOperation); |
| 250 } | 247 } |
| 251 | 248 |
| 252 void didFail(DOMException* exception) override | 249 void didFetchDataLoadFailed() override |
| 253 { | 250 { |
| 254 m_barrierCallback->onError(exception); | 251 m_barrierCallback->onError("network error"); |
| 255 } | 252 } |
| 256 | 253 |
| 257 DEFINE_INLINE_VIRTUAL_TRACE() | 254 DEFINE_INLINE_VIRTUAL_TRACE() |
| 258 { | 255 { |
| 259 visitor->trace(m_barrierCallback); | 256 visitor->trace(m_barrierCallback); |
| 260 BlobHandleCreatorClient::trace(visitor); | 257 FetchDataLoader::Client::trace(visitor); |
| 261 } | 258 } |
| 262 | 259 |
| 263 private: | 260 private: |
| 264 const size_t m_index; | 261 const size_t m_index; |
| 265 Member<BarrierCallbackForPut> m_barrierCallback; | 262 Member<BarrierCallbackForPut> m_barrierCallback; |
| 266 | 263 |
| 267 WebServiceWorkerRequest m_webRequest; | 264 WebServiceWorkerRequest m_webRequest; |
| 268 WebServiceWorkerResponse m_webResponse; | 265 WebServiceWorkerResponse m_webResponse; |
| 269 }; | 266 }; |
| 270 | 267 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 | 425 |
| 429 ScriptPromise Cache::putImpl(ScriptState* scriptState, const HeapVector<Member<R
equest>>& requests, const HeapVector<Member<Response>>& responses) | 426 ScriptPromise Cache::putImpl(ScriptState* scriptState, const HeapVector<Member<R
equest>>& requests, const HeapVector<Member<Response>>& responses) |
| 430 { | 427 { |
| 431 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 428 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 432 const ScriptPromise promise = resolver->promise(); | 429 const ScriptPromise promise = resolver->promise(); |
| 433 BarrierCallbackForPut* barrierCallback = new BarrierCallbackForPut(requests.
size(), this, resolver.get()); | 430 BarrierCallbackForPut* barrierCallback = new BarrierCallbackForPut(requests.
size(), this, resolver.get()); |
| 434 | 431 |
| 435 for (size_t i = 0; i < requests.size(); ++i) { | 432 for (size_t i = 0; i < requests.size(); ++i) { |
| 436 KURL url(KURL(), requests[i]->url()); | 433 KURL url(KURL(), requests[i]->url()); |
| 437 if (!url.protocolIsInHTTPFamily()) { | 434 if (!url.protocolIsInHTTPFamily()) { |
| 438 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request scheme '" + url.protocol() + "' is unsupported")); | 435 barrierCallback->onError("Request scheme '" + url.protocol() + "' is
unsupported"); |
| 439 return promise; | 436 return promise; |
| 440 } | 437 } |
| 441 if (requests[i]->method() != "GET") { | 438 if (requests[i]->method() != "GET") { |
| 442 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request method '" + requests[i]->method() + "' is unsupported"))
; | 439 barrierCallback->onError("Request method '" + requests[i]->method()
+ "' is unsupported"); |
| 443 return promise; | 440 return promise; |
| 444 } | 441 } |
| 445 if (requests[i]->hasBody() && requests[i]->bodyUsed()) { | 442 if (requests[i]->hasBody() && requests[i]->bodyUsed()) { |
| 446 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request body is already used")); | 443 barrierCallback->onError("Request body is already used"); |
| 447 return promise; | 444 return promise; |
| 448 } | 445 } |
| 449 if (responses[i]->hasBody() && responses[i]->bodyUsed()) { | 446 if (responses[i]->hasBody() && responses[i]->bodyUsed()) { |
| 450 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Response body is already used")); | 447 barrierCallback->onError("Response body is already used"); |
| 451 return promise; | 448 return promise; |
| 452 } | 449 } |
| 453 | 450 |
| 454 if (requests[i]->hasBody()) | 451 if (requests[i]->hasBody()) |
| 455 requests[i]->lockBody(Body::PassBody); | 452 requests[i]->lockBody(Body::PassBody); |
| 456 if (responses[i]->hasBody()) | 453 if (responses[i]->hasBody()) |
| 457 responses[i]->lockBody(Body::PassBody); | 454 responses[i]->lockBody(Body::PassBody); |
| 458 | 455 |
| 459 if (BodyStreamBuffer* buffer = responses[i]->internalBuffer()) { | 456 if (OwnPtr<DrainingBodyStreamBuffer> buffer = responses[i]->createIntern
alDrainingStream()) { |
| 460 if (buffer == responses[i]->buffer() && responses[i]->isBodyConsumed
()) | 457 // If the response has body, read the all data and create |
| 461 buffer = responses[i]->createDrainingStream(); | |
| 462 // If the response body type is stream, read the all data and create | |
| 463 // the blob handle and dispatch the put batch asynchronously. | 458 // the blob handle and dispatch the put batch asynchronously. |
| 464 buffer->readAllAndCreateBlobHandle(responses[i]->internalMIMEType(),
new BlobHandleCallbackForPut(i, barrierCallback, requests[i], responses[i])); | 459 FetchDataLoader* loader = FetchDataLoader::createLoaderAsBlobHandle(
responses[i]->internalMIMEType()); |
| 460 buffer->startLoading(loader, new BlobHandleCallbackForPut(i, barrier
Callback, requests[i], responses[i])); |
| 465 continue; | 461 continue; |
| 466 } | 462 } |
| 467 | 463 |
| 468 WebServiceWorkerCache::BatchOperation batchOperation; | 464 WebServiceWorkerCache::BatchOperation batchOperation; |
| 469 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; | 465 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
| 470 requests[i]->populateWebServiceWorkerRequest(batchOperation.request); | 466 requests[i]->populateWebServiceWorkerRequest(batchOperation.request); |
| 471 responses[i]->populateWebServiceWorkerResponse(batchOperation.response); | 467 responses[i]->populateWebServiceWorkerResponse(batchOperation.response); |
| 472 barrierCallback->onSuccess(i, batchOperation); | 468 barrierCallback->onSuccess(i, batchOperation); |
| 473 } | 469 } |
| 474 | 470 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 493 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, toWebQ
ueryParams(options)); | 489 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, toWebQ
ueryParams(options)); |
| 494 return promise; | 490 return promise; |
| 495 } | 491 } |
| 496 | 492 |
| 497 WebServiceWorkerCache* Cache::webCache() const | 493 WebServiceWorkerCache* Cache::webCache() const |
| 498 { | 494 { |
| 499 return m_webCache.get(); | 495 return m_webCache.get(); |
| 500 } | 496 } |
| 501 | 497 |
| 502 } // namespace blink | 498 } // namespace blink |
| OLD | NEW |