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
40namespace WebCore {
41class AuthenticationChallenge;
42class ResourceError;
43class ResourceResponse;
44class SharedBuffer;
45}
46
47namespace WebKit {
48
49class NetworkLoadParameters;
50class NetworkSession;
51class PendingDownload;
52enum class AuthenticationChallengeDisposition : uint8_t;
53
54using RedirectCompletionHandler = CompletionHandler<void(WebCore::ResourceRequest&&)>;
55using ChallengeCompletionHandler = CompletionHandler<void(AuthenticationChallengeDisposition, const WebCore::Credential&)>;
56using ResponseCompletionHandler = CompletionHandler<void(WebCore::PolicyAction)>;
57
58class NetworkDataTaskClient {
59public:
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 virtual void wasBlockedByRestrictions() = 0;
69
70 virtual bool shouldCaptureExtraNetworkLoadMetrics() const { return false; }
71
72 void didCompleteWithError(const WebCore::ResourceError& error)
73 {
74 WebCore::NetworkLoadMetrics emptyMetrics;
75 didCompleteWithError(error, emptyMetrics);
76 }
77
78 virtual ~NetworkDataTaskClient() { }
79};
80
81class NetworkDataTask : public RefCounted<NetworkDataTask>, public CanMakeWeakPtr<NetworkDataTask> {
82public:
83 static Ref<NetworkDataTask> create(NetworkSession&, NetworkDataTaskClient&, const NetworkLoadParameters&);
84
85 virtual ~NetworkDataTask();
86
87 virtual void cancel() = 0;
88 virtual void resume() = 0;
89 virtual void invalidateAndCancel() = 0;
90
91 void didReceiveResponse(WebCore::ResourceResponse&&, ResponseCompletionHandler&&);
92 bool shouldCaptureExtraNetworkLoadMetrics() const;
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, 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 const String& partition() { return m_partition; }
127
128 bool isTopLevelNavigation() const { return m_dataTaskIsForMainFrameNavigation; }
129
130 virtual String description() const;
131
132protected:
133 NetworkDataTask(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, WebCore::StoredCredentialsPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, bool dataTaskIsForMainFrameNavigation);
134
135 enum FailureType {
136 NoFailure,
137 BlockedFailure,
138 InvalidURLFailure,
139 RestrictedURLFailure
140 };
141 void failureTimerFired();
142 void scheduleFailure(FailureType);
143
144 FailureType m_scheduledFailureType { NoFailure };
145 WebCore::Timer m_failureTimer;
146 Ref<NetworkSession> m_session;
147 NetworkDataTaskClient* m_client { nullptr };
148 PendingDownload* m_pendingDownload { nullptr };
149 DownloadID m_pendingDownloadID;
150 String m_user;
151 String m_password;
152 String m_partition;
153#if USE(CREDENTIAL_STORAGE_WITH_NETWORK_SESSION)
154 WebCore::Credential m_initialCredential;
155#endif
156 WebCore::StoredCredentialsPolicy m_storedCredentialsPolicy { WebCore::StoredCredentialsPolicy::DoNotUse };
157 String m_lastHTTPMethod;
158 String m_pendingDownloadLocation;
159 WebCore::ResourceRequest m_firstRequest;
160 bool m_shouldClearReferrerOnHTTPSToHTTPRedirect { true };
161 String m_suggestedFilename;
162 bool m_dataTaskIsForMainFrameNavigation { false };
163};
164
165} // namespace WebKit
166