[Resource Timing] Gather timing information with reliable responseEnd time
[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 #pragma once
27
28 #if USE(NETWORK_SESSION)
29
30 #include "DownloadID.h"
31 #include "SandboxExtension.h"
32 #include <WebCore/Credential.h>
33 #include <WebCore/FrameLoaderTypes.h>
34 #include <WebCore/NetworkLoadMetrics.h>
35 #include <WebCore/ResourceHandleTypes.h>
36 #include <WebCore/ResourceLoaderOptions.h>
37 #include <WebCore/ResourceRequest.h>
38 #include <WebCore/Timer.h>
39 #include <wtf/Function.h>
40 #include <wtf/text/WTFString.h>
41
42 namespace WebCore {
43 class AuthenticationChallenge;
44 class ResourceError;
45 class ResourceResponse;
46 class SharedBuffer;
47 }
48
49 namespace WebKit {
50
51 class Download;
52 class NetworkLoadParameters;
53 class NetworkSession;
54 class PendingDownload;
55 enum class AuthenticationChallengeDisposition;
56
57 typedef Function<void(const WebCore::ResourceRequest&)> RedirectCompletionHandler;
58 typedef Function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)> ChallengeCompletionHandler;
59 typedef Function<void(WebCore::PolicyAction)> ResponseCompletionHandler;
60
61 class NetworkDataTaskClient {
62 public:
63     virtual void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&) = 0;
64     virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&) = 0;
65     virtual void didReceiveResponseNetworkSession(WebCore::ResourceResponse&&, ResponseCompletionHandler&&) = 0;
66     virtual void didReceiveData(Ref<WebCore::SharedBuffer>&&) = 0;
67     virtual void didCompleteWithError(const WebCore::ResourceError&, const WebCore::NetworkLoadMetrics&) = 0;
68     virtual void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend) = 0;
69     virtual void wasBlocked() = 0;
70     virtual void cannotShowURL() = 0;
71
72     void didCompleteWithError(const WebCore::ResourceError& error)
73     {
74         WebCore::NetworkLoadMetrics emptyMetrics;
75         didCompleteWithError(error, emptyMetrics);
76     }
77
78     virtual ~NetworkDataTaskClient() { }
79 };
80
81 class NetworkDataTask : public RefCounted<NetworkDataTask> {
82 public:
83     static Ref<NetworkDataTask> create(NetworkSession&, NetworkDataTaskClient&, const NetworkLoadParameters&);
84
85     virtual ~NetworkDataTask();
86
87     virtual void suspend() = 0;
88     virtual void cancel() = 0;
89     virtual void resume() = 0;
90     virtual void invalidateAndCancel() = 0;
91
92     void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&);
93
94     enum class State {
95         Running,
96         Suspended,
97         Canceling,
98         Completed
99     };
100     virtual State state() const = 0;
101
102     NetworkDataTaskClient* client() const { return m_client; }
103     void clearClient() { m_client = nullptr; }
104
105     DownloadID pendingDownloadID() const { return m_pendingDownloadID; }
106     PendingDownload* pendingDownload() const { return m_pendingDownload; }
107     void setPendingDownloadID(DownloadID downloadID)
108     {
109         ASSERT(!m_pendingDownloadID.downloadID());
110         ASSERT(downloadID.downloadID());
111         m_pendingDownloadID = downloadID;
112     }
113     void setPendingDownload(PendingDownload& pendingDownload)
114     {
115         ASSERT(!m_pendingDownload);
116         m_pendingDownload = &pendingDownload;
117     }
118
119     virtual void setPendingDownloadLocation(const String& filename, const SandboxExtension::Handle&, bool /*allowOverwrite*/) { m_pendingDownloadLocation = filename; }
120     const String& pendingDownloadLocation() const { return m_pendingDownloadLocation; }
121     bool isDownload() const { return !!m_pendingDownloadID.downloadID(); }
122
123     const WebCore::ResourceRequest& firstRequest() const { return m_firstRequest; }
124     virtual String suggestedFilename() const { return String(); }
125     void setSuggestedFilename(const String& suggestedName) { m_suggestedFilename = suggestedName; }
126     virtual bool allowsSpecificHTTPSCertificateForHost(const WebCore::AuthenticationChallenge&) { return false; }
127     const String& partition() { return m_partition; }
128
129 protected:
130     NetworkDataTask(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::StoredCredentials, bool shouldClearReferrerOnHTTPSToHTTPRedirect);
131
132     enum FailureType {
133         NoFailure,
134         BlockedFailure,
135         InvalidURLFailure
136     };
137     void failureTimerFired();
138     void scheduleFailure(FailureType);
139
140     FailureType m_scheduledFailureType { NoFailure };
141     WebCore::Timer m_failureTimer;
142     Ref<NetworkSession> m_session;
143     NetworkDataTaskClient* m_client { nullptr };
144     PendingDownload* m_pendingDownload { nullptr };
145     DownloadID m_pendingDownloadID;
146     String m_user;
147     String m_password;
148     String m_partition;
149 #if USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
150     WebCore::Credential m_initialCredential;
151 #endif
152     WebCore::StoredCredentials m_storedCredentials { WebCore::DoNotAllowStoredCredentials };
153     String m_lastHTTPMethod;
154     String m_pendingDownloadLocation;
155     WebCore::ResourceRequest m_firstRequest;
156     bool m_shouldClearReferrerOnHTTPSToHTTPRedirect { true };
157     String m_suggestedFilename;
158 };
159
160 } // namespace WebKit
161
162 #endif // USE(NETWORK_SESSION)