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
96 void applicationDidEnterBackground() { m_monitor.applicationDidEnterBackground(); }
97 void applicationWillEnterForeground() { m_monitor.applicationWillEnterForeground(); }
98 DownloadManager& manager() const { return m_downloadManager; }
99
100private:
101 // IPC::MessageSender
102 IPC::Connection* messageSenderConnection() const override;
103 uint64_t messageSenderDestinationID() const override;
104
105 void platformCancelNetworkLoad();
106 void platformDestroyDownload();
107
108 DownloadManager& m_downloadManager;
109 DownloadID m_downloadID;
110 Ref<DownloadManager::Client> m_client;
111
112 Vector<RefPtr<WebCore::BlobDataFileReference>> m_blobFileReferences;
113 RefPtr<SandboxExtension> m_sandboxExtension;
114
115 RefPtr<NetworkDataTask> m_download;
116#if PLATFORM(COCOA)
117 RetainPtr<NSURLSessionDownloadTask> m_downloadTask;
118 RetainPtr<NSProgress> m_progress;
119#endif
120 PAL::SessionID m_sessionID;
121 String m_suggestedName;
122 bool m_hasReceivedData { false };
123 DownloadMonitor m_monitor { *this };
124};
125
126} // namespace WebKit
127