c07e6044935a3a6a9588dcdb1e14b150b4caee35
[WebKit-https.git] / Source / WebKit2 / UIProcess / Network / NetworkProcessProxy.cpp
1 /*
2  * Copyright (C) 2012 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 "NetworkProcessProxy.h"
28
29 #include "NetworkProcessCreationParameters.h"
30 #include "NetworkProcessMessages.h"
31 #include "WebContext.h"
32 #include "WebProcessMessages.h"
33 #include <WebCore/RunLoop.h>
34
35 #if ENABLE(NETWORK_PROCESS)
36
37 using namespace WebCore;
38
39 namespace WebKit {
40
41 PassRefPtr<NetworkProcessProxy> NetworkProcessProxy::create(WebContext* webContext)
42 {
43     return adoptRef(new NetworkProcessProxy(webContext));
44 }
45
46 NetworkProcessProxy::NetworkProcessProxy(WebContext* webContext)
47     : m_webContext(webContext)
48     , m_numPendingConnectionRequests(0)
49 {
50     connect();
51 }
52
53 void NetworkProcessProxy::getLaunchOptions(ProcessLauncher::LaunchOptions& launchOptions)
54 {
55     launchOptions.processType = ProcessLauncher::NetworkProcess;
56
57 #if PLATFORM(MAC)
58     launchOptions.architecture = ProcessLauncher::LaunchOptions::MatchCurrentArchitecture;
59     launchOptions.executableHeap = false;
60 #if HAVE(XPC)
61     launchOptions.useXPC = false;
62 #endif
63 #endif
64 }
65
66 NetworkProcessProxy::~NetworkProcessProxy()
67 {
68 }
69
70 void NetworkProcessProxy::getNetworkProcessConnection(PassRefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply)
71 {
72     m_pendingConnectionReplies.append(reply);
73
74     if (isLaunching()) {
75         m_numPendingConnectionRequests++;
76         return;
77     }
78
79     connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0, CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
80 }
81
82 DownloadProxy* NetworkProcessProxy::createDownloadProxy()
83 {
84     if (!m_downloadProxyMap)
85         m_downloadProxyMap = adoptPtr(new DownloadProxyMap(m_messageReceiverMap));
86
87     return m_downloadProxyMap->createDownloadProxy(m_webContext);
88 }
89
90 void NetworkProcessProxy::networkProcessCrashedOrFailedToLaunch()
91 {
92     // The network process must have crashed or exited, send any pending sync replies we might have.
93     while (!m_pendingConnectionReplies.isEmpty()) {
94         RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
95
96 #if PLATFORM(MAC)
97         reply->send(CoreIPC::Attachment(0, MACH_MSG_TYPE_MOVE_SEND));
98 #else
99         notImplemented();
100 #endif
101     }
102
103     // Tell the network process manager to forget about this network process proxy. This may cause us to be deleted.
104     m_webContext->removeNetworkProcessProxy(this);
105 }
106
107 void NetworkProcessProxy::didReceiveMessage(CoreIPC::Connection* connection, CoreIPC::MessageID messageID, CoreIPC::MessageDecoder& decoder)
108 {
109     didReceiveNetworkProcessProxyMessage(connection, messageID, decoder);
110 }
111
112 void NetworkProcessProxy::didClose(CoreIPC::Connection*)
113 {
114     // Notify all WebProcesses that the NetworkProcess crashed.
115     const Vector<WebContext*>& contexts = WebContext::allContexts();
116     for (size_t i = 0; i < contexts.size(); ++i)
117         contexts[i]->sendToAllProcesses(Messages::WebProcess::NetworkProcessCrashed());
118
119     if (m_downloadProxyMap)
120         m_downloadProxyMap->processDidClose();
121
122     // This may cause us to be deleted.
123     networkProcessCrashedOrFailedToLaunch();
124 }
125
126 void NetworkProcessProxy::didReceiveInvalidMessage(CoreIPC::Connection*, CoreIPC::StringReference, CoreIPC::StringReference)
127 {
128 }
129
130 void NetworkProcessProxy::didCreateNetworkConnectionToWebProcess(const CoreIPC::Attachment& connectionIdentifier)
131 {
132     ASSERT(!m_pendingConnectionReplies.isEmpty());
133
134     // Grab the first pending connection reply.
135     RefPtr<Messages::WebProcessProxy::GetNetworkProcessConnection::DelayedReply> reply = m_pendingConnectionReplies.takeFirst();
136
137 #if PLATFORM(MAC)
138     reply->send(CoreIPC::Attachment(connectionIdentifier.port(), MACH_MSG_TYPE_MOVE_SEND));
139 #else
140     notImplemented();
141 #endif
142 }
143
144 void NetworkProcessProxy::didFinishLaunching(ProcessLauncher* launcher, CoreIPC::Connection::Identifier connectionIdentifier)
145 {
146     ChildProcessProxy::didFinishLaunching(launcher, connectionIdentifier);
147
148     if (CoreIPC::Connection::identifierIsNull(connectionIdentifier)) {
149         // FIXME: Do better cleanup here.
150         return;
151     }
152
153     NetworkProcessCreationParameters parameters;
154     platformInitializeNetworkProcess(parameters);
155
156     // Initialize the network host process.
157     connection()->send(Messages::NetworkProcess::InitializeNetworkProcess(parameters), 0);
158
159     for (unsigned i = 0; i < m_numPendingConnectionRequests; ++i)
160         connection()->send(Messages::NetworkProcess::CreateNetworkConnectionToWebProcess(), 0);
161     
162     m_numPendingConnectionRequests = 0;
163
164 #if PLATFORM(MAC)
165     if (WebContext::applicationIsOccluded())
166         connection()->send(Messages::NetworkProcess::SetApplicationIsOccluded(true), 0);
167 #endif
168 }
169
170 } // namespace WebKit
171
172 #endif // ENABLE(NETWORK_PROCESS)