1/*
2 * Copyright (C) 2016 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 "PendingDownload.h"
28
29#include "DataReference.h"
30#include "Download.h"
31#include "DownloadProxyMessages.h"
32#include "NetworkLoad.h"
33#include "NetworkProcess.h"
34#include "WebCoreArgumentCoders.h"
35
36namespace WebKit {
37using namespace WebCore;
38
39PendingDownload::PendingDownload(IPC::Connection* parentProcessConnection, NetworkLoadParameters&& parameters, DownloadID downloadID, NetworkSession& networkSession, WebCore::BlobRegistryImpl* blobRegistry, const String& suggestedName)
40 : m_networkLoad(std::make_unique<NetworkLoad>(*this, blobRegistry, WTFMove(parameters), networkSession))
41 , m_parentProcessConnection(parentProcessConnection)
42{
43 m_isAllowedToAskUserForCredentials = parameters.clientCredentialPolicy == ClientCredentialPolicy::MayAskClientForCredentials;
44
45 m_networkLoad->setPendingDownloadID(downloadID);
46 m_networkLoad->setPendingDownload(*this);
47 m_networkLoad->setSuggestedFilename(suggestedName);
48
49 send(Messages::DownloadProxy::DidStart(m_networkLoad->currentRequest(), suggestedName));
50}
51
52PendingDownload::PendingDownload(IPC::Connection* parentProcessConnection, std::unique_ptr<NetworkLoad>&& networkLoad, ResponseCompletionHandler&& completionHandler, DownloadID downloadID, const ResourceRequest& request, const ResourceResponse& response)
53 : m_networkLoad(WTFMove(networkLoad))
54 , m_parentProcessConnection(parentProcessConnection)
55{
56 m_isAllowedToAskUserForCredentials = m_networkLoad->isAllowedToAskUserForCredentials();
57
58 m_networkLoad->setPendingDownloadID(downloadID);
59 send(Messages::DownloadProxy::DidStart(request, String()));
60
61 m_networkLoad->convertTaskToDownload(*this, request, response, WTFMove(completionHandler));
62}
63
64void PendingDownload::willSendRedirectedRequest(WebCore::ResourceRequest&&, WebCore::ResourceRequest&& redirectRequest, WebCore::ResourceResponse&& redirectResponse)
65{
66 send(Messages::DownloadProxy::WillSendRequest(WTFMove(redirectRequest), WTFMove(redirectResponse)));
67};
68
69void PendingDownload::continueWillSendRequest(WebCore::ResourceRequest&& newRequest)
70{
71 m_networkLoad->continueWillSendRequest(WTFMove(newRequest));
72}
73
74void PendingDownload::cancel()
75{
76 ASSERT(m_networkLoad);
77 m_networkLoad->cancel();
78 send(Messages::DownloadProxy::DidCancel({ }));
79}
80
81#if PLATFORM(COCOA)
82void PendingDownload::publishProgress(const URL& url, SandboxExtension::Handle&& sandboxExtension)
83{
84 ASSERT(!m_progressURL.isValid());
85 m_progressURL = url;
86 m_progressSandboxExtension = WTFMove(sandboxExtension);
87}
88
89void PendingDownload::didBecomeDownload(const std::unique_ptr<Download>& download)
90{
91 if (m_progressURL.isValid())
92 download->publishProgress(m_progressURL, WTFMove(m_progressSandboxExtension));
93}
94#endif // PLATFORM(COCOA)
95
96void PendingDownload::didFailLoading(const WebCore::ResourceError& error)
97{
98 send(Messages::DownloadProxy::DidFail(error, { }));
99}
100
101IPC::Connection* PendingDownload::messageSenderConnection() const
102{
103 return m_parentProcessConnection.get();
104}
105
106void PendingDownload::didReceiveResponse(WebCore::ResourceResponse&& response, ResponseCompletionHandler&& completionHandler)
107{
108 completionHandler(WebCore::PolicyAction::Download);
109}
110
111uint64_t PendingDownload::messageSenderDestinationID() const
112{
113 return m_networkLoad->pendingDownloadID().downloadID();
114}
115
116}
117