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 "DownloadMap.h"
30#include "NetworkDataTask.h"
31#include "PendingDownload.h"
32#include "SandboxExtension.h"
33#include <WebCore/NotImplemented.h>
34#include <wtf/Forward.h>
35#include <wtf/HashMap.h>
36#include <wtf/Noncopyable.h>
37
38namespace PAL {
39class SessionID;
40}
41
42namespace WebCore {
43class BlobDataFileReference;
44class ResourceHandle;
45class ResourceRequest;
46class ResourceResponse;
47}
48
49namespace IPC {
50class Connection;
51class DataReference;
52}
53
54namespace WebKit {
55
56class AuthenticationManager;
57class Download;
58class NetworkBlobRegistry;
59class NetworkConnectionToWebProcess;
60class NetworkLoad;
61class PendingDownload;
62
63class DownloadManager {
64 WTF_MAKE_NONCOPYABLE(DownloadManager);
65
66public:
67 class Client {
68 public:
69 virtual ~Client() { }
70
71 virtual void didCreateDownload() = 0;
72 virtual void didDestroyDownload() = 0;
73 virtual IPC::Connection* downloadProxyConnection() = 0;
74 virtual IPC::Connection* parentProcessConnectionForDownloads() = 0;
75 virtual AuthenticationManager& downloadsAuthenticationManager() = 0;
76 virtual void pendingDownloadCanceled(DownloadID) = 0;
77 virtual NetworkSession* networkSession(const PAL::SessionID&) const = 0;
78 virtual NetworkBlobRegistry& networkBlobRegistry() = 0;
79 virtual void ref() const = 0;
80 virtual void deref() const = 0;
81 virtual uint32_t downloadMonitorSpeedMultiplier() const = 0;
82 };
83
84 explicit DownloadManager(Client&);
85
86 void startDownload(PAL::SessionID, DownloadID, const WebCore::ResourceRequest&, const String& suggestedName = { });
87 void dataTaskBecameDownloadTask(DownloadID, std::unique_ptr<Download>&&);
88 void continueWillSendRequest(DownloadID, WebCore::ResourceRequest&&);
89 void willDecidePendingDownloadDestination(NetworkDataTask&, ResponseCompletionHandler&&);
90 void convertNetworkLoadToDownload(DownloadID, std::unique_ptr<NetworkLoad>&&, ResponseCompletionHandler&&, Vector<RefPtr<WebCore::BlobDataFileReference>>&&, const WebCore::ResourceRequest&, const WebCore::ResourceResponse&);
91 void continueDecidePendingDownloadDestination(DownloadID, String destination, SandboxExtension::Handle&&, bool allowOverwrite);
92
93 void resumeDownload(PAL::SessionID, DownloadID, const IPC::DataReference& resumeData, const String& path, SandboxExtension::Handle&&);
94
95 void cancelDownload(DownloadID);
96#if PLATFORM(COCOA)
97 void publishDownloadProgress(DownloadID, const URL&, SandboxExtension::Handle&&);
98#endif
99
100 Download* download(DownloadID downloadID) { return m_downloads.get(downloadID); }
101
102 void downloadFinished(Download&);
103 bool isDownloading() const { return !m_downloads.isEmpty(); }
104 uint64_t activeDownloadCount() const { return m_downloads.size(); }
105
106 void applicationDidEnterBackground();
107 void applicationWillEnterForeground();
108
109 void didCreateDownload();
110 void didDestroyDownload();
111
112 IPC::Connection* downloadProxyConnection();
113 AuthenticationManager& downloadsAuthenticationManager();
114
115 Client& client() { return m_client; }
116
117private:
118 Client& m_client;
119 HashMap<DownloadID, std::unique_ptr<PendingDownload>> m_pendingDownloads;
120 HashMap<DownloadID, std::pair<RefPtr<NetworkDataTask>, ResponseCompletionHandler>> m_downloadsWaitingForDestination;
121 HashMap<DownloadID, RefPtr<NetworkDataTask>> m_downloadsAfterDestinationDecided;
122 DownloadMap m_downloads;
123};
124
125} // namespace WebKit
126