| 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 "FetchHeaders.h" |
| 30 | #include "FetchOptions.h" |
| 31 | #include "ResourceRequest.h" |
| 32 | #include "ResourceResponse.h" |
| 33 | #include "ScriptExecutionContext.h" |
| 34 | #include "SharedBuffer.h" |
| 35 | |
| 36 | namespace WebCore { |
| 37 | |
| 38 | struct CacheQueryOptions; |
| 39 | |
| 40 | namespace DOMCacheEngine { |
| 41 | |
| 42 | enum class Error { |
| 43 | NotImplemented, |
| 44 | ReadDisk, |
| 45 | WriteDisk, |
| 46 | QuotaExceeded, |
| 47 | Internal, |
| 48 | Stopped |
| 49 | }; |
| 50 | |
| 51 | Exception convertToExceptionAndLog(ScriptExecutionContext*, Error); |
| 52 | |
| 53 | WEBCORE_EXPORT bool queryCacheMatch(const ResourceRequest& request, const ResourceRequest& cachedRequest, const ResourceResponse&, const CacheQueryOptions&); |
| 54 | WEBCORE_EXPORT bool queryCacheMatch(const ResourceRequest& request, const URL& url, bool hasVaryStar, const HashMap<String, String>& , const CacheQueryOptions&); |
| 55 | |
| 56 | using ResponseBody = Variant<std::nullptr_t, Ref<FormData>, Ref<SharedBuffer>>; |
| 57 | ResponseBody isolatedResponseBody(const ResponseBody&); |
| 58 | WEBCORE_EXPORT ResponseBody copyResponseBody(const ResponseBody&); |
| 59 | |
| 60 | struct Record { |
| 61 | WEBCORE_EXPORT Record copy() const; |
| 62 | |
| 63 | uint64_t identifier; |
| 64 | uint64_t updateResponseCounter; |
| 65 | |
| 66 | FetchHeaders::Guard ; |
| 67 | ResourceRequest request; |
| 68 | FetchOptions options; |
| 69 | String referrer; |
| 70 | |
| 71 | FetchHeaders::Guard ; |
| 72 | ResourceResponse response; |
| 73 | ResponseBody responseBody; |
| 74 | uint64_t responseBodySize; |
| 75 | }; |
| 76 | |
| 77 | struct CacheInfo { |
| 78 | uint64_t identifier; |
| 79 | String name; |
| 80 | }; |
| 81 | |
| 82 | struct CacheInfos { |
| 83 | CacheInfos isolatedCopy(); |
| 84 | |
| 85 | template<class Encoder> void encode(Encoder&) const; |
| 86 | template<class Decoder> static Optional<CacheInfos> decode(Decoder&); |
| 87 | |
| 88 | Vector<CacheInfo> infos; |
| 89 | uint64_t updateCounter; |
| 90 | }; |
| 91 | |
| 92 | struct CacheIdentifierOperationResult { |
| 93 | template<class Encoder> void encode(Encoder&) const; |
| 94 | template<class Decoder> static Optional<CacheIdentifierOperationResult> decode(Decoder&); |
| 95 | |
| 96 | uint64_t identifier { 0 }; |
| 97 | // True in case storing cache list on the filesystem failed. |
| 98 | bool hadStorageError { false }; |
| 99 | }; |
| 100 | |
| 101 | using CacheIdentifierOrError = Expected<CacheIdentifierOperationResult, Error>; |
| 102 | using CacheIdentifierCallback = WTF::Function<void(const CacheIdentifierOrError&)>; |
| 103 | |
| 104 | using RecordIdentifiersOrError = Expected<Vector<uint64_t>, Error>; |
| 105 | using RecordIdentifiersCallback = WTF::Function<void(RecordIdentifiersOrError&&)>; |
| 106 | |
| 107 | |
| 108 | using CacheInfosOrError = Expected<CacheInfos, Error>; |
| 109 | using CacheInfosCallback = WTF::Function<void(CacheInfosOrError&&)>; |
| 110 | |
| 111 | using RecordsOrError = Expected<Vector<Record>, Error>; |
| 112 | using RecordsCallback = WTF::Function<void(RecordsOrError&&)>; |
| 113 | |
| 114 | using CompletionCallback = WTF::Function<void(Optional<Error>&&)>; |
| 115 | |
| 116 | template<class Encoder> inline void CacheInfos::encode(Encoder& encoder) const |
| 117 | { |
| 118 | encoder << infos; |
| 119 | encoder << updateCounter; |
| 120 | } |
| 121 | |
| 122 | template<class Decoder> inline Optional<CacheInfos> CacheInfos::decode(Decoder& decoder) |
| 123 | { |
| 124 | Optional<Vector<CacheInfo>> infos; |
| 125 | decoder >> infos; |
| 126 | if (!infos) |
| 127 | return WTF::nullopt; |
| 128 | |
| 129 | Optional<uint64_t> updateCounter; |
| 130 | decoder >> updateCounter; |
| 131 | if (!updateCounter) |
| 132 | return WTF::nullopt; |
| 133 | |
| 134 | return {{ WTFMove(*infos), WTFMove(*updateCounter) }}; |
| 135 | } |
| 136 | |
| 137 | template<class Encoder> inline void CacheIdentifierOperationResult::encode(Encoder& encoder) const |
| 138 | { |
| 139 | encoder << identifier; |
| 140 | encoder << hadStorageError; |
| 141 | } |
| 142 | |
| 143 | template<class Decoder> inline Optional<CacheIdentifierOperationResult> CacheIdentifierOperationResult::decode(Decoder& decoder) |
| 144 | { |
| 145 | Optional<uint64_t> identifier; |
| 146 | decoder >> identifier; |
| 147 | if (!identifier) |
| 148 | return WTF::nullopt; |
| 149 | |
| 150 | Optional<bool> hadStorageError; |
| 151 | decoder >> hadStorageError; |
| 152 | if (!hadStorageError) |
| 153 | return WTF::nullopt; |
| 154 | return {{ WTFMove(*identifier), WTFMove(*hadStorageError) }}; |
| 155 | } |
| 156 | |
| 157 | } // namespace DOMCacheEngine |
| 158 | |
| 159 | } // namespace WebCore |
| 160 | |
| 161 | namespace WTF { |
| 162 | template<> struct EnumTraits<WebCore::DOMCacheEngine::Error> { |
| 163 | using values = EnumValues< |
| 164 | WebCore::DOMCacheEngine::Error, |
| 165 | WebCore::DOMCacheEngine::Error::NotImplemented, |
| 166 | WebCore::DOMCacheEngine::Error::ReadDisk, |
| 167 | WebCore::DOMCacheEngine::Error::WriteDisk, |
| 168 | WebCore::DOMCacheEngine::Error::QuotaExceeded, |
| 169 | WebCore::DOMCacheEngine::Error::Internal |
| 170 | >; |
| 171 | }; |
| 172 | } |
| 173 | |