| 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/V8Binding.h" | 12 #include "bindings/core/v8/V8Binding.h" |
| 13 #include "bindings/core/v8/V8ThrowException.h" | 13 #include "bindings/core/v8/V8ThrowException.h" |
| 14 #include "bindings/modules/v8/V8Response.h" | 14 #include "bindings/modules/v8/V8Response.h" |
| 15 #include "core/dom/DOMException.h" | 15 #include "core/dom/DOMException.h" |
| 16 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
| 17 #include "modules/cachestorage/CacheStorageError.h" | 17 #include "modules/cachestorage/CacheStorageError.h" |
| 18 #include "modules/fetch/BodyStreamBuffer.h" | 18 #include "modules/fetch/BodyStreamBuffer.h" |
| 19 #include "modules/fetch/FetchDataLoader.h" |
| 19 #include "modules/fetch/GlobalFetch.h" | 20 #include "modules/fetch/GlobalFetch.h" |
| 20 #include "modules/fetch/Request.h" | 21 #include "modules/fetch/Request.h" |
| 21 #include "modules/fetch/Response.h" | 22 #include "modules/fetch/Response.h" |
| 22 #include "public/platform/WebServiceWorkerCache.h" | 23 #include "public/platform/WebServiceWorkerCache.h" |
| 23 | 24 |
| 24 namespace blink { | 25 namespace blink { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 // FIXME: Consider using CallbackPromiseAdapter. | 29 // FIXME: Consider using CallbackPromiseAdapter. |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 { | 289 { |
| 289 ASSERT(index < m_batchOperations.size()); | 290 ASSERT(index < m_batchOperations.size()); |
| 290 if (m_completed) | 291 if (m_completed) |
| 291 return; | 292 return; |
| 292 m_batchOperations[index] = batchOperation; | 293 m_batchOperations[index] = batchOperation; |
| 293 if (--m_numberOfRemainingOperations != 0) | 294 if (--m_numberOfRemainingOperations != 0) |
| 294 return; | 295 return; |
| 295 m_cache->webCache()->dispatchBatch(new CallbackPromiseAdapter<void, Cach
eStorageError>(m_resolver), m_batchOperations); | 296 m_cache->webCache()->dispatchBatch(new CallbackPromiseAdapter<void, Cach
eStorageError>(m_resolver), m_batchOperations); |
| 296 } | 297 } |
| 297 | 298 |
| 298 void onError(DOMException* exception) | 299 void onError(const String& errorMessage) |
| 299 { | 300 { |
| 300 if (m_completed) | 301 if (m_completed) |
| 301 return; | 302 return; |
| 302 m_completed = true; | 303 m_completed = true; |
| 303 m_resolver->reject(exception); | 304 ScriptState* state = m_resolver->scriptState(); |
| 304 } | 305 ScriptState::Scope scope(state); |
| 305 | 306 m_resolver->reject(V8ThrowException::createTypeError(state->isolate(), e
rrorMessage)); |
| 306 void onError(v8::Local<v8::Value> exception) | |
| 307 { | |
| 308 if (m_completed) | |
| 309 return; | |
| 310 m_completed = true; | |
| 311 m_resolver->reject(exception); | |
| 312 } | 307 } |
| 313 | 308 |
| 314 DEFINE_INLINE_VIRTUAL_TRACE() | 309 DEFINE_INLINE_VIRTUAL_TRACE() |
| 315 { | 310 { |
| 316 visitor->trace(m_cache); | 311 visitor->trace(m_cache); |
| 317 visitor->trace(m_resolver); | 312 visitor->trace(m_resolver); |
| 318 } | 313 } |
| 319 | 314 |
| 320 private: | 315 private: |
| 321 bool m_completed = false; | 316 bool m_completed = false; |
| 322 int m_numberOfRemainingOperations; | 317 int m_numberOfRemainingOperations; |
| 323 Member<Cache> m_cache; | 318 Member<Cache> m_cache; |
| 324 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; | 319 RefPtrWillBeMember<ScriptPromiseResolver> m_resolver; |
| 325 Vector<WebServiceWorkerCache::BatchOperation> m_batchOperations; | 320 Vector<WebServiceWorkerCache::BatchOperation> m_batchOperations; |
| 326 }; | 321 }; |
| 327 | 322 |
| 328 class Cache::BlobHandleCallbackForPut final : public BodyStreamBuffer::BlobHandl
eCreatorClient { | 323 class Cache::BlobHandleCallbackForPut final : public GarbageCollectedFinalized<B
lobHandleCallbackForPut>, public FetchDataLoader::Client { |
| 324 USING_GARBAGE_COLLECTED_MIXIN(BlobHandleCallbackForPut); |
| 329 public: | 325 public: |
| 330 BlobHandleCallbackForPut(size_t index, BarrierCallbackForPut* barrierCallbac
k, Request* request, Response* response) | 326 BlobHandleCallbackForPut(size_t index, BarrierCallbackForPut* barrierCallbac
k, Request* request, Response* response) |
| 331 : m_index(index) | 327 : m_index(index) |
| 332 , m_barrierCallback(barrierCallback) | 328 , m_barrierCallback(barrierCallback) |
| 333 { | 329 { |
| 334 request->populateWebServiceWorkerRequest(m_webRequest); | 330 request->populateWebServiceWorkerRequest(m_webRequest); |
| 335 response->populateWebServiceWorkerResponse(m_webResponse); | 331 response->populateWebServiceWorkerResponse(m_webResponse); |
| 336 } | 332 } |
| 337 ~BlobHandleCallbackForPut() override { } | 333 ~BlobHandleCallbackForPut() override { } |
| 338 | 334 |
| 339 void didCreateBlobHandle(PassRefPtr<BlobDataHandle> handle) override | 335 void didFetchDataLoadedBlobHandle(PassRefPtr<BlobDataHandle> handle) overrid
e |
| 340 { | 336 { |
| 341 WebServiceWorkerCache::BatchOperation batchOperation; | 337 WebServiceWorkerCache::BatchOperation batchOperation; |
| 342 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; | 338 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
| 343 batchOperation.request = m_webRequest; | 339 batchOperation.request = m_webRequest; |
| 344 batchOperation.response = m_webResponse; | 340 batchOperation.response = m_webResponse; |
| 345 batchOperation.response.setBlobDataHandle(handle); | 341 batchOperation.response.setBlobDataHandle(handle); |
| 346 m_barrierCallback->onSuccess(m_index, batchOperation); | 342 m_barrierCallback->onSuccess(m_index, batchOperation); |
| 347 } | 343 } |
| 348 | 344 |
| 349 void didFail(DOMException* exception) override | 345 void didFetchDataLoadFailed() override |
| 350 { | 346 { |
| 351 m_barrierCallback->onError(exception); | 347 m_barrierCallback->onError("network error"); |
| 352 } | 348 } |
| 353 | 349 |
| 354 DEFINE_INLINE_VIRTUAL_TRACE() | 350 DEFINE_INLINE_VIRTUAL_TRACE() |
| 355 { | 351 { |
| 356 visitor->trace(m_barrierCallback); | 352 visitor->trace(m_barrierCallback); |
| 357 BlobHandleCreatorClient::trace(visitor); | 353 FetchDataLoader::Client::trace(visitor); |
| 358 } | 354 } |
| 359 | 355 |
| 360 private: | 356 private: |
| 361 const size_t m_index; | 357 const size_t m_index; |
| 362 Member<BarrierCallbackForPut> m_barrierCallback; | 358 Member<BarrierCallbackForPut> m_barrierCallback; |
| 363 | 359 |
| 364 WebServiceWorkerRequest m_webRequest; | 360 WebServiceWorkerRequest m_webRequest; |
| 365 WebServiceWorkerResponse m_webResponse; | 361 WebServiceWorkerResponse m_webResponse; |
| 366 }; | 362 }; |
| 367 | 363 |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 | 530 |
| 535 ScriptPromise Cache::putImpl(ScriptState* scriptState, const HeapVector<Member<R
equest>>& requests, const HeapVector<Member<Response>>& responses) | 531 ScriptPromise Cache::putImpl(ScriptState* scriptState, const HeapVector<Member<R
equest>>& requests, const HeapVector<Member<Response>>& responses) |
| 536 { | 532 { |
| 537 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 533 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 538 const ScriptPromise promise = resolver->promise(); | 534 const ScriptPromise promise = resolver->promise(); |
| 539 BarrierCallbackForPut* barrierCallback = new BarrierCallbackForPut(requests.
size(), this, resolver.get()); | 535 BarrierCallbackForPut* barrierCallback = new BarrierCallbackForPut(requests.
size(), this, resolver.get()); |
| 540 | 536 |
| 541 for (size_t i = 0; i < requests.size(); ++i) { | 537 for (size_t i = 0; i < requests.size(); ++i) { |
| 542 KURL url(KURL(), requests[i]->url()); | 538 KURL url(KURL(), requests[i]->url()); |
| 543 if (!url.protocolIsInHTTPFamily()) { | 539 if (!url.protocolIsInHTTPFamily()) { |
| 544 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request scheme '" + url.protocol() + "' is unsupported")); | 540 barrierCallback->onError("Request scheme '" + url.protocol() + "' is
unsupported"); |
| 545 return promise; | 541 return promise; |
| 546 } | 542 } |
| 547 if (requests[i]->method() != "GET") { | 543 if (requests[i]->method() != "GET") { |
| 548 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request method '" + requests[i]->method() + "' is unsupported"))
; | 544 barrierCallback->onError("Request method '" + requests[i]->method()
+ "' is unsupported"); |
| 549 return promise; | 545 return promise; |
| 550 } | 546 } |
| 551 if (requests[i]->hasBody() && requests[i]->bodyUsed()) { | 547 if (requests[i]->hasBody() && requests[i]->bodyUsed()) { |
| 552 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Request body is already used")); | 548 barrierCallback->onError("Request body is already used"); |
| 553 return promise; | 549 return promise; |
| 554 } | 550 } |
| 555 if (responses[i]->hasBody() && responses[i]->bodyUsed()) { | 551 if (responses[i]->hasBody() && responses[i]->bodyUsed()) { |
| 556 barrierCallback->onError(V8ThrowException::createTypeError(scriptSta
te->isolate(), "Response body is already used")); | 552 barrierCallback->onError("Response body is already used"); |
| 557 return promise; | 553 return promise; |
| 558 } | 554 } |
| 559 | 555 |
| 560 if (requests[i]->hasBody()) | 556 if (requests[i]->hasBody()) |
| 561 requests[i]->lockBody(Body::PassBody); | 557 requests[i]->lockBody(Body::PassBody); |
| 562 if (responses[i]->hasBody()) | 558 if (responses[i]->hasBody()) |
| 563 responses[i]->lockBody(Body::PassBody); | 559 responses[i]->lockBody(Body::PassBody); |
| 564 | 560 |
| 565 if (BodyStreamBuffer* buffer = responses[i]->internalBuffer()) { | 561 if (OwnPtr<DrainingBodyStreamBuffer> buffer = responses[i]->createIntern
alDrainingStream()) { |
| 566 if (buffer == responses[i]->buffer() && responses[i]->isBodyConsumed
()) | 562 // If the response has body, read the all data and create |
| 567 buffer = responses[i]->createDrainingStream(); | |
| 568 // If the response body type is stream, read the all data and create | |
| 569 // the blob handle and dispatch the put batch asynchronously. | 563 // the blob handle and dispatch the put batch asynchronously. |
| 570 buffer->readAllAndCreateBlobHandle(responses[i]->internalMIMEType(),
new BlobHandleCallbackForPut(i, barrierCallback, requests[i], responses[i])); | 564 FetchDataLoader* loader = FetchDataLoader::createLoaderAsBlobHandle(
responses[i]->internalMIMEType()); |
| 565 buffer->startLoading(loader, new BlobHandleCallbackForPut(i, barrier
Callback, requests[i], responses[i])); |
| 571 continue; | 566 continue; |
| 572 } | 567 } |
| 573 | 568 |
| 574 WebServiceWorkerCache::BatchOperation batchOperation; | 569 WebServiceWorkerCache::BatchOperation batchOperation; |
| 575 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; | 570 batchOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
| 576 requests[i]->populateWebServiceWorkerRequest(batchOperation.request); | 571 requests[i]->populateWebServiceWorkerRequest(batchOperation.request); |
| 577 responses[i]->populateWebServiceWorkerResponse(batchOperation.response); | 572 responses[i]->populateWebServiceWorkerResponse(batchOperation.response); |
| 578 barrierCallback->onSuccess(i, batchOperation); | 573 barrierCallback->onSuccess(i, batchOperation); |
| 579 } | 574 } |
| 580 | 575 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 599 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, toWebQ
ueryParams(options)); | 594 m_webCache->dispatchKeys(new CacheWithRequestsCallbacks(resolver), 0, toWebQ
ueryParams(options)); |
| 600 return promise; | 595 return promise; |
| 601 } | 596 } |
| 602 | 597 |
| 603 WebServiceWorkerCache* Cache::webCache() const | 598 WebServiceWorkerCache* Cache::webCache() const |
| 604 { | 599 { |
| 605 return m_webCache.get(); | 600 return m_webCache.get(); |
| 606 } | 601 } |
| 607 | 602 |
| 608 } // namespace blink | 603 } // namespace blink |
| OLD | NEW |