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 "NetworkProcessMessages.h"
34#include "WebCacheStorageProvider.h"
35#include "WebCoreArgumentCoders.h"
36#include "WebProcess.h"
37#include <wtf/MainThread.h>
38
39namespace WebKit {
40using namespace WebCore::DOMCacheEngine;
41using namespace CacheStorage;
42
43WebCacheStorageConnection::WebCacheStorageConnection(WebCacheStorageProvider& provider, PAL::SessionID sessionID)
44 : m_provider(provider)
45 , m_sessionID(sessionID)
46{
47}
48
49WebCacheStorageConnection::~WebCacheStorageConnection()
50{
51 ASSERT(isMainThread());
52}
53
54IPC::Connection& WebCacheStorageConnection::connection()
55{
56 return WebProcess::singleton().ensureNetworkProcessConnection().connection();
57}
58
59void WebCacheStorageConnection::open(const WebCore::ClientOrigin& origin, const String& cacheName, WebCore::DOMCacheEngine::CacheIdentifierCallback&& callback)
60{
61 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::Open(m_sessionID, origin, cacheName), WTFMove(callback));
62}
63
64void WebCacheStorageConnection::remove(uint64_t cacheIdentifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&& callback)
65{
66 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::Remove(m_sessionID, cacheIdentifier), WTFMove(callback));
67}
68
69void WebCacheStorageConnection::retrieveCaches(const WebCore::ClientOrigin& origin, uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&& callback)
70{
71 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::Caches(m_sessionID, origin, updateCounter), WTFMove(callback));
72}
73
74void WebCacheStorageConnection::retrieveRecords(uint64_t cacheIdentifier, const URL& url, WebCore::DOMCacheEngine::RecordsCallback&& callback)
75{
76 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::RetrieveRecords(m_sessionID, cacheIdentifier, url), WTFMove(callback));
77}
78
79void WebCacheStorageConnection::batchDeleteOperation(uint64_t cacheIdentifier, const WebCore::ResourceRequest& request, WebCore::CacheQueryOptions&& options, WebCore::DOMCacheEngine::RecordIdentifiersCallback&& callback)
80{
81 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::DeleteMatchingRecords(m_sessionID, cacheIdentifier, request, options), WTFMove(callback));
82}
83
84void WebCacheStorageConnection::batchPutOperation(uint64_t cacheIdentifier, Vector<Record>&& records, WebCore::DOMCacheEngine::RecordIdentifiersCallback&& callback)
85{
86 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::PutRecords(m_sessionID, cacheIdentifier, records), WTFMove(callback));
87}
88
89void WebCacheStorageConnection::reference(uint64_t cacheIdentifier)
90{
91 connection().send(Messages::CacheStorageEngineConnection::Reference(m_sessionID, cacheIdentifier), 0);
92}
93
94void WebCacheStorageConnection::dereference(uint64_t cacheIdentifier)
95{
96 connection().send(Messages::CacheStorageEngineConnection::Dereference(m_sessionID, cacheIdentifier), 0);
97}
98
99void WebCacheStorageConnection::clearMemoryRepresentation(const WebCore::ClientOrigin& origin, CompletionCallback&& callback)
100{
101 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::ClearMemoryRepresentation { m_sessionID, origin }, WTFMove(callback));
102}
103
104void WebCacheStorageConnection::engineRepresentation(CompletionHandler<void(const String&)>&& callback)
105{
106 connection().sendWithAsyncReply(Messages::CacheStorageEngineConnection::EngineRepresentation { m_sessionID }, WTFMove(callback));
107}
108
109void WebCacheStorageConnection::updateQuotaBasedOnSpaceUsage(const WebCore::ClientOrigin& origin)
110{
111 connection().send(Messages::NetworkProcess::UpdateQuotaBasedOnSpaceUsageForTesting(m_sessionID, origin), 0);
112}
113
114}
115