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