f3188fdc6756693fd158ebc76dbb7c4bdf2c4448
[WebKit-https.git] / Source / WebKit / WebProcess / Cache / WebCacheStorageConnection.cpp
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 #include "config.h"
27 #include "WebCacheStorageConnection.h"
28
29 #include "CacheStorageEngine.h"
30 #include "CacheStorageEngineConnectionMessages.h"
31 #include "NetworkConnectionToWebProcessMessages.h"
32 #include "NetworkProcessConnection.h"
33 #include "WebCacheStorageProvider.h"
34 #include "WebCoreArgumentCoders.h"
35 #include "WebProcess.h"
36 #include <wtf/MainThread.h>
37
38 using namespace WebCore::DOMCacheEngine;
39 using namespace WebKit::CacheStorage;
40
41 namespace WebKit {
42
43 WebCacheStorageConnection::WebCacheStorageConnection(WebCacheStorageProvider& provider, PAL::SessionID sessionID)
44     : m_provider(provider)
45     , m_sessionID(sessionID)
46 {
47 }
48
49 WebCacheStorageConnection::~WebCacheStorageConnection()
50 {
51     ASSERT(isMainThread());
52 }
53
54 IPC::Connection& WebCacheStorageConnection::connection()
55 {
56     return WebProcess::singleton().ensureNetworkProcessConnection().connection();
57 }
58
59 void WebCacheStorageConnection::doOpen(uint64_t requestIdentifier, const String& origin, const String& cacheName)
60 {
61     connection().send(Messages::CacheStorageEngineConnection::Open(m_sessionID, requestIdentifier, origin, cacheName), 0);
62 }
63
64 void WebCacheStorageConnection::doRemove(uint64_t requestIdentifier, uint64_t cacheIdentifier)
65 {
66     connection().send(Messages::CacheStorageEngineConnection::Remove(m_sessionID, requestIdentifier, cacheIdentifier), 0);
67 }
68
69 void WebCacheStorageConnection::doRetrieveCaches(uint64_t requestIdentifier, const String& origin, uint64_t updateCounter)
70 {
71     connection().send(Messages::CacheStorageEngineConnection::Caches(m_sessionID, requestIdentifier, origin, updateCounter), 0);
72 }
73
74 void WebCacheStorageConnection::doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::URL& url)
75 {
76     connection().send(Messages::CacheStorageEngineConnection::RetrieveRecords(m_sessionID, requestIdentifier, cacheIdentifier, url), 0);
77 }
78
79 void WebCacheStorageConnection::doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::ResourceRequest& request, WebCore::CacheQueryOptions&& options)
80 {
81     connection().send(Messages::CacheStorageEngineConnection::DeleteMatchingRecords(m_sessionID, requestIdentifier, cacheIdentifier, request, options), 0);
82 }
83
84 void WebCacheStorageConnection::doBatchPutOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, Vector<Record>&& records)
85 {
86     connection().send(Messages::CacheStorageEngineConnection::PutRecords(m_sessionID, requestIdentifier, cacheIdentifier, records), 0);
87 }
88
89 void WebCacheStorageConnection::reference(uint64_t cacheIdentifier)
90 {
91     connection().send(Messages::CacheStorageEngineConnection::Reference(m_sessionID, cacheIdentifier), 0);
92 }
93
94 void WebCacheStorageConnection::dereference(uint64_t cacheIdentifier)
95 {
96     connection().send(Messages::CacheStorageEngineConnection::Dereference(m_sessionID, cacheIdentifier), 0);
97 }
98
99 void WebCacheStorageConnection::openCompleted(uint64_t requestIdentifier, const CacheIdentifierOrError& result)
100 {
101     CacheStorageConnection::openCompleted(requestIdentifier, result);
102 }
103
104 void WebCacheStorageConnection::removeCompleted(uint64_t requestIdentifier, const CacheIdentifierOrError& result)
105 {
106     CacheStorageConnection::removeCompleted(requestIdentifier, result);
107 }
108
109 void WebCacheStorageConnection::updateCaches(uint64_t requestIdentifier, CacheInfosOrError&& result)
110 {
111     CacheStorageConnection::updateCaches(requestIdentifier, WTFMove(result));
112 }
113
114 void WebCacheStorageConnection::updateRecords(uint64_t requestIdentifier, RecordsOrError&& result)
115 {
116     CacheStorageConnection::updateRecords(requestIdentifier, WTFMove(result));
117 }
118
119 void WebCacheStorageConnection::deleteRecordsCompleted(uint64_t requestIdentifier, RecordIdentifiersOrError&& result)
120 {
121     CacheStorageConnection::deleteRecordsCompleted(requestIdentifier, WTFMove(result));
122 }
123
124 void WebCacheStorageConnection::putRecordsCompleted(uint64_t requestIdentifier, RecordIdentifiersOrError&& result)
125 {
126     CacheStorageConnection::putRecordsCompleted(requestIdentifier, WTFMove(result));
127 }
128
129 void WebCacheStorageConnection::clearMemoryRepresentation(const String& origin, CompletionCallback&& callback)
130 {
131     uint64_t requestIdentifier = ++m_engineRepresentationNextIdentifier;
132     m_clearRepresentationCallbacks.set(requestIdentifier, WTFMove(callback));
133     connection().send(Messages::CacheStorageEngineConnection::ClearMemoryRepresentation(m_sessionID, requestIdentifier, origin), 0);
134 }
135
136 void WebCacheStorageConnection::clearMemoryRepresentationCompleted(uint64_t requestIdentifier, std::optional<Error>&& result)
137 {
138     if (auto callback = m_clearRepresentationCallbacks.take(requestIdentifier))
139         callback(WTFMove(result));
140 }
141
142 void WebCacheStorageConnection::engineRepresentation(WTF::Function<void(const String&)>&& callback)
143 {
144     uint64_t requestIdentifier = ++m_engineRepresentationNextIdentifier;
145     m_engineRepresentationCallbacks.set(requestIdentifier, WTFMove(callback));
146     connection().send(Messages::CacheStorageEngineConnection::EngineRepresentation(m_sessionID, requestIdentifier), 0);
147 }
148
149 void WebCacheStorageConnection::engineRepresentationCompleted(uint64_t requestIdentifier, const String& result)
150 {
151     if (auto callback = m_engineRepresentationCallbacks.take(requestIdentifier))
152         callback(result);
153 }
154
155 }