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