4c0ad60d9fa014755d5ce40f626cc3d603348399
[WebKit-https.git] / Source / WebKit / UIProcess / Storage / StorageProcessProxy.cpp
1 /*
2  * Copyright (C) 2013 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 "StorageProcessProxy.h"
28
29 #include "NetworkProcessMessages.h"
30 #include "StorageProcessMessages.h"
31 #include "StorageProcessProxyMessages.h"
32 #include "WebProcessPool.h"
33 #include "WebsiteData.h"
34 #include <WebCore/NotImplemented.h>
35
36 using namespace WebCore;
37
38 namespace WebKit {
39
40 static uint64_t generateCallbackID()
41 {
42     static uint64_t callbackID;
43
44     return ++callbackID;
45 }
46
47 Ref<StorageProcessProxy> StorageProcessProxy::create(WebProcessPool& processPool)
48 {
49     return adoptRef(*new StorageProcessProxy(processPool));
50 }
51
52 StorageProcessProxy::StorageProcessProxy(WebProcessPool& processPool)
53     : ChildProcessProxy(processPool.alwaysRunsAtBackgroundPriority())
54     , m_processPool(processPool)
55     , m_numPendingConnectionRequests(0)
56 {
57     connect();
58 }
59
60 StorageProcessProxy::~StorageProcessProxy()
61 {
62     ASSERT(m_pendingFetchWebsiteDataCallbacks.isEmpty());
63     ASSERT(m_pendingDeleteWebsiteDataCallbacks.isEmpty());
64     ASSERT(m_pendingDeleteWebsiteDataForOriginsCallbacks.isEmpty());
65 }
66
67 void StorageProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
68 {
69     launchOptions.processType = ProcessLauncher::ProcessType::Storage;
70     ChildProcessProxy::getLaunchOptions(launchOptions);
71 }
72
73 void StorageProcessProxy::processWillShutDown(IPC::Connection& connection)
74 {
75     ASSERT_UNUSED(connection, this->connection() == &connection);
76 }
77
78 void StorageProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
79 {
80     if (decoder.messageReceiverName() == Messages::StorageProcessProxy::messageReceiverName()) {
81         didReceiveStorageProcessProxyMessage(connection, decoder);
82         return;
83     }
84 }
85
86 void StorageProcessProxy::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, WTF::Function<void (WebsiteData)>&& completionHandler)
87 {
88     ASSERT(canSendMessage());
89
90     uint64_t callbackID = generateCallbackID();
91     m_pendingFetchWebsiteDataCallbacks.add(callbackID, WTFMove(completionHandler));
92
93     send(Messages::StorageProcess::FetchWebsiteData(sessionID, dataTypes, callbackID), 0);
94 }
95
96 void StorageProcessProxy::deleteWebsiteData(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, std::chrono::system_clock::time_point modifiedSince, WTF::Function<void ()>&& completionHandler)
97 {
98     auto callbackID = generateCallbackID();
99
100     m_pendingDeleteWebsiteDataCallbacks.add(callbackID, WTFMove(completionHandler));
101     send(Messages::StorageProcess::DeleteWebsiteData(sessionID, dataTypes, modifiedSince, callbackID), 0);
102 }
103
104 void StorageProcessProxy::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, OptionSet<WebsiteDataType> dataTypes, const Vector<WebCore::SecurityOriginData>& origins, WTF::Function<void()>&& completionHandler)
105 {
106     ASSERT(canSendMessage());
107
108     uint64_t callbackID = generateCallbackID();
109     m_pendingDeleteWebsiteDataForOriginsCallbacks.add(callbackID, WTFMove(completionHandler));
110
111     send(Messages::StorageProcess::DeleteWebsiteDataForOrigins(sessionID, dataTypes, origins, callbackID), 0);
112 }
113
114 void StorageProcessProxy::getStorageProcessConnection(bool isServiceWorkerProcess, Ref<Messages::WebProcessProxy::GetStorageProcessConnection::DelayedReply>&& reply)
115 {
116     m_pendingConnectionReplies.append(WTFMove(reply));
117
118     if (state() == State::Launching) {
119         m_numPendingConnectionRequests++;
120         return;
121     }
122
123     send(Messages::StorageProcess::CreateStorageToWebProcessConnection(isServiceWorkerProcess), 0, IPC::SendOption::DispatchMessageEvenWhenWaitingForSyncReply);
124 }
125
126 void StorageProcessProxy::didClose(IPC::Connection&)
127 {
128     // The storage process must have crashed or exited, so send any pending sync replies we might have.
129     while (!m_pendingConnectionReplies.isEmpty()) {
130         auto reply = m_pendingConnectionReplies.takeFirst();
131
132 #if USE(UNIX_DOMAIN_SOCKETS)
133         reply->send(IPC::Attachment());
134 #elif OS(DARWIN)
135         reply->send(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
136 #else
137         notImplemented();
138 #endif
139     }
140
141     for (const auto& callback : m_pendingFetchWebsiteDataCallbacks.values())
142         callback(WebsiteData());
143     m_pendingFetchWebsiteDataCallbacks.clear();
144
145     for (const auto& callback : m_pendingDeleteWebsiteDataCallbacks.values())
146         callback();
147     m_pendingDeleteWebsiteDataCallbacks.clear();
148
149     for (const auto& callback : m_pendingDeleteWebsiteDataForOriginsCallbacks.values())
150         callback();
151     m_pendingDeleteWebsiteDataForOriginsCallbacks.clear();
152
153     // Tell ProcessPool to forget about this storage process. This may cause us to be deleted.
154     m_processPool.storageProcessCrashed(this);
155 }
156
157 void StorageProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName)
158 {
159 }
160
161 void StorageProcessProxy::didCreateStorageToWebProcessConnection(const IPC::Attachment& connectionIdentifier)
162 {
163     ASSERT(!m_pendingConnectionReplies.isEmpty());
164
165     RefPtr<Messages::WebProcessProxy::GetStorageProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
166
167 #if USE(UNIX_DOMAIN_SOCKETS)
168     reply->send(connectionIdentifier);
169 #elif OS(DARWIN)
170     reply->send(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
171 #else
172     notImplemented();
173 #endif
174 }
175
176 void StorageProcessProxy::didFetchWebsiteData(uint64_t callbackID, const WebsiteData& websiteData)
177 {
178     auto callback = m_pendingFetchWebsiteDataCallbacks.take(callbackID);
179     callback(websiteData);
180 }
181
182 void StorageProcessProxy::didDeleteWebsiteData(uint64_t callbackID)
183 {
184     auto callback = m_pendingDeleteWebsiteDataCallbacks.take(callbackID);
185     callback();
186 }
187
188 void StorageProcessProxy::didDeleteWebsiteDataForOrigins(uint64_t callbackID)
189 {
190     auto callback = m_pendingDeleteWebsiteDataForOriginsCallbacks.take(callbackID);
191     callback();
192 }
193
194 #if ENABLE(SANDBOX_EXTENSIONS)
195 void StorageProcessProxy::getSandboxExtensionsForBlobFiles(uint64_t requestID, const Vector<String>& paths)
196 {
197     SandboxExtension::HandleArray extensions;
198     extensions.allocate(paths.size());
199     for (size_t i = 0; i < paths.size(); ++i) {
200         // ReadWrite is required for creating hard links, which is something that might be done with these extensions.
201         SandboxExtension::createHandle(paths[i], SandboxExtension::Type::ReadWrite, extensions[i]);
202     }
203
204     send(Messages::StorageProcess::DidGetSandboxExtensionsForBlobFiles(requestID, extensions), 0);
205 }
206 #endif
207
208 void StorageProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
209 {
210     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
211
212     if (IPC::Connection::identifierIsNull(connectionIdentifier)) {
213         // FIXME: Do better cleanup here.
214         return;
215     }
216
217     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
218         send(Messages::StorageProcess::CreateStorageToWebProcessConnection(false), 0);
219     
220     m_numPendingConnectionRequests = 0;
221 }
222
223 #if ENABLE(SERVICE_WORKER)
224 void StorageProcessProxy::establishWorkerContextConnectionToStorageProcess()
225 {
226     m_processPool.establishWorkerContextConnectionToStorageProcess(*this);
227 }
228 #endif
229
230 } // namespace WebKit