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