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
30 OBJC_CLASS NSURLSession;
31 OBJC_CLASS NSURLSessionDataTask;
32 OBJC_CLASS NSOperationQueue;
33 OBJC_CLASS WKNetworkSessionDelegate;
36 #include "DownloadID.h"
37 #include <WebCore/FrameLoaderTypes.h>
38 #include <WebCore/SessionID.h>
39 #include <wtf/HashMap.h>
41 #include <wtf/RefCounted.h>
42 #include <wtf/RetainPtr.h>
43 #include <wtf/WeakPtr.h>
46 class AuthenticationChallenge;
49 class ResourceRequest;
50 class ResourceResponse;
56 enum class AuthenticationChallengeDisposition {
58 PerformDefaultHandling,
65 class NetworkSessionTaskClient {
67 typedef std::function<void(const WebCore::ResourceRequest&)> RedirectCompletionHandler;
68 virtual void willPerformHTTPRedirection(const WebCore::ResourceResponse&, const WebCore::ResourceRequest&, RedirectCompletionHandler) = 0;
69 typedef std::function<void(AuthenticationChallengeDisposition, const WebCore::Credential&)> ChallengeCompletionHandler;
70 virtual void didReceiveChallenge(const WebCore::AuthenticationChallenge&, ChallengeCompletionHandler) = 0;
71 typedef std::function<void(WebCore::PolicyAction)> ResponseCompletionHandler;
72 virtual void didReceiveResponse(const WebCore::ResourceResponse&, ResponseCompletionHandler) = 0;
73 virtual void didReceiveData(RefPtr<WebCore::SharedBuffer>&&) = 0;
74 virtual void didCompleteWithError(const WebCore::ResourceError&) = 0;
75 virtual void didBecomeDownload() = 0;
77 virtual ~NetworkSessionTaskClient() { }
80 class NetworkDataTask : public RefCounted<NetworkDataTask> {
81 friend class NetworkSession;
86 typedef uint64_t TaskIdentifier;
87 TaskIdentifier taskIdentifier();
91 NetworkSessionTaskClient* client() { return m_client; }
92 void clearClient() { m_client = nullptr; }
95 NetworkSession& m_session;
96 NetworkSessionTaskClient* m_client;
98 explicit NetworkDataTask(NetworkSession&, NetworkSessionTaskClient&, RetainPtr<NSURLSessionDataTask>&&);
99 RetainPtr<NSURLSessionDataTask> m_task;
101 explicit NetworkDataTask(NetworkSession&, NetworkSessionTaskClient&);
105 class NetworkSession {
106 friend class NetworkDataTask;
112 NetworkSession(Type, WebCore::SessionID);
115 static NetworkSession& defaultSession();
117 Ref<NetworkDataTask> createDataTaskWithRequest(const WebCore::ResourceRequest&, NetworkSessionTaskClient&);
119 NetworkDataTask* dataTaskForIdentifier(NetworkDataTask::TaskIdentifier);
122 WebCore::SessionID m_sessionID;
123 HashMap<NetworkDataTask::TaskIdentifier, NetworkDataTask*> m_dataTaskMap;
124 HashMap<NetworkDataTask::TaskIdentifier, DownloadID> m_downloadMap;
126 RetainPtr<NSURLSession> m_session;
127 RetainPtr<WKNetworkSessionDelegate> m_sessionDelegate;
131 } // namespace WebKit
133 #endif // NetworkSession_h