[Beacon] Improve error reporting
[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 }
63
64 void NetworkProcessConnection::didReceiveMessage(IPC::Connection& connection, IPC::Decoder& decoder)
65 {
66     if (decoder.messageReceiverName() == Messages::WebResourceLoader::messageReceiverName()) {
67         if (auto* webResourceLoader = WebProcess::singleton().webLoaderStrategy().webResourceLoaderForIdentifier(decoder.destinationID()))
68             webResourceLoader->didReceiveWebResourceLoaderMessage(connection, decoder);
69         return;
70     }
71     if (decoder.messageReceiverName() == Messages::WebSocketStream::messageReceiverName()) {
72         if (auto* stream = WebSocketStream::streamWithIdentifier(decoder.destinationID()))
73             stream->didReceiveMessage(connection, decoder);
74         return;
75     }
76
77 #if USE(LIBWEBRTC)
78     if (decoder.messageReceiverName() == Messages::WebRTCSocket::messageReceiverName()) {
79         WebProcess::singleton().libWebRTCNetwork().socket(decoder.destinationID()).didReceiveMessage(connection, decoder);
80         return;
81     }
82     if (decoder.messageReceiverName() == Messages::WebRTCMonitor::messageReceiverName()) {
83         WebProcess::singleton().libWebRTCNetwork().monitor().didReceiveMessage(connection, decoder);
84         return;
85     }
86     if (decoder.messageReceiverName() == Messages::WebRTCResolver::messageReceiverName()) {
87         WebProcess::singleton().libWebRTCNetwork().resolver(decoder.destinationID()).didReceiveMessage(connection, decoder);
88         return;
89     }
90 #endif
91     if (decoder.messageReceiverName() == Messages::WebCacheStorageConnection::messageReceiverName()) {
92         WebProcess::singleton().cacheStorageProvider().process(connection, decoder);
93         return;
94     }
95
96     didReceiveNetworkProcessConnectionMessage(connection, decoder);
97 }
98
99 void NetworkProcessConnection::didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&)
100 {
101     ASSERT_NOT_REACHED();
102 }
103
104 void NetworkProcessConnection::didClose(IPC::Connection&)
105 {
106     // The NetworkProcess probably crashed.
107     Ref<NetworkProcessConnection> protector(*this);
108     WebProcess::singleton().networkProcessConnectionClosed(this);
109
110     Vector<String> dummyFilenames;
111     for (auto& handler : m_writeBlobToFileCompletionHandlers.values())
112         handler(dummyFilenames);
113
114     m_writeBlobToFileCompletionHandlers.clear();
115 }
116
117 void NetworkProcessConnection::didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference, IPC::StringReference)
118 {
119 }
120
121 void NetworkProcessConnection::writeBlobsToTemporaryFiles(const Vector<String>& blobURLs, Function<void (const Vector<String>& filePaths)>&& completionHandler)
122 {
123     static uint64_t writeBlobToFileIdentifier;
124     uint64_t requestIdentifier = ++writeBlobToFileIdentifier;
125
126     m_writeBlobToFileCompletionHandlers.set(requestIdentifier, WTFMove(completionHandler));
127
128     WebProcess::singleton().networkConnection().connection().send(Messages::NetworkConnectionToWebProcess::WriteBlobsToTemporaryFiles(blobURLs, requestIdentifier), 0);
129 }
130
131 void NetworkProcessConnection::didWriteBlobsToTemporaryFiles(uint64_t requestIdentifier, const Vector<String>& filenames)
132 {
133     auto handler = m_writeBlobToFileCompletionHandlers.take(requestIdentifier);
134     if (handler)
135         handler(filenames);
136 }
137
138 void NetworkProcessConnection::didFinishPingLoad(uint64_t pingLoadIdentifier, ResourceError&& error)
139 {
140     WebProcess::singleton().webLoaderStrategy().didFinishPingLoad(pingLoadIdentifier, WTFMove(error));
141 }
142
143 #if ENABLE(SHAREABLE_RESOURCE)
144 void NetworkProcessConnection::didCacheResource(const ResourceRequest& request, const ShareableResource::Handle& handle, PAL::SessionID sessionID)
145 {
146     CachedResource* resource = MemoryCache::singleton().resourceForRequest(request, sessionID);
147     if (!resource)
148         return;
149     
150     RefPtr<SharedBuffer> buffer = handle.tryWrapInSharedBuffer();
151     if (!buffer) {
152         LOG_ERROR("Unable to create SharedBuffer from ShareableResource handle for resource url %s", request.url().string().utf8().data());
153         return;
154     }
155
156     resource->tryReplaceEncodedData(*buffer);
157 }
158 #endif
159
160 } // namespace WebKit