1/*
2 * Copyright (C) 2010-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 "Download.h"
28
29#include "AuthenticationChallengeDisposition.h"
30#include "AuthenticationManager.h"
31#include "Connection.h"
32#include "DataReference.h"
33#include "DownloadManager.h"
34#include "DownloadMonitor.h"
35#include "DownloadProxyMessages.h"
36#include "Logging.h"
37#include "NetworkDataTask.h"
38#include "NetworkProcess.h"
39#include "NetworkSession.h"
40#include "SandboxExtension.h"
41#include "WebCoreArgumentCoders.h"
42#include <WebCore/NotImplemented.h>
43
44#if PLATFORM(COCOA)
45#include "NetworkDataTaskCocoa.h"
46#endif
47
48#define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(isAlwaysOnLoggingAllowed(), Network, "%p - Download::" fmt, this, ##__VA_ARGS__)
49
50namespace WebKit {
51using namespace WebCore;
52
53Download::Download(DownloadManager& downloadManager, DownloadID downloadID, NetworkDataTask& download, const PAL::SessionID& sessionID, const String& suggestedName)
54 : m_downloadManager(downloadManager)
55 , m_downloadID(downloadID)
56 , m_client(downloadManager.client())
57 , m_download(&download)
58 , m_sessionID(sessionID)
59 , m_suggestedName(suggestedName)
60{
61 ASSERT(m_downloadID.downloadID());
62
63 m_downloadManager.didCreateDownload();
64}
65
66#if PLATFORM(COCOA)
67Download::Download(DownloadManager& downloadManager, DownloadID downloadID, NSURLSessionDownloadTask* download, const PAL::SessionID& sessionID, const String& suggestedName)
68 : m_downloadManager(downloadManager)
69 , m_downloadID(downloadID)
70 , m_client(downloadManager.client())
71 , m_downloadTask(download)
72 , m_sessionID(sessionID)
73 , m_suggestedName(suggestedName)
74{
75 ASSERT(m_downloadID.downloadID());
76
77 m_downloadManager.didCreateDownload();
78}
79#endif
80
81Download::~Download()
82{
83 platformDestroyDownload();
84 m_downloadManager.didDestroyDownload();
85}
86
87void Download::cancel()
88{
89 if (m_download) {
90 m_download->cancel();
91 didCancel({ });
92 return;
93 }
94 platformCancelNetworkLoad();
95}
96
97void Download::didReceiveChallenge(const WebCore::AuthenticationChallenge& challenge, ChallengeCompletionHandler&& completionHandler)
98{
99 if (challenge.protectionSpace().isPasswordBased() && !challenge.proposedCredential().isEmpty() && !challenge.previousFailureCount()) {
100 completionHandler(AuthenticationChallengeDisposition::UseCredential, challenge.proposedCredential());
101 return;
102 }
103
104 m_client->downloadsAuthenticationManager().didReceiveAuthenticationChallenge(*this, challenge, WTFMove(completionHandler));
105}
106
107void Download::didCreateDestination(const String& path)
108{
109 send(Messages::DownloadProxy::DidCreateDestination(path));
110}
111
112void Download::didReceiveData(uint64_t length)
113{
114 if (!m_hasReceivedData) {
115 RELEASE_LOG_IF_ALLOWED("didReceiveData: Started receiving data (id = %" PRIu64 ")", downloadID().downloadID());
116 m_hasReceivedData = true;
117 }
118
119 m_monitor.downloadReceivedBytes(length);
120
121 send(Messages::DownloadProxy::DidReceiveData(length));
122}
123
124void Download::didFinish()
125{
126 RELEASE_LOG_IF_ALLOWED("didFinish: (id = %" PRIu64 ")", downloadID().downloadID());
127
128 send(Messages::DownloadProxy::DidFinish());
129
130 if (m_sandboxExtension) {
131 m_sandboxExtension->revoke();
132 m_sandboxExtension = nullptr;
133 }
134
135 m_downloadManager.downloadFinished(*this);
136}
137
138void Download::didFail(const ResourceError& error, const IPC::DataReference& resumeData)
139{
140 RELEASE_LOG_IF_ALLOWED("didFail: (id = %" PRIu64 ", isTimeout = %d, isCancellation = %d, errCode = %d)",
141 downloadID().downloadID(), error.isTimeout(), error.isCancellation(), error.errorCode());
142
143 send(Messages::DownloadProxy::DidFail(error, resumeData));
144
145 if (m_sandboxExtension) {
146 m_sandboxExtension->revoke();
147 m_sandboxExtension = nullptr;
148 }
149 m_downloadManager.downloadFinished(*this);
150}
151
152void Download::didCancel(const IPC::DataReference& resumeData)
153{
154 RELEASE_LOG_IF_ALLOWED("didCancel: (id = %" PRIu64 ")", downloadID().downloadID());
155
156 send(Messages::DownloadProxy::DidCancel(resumeData));
157
158 if (m_sandboxExtension) {
159 m_sandboxExtension->revoke();
160 m_sandboxExtension = nullptr;
161 }
162 m_downloadManager.downloadFinished(*this);
163}
164
165IPC::Connection* Download::messageSenderConnection() const
166{
167 return m_downloadManager.downloadProxyConnection();
168}
169
170uint64_t Download::messageSenderDestinationID() const
171{
172 return m_downloadID.downloadID();
173}
174
175bool Download::isAlwaysOnLoggingAllowed() const
176{
177#if PLATFORM(COCOA)
178 return m_sessionID.isAlwaysOnLoggingAllowed();
179#else
180 return false;
181#endif
182}
183
184#if !PLATFORM(COCOA)
185void Download::platformCancelNetworkLoad()
186{
187}
188
189void Download::platformDestroyDownload()
190{
191}
192#endif
193
194} // namespace WebKit
195
196#undef RELEASE_LOG_IF_ALLOWED
197