Web Inspector: Include Beacon and Ping requests in Network tab
[WebKit-https.git] / Source / WebKit / WebProcess / Network / NetworkProcessConnection.cpp
1 /*
2  * Copyright (C) 2010, 2011, 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 "NetworkProcessConnection.h"
28
29 #include "DataReference.h"
30 #include "LibWebRTCNetwork.h"
31 #include "NetworkConnectionToWebProcessMessages.h"
32 #include "WebCacheStorageConnection.h"
33 #include "WebCacheStorageConnectionMessages.h"
34 #include "WebCacheStorageProvider.h"
35 #include "WebCoreArgumentCoders.h"
36 #include "WebLoaderStrategy.h"
37 #include "WebProcess.h"
38 #include "WebRTCMonitor.h"
39 #include "WebRTCMonitorMessages.h"
40 #include "WebRTCResolverMessages.h"
41 #include "WebRTCSocketMessages.h"
42 #include "WebResourceLoaderMessages.h"
43 #include "WebSocketStream.h"
44 #include "WebSocketStreamMessages.h"
45 #include <WebCore/CachedResource.h>
46 #include <WebCore/MemoryCache.h>
47 #include <WebCore/SharedBuffer.h>
48 #include <pal/SessionID.h>
49
50 using namespace WebCore;
51
52 namespace WebKit {
53
54 NetworkProcessConnection::NetworkProcessConnection(IPC::Connection::Identifier connectionIdentifier)
55     : m_connection(IPC::Connection::createClientConnection(connectionIdentifier, *this))
56 {
57     m_connection->open();
58 }
59
60 NetworkProcessConnection::~NetworkProcessConnection()
61 {
62     m_connection->invalidate();
63 }
64
65 void NetworkProcessConnection::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
66 {
67     if (decoder.messageReceiverName() == Messages::WebResourceLoader::messageReceiverName()) {
68         if (auto* webResourceLoader = WebProcess::singleton().webLoaderStrategy().webResourceLoaderForIdentifier(decoder.destinationID()))
69             webResourceLoader->didReceiveWebResourceLoaderMessage(connection, decoder);
70         return;
71     }
72     if (decoder.messageReceiverName() == Messages::WebSocketStream::messageReceiverName()) {
73         if (auto* stream = WebSocketStream::streamWithIdentifier(decoder.destinationID()))
74             stream->didReceiveMessage(connection, decoder);
75         return;
76     }
77
78 #if USE(LIBWEBRTC)
79     if (decoder.messageReceiverName() == Messages::WebRTCSocket::messageReceiverName()) {
80         WebProcess::singleton().libWebRTCNetwork().socket(decoder.destinationID()).didReceiveMessage(connection, decoder);
81         return;
82     }
83     if (decoder.messageReceiverName() == Messages::WebRTCMonitor::messageReceiverName()) {
84         WebProcess::singleton().libWebRTCNetwork().monitor().didReceiveMessage(connection, decoder);
85         return;
86     }
87     if (decoder.messageReceiverName() == Messages::WebRTCResolver::messageReceiverName()) {
88         WebProcess::singleton().libWebRTCNetwork().resolver(decoder.destinationID()).didReceiveMessage(connection, decoder);
89         return;
90     }
91 #endif
92     if (decoder.messageReceiverName() == Messages::WebCacheStorageConnection::messageReceiverName()) {
93         WebProcess::singleton().cacheStorageProvider().process(connection, decoder);
94         return;
95     }
96
97     didReceiveNetworkProcessConnectionMessage(connection, decoder);
98 }
99
100 void NetworkProcessConnection::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&)
101 {
102     ASSERT_NOT_REACHED();
103 }
104
105 void NetworkProcessConnection::didClose(IPC::Connection&)
106 {
107     // The NetworkProcess probably crashed.
108     Ref<NetworkProcessConnection> protector(*this);
109     WebProcess::singleton().networkProcessConnectionClosed(this);
110
111     Vector<String> dummyFilenames;
112     for (auto& handler : m_writeBlobToFileCompletionHandlers.values())
113         handler(dummyFilenames);
114
115     m_writeBlobToFileCompletionHandlers.clear();
116 }
117
118 void NetworkProcessConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
119 {
120 }
121
122 void NetworkProcessConnection::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, Function<void (const Vector<String>& filePaths)>&& completionHandler)
123 {
124     static uint64_t writeBlobToFileIdentifier;
125     uint64_t requestIdentifier = ++writeBlobToFileIdentifier;
126
127     m_writeBlobToFileCompletionHandlers.set(requestIdentifier, WTFMove(completionHandler));
128
129     WebProcess::singleton().networkConnection().connection().send(Messages::NetworkConnectionToWebProcess::WriteBlobsToTemporaryFiles(blobURLs, requestIdentifier), 0);
130 }
131
132 void NetworkProcessConnection::didWriteBlobsToTemporaryFiles(uint64_t requestIdentifier, const Vector<String>& filenames)
133 {
134     auto handler = m_writeBlobToFileCompletionHandlers.take(requestIdentifier);
135     if (handler)
136         handler(filenames);
137 }
138
139 void NetworkProcessConnection::didFinishPingLoad(uint64_t pingLoadIdentifier, ResourceError&& error, ResourceResponse&& response)
140 {
141     WebProcess::singleton().webLoaderStrategy().didFinishPingLoad(pingLoadIdentifier, WTFMove(error), WTFMove(response));
142 }
143
144 void NetworkProcessConnection::didFinishPreconnection(uint64_t preconnectionIdentifier, ResourceError&& error)
145 {
146     WebProcess::singleton().webLoaderStrategy().didFinishPreconnection(preconnectionIdentifier, WTFMove(error));
147 }
148
149 #if ENABLE(SHAREABLE_RESOURCE)
150 void NetworkProcessConnection::didCacheResource(const ResourceRequest& request, const ShareableResource::Handle& handle, PAL::SessionID sessionID)
151 {
152     CachedResource* resource = MemoryCache::singleton().resourceForRequest(request, sessionID);
153     if (!resource)
154         return;
155     
156     RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
157     if (!buffer) {
158         LOG_ERROR("Unable to create SharedBuffer from ShareableResource handle for resource url %s", request.url().string().utf8().data());
159         return;
160     }
161
162     resource->tryReplaceEncodedData(*buffer);
163 }
164 #endif
165
166 } // namespace WebKit