debb6f1e5c09b2a071e2a3d7b977545de1d93be8
[WebKit-https.git] / Source / WebKit / NetworkProcess / cocoa / NetworkDataTaskCocoa.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 "NetworkDataTask.h"
31 #include "NetworkLoadParameters.h"
32 #include <WebCore/NetworkLoadMetrics.h>
33 #include <wtf/RetainPtr.h>
34
35 OBJC_CLASS NSURLSessionDataTask;
36
37 namespace WebKit {
38
39 class NetworkSessionCocoa;
40
41 class NetworkDataTaskCocoa final : public NetworkDataTask {
42     friend class NetworkSessionCocoa;
43 public:
44     static Ref<NetworkDataTask> create(NetworkSession& session, NetworkDataTaskClient& client, const WebCore::ResourceRequest& request, uint64_t frameID, uint64_t pageID, WebCore::StoredCredentialsPolicy storedCredentialsPolicy, WebCore::ContentSniffingPolicy shouldContentSniff, WebCore::ContentEncodingSniffingPolicy shouldContentEncodingSniff, bool shouldClearReferrerOnHTTPSToHTTPRedirect, PreconnectOnly shouldPreconnectOnly)
45     {
46         return adoptRef(*new NetworkDataTaskCocoa(session, client, request, frameID, pageID, storedCredentialsPolicy, shouldContentSniff, shouldContentEncodingSniff, shouldClearReferrerOnHTTPSToHTTPRedirect, shouldPreconnectOnly));
47     }
48
49     ~NetworkDataTaskCocoa();
50
51     typedef uint64_t TaskIdentifier;
52
53     void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend);
54     void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&);
55     void didCompleteWithError(const WebCore::ResourceError&, const WebCore::NetworkLoadMetrics&);
56     void didReceiveData(Ref<WebCore::SharedBuffer>&&);
57
58     void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&);
59     void transferSandboxExtensionToDownload(Download&);
60
61     void suspend() override;
62     void cancel() override;
63     void resume() override;
64     void invalidateAndCancel() override { }
65     NetworkDataTask::State state() const override;
66
67     void setPendingDownloadLocation(const String&, SandboxExtension::Handle&&, bool /*allowOverwrite*/) override;
68     String suggestedFilename() const override;
69
70     WebCore::NetworkLoadMetrics& networkLoadMetrics() { return m_networkLoadMetrics; }
71
72     uint64_t frameID() const { return m_frameID; };
73     uint64_t pageID() const { return m_pageID; };
74
75 private:
76     NetworkDataTaskCocoa(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, uint64_t frameID, uint64_t pageID, WebCore::StoredCredentialsPolicy, WebCore::ContentSniffingPolicy, WebCore::ContentEncodingSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, PreconnectOnly);
77
78     bool tryPasswordBasedAuthentication(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&);
79     void applySniffingPoliciesAndBindRequestToInferfaceIfNeeded(NSURLRequest*&, bool shouldContentSniff, bool shouldContentEncodingSniff);
80
81     RefPtr<SandboxExtension> m_sandboxExtension;
82     RetainPtr<NSURLSessionDataTask> m_task;
83     WebCore::NetworkLoadMetrics m_networkLoadMetrics;
84     uint64_t m_frameID;
85     uint64_t m_pageID;
86 };
87
88 WebCore::Credential serverTrustCredential(const WebCore::AuthenticationChallenge&);
89
90 } // namespace WebKit
91
92 #endif // USE(NETWORK_SESSION)