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 | #include "config.h" |
28 | #include "CacheStorageEngineConnection.h" |
29 | |
30 | #include "Logging.h" |
31 | #include "NetworkConnectionToWebProcess.h" |
32 | #include "WebCoreArgumentCoders.h" |
33 | #include <WebCore/CacheQueryOptions.h> |
34 | |
35 | namespace WebKit { |
36 | using namespace WebCore::DOMCacheEngine; |
37 | using namespace CacheStorage; |
38 | |
39 | #undef RELEASE_LOG_IF_ALLOWED |
40 | #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), CacheStorage, "%p - CacheStorageEngineConnection::" fmt, &m_connection.connection(), ##__VA_ARGS__) |
41 | #define RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK(functionName, fmt, resultGetter) \ |
42 | if (!result.has_value())\ |
43 | RELEASE_LOG_ERROR_IF(sessionID.isAlwaysOnLoggingAllowed(), CacheStorage, "CacheStorageEngineConnection::%s - failed - error %d", functionName, (int)result.error()); \ |
44 | else {\ |
45 | auto value = resultGetter(result.value()); \ |
46 | UNUSED_PARAM(value); \ |
47 | RELEASE_LOG_IF(sessionID.isAlwaysOnLoggingAllowed(), CacheStorage, "CacheStorageEngineConnection::%s - succeeded - " fmt, functionName, value); \ |
48 | } |
49 | CacheStorageEngineConnection::CacheStorageEngineConnection(NetworkConnectionToWebProcess& connection) |
50 | : m_connection(connection) |
51 | { |
52 | } |
53 | |
54 | CacheStorageEngineConnection::~CacheStorageEngineConnection() |
55 | { |
56 | for (auto& keyValue : m_cachesLocks) { |
57 | auto& sessionID = keyValue.key; |
58 | for (auto& references : keyValue.value) { |
59 | ASSERT(references.value); |
60 | Engine::unlock(m_connection.networkProcess(), sessionID, references.key); |
61 | } |
62 | } |
63 | } |
64 | |
65 | void CacheStorageEngineConnection::open(PAL::SessionID sessionID, WebCore::ClientOrigin&& origin, String&& cacheName, CacheIdentifierCallback&& callback) |
66 | { |
67 | RELEASE_LOG_IF_ALLOWED("open cache" ); |
68 | Engine::open(m_connection.networkProcess(), sessionID, WTFMove(origin), WTFMove(cacheName), [callback = WTFMove(callback), sessionID](auto& result) mutable { |
69 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("open" , "cache identifier is %" PRIu64, [](const auto& value) { return value.identifier; }); |
70 | callback(result); |
71 | }); |
72 | } |
73 | |
74 | void CacheStorageEngineConnection::remove(PAL::SessionID sessionID, uint64_t cacheIdentifier, CacheIdentifierCallback&& callback) |
75 | { |
76 | RELEASE_LOG_IF_ALLOWED("remove cache %" PRIu64, cacheIdentifier); |
77 | Engine::remove(m_connection.networkProcess(), sessionID, cacheIdentifier, [callback = WTFMove(callback), sessionID](auto& result) mutable { |
78 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("remove" , "removed cache %" PRIu64, [](const auto& value) { return value.identifier; }); |
79 | callback(result); |
80 | }); |
81 | } |
82 | |
83 | void CacheStorageEngineConnection::caches(PAL::SessionID sessionID, WebCore::ClientOrigin&& origin, uint64_t updateCounter, CacheInfosCallback&& callback) |
84 | { |
85 | RELEASE_LOG_IF_ALLOWED("caches" ); |
86 | Engine::retrieveCaches(m_connection.networkProcess(), sessionID, WTFMove(origin), updateCounter, [callback = WTFMove(callback), sessionID, origin](auto&& result) mutable { |
87 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("caches" , "caches size is %lu" , [](const auto& value) { return value.infos.size(); }); |
88 | callback(WTFMove(result)); |
89 | }); |
90 | } |
91 | |
92 | void CacheStorageEngineConnection::retrieveRecords(PAL::SessionID sessionID, uint64_t cacheIdentifier, URL&& url, RecordsCallback&& callback) |
93 | { |
94 | RELEASE_LOG_IF_ALLOWED("retrieveRecords in cache %" PRIu64, cacheIdentifier); |
95 | Engine::retrieveRecords(m_connection.networkProcess(), sessionID, cacheIdentifier, WTFMove(url), [callback = WTFMove(callback), sessionID](auto&& result) mutable { |
96 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("retrieveRecords" , "records size is %lu" , [](const auto& value) { return value.size(); }); |
97 | callback(WTFMove(result)); |
98 | }); |
99 | } |
100 | |
101 | void CacheStorageEngineConnection::deleteMatchingRecords(PAL::SessionID sessionID, uint64_t cacheIdentifier, WebCore::ResourceRequest&& request, WebCore::CacheQueryOptions&& options, RecordIdentifiersCallback&& callback) |
102 | { |
103 | RELEASE_LOG_IF_ALLOWED("deleteMatchingRecords in cache %" PRIu64, cacheIdentifier); |
104 | Engine::deleteMatchingRecords(m_connection.networkProcess(), sessionID, cacheIdentifier, WTFMove(request), WTFMove(options), [callback = WTFMove(callback), sessionID](auto&& result) mutable { |
105 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("deleteMatchingRecords" , "deleted %lu records" , [](const auto& value) { return value.size(); }); |
106 | callback(WTFMove(result)); |
107 | }); |
108 | } |
109 | |
110 | void CacheStorageEngineConnection::putRecords(PAL::SessionID sessionID, uint64_t cacheIdentifier, Vector<Record>&& records, RecordIdentifiersCallback&& callback) |
111 | { |
112 | RELEASE_LOG_IF_ALLOWED("putRecords in cache %" PRIu64 ", %lu records" , cacheIdentifier, records.size()); |
113 | Engine::putRecords(m_connection.networkProcess(), sessionID, cacheIdentifier, WTFMove(records), [callback = WTFMove(callback), sessionID](auto&& result) mutable { |
114 | RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK("putRecords" , "put %lu records" , [](const auto& value) { return value.size(); }); |
115 | callback(WTFMove(result)); |
116 | }); |
117 | } |
118 | |
119 | void CacheStorageEngineConnection::reference(PAL::SessionID sessionID, uint64_t cacheIdentifier) |
120 | { |
121 | RELEASE_LOG_IF_ALLOWED("reference cache %" PRIu64, cacheIdentifier); |
122 | auto& references = m_cachesLocks.ensure(sessionID, []() { |
123 | return HashMap<CacheIdentifier, LockCount> { }; |
124 | }).iterator->value; |
125 | auto& counter = references.ensure(cacheIdentifier, []() { |
126 | return 0; |
127 | }).iterator->value; |
128 | if (!counter++) |
129 | Engine::lock(m_connection.networkProcess(), sessionID, cacheIdentifier); |
130 | } |
131 | |
132 | void CacheStorageEngineConnection::dereference(PAL::SessionID sessionID, uint64_t cacheIdentifier) |
133 | { |
134 | RELEASE_LOG_IF_ALLOWED("dereference cache %" PRIu64, cacheIdentifier); |
135 | auto& references = m_cachesLocks.ensure(sessionID, []() { |
136 | return HashMap<CacheIdentifier, LockCount> { }; |
137 | }).iterator->value; |
138 | |
139 | auto referenceResult = references.find(cacheIdentifier); |
140 | if (referenceResult == references.end()) |
141 | return; |
142 | |
143 | ASSERT(referenceResult->value); |
144 | if (--referenceResult->value) |
145 | return; |
146 | |
147 | Engine::unlock(m_connection.networkProcess(), sessionID, cacheIdentifier); |
148 | references.remove(referenceResult); |
149 | } |
150 | |
151 | void CacheStorageEngineConnection::clearMemoryRepresentation(PAL::SessionID sessionID, WebCore::ClientOrigin&& origin, CompletionHandler<void(Optional<Error>&&)>&& completionHandler) |
152 | { |
153 | Engine::clearMemoryRepresentation(m_connection.networkProcess(), sessionID, WTFMove(origin), WTFMove(completionHandler)); |
154 | } |
155 | |
156 | void CacheStorageEngineConnection::engineRepresentation(PAL::SessionID sessionID , CompletionHandler<void(String&&)>&& completionHandler) |
157 | { |
158 | Engine::representation(m_connection.networkProcess(), sessionID, WTFMove(completionHandler)); |
159 | } |
160 | |
161 | } |
162 | |
163 | #undef RELEASE_LOG_IF_ALLOWED |
164 | #undef RELEASE_LOG_FUNCTION_IF_ALLOWED_IN_CALLBACK |
165 | |