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#pragma once
27
28#include "DownloadID.h"
29#include "DownloadManager.h"
30#include "DownloadMonitor.h"
31#include "MessageSender.h"
32#include "NetworkDataTask.h"
33#include "SandboxExtension.h"
34#include <WebCore/AuthenticationChallenge.h>
35#include <WebCore/ResourceRequest.h>
36#include <memory>
37#include <pal/SessionID.h>
38#include <wtf/Noncopyable.h>
39#include <wtf/RetainPtr.h>
40
41#if PLATFORM(COCOA)
42OBJC_CLASS NSProgress;
43OBJC_CLASS NSURLSessionDownloadTask;
44#endif
45
46namespace IPC {
47class DataReference;
48}
49
50namespace WebCore {
51class AuthenticationChallenge;
52class BlobDataFileReference;
53class Credential;
54class ResourceError;
55class ResourceHandle;
56class ResourceRequest;
57class ResourceResponse;
58}
59
60namespace WebKit {
61
62class DownloadMonitor;
63class NetworkDataTask;
64class NetworkSession;
65class WebPage;
66
67class Download : public IPC::MessageSender {
68 WTF_MAKE_NONCOPYABLE(Download); WTF_MAKE_FAST_ALLOCATED;
69public:
70 Download(DownloadManager&, DownloadID, NetworkDataTask&, const PAL::SessionID& sessionID, const String& suggestedFilename = { });
71#if PLATFORM(COCOA)
72 Download(DownloadManager&, DownloadID, NSURLSessionDownloadTask*, const PAL::SessionID& sessionID, const String& suggestedFilename = { });
73#endif
74
75 ~Download();
76
77 void resume(const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
78 void cancel();
79#if PLATFORM(COCOA)
80 void publishProgress(const URL&, SandboxExtension::Handle&&);
81#endif
82
83 DownloadID downloadID() const { return m_downloadID; }
84 const String& suggestedName() const { return m_suggestedName; }
85
86 void setSandboxExtension(RefPtr<SandboxExtension>&& sandboxExtension) { m_sandboxExtension = WTFMove(sandboxExtension); }
87 void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&);
88 void didCreateDestination(const String& path);
89 void didReceiveData(uint64_t length);
90 void didFinish();
91 void didFail(const WebCore::ResourceError&, const IPC::DataReference& resumeData);
92 void didCancel(const IPC::DataReference& resumeData);
93
94 bool isAlwaysOnLoggingAllowed() const;
95 bool wasCanceled() const { return m_wasCanceled; }
96
97 void applicationDidEnterBackground() { m_monitor.applicationDidEnterBackground(); }
98 void applicationWillEnterForeground() { m_monitor.applicationWillEnterForeground(); }
99 DownloadManager& manager() const { return m_downloadManager; }
100
101private:
102 // IPC::MessageSender
103 IPC::Connection* messageSenderConnection() const override;
104 uint64_t messageSenderDestinationID() const override;
105
106 void platformCancelNetworkLoad();
107 void platformDestroyDownload();
108
109 DownloadManager& m_downloadManager;
110 DownloadID m_downloadID;
111 Ref<DownloadManager::Client> m_client;
112
113 Vector<RefPtr<WebCore::BlobDataFileReference>> m_blobFileReferences;
114 RefPtr<SandboxExtension> m_sandboxExtension;
115
116 RefPtr<NetworkDataTask> m_download;
117#if PLATFORM(COCOA)
118 RetainPtr<NSURLSessionDownloadTask> m_downloadTask;
119 RetainPtr<NSProgress> m_progress;
120#endif
121 PAL::SessionID m_sessionID;
122 String m_suggestedName;
123 bool m_wasCanceled { false };
124 bool m_hasReceivedData { false };
125 DownloadMonitor m_monitor { *this };
126};
127
128} // namespace WebKit
129