[SOUP] Add NetworkSession implementation and switch to use it
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / NetworkSession.h
1 /*
2  * Copyright (C) 2015 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 #if PLATFORM(COCOA)
31 OBJC_CLASS NSURLSession;
32 OBJC_CLASS NSOperationQueue;
33 OBJC_CLASS WKNetworkSessionDelegate;
34 #endif
35
36 #include "DownloadID.h"
37 #include "NetworkDataTask.h"
38 #include <WebCore/SessionID.h>
39 #include <wtf/HashMap.h>
40 #include <wtf/HashSet.h>
41 #include <wtf/Ref.h>
42 #include <wtf/RefCounted.h>
43
44 #if USE(SOUP)
45 typedef struct _SoupSession SoupSession;
46 #endif
47
48 namespace WebCore {
49 class NetworkStorageSession;
50 }
51
52 namespace WebKit {
53
54 class CustomProtocolManager;
55
56 class NetworkSession : public RefCounted<NetworkSession> {
57     friend class NetworkDataTask;
58 public:
59     enum class Type {
60         Normal,
61         Ephemeral
62     };
63
64     static Ref<NetworkSession> create(Type, WebCore::SessionID, CustomProtocolManager*);
65     static NetworkSession& defaultSession();
66     ~NetworkSession();
67
68     void invalidateAndCancel();
69
70     WebCore::SessionID sessionID() const { return m_sessionID; }
71
72 #if USE(SOUP)
73     SoupSession* soupSession() const;
74 #endif
75
76 #if PLATFORM(COCOA)
77     // Must be called before any NetworkSession has been created.
78     static void setCustomProtocolManager(CustomProtocolManager*);
79     static void setSourceApplicationAuditTokenData(RetainPtr<CFDataRef>&&);
80     static void setSourceApplicationBundleIdentifier(const String&);
81     static void setSourceApplicationSecondaryIdentifier(const String&);
82 #if PLATFORM(IOS)
83     static void setCTDataConnectionServiceType(const String&);
84 #endif
85 #endif
86
87     void clearCredentials();
88 #if PLATFORM(COCOA)
89     NetworkDataTask* dataTaskForIdentifier(NetworkDataTask::TaskIdentifier, WebCore::StoredCredentials);
90
91     void addDownloadID(NetworkDataTask::TaskIdentifier, DownloadID);
92     DownloadID downloadID(NetworkDataTask::TaskIdentifier);
93     DownloadID takeDownloadID(NetworkDataTask::TaskIdentifier);
94 #endif
95
96 private:
97     NetworkSession(Type, WebCore::SessionID, CustomProtocolManager*);
98     WebCore::NetworkStorageSession& networkStorageSession() const;
99
100     WebCore::SessionID m_sessionID;
101
102 #if PLATFORM(COCOA)
103     HashMap<NetworkDataTask::TaskIdentifier, NetworkDataTask*> m_dataTaskMapWithCredentials;
104     HashMap<NetworkDataTask::TaskIdentifier, NetworkDataTask*> m_dataTaskMapWithoutCredentials;
105     HashMap<NetworkDataTask::TaskIdentifier, DownloadID> m_downloadMap;
106
107     RetainPtr<NSURLSession> m_sessionWithCredentialStorage;
108     RetainPtr<WKNetworkSessionDelegate> m_sessionWithCredentialStorageDelegate;
109     RetainPtr<NSURLSession> m_sessionWithoutCredentialStorage;
110     RetainPtr<WKNetworkSessionDelegate> m_sessionWithoutCredentialStorageDelegate;
111 #elif USE(SOUP)
112     HashSet<NetworkDataTask*> m_dataTaskSet;
113 #endif
114 };
115
116 } // namespace WebKit
117
118 #endif // USE(NETWORK_SESSION)