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 "SynchronousNetworkLoaderClient.h"
28
29#if ENABLE(NETWORK_PROCESS)
30
31#include "DataReference.h"
32#include "NetworkResourceLoader.h"
33#include "WebErrors.h"
34#include <WebCore/ResourceRequest.h>
35#include <WebCore/SharedBuffer.h>
36#include <WebCore/SynchronousLoaderClient.h>
37
38using namespace WebCore;
39
40namespace WebKit {
41
42SynchronousNetworkLoaderClient::SynchronousNetworkLoaderClient(const ResourceRequest& request, PassRefPtr<Messages::NetworkConnectionToWebProcess::PerformSynchronousLoad::DelayedReply> reply)
43    : m_originalRequest(request)
44    , m_delayedReply(reply)
45{
46    ASSERT(m_delayedReply);
47}
48
49SynchronousNetworkLoaderClient::~SynchronousNetworkLoaderClient()
50{
51    // By the time a SynchronousNetworkLoaderClient is being destroyed, it must always have sent its reply to the WebProcess.
52    ASSERT(!m_delayedReply);
53}
54
55void SynchronousNetworkLoaderClient::willSendRequest(NetworkResourceLoader* loader, ResourceRequest& proposedRequest, const ResourceResponse& /* redirectResponse */)
56{
57    // FIXME: This needs to be fixed to follow the redirect correctly even for cross-domain requests.
58    // This includes at least updating host records, and comparing the current request instead of the original request here.
59    if (!protocolHostAndPortAreEqual(m_originalRequest.url(), proposedRequest.url())) {
60        ASSERT(m_error.isNull());
61        m_error = SynchronousLoaderClient::platformBadResponseError();
62        proposedRequest = ResourceRequest();
63    }
64
65    m_currentRequest = proposedRequest;
66    loader->continueWillSendRequest(m_currentRequest);
67}
68
69#if USE(PROTECTION_SPACE_AUTH_CALLBACK)
70void SynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace&)
71{
72    // FIXME: We should ask the WebProcess like the asynchronous case below does.
73    // This is currently impossible as the WebProcess is blocked waiting on this synchronous load.
74    // It's possible that we can jump straight to the UI process to resolve this.
75    loader->continueCanAuthenticateAgainstProtectionSpace(true);
76}
77#endif
78
79void SynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader*, const ResourceResponse& response)
80{
81    m_response = response;
82}
83
84void SynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader*, SharedBuffer*, int /* encodedDataLength */)
85{
86    // Nothing to do here. Data is buffered in NetworkResourceLoader and we will grab it from there
87    // in sendDelayedReply().
88}
89
90void SynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader* loader, double /* finishTime */)
91{
92    sendDelayedReply(*loader);
93}
94
95void SynchronousNetworkLoaderClient::didFail(NetworkResourceLoader* loader, const ResourceError& error)
96{
97    m_error = error;
98    sendDelayedReply(*loader);
99}
100
101void SynchronousNetworkLoaderClient::sendDelayedReply(NetworkResourceLoader& loader)
102{
103    ASSERT(m_delayedReply);
104
105    if (m_response.isNull()) {
106        ASSERT(!m_error.isNull());
107        //platformSynthesizeErrorResponse();
108    }
109
110    Vector<char> responseData;
111    SharedBuffer* buffer = loader.bufferedData();
112    if (buffer && buffer->size())
113        responseData.append(buffer->data(), buffer->size());
114
115    m_delayedReply->send(m_error, m_response, responseData);
116    m_delayedReply = nullptr;
117}
118
119} // namespace WebKit
120
121#endif // ENABLE(NETWORK_PROCESS)
122