2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef NetworkSession_h
27 #define NetworkSession_h
29 OBJC_CLASS NSURLSession;
30 OBJC_CLASS NSURLSessionDataTask;
31 OBJC_CLASS NSOperationQueue;
32 OBJC_CLASS WKNetworkSessionDelegate;
34 #include <WebCore/FrameLoaderTypes.h>
35 #include <WebCore/SessionID.h>
36 #include <wtf/HashMap.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/RetainPtr.h>
40 #include <wtf/WeakPtr.h>
43 class AuthenticationChallenge;
46 class ResourceRequest;
47 class ResourceResponse;
53 enum class AuthenticationChallengeDisposition {
55 PerformDefaultHandling,
62 class NetworkSessionTaskClient {
64 typedef std::function<void(const WebCore::ResourceRequest&)> RedirectCompletionHandler;
65 virtual void willPerformHTTPRedirection(const WebCore::ResourceResponse&, const WebCore::ResourceRequest&, RedirectCompletionHandler) = 0;
66 typedef std::function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)> ChallengeCompletionHandler;
67 virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler) = 0;
68 typedef std::function<void(WebCore::PolicyAction)> ResponseCompletionHandler;
69 virtual void didReceiveResponse(const WebCore::ResourceResponse&, ResponseCompletionHandler) = 0;
70 virtual void didReceiveData(RefPtr<WebCore::SharedBuffer>&&) = 0;
71 virtual void didCompleteWithError(const WebCore::ResourceError&) = 0;
72 virtual void didBecomeDownload() = 0;
74 virtual ~NetworkSessionTaskClient() { }
77 class NetworkDataTask : public RefCounted<NetworkDataTask> {
78 friend class NetworkSession;
83 uint64_t taskIdentifier();
87 NetworkSessionTaskClient* client() { return m_client; }
88 void clearClient() { m_client = nullptr; }
91 NetworkSession& m_session;
92 NetworkSessionTaskClient* m_client;
94 explicit NetworkDataTask(NetworkSession&, NetworkSessionTaskClient&, RetainPtr<NSURLSessionDataTask>&&);
95 RetainPtr<NSURLSessionDataTask> m_task;
97 explicit NetworkDataTask(NetworkSession&, NetworkSessionTaskClient&);
101 class NetworkSession {
102 friend class NetworkDataTask;
108 NetworkSession(Type, WebCore::SessionID);
111 static NetworkSession& defaultSession();
113 Ref<NetworkDataTask> createDataTaskWithRequest(const WebCore::ResourceRequest&, NetworkSessionTaskClient&);
115 NetworkDataTask* dataTaskForIdentifier(uint64_t);
118 WebCore::SessionID m_sessionID;
119 HashMap<uint64_t, NetworkDataTask*> m_dataTaskMap;
121 RetainPtr<NSURLSession> m_session;
122 RetainPtr<WKNetworkSessionDelegate> m_sessionDelegate;
126 } // namespace WebKit
128 #endif // NetworkSession_h