1/*
2 * Copyright (C) 2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "ActiveDOMObject.h"
29#include "CacheStorageConnection.h"
30#include "CacheStorageRecord.h"
31
32namespace WebCore {
33
34class ScriptExecutionContext;
35
36class DOMCache final : public RefCounted<DOMCache>, public ActiveDOMObject {
37public:
38 static Ref<DOMCache> create(ScriptExecutionContext& context, String&& name, uint64_t identifier, Ref<CacheStorageConnection>&& connection) { return adoptRef(*new DOMCache(context, WTFMove(name), identifier, WTFMove(connection))); }
39 ~DOMCache();
40
41 using RequestInfo = FetchRequest::Info;
42
43 using KeysPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<FetchRequest>>>;
44
45 void match(RequestInfo&&, CacheQueryOptions&&, Ref<DeferredPromise>&&);
46
47 using MatchAllPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<FetchResponse>>>;
48 void matchAll(Optional<RequestInfo>&&, CacheQueryOptions&&, MatchAllPromise&&);
49 void add(RequestInfo&&, DOMPromiseDeferred<void>&&);
50
51 void addAll(Vector<RequestInfo>&&, DOMPromiseDeferred<void>&&);
52 void put(RequestInfo&&, Ref<FetchResponse>&&, DOMPromiseDeferred<void>&&);
53 void remove(RequestInfo&&, CacheQueryOptions&&, DOMPromiseDeferred<IDLBoolean>&&);
54 void keys(Optional<RequestInfo>&&, CacheQueryOptions&&, KeysPromise&&);
55
56 const String& name() const { return m_name; }
57 uint64_t identifier() const { return m_identifier; }
58
59 using MatchCallback = WTF::Function<void(ExceptionOr<FetchResponse*>)>;
60 void doMatch(RequestInfo&&, CacheQueryOptions&&, MatchCallback&&);
61
62 CacheStorageConnection& connection() { return m_connection.get(); }
63
64private:
65 DOMCache(ScriptExecutionContext&, String&& name, uint64_t identifier, Ref<CacheStorageConnection>&&);
66
67 ExceptionOr<Ref<FetchRequest>> requestFromInfo(RequestInfo&&, bool ignoreMethod);
68
69 // ActiveDOMObject
70 void stop() final;
71 const char* activeDOMObjectName() const final;
72 bool canSuspendForDocumentSuspension() const final;
73
74 void putWithResponseData(DOMPromiseDeferred<void>&&, Ref<FetchRequest>&&, Ref<FetchResponse>&&, ExceptionOr<RefPtr<SharedBuffer>>&&);
75
76 void retrieveRecords(const URL&, WTF::Function<void(Optional<Exception>&&)>&&);
77 Vector<CacheStorageRecord> queryCacheWithTargetStorage(const FetchRequest&, const CacheQueryOptions&, const Vector<CacheStorageRecord>&);
78 void queryCache(Ref<FetchRequest>&&, CacheQueryOptions&&, WTF::Function<void(ExceptionOr<Vector<CacheStorageRecord>>&&)>&&);
79 void batchDeleteOperation(const FetchRequest&, CacheQueryOptions&&, WTF::Function<void(ExceptionOr<bool>&&)>&&);
80 void batchPutOperation(const FetchRequest&, FetchResponse&, DOMCacheEngine::ResponseBody&&, WTF::Function<void(ExceptionOr<void>&&)>&&);
81 void batchPutOperation(Vector<DOMCacheEngine::Record>&&, WTF::Function<void(ExceptionOr<void>&&)>&&);
82
83 void updateRecords(Vector<DOMCacheEngine::Record>&&);
84 Vector<Ref<FetchResponse>> cloneResponses(const Vector<CacheStorageRecord>&);
85 DOMCacheEngine::Record toConnectionRecord(const FetchRequest&, FetchResponse&, DOMCacheEngine::ResponseBody&&);
86
87 String m_name;
88 uint64_t m_identifier;
89 Ref<CacheStorageConnection> m_connection;
90
91 Vector<CacheStorageRecord> m_records;
92 bool m_isStopped { false };
93};
94
95} // namespace WebCore
96