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 = 0;
63    }
64
65    m_currentRequest = proposedRequest;
66    loader->continueWillSendRequest(m_currentRequest);
67}
68
69void SynchronousNetworkLoaderClient::canAuthenticateAgainstProtectionSpace(NetworkResourceLoader* loader, const ProtectionSpace&)
70{
71    // FIXME: We should ask the WebProcess like the asynchronous case below does.
72    // This is currently impossible as the WebProcess is blocked waiting on this synchronous load.
73    // It's possible that we can jump straight to the UI process to resolve this.
74    loader->continueCanAuthenticateAgainstProtectionSpace(true);
75}
76
77void SynchronousNetworkLoaderClient::didReceiveResponse(NetworkResourceLoader*, const ResourceResponse& response)
78{
79    m_response = response;
80}
81
82void SynchronousNetworkLoaderClient::didReceiveBuffer(NetworkResourceLoader*, SharedBuffer* buffer, int encodedDataLength)
83{
84    // FIXME: There's a potential performance improvement here by preallocating a SharedMemory region
85    // of the expected content length to avoid a copy when we send it to the WebProcess on completion.
86    // It's unclear if the potential complexities of that approach are worth it.
87
88    if (!m_responseData)
89        m_responseData = adoptPtr(new Vector<char>);
90
91    m_responseData->append(buffer->data(), buffer->size());
92}
93
94void SynchronousNetworkLoaderClient::didFinishLoading(NetworkResourceLoader*, double /* finishTime */)
95{
96    sendDelayedReply();
97}
98
99void SynchronousNetworkLoaderClient::didFail(NetworkResourceLoader*, const ResourceError& error)
100{
101    m_error = error;
102    sendDelayedReply();
103}
104
105void SynchronousNetworkLoaderClient::sendDelayedReply()
106{
107    ASSERT(m_delayedReply);
108
109    if (m_response.isNull()) {
110        ASSERT(!m_error.isNull());
111        //platformSynthesizeErrorResponse();
112    }
113
114    m_delayedReply->send(m_error, m_response, m_responseData ? *m_responseData : Vector<char>());
115    m_delayedReply = nullptr;
116}
117
118} // namespace WebKit
119
120#endif // ENABLE(NETWORK_PROCESS)
121