[GTK] Add initial database process support
[WebKit-https.git] / Source / WebKit2 / UIProcess / Databases / DatabaseProcessProxy.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 "DatabaseProcessProxy.h"
28
29 #include "DatabaseProcessMessages.h"
30 #include "DatabaseProcessProxyMessages.h"
31 #include "WebOriginDataManagerProxy.h"
32 #include "WebOriginDataManagerProxyMessages.h"
33 #include "WebProcessPool.h"
34 #include <WebCore/NotImplemented.h>
35
36 #if ENABLE(DATABASE_PROCESS)
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 PassRefPtr<DatabaseProcessProxy> DatabaseProcessProxy::create(WebProcessPool* processPool)
43 {
44     return adoptRef(new DatabaseProcessProxy(processPool));
45 }
46
47 DatabaseProcessProxy::DatabaseProcessProxy(WebProcessPool* processPool)
48     : m_processPool(processPool)
49     , m_numPendingConnectionRequests(0)
50 {
51     connect();
52 }
53
54 DatabaseProcessProxy::~DatabaseProcessProxy()
55 {
56 }
57
58
59 void DatabaseProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
60 {
61     launchOptions.processType = ProcessLauncher::DatabaseProcess;
62     platformGetLaunchOptions(launchOptions);
63 }
64
65 void DatabaseProcessProxy::didReceiveMessage(IPC::Connection& connection, IPC::MessageDecoder& decoder)
66 {
67     if (decoder.messageReceiverName() == Messages::DatabaseProcessProxy::messageReceiverName()) {
68         didReceiveDatabaseProcessProxyMessage(connection, decoder);
69         return;
70     }
71
72     if (decoder.messageReceiverName() == Messages::WebOriginDataManagerProxy::messageReceiverName()) {
73         m_processPool->supplement<WebOriginDataManagerProxy>()->didReceiveMessage(connection, decoder);
74         return;
75     }
76 }
77
78 void DatabaseProcessProxy::getDatabaseProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply> reply)
79 {
80     m_pendingConnectionReplies.append(reply);
81
82     if (state() == State::Launching) {
83         m_numPendingConnectionRequests++;
84         return;
85     }
86
87     connection()->send(Messages::DatabaseProcess::CreateDatabaseToWebProcessConnection(), 0, IPC::DispatchMessageEvenWhenWaitingForSyncReply);
88 }
89
90 void DatabaseProcessProxy::didClose(IPC::Connection&)
91 {
92     // The database process must have crashed or exited, so send any pending sync replies we might have.
93     while (!m_pendingConnectionReplies.isEmpty()) {
94         auto reply = m_pendingConnectionReplies.takeFirst();
95
96 #if OS(DARWIN)
97         reply->send(IPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
98 #elif USE(UNIX_DOMAIN_SOCKETS)
99         reply->send(IPC::Attachment());
100 #else
101         notImplemented();
102 #endif
103     }
104
105     // Tell ProcessPool to forget about this database process. This may cause us to be deleted.
106     m_processPool->databaseProcessCrashed(this);
107 }
108
109 void DatabaseProcessProxy::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName)
110 {
111 }
112
113 void DatabaseProcessProxy::didCreateDatabaseToWebProcessConnection(const IPC::Attachment& connectionIdentifier)
114 {
115     ASSERT(!m_pendingConnectionReplies.isEmpty());
116
117     RefPtr<Messages::WebProcessProxy::GetDatabaseProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
118
119 #if OS(DARWIN)
120     reply->send(IPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
121 #elif USE(UNIX_DOMAIN_SOCKETS)
122     reply->send(connectionIdentifier);
123 #else
124     notImplemented();
125 #endif
126 }
127
128 void DatabaseProcessProxy::didFinishLaunching(ProcessLauncher* launcher, IPC::Connection::Identifier connectionIdentifier)
129 {
130     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
131
132     if (IPC::Connection::identifierIsNull(connectionIdentifier)) {
133         // FIXME: Do better cleanup here.
134         return;
135     }
136
137     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
138         connection()->send(Messages::DatabaseProcess::CreateDatabaseToWebProcessConnection(), 0);
139     
140     m_numPendingConnectionRequests = 0;
141 }
142
143 } // namespace WebKit
144
145 #endif // ENABLE(DATABASE_PROCESS)