Resource Load Statistics: Block cookies for prevalent resources without user interaction
[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 NSHTTPCookieStorage;
36 OBJC_CLASS NSURLSessionDataTask;
37
38 namespace WebKit {
39
40 class NetworkSessionCocoa;
41
42 class NetworkDataTaskCocoa final : public NetworkDataTask {
43     friend class NetworkSessionCocoa;
44 public:
45     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)
46     {
47         return adoptRef(*new NetworkDataTaskCocoa(session, client, request, frameID, pageID, storedCredentialsPolicy, shouldContentSniff, shouldContentEncodingSniff, shouldClearReferrerOnHTTPSToHTTPRedirect, shouldPreconnectOnly));
48     }
49
50     ~NetworkDataTaskCocoa();
51
52     typedef uint64_t TaskIdentifier;
53
54     void didSendData(uint64_t totalBytesSent, uint64_t totalBytesExpectedToSend);
55     void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&&);
56     void didCompleteWithError(const WebCore::ResourceError&, const WebCore::NetworkLoadMetrics&);
57     void didReceiveData(Ref<WebCore::SharedBuffer>&&);
58
59     void willPerformHTTPRedirection(WebCore::ResourceResponse&&, WebCore::ResourceRequest&&, RedirectCompletionHandler&&);
60     void transferSandboxExtensionToDownload(Download&);
61
62     void suspend() override;
63     void cancel() override;
64     void resume() override;
65     void invalidateAndCancel() override { }
66     NetworkDataTask::State state() const override;
67
68     void setPendingDownloadLocation(const String&, SandboxExtension::Handle&&, bool /*allowOverwrite*/) override;
69     String suggestedFilename() const override;
70
71     WebCore::NetworkLoadMetrics& networkLoadMetrics() { return m_networkLoadMetrics; }
72
73     uint64_t frameID() const { return m_frameID; };
74     uint64_t pageID() const { return m_pageID; };
75
76 private:
77     NetworkDataTaskCocoa(NetworkSession&, NetworkDataTaskClient&, const WebCore::ResourceRequest&, uint64_t frameID, uint64_t pageID, WebCore::StoredCredentialsPolicy, WebCore::ContentSniffingPolicy, WebCore::ContentEncodingSniffingPolicy, bool shouldClearReferrerOnHTTPSToHTTPRedirect, PreconnectOnly);
78
79     bool tryPasswordBasedAuthentication(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler&);
80     void applySniffingPoliciesAndBindRequestToInferfaceIfNeeded(NSURLRequest*&, bool shouldContentSniff, bool shouldContentEncodingSniff);
81
82 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
83     static NSHTTPCookieStorage *statelessCookieStorage();
84     void applyCookieBlockingPolicy(bool shouldBlock);
85     void applyCookiePartitioningPolicy(const String& requiredStoragePartition, const String& currentStoragePartition);
86 #endif
87
88     RefPtr<SandboxExtension> m_sandboxExtension;
89     RetainPtr<NSURLSessionDataTask> m_task;
90     WebCore::NetworkLoadMetrics m_networkLoadMetrics;
91     uint64_t m_frameID;
92     uint64_t m_pageID;
93
94 #if HAVE(CFNETWORK_STORAGE_PARTITIONING)
95     bool m_hasBeenSetToUseStatelessCookieStorage { false };
96 #endif
97 };
98
99 WebCore::Credential serverTrustCredential(const WebCore::AuthenticationChallenge&);
100
101 } // namespace WebKit
102
103 #endif // USE(NETWORK_SESSION)