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
40using namespace WebCore;
41
42namespace WebKit {
43
44AsynchronousNetworkLoaderClient::AsynchronousNetworkLoaderClient()
45{
46}
47
48void AsynchronousNetworkLoaderClient::willSendRequest(NetworkResourceLoader* loader, ResourceRequest& request, const ResourceResponse& redirectResponse)
49{
50    loader->sendAbortingOnFailure(Messages::WebResourceLoader::WillSendRequest(request, redirectResponse));
51}
52
53#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
54void AsynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace& protectionSpace)
55{
56    loader->sendAbortingOnFailure(Messages::WebResourceLoader::CanAuthenticateAgainstProtectionSpace(protectionSpace));
57}
58#endif
59
60void AsynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader* loader, const ResourceResponse& response)
61{
62    loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveResponseWithCertificateInfo(response, CertificateInfo(response), loader->isLoadingMainResource()));
63}
64
65void AsynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader* loader, SharedBuffer* buffer, int encodedDataLength)
66{
67#if PLATFORM(IOS) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090)
68    ShareableResource::Handle shareableResourceHandle;
69    NetworkResourceLoader::tryGetShareableHandleFromSharedBuffer(shareableResourceHandle, buffer);
70    if (!shareableResourceHandle.isNull()) {
71        // 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.
72        loader->abort();
73        loader->send(Messages::WebResourceLoader::DidReceiveResource(shareableResourceHandle, currentTime()));
74        return;
75    }
76#endif // __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
77
78    IPC::SharedBufferDataReference dataReference(buffer);
79    loader->sendAbortingOnFailure(Messages::WebResourceLoader::DidReceiveData(dataReference, encodedDataLength));
80}
81
82void AsynchronousNetworkLoaderClient::didSendData(NetworkResourceLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
83{
84    loader->send(Messages::WebResourceLoader::DidSendData(bytesSent, totalBytesToBeSent));
85}
86
87void AsynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader* loader, double finishTime)
88{
89    loader->send(Messages::WebResourceLoader::DidFinishResourceLoad(finishTime));
90}
91
92void AsynchronousNetworkLoaderClient::didFail(NetworkResourceLoader* loader, const ResourceError& error)
93{
94    loader->send(Messages::WebResourceLoader::DidFailResourceLoad(error));
95}
96
97} // namespace WebKit
98
99#endif // ENABLE(NETWORK_PROCESS)
100