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 "PlatformCertificateInfo.h"
32#include "WebCoreArgumentCoders.h"
33#include "WebResourceLoaderMessages.h"
34#include <WebCore/ResourceError.h>
35#include <WebCore/SharedBuffer.h>
36#include <wtf/CurrentTime.h>
37
38#if ENABLE(NETWORK_PROCESS)
39
40using namespace WebCore;
41
42namespace WebKit {
43
44AsynchronousNetworkLoaderClient::AsynchronousNetworkLoaderClient()
45{
46}
47
48void AsynchronousNetworkLoaderClient::willSendRequest(NetworkResourceLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
49{
50    // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
51    // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
52    loader->sendAbortingOnFailure(Messages::WebResourceLoader::WillSendRequest(request, redirectResponse), CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
53}
54
55void AsynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace& protectionSpace)
56{
57    // This message is DispatchMessageEvenWhenWaitingForSyncReply to avoid a situation where the NetworkProcess is deadlocked
58    // waiting for 6 connections to complete while the WebProcess is waiting for a 7th (Synchronous XHR) to complete.
59    loader->sendAbortingOnFailure(Messages::WebResourceLoader::CanAuthenticateAgainstProtectionSpace(protectionSpace), CoreIPC::DispatchMessageEvenWhenWaitingForSyncReply);
60}
61
62void AsynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader* loader, const ResourceResponse& response)
63{
64    loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(response, PlatformCertificateInfo(response), loader->isLoadingMainResource()));
65}
66
67void AsynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader* loader, SharedBuffer* buffer, int encodedDataLength)
68{
69#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
70    ShareableResource::Handle shareableResourceHandle;
71    NetworkResourceLoader::tryGetShareableHandleFromSharedBuffer(shareableResourceHandle, buffer);
72    if (!shareableResourceHandle.isNull()) {
73        // 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.
74        loader->abort();
75        loader->send(Messages::WebResourceLoader::DidReceiveResource(shareableResourceHandle, currentTime()));
76        return;
77    }
78#endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
79
80    CoreIPC::DataReference dataReference(reinterpret_cast<const uint8_t*>(buffer->data()), buffer->size());
81    loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveData(dataReference, encodedDataLength));
82}
83
84void AsynchronousNetworkLoaderClient::didSendData(NetworkResourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
85{
86    loader->send(Messages::WebResourceLoader::DidSendData(bytesSent, totalBytesToBeSent));
87}
88
89void AsynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader* loader, double finishTime)
90{
91    loader->send(Messages::WebResourceLoader::DidFinishResourceLoad(finishTime));
92}
93
94void AsynchronousNetworkLoaderClient::didFail(NetworkResourceLoader* loader, const ResourceError& error)
95{
96    loader->send(Messages::WebResourceLoader::DidFailResourceLoad(error));
97}
98
99} // namespace WebKit
100
101#endif // ENABLE(NETWORK_PROCESS)
102