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