630580f35170e2a3e49cf57b03839251dccc8112
[WebKit-https.git] / Source / WebKit2 / Shared / Downloads / Download.h
1 /*
2  * Copyright (C) 2010, 2011 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 #ifndef Download_h
27 #define Download_h
28
29 #include "MessageSender.h"
30 #include "SandboxExtension.h"
31 #include <WebCore/ResourceRequest.h>
32 #include <wtf/Noncopyable.h>
33
34 #if PLATFORM(COCOA)
35 #include <wtf/RetainPtr.h>
36
37 #if USE(NETWORK_SESSION)
38 OBJC_CLASS NSURLSessionDownloadTask;
39 #else
40 OBJC_CLASS NSURLDownload;
41 OBJC_CLASS WKDownloadAsDelegate;
42 #endif
43 #endif
44
45 #if PLATFORM(GTK) || PLATFORM(EFL)
46 #include <WebCore/ResourceHandle.h>
47 #include <WebCore/ResourceHandleClient.h>
48 #include <memory>
49 #endif
50
51 #if USE(CFNETWORK)
52 #include <CFNetwork/CFURLDownloadPriv.h>
53 #endif
54
55 namespace IPC {
56 class DataReference;
57 }
58
59 namespace WebCore {
60 class AuthenticationChallenge;
61 class Credential;
62 class ResourceError;
63 class ResourceHandle;
64 class ResourceResponse;
65 }
66
67 namespace WebKit {
68
69 class DownloadAuthenticationClient;
70 class DownloadManager;
71 class NetworkDataTask;
72 class NetworkSession;
73 class WebPage;
74
75 class Download : public IPC::MessageSender {
76     WTF_MAKE_NONCOPYABLE(Download);
77 public:
78 #if USE(NETWORK_SESSION)
79     Download(DownloadManager&, const NetworkSession&, uint64_t downloadID, const WebCore::ResourceRequest&);
80 #else
81     Download(DownloadManager&, uint64_t downloadID, const WebCore::ResourceRequest&);
82 #endif
83     ~Download();
84
85     void start();
86 #if USE(NETWORK_SESSION) && PLATFORM(COCOA)
87     void dataTaskDidBecomeDownloadTask(const NetworkSession&, RetainPtr<NSURLSessionDownloadTask>&&);
88 #else
89     void startWithHandle(WebCore::ResourceHandle*, const WebCore::ResourceResponse&);
90 #endif
91     void resume(const IPC::DataReference& resumeData, const String& path, const SandboxExtension::Handle&);
92     void cancel();
93
94     uint64_t downloadID() const { return m_downloadID; }
95
96     void didStart();
97     void didReceiveAuthenticationChallenge(const WebCore::AuthenticationChallenge&);
98     void didReceiveResponse(const WebCore::ResourceResponse&);
99     void didReceiveData(uint64_t length);
100     bool shouldDecodeSourceDataOfMIMEType(const String& mimeType);
101     String decideDestinationWithSuggestedFilename(const String& filename, bool& allowOverwrite);
102     void didCreateDestination(const String& path);
103     void didFinish();
104     void platformDidFinish();
105     void didFail(const WebCore::ResourceError&, const IPC::DataReference& resumeData);
106     void didCancel(const IPC::DataReference& resumeData);
107
108 #if USE(CFNETWORK)
109     DownloadAuthenticationClient* authenticationClient();
110 #endif
111
112     // Authentication
113     static void receivedCredential(const WebCore::AuthenticationChallenge&, const WebCore::Credential&);
114     static void receivedRequestToContinueWithoutCredential(const WebCore::AuthenticationChallenge&);
115     static void receivedCancellation(const WebCore::AuthenticationChallenge&);
116     static void receivedRequestToPerformDefaultHandling(const WebCore::AuthenticationChallenge&);
117     static void receivedChallengeRejection(const WebCore::AuthenticationChallenge&);
118
119     void useCredential(const WebCore::AuthenticationChallenge&, const WebCore::Credential&);
120     void continueWithoutCredential(const WebCore::AuthenticationChallenge&);
121     void cancelAuthenticationChallenge(const WebCore::AuthenticationChallenge&);
122
123 private:
124     // IPC::MessageSender
125     virtual IPC::Connection* messageSenderConnection() override;
126     virtual uint64_t messageSenderDestinationID() override;
127
128     void platformInvalidate();
129
130     DownloadManager& m_downloadManager;
131     uint64_t m_downloadID;
132     WebCore::ResourceRequest m_request;
133
134     RefPtr<SandboxExtension> m_sandboxExtension;
135
136 #if PLATFORM(COCOA)
137 #if USE(NETWORK_SESSION)
138     const NetworkSession& m_session;
139     RetainPtr<NSURLSessionDownloadTask> m_download;
140 #else
141     RetainPtr<NSURLDownload> m_nsURLDownload;
142     RetainPtr<WKDownloadAsDelegate> m_delegate;
143 #endif
144 #endif
145 #if USE(CFNETWORK)
146     RetainPtr<CFURLDownloadRef> m_download;
147     RefPtr<DownloadAuthenticationClient> m_authenticationClient;
148 #endif
149 #if PLATFORM(GTK) || PLATFORM(EFL)
150     std::unique_ptr<WebCore::ResourceHandleClient> m_downloadClient;
151     RefPtr<WebCore::ResourceHandle> m_resourceHandle;
152 #endif
153 };
154
155 } // namespace WebKit
156
157 #endif // Download_h