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