| 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 15 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 18 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 24 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include "DOMCacheEngine.h" |
| 30 | #include <wtf/HashMap.h> |
| 31 | #include <wtf/ThreadSafeRefCounted.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | struct ClientOrigin; |
| 36 | class FetchResponse; |
| 37 | |
| 38 | class CacheStorageConnection : public ThreadSafeRefCounted<CacheStorageConnection> { |
| 39 | public: |
| 40 | static Ref<CacheStorageConnection> create() { return adoptRef(*new CacheStorageConnection()); } |
| 41 | virtual ~CacheStorageConnection() = default; |
| 42 | |
| 43 | void open(const ClientOrigin&, const String& cacheName, DOMCacheEngine::CacheIdentifierCallback&&); |
| 44 | void remove(uint64_t cacheIdentifier, DOMCacheEngine::CacheIdentifierCallback&&); |
| 45 | void retrieveCaches(const ClientOrigin&, uint64_t updateCounter, DOMCacheEngine::CacheInfosCallback&&); |
| 46 | |
| 47 | void retrieveRecords(uint64_t cacheIdentifier, const URL&, DOMCacheEngine::RecordsCallback&&); |
| 48 | void batchDeleteOperation(uint64_t cacheIdentifier, const ResourceRequest&, CacheQueryOptions&&, DOMCacheEngine::RecordIdentifiersCallback&&); |
| 49 | void batchPutOperation(uint64_t cacheIdentifier, Vector<DOMCacheEngine::Record>&&, DOMCacheEngine::RecordIdentifiersCallback&&); |
| 50 | uint64_t computeRecordBodySize(const FetchResponse&, const DOMCacheEngine::ResponseBody&); |
| 51 | |
| 52 | virtual void reference(uint64_t /* cacheIdentifier */) { } |
| 53 | virtual void dereference(uint64_t /* cacheIdentifier */) { } |
| 54 | |
| 55 | // Used only for testing purposes. |
| 56 | virtual void clearMemoryRepresentation(const ClientOrigin&, DOMCacheEngine::CompletionCallback&& callback) { callback(DOMCacheEngine::Error::NotImplemented); } |
| 57 | virtual void engineRepresentation(WTF::Function<void(const String&)>&& callback) { callback(String { }); } |
| 58 | virtual void updateQuotaBasedOnSpaceUsage(const ClientOrigin&) { } |
| 59 | |
| 60 | void clearPendingRequests(); |
| 61 | |
| 62 | protected: |
| 63 | CacheStorageConnection() = default; |
| 64 | |
| 65 | void openCompleted(uint64_t identifier, const DOMCacheEngine::CacheIdentifierOrError& result) { openOrRemoveCompleted(identifier, result); } |
| 66 | void removeCompleted(uint64_t identifier, const DOMCacheEngine::CacheIdentifierOrError& result) { openOrRemoveCompleted(identifier, result); } |
| 67 | WEBCORE_EXPORT void updateCaches(uint64_t requestIdentifier, DOMCacheEngine::CacheInfosOrError&&); |
| 68 | |
| 69 | WEBCORE_EXPORT void updateRecords(uint64_t requestIdentifier, DOMCacheEngine::RecordsOrError&&); |
| 70 | WEBCORE_EXPORT void deleteRecordsCompleted(uint64_t requestIdentifier, DOMCacheEngine::RecordIdentifiersOrError&&); |
| 71 | WEBCORE_EXPORT void putRecordsCompleted(uint64_t requestIdentifier, DOMCacheEngine::RecordIdentifiersOrError&&); |
| 72 | |
| 73 | private: |
| 74 | virtual void doOpen(uint64_t requestIdentifier, const ClientOrigin&, const String& /* cacheName */) { openCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); } |
| 75 | virtual void doRemove(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */) { removeCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); } |
| 76 | virtual void doRetrieveCaches(uint64_t requestIdentifier, const ClientOrigin&, uint64_t /* updateCounter */) { updateCaches(requestIdentifier, { }); } |
| 77 | |
| 78 | virtual void doRetrieveRecords(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, const URL& /* url */) { updateRecords(requestIdentifier, { }); } |
| 79 | virtual void doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, const ResourceRequest&, CacheQueryOptions&&) { deleteRecordsCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); } |
| 80 | virtual void doBatchPutOperation(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, Vector<DOMCacheEngine::Record>&&) { putRecordsCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); } |
| 81 | |
| 82 | WEBCORE_EXPORT void openOrRemoveCompleted(uint64_t requestIdentifier, const DOMCacheEngine::CacheIdentifierOrError&); |
| 83 | |
| 84 | HashMap<uint64_t, DOMCacheEngine::CacheIdentifierCallback> m_openAndRemoveCachePendingRequests; |
| 85 | HashMap<uint64_t, DOMCacheEngine::CacheInfosCallback> m_retrieveCachesPendingRequests; |
| 86 | HashMap<uint64_t, DOMCacheEngine::RecordsCallback> m_retrieveRecordsPendingRequests; |
| 87 | HashMap<uint64_t, DOMCacheEngine::RecordIdentifiersCallback> m_batchDeleteAndPutPendingRequests; |
| 88 | HashMap<uint64_t, uint64_t> m_opaqueResponseToSizeWithPaddingMap; |
| 89 | |
| 90 | uint64_t m_lastRequestIdentifier { 0 }; |
| 91 | }; |
| 92 | } // namespace WebCore |
| 93 | |