3242c7e5dcb6daa2031947f217fa2764b76dbe41
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / NetworkDataTask.h
1 /*
2  * Copyright (C) 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 #ifndef NetworkDataTask_h
27 #define NetworkDataTask_h
28
29 #include "SandboxExtension.h"
30 #include <WebCore/Credential.h>
31 #include <WebCore/FrameLoaderTypes.h>
32 #include <WebCore/ResourceHandleTypes.h>
33 #include <WebCore/ResourceLoaderOptions.h>
34 #include <WebCore/ResourceRequest.h>
35 #include <WebCore/Timer.h>
36 #include <wtf/Function.h>
37 #include <wtf/RetainPtr.h>
38 #include <wtf/text/WTFString.h>
39
40 #if PLATFORM(COCOA)
41 OBJC_CLASS NSURLSessionDataTask;
42 #endif
43
44 namespace WebCore {
45 class AuthenticationChallenge;
46 class Credential;
47 class ResourceError;
48 class ResourceRequest;
49 class ResourceResponse;
50 class SharedBuffer;
51 }
52
53 namespace WebKit {
54
55 class Download;
56 class NetworkSession;
57 class PendingDownload;
58
59 enum class AuthenticationChallengeDisposition {
60     UseCredential,
61     PerformDefaultHandling,
62     Cancel,
63     RejectProtectionSpace
64 };
65     
66 typedef Function<void(const WebCore::ResourceRequest&)> RedirectCompletionHandler;
67 typedef Function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)> ChallengeCompletionHandler;
68 typedef Function<void(WebCore::PolicyAction)> ResponseCompletionHandler;
69
70 class NetworkDataTaskClient {
71 public:
72     virtual void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&) = 0;
73     virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&) = 0;
74     virtual void didReceiveResponseNetworkSession(WebCore::ResourceResponse&&, ResponseCompletionHandler&&) = 0;
75     virtual void didReceiveData(Ref<WebCore::SharedBuffer>&&) = 0;
76     virtual void didCompleteWithError(const WebCore::ResourceError&) = 0;
77     virtual void didBecomeDownload() = 0;
78     virtual void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend) = 0;
79     virtual void wasBlocked() = 0;
80     virtual void cannotShowURL() = 0;
81     
82     virtual ~NetworkDataTaskClient() { }
83 };
84
85 class NetworkDataTask : public RefCounted<NetworkDataTask> {
86     friend class NetworkSession;
87 public:
88     static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, WebCore::StoredCredentials storedCredentials, WebCore::ContentSniffingPolicy shouldContentSniff, bool shouldClearReferrerOnHTTPSToHTTPRedirect)
89     {
90         return adoptRef(*new NetworkDataTask(session, client, request, storedCredentials, shouldContentSniff, shouldClearReferrerOnHTTPSToHTTPRedirect));
91     }
92     
93     void suspend();
94     void cancel();
95     void resume();
96
97     enum class State {
98         Running,
99         Suspended,
100         Canceling,
101         Completed
102     };
103     State state() const;
104
105     typedef uint64_t TaskIdentifier;
106     
107     ~NetworkDataTask();
108     
109     void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend);
110     void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&);
111     void didCompleteWithError(const WebCore::ResourceError&);
112     void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&);
113     void didReceiveData(Ref<WebCore::SharedBuffer>&&);
114     void didBecomeDownload();
115     
116     NetworkDataTaskClient* client() const { return m_client; }
117     void clearClient() { m_client = nullptr; }
118     
119     DownloadID pendingDownloadID() { return m_pendingDownloadID; }
120     PendingDownload* pendingDownload() { return m_pendingDownload; }
121     void setPendingDownloadID(DownloadID downloadID)
122     {
123         ASSERT(!m_pendingDownloadID.downloadID());
124         ASSERT(downloadID.downloadID());
125         m_pendingDownloadID = downloadID;
126     }
127     void setPendingDownload(PendingDownload& pendingDownload)
128     {
129         ASSERT(!m_pendingDownload);
130         m_pendingDownload = &pendingDownload;
131     }
132     void setPendingDownloadLocation(const String& filename, const SandboxExtension::Handle&, bool allowOverwrite);
133     const String& pendingDownloadLocation() { return m_pendingDownloadLocation; }
134
135     const WebCore::ResourceRequest& firstRequest() const { return m_firstRequest; }
136     String suggestedFilename();
137     void setSuggestedFilename(const String&);
138     void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&);
139     void transferSandboxExtensionToDownload(Download&);
140     bool allowsSpecificHTTPSCertificateForHost(const WebCore::AuthenticationChallenge&);
141     
142 private:
143     NetworkDataTask(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::StoredCredentials, WebCore::ContentSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
144
145     bool tryPasswordBasedAuthentication(const WebCore::AuthenticationChallenge&, const ChallengeCompletionHandler&);
146     
147     enum FailureType {
148         NoFailure,
149         BlockedFailure,
150         InvalidURLFailure
151     };
152     FailureType m_scheduledFailureType { NoFailure };
153     WebCore::Timer m_failureTimer;
154     void failureTimerFired();
155     void scheduleFailure(FailureType);
156     
157     RefPtr<NetworkSession> m_session;
158     NetworkDataTaskClient* m_client;
159     PendingDownload* m_pendingDownload { nullptr };
160     DownloadID m_pendingDownloadID;
161     String m_user;
162     String m_password;
163 #if USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
164     WebCore::Credential m_initialCredential;
165 #endif
166     WebCore::StoredCredentials m_storedCredentials;
167     String m_lastHTTPMethod;
168     String m_pendingDownloadLocation;
169     WebCore::ResourceRequest m_firstRequest;
170     bool m_shouldClearReferrerOnHTTPSToHTTPRedirect;
171     RefPtr<SandboxExtension> m_sandboxExtension;
172 #if PLATFORM(COCOA)
173     RetainPtr<NSURLSessionDataTask> m_task;
174 #endif
175     String m_suggestedFilename;
176 };
177
178 #if PLATFORM(COCOA)
179 WebCore::Credential serverTrustCredential(const WebCore::AuthenticationChallenge&);
180 #endif
181     
182 }
183
184 #endif