04e68d245eefb8df8216b0df9532415d5c6e06f4
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / AsynchronousNetworkLoaderClient.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 "AsynchronousNetworkLoaderClient.h"
28
29 #include "DataReference.h"
30 #include "NetworkResourceLoader.h"
31 #include "WebCoreArgumentCoders.h"
32 #include "WebResourceLoaderMessages.h"
33 #include <WebCore/CertificateInfo.h>
34 #include <WebCore/ResourceError.h>
35 #include <WebCore/SharedBuffer.h>
36 #include <wtf/CurrentTime.h>
37
38 #if ENABLE(NETWORK_PROCESS)
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
44 AsynchronousNetworkLoaderClient::AsynchronousNetworkLoaderClient()
45     : m_responseCoalescingTimer(RunLoop::main(), this, &AsynchronousNetworkLoaderClient::responseCoalescingTimerFired)
46     , m_coalescingLoader(nullptr)
47     , m_coalescingResponseEncodedDataLength(0)
48 {
49 }
50
51 void AsynchronousNetworkLoaderClient::willSendRequest(NetworkResourceLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
52 {
53     // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
54     // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
55     loader->sendAbortingOnFailure(Messages::WebResourceLoader::WillSendRequest(request, redirectResponse), IPC::DispatchMessageEvenWhenWaitingForSyncReply);
56 }
57
58 #if USE(PROTECTION_SPACE_AUTH_CALLBACK)
59 void AsynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace& protectionSpace)
60 {
61     // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
62     // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
63     loader->sendAbortingOnFailure(Messages::WebResourceLoader::CanAuthenticateAgainstProtectionSpace(protectionSpace), IPC::DispatchMessageEvenWhenWaitingForSyncReply);
64 }
65 #endif
66
67 void AsynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader* loader, const ResourceResponse& response)
68 {
69     if (!loader->isLoadingMainResource()) {
70         ASSERT(!m_coalescingLoader);
71         m_coalescingResponse = response;
72         m_coalescingLoader = loader;
73
74         // FIXME: Some resources can only be used when completely loaded. We should always delay those until completion.
75         static const double responseCoalescingTime = 0.1;
76         m_responseCoalescingTimer.startOneShot(responseCoalescingTime);
77         return;
78     }
79
80     loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(response, CertificateInfo(response), loader->isLoadingMainResource()));
81 }
82
83 void AsynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader* loader, SharedBuffer* buffer, int encodedDataLength)
84 {
85 #if PLATFORM(IOS) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)
86     ShareableResource::Handle shareableResourceHandle;
87     NetworkResourceLoader::tryGetShareableHandleFromSharedBuffer(shareableResourceHandle, buffer);
88     if (!shareableResourceHandle.isNull()) {
89         if (m_responseCoalescingTimer.isActive()) {
90             ASSERT(!m_coalescingResponse.isNull());
91             ASSERT(!m_coalescingResponseData);
92             loader->send(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(m_coalescingResponse, CertificateInfo(m_coalescingResponse), false));
93             clearCoalescedResponse();
94         }
95         // Since we're delivering this resource by ourselves all at once and don't need anymore data or callbacks from the network layer, abort the loader.
96         loader->abort();
97         loader->send(Messages::WebResourceLoader::DidReceiveResource(shareableResourceHandle, currentTime()));
98         return;
99     }
100 #endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
101
102     if (m_responseCoalescingTimer.isActive()) {
103         ASSERT(m_coalescingLoader == loader);
104         ASSERT(!m_coalescingResponse.isNull());
105         if (m_coalescingResponseData)
106             m_coalescingResponseData->append(buffer);
107         else
108             m_coalescingResponseData = buffer;
109         m_coalescingResponseEncodedDataLength += encodedDataLength;
110         return;
111     }
112
113     IPC::DataReference dataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
114     loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveData(dataReference, encodedDataLength));
115 }
116
117 void AsynchronousNetworkLoaderClient::didSendData(NetworkResourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
118 {
119     loader->send(Messages::WebResourceLoader::DidSendData(bytesSent, totalBytesToBeSent));
120 }
121
122 void AsynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader* loader, double finishTime)
123 {
124     if (m_responseCoalescingTimer.isActive()) {
125         ASSERT(!m_coalescingResponse.isNull());
126         IPC::DataReference dataReference;
127         if (m_coalescingResponseData)
128             dataReference = IPC::DataReference(reinterpret_cast<const uint8_t*>(m_coalescingResponseData->data()), m_coalescingResponseData->size());
129
130         loader->send(Messages::WebResourceLoader::DidReceiveCompleteResponse(m_coalescingResponse, CertificateInfo(m_coalescingResponse), dataReference, m_coalescingResponseEncodedDataLength, finishTime));
131         clearCoalescedResponse();
132         return;
133     }
134
135     loader->send(Messages::WebResourceLoader::DidFinishResourceLoad(finishTime));
136 }
137
138 void AsynchronousNetworkLoaderClient::didFail(NetworkResourceLoader* loader, const ResourceError& error)
139 {
140     if (m_responseCoalescingTimer.isActive())
141         dispatchPartialCoalescedResponse(loader);
142
143     loader->send(Messages::WebResourceLoader::DidFailResourceLoad(error));
144 }
145
146 void AsynchronousNetworkLoaderClient::dispatchPartialCoalescedResponse(NetworkResourceLoader* loader)
147 {
148     ASSERT(m_coalescingLoader == loader);
149     ASSERT(!m_coalescingResponse.isNull());
150     loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(m_coalescingResponse, CertificateInfo(m_coalescingResponse), false));
151
152     if (m_coalescingResponseData) {
153         IPC::DataReference dataReference(reinterpret_cast<const uint8_t*>(m_coalescingResponseData->data()), m_coalescingResponseData->size());
154         loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveData(dataReference, m_coalescingResponseEncodedDataLength));
155     }
156
157     clearCoalescedResponse();
158 }
159
160 void AsynchronousNetworkLoaderClient::clearCoalescedResponse()
161 {
162     m_coalescingResponse = ResourceResponse();
163     m_coalescingLoader = nullptr;
164     m_coalescingResponseData = nullptr;
165     m_coalescingResponseEncodedDataLength = 0;
166     m_responseCoalescingTimer.stop();
167 }
168
169 void AsynchronousNetworkLoaderClient::responseCoalescingTimerFired()
170 {
171     ASSERT(m_coalescingLoader);
172     dispatchPartialCoalescedResponse(m_coalescingLoader);
173 }
174
175
176 } // namespace WebKit
177
178 #endif // ENABLE(NETWORK_PROCESS)