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 m_wasCanceled = true;
90 if (m_download) {
91 m_download->cancel();
92 didCancel({ });
93 return;
94 }
95 platformCancelNetworkLoad();
96}
97
98void Download::didReceiveChallenge(const WebCore::AuthenticationChallenge& challenge, ChallengeCompletionHandler&& completionHandler)
99{
100 if (challenge.protectionSpace().isPasswordBased() && !challenge.proposedCredential().isEmpty() && !challenge.previousFailureCount()) {
101 completionHandler(AuthenticationChallengeDisposition::UseCredential, challenge.proposedCredential());
102 return;
103 }
104
105 m_client->downloadsAuthenticationManager().didReceiveAuthenticationChallenge(*this, challenge, WTFMove(completionHandler));
106}
107
108void Download::didCreateDestination(const String& path)
109{
110 send(Messages::DownloadProxy::DidCreateDestination(path));
111}
112
113void Download::didReceiveData(uint64_t length)
114{
115 if (!m_hasReceivedData) {
116 RELEASE_LOG_IF_ALLOWED("didReceiveData: Started receiving data (id = %" PRIu64 ")", downloadID().downloadID());
117 m_hasReceivedData = true;
118 }
119
120 m_monitor.downloadReceivedBytes(length);
121
122 send(Messages::DownloadProxy::DidReceiveData(length));
123}
124
125void Download::didFinish()
126{
127 RELEASE_LOG_IF_ALLOWED("didFinish: (id = %" PRIu64 ")", downloadID().downloadID());
128
129 send(Messages::DownloadProxy::DidFinish());
130
131 if (m_sandboxExtension) {
132 m_sandboxExtension->revoke();
133 m_sandboxExtension = nullptr;
134 }
135
136 m_downloadManager.downloadFinished(*this);
137}
138
139void Download::didFail(const ResourceError& error, const IPC::DataReference& resumeData)
140{
141 RELEASE_LOG_IF_ALLOWED("didFail: (id = %" PRIu64 ", isTimeout = %d, isCancellation = %d, errCode = %d)",
142 downloadID().downloadID(), error.isTimeout(), error.isCancellation(), error.errorCode());
143
144 send(Messages::DownloadProxy::DidFail(error, resumeData));
145
146 if (m_sandboxExtension) {
147 m_sandboxExtension->revoke();
148 m_sandboxExtension = nullptr;
149 }
150 m_downloadManager.downloadFinished(*this);
151}
152
153void Download::didCancel(const IPC::DataReference& resumeData)
154{
155 RELEASE_LOG_IF_ALLOWED("didCancel: (id = %" PRIu64 ")", downloadID().downloadID());
156
157 send(Messages::DownloadProxy::DidCancel(resumeData));
158
159 if (m_sandboxExtension) {
160 m_sandboxExtension->revoke();
161 m_sandboxExtension = nullptr;
162 }
163 m_downloadManager.downloadFinished(*this);
164}
165
166IPC::Connection* Download::messageSenderConnection() const
167{
168 return m_downloadManager.downloadProxyConnection();
169}
170
171uint64_t Download::messageSenderDestinationID() const
172{
173 return m_downloadID.downloadID();
174}
175
176bool Download::isAlwaysOnLoggingAllowed() const
177{
178#if PLATFORM(COCOA)
179 return m_sessionID.isAlwaysOnLoggingAllowed();
180#else
181 return false;
182#endif
183}
184
185#if !PLATFORM(COCOA)
186void Download::platformCancelNetworkLoad()
187{
188}
189
190void Download::platformDestroyDownload()
191{
192}
193#endif
194
195} // namespace WebKit
196
197#undef RELEASE_LOG_IF_ALLOWED
198