Use the same cookie storage as WebCore when using NETWORK_SESSION
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / cocoa / NetworkSessionCocoa.mm
index 664d65abfacdc70f987e40e0773e2318492d958a..73371b1b8f8b16fc975b6f7e8edd57d38628e64a 100644 (file)
 
 #if USE(NETWORK_SESSION)
 
+#import "SessionTracker.h"
 #import <Foundation/NSURLSession.h>
-
 #import <WebCore/AuthenticationChallenge.h>
+#import <WebCore/CFNetworkSPI.h>
 #import <WebCore/Credential.h>
 #import <WebCore/ResourceError.h>
 #import <WebCore/ResourceRequest.h>
@@ -134,7 +135,7 @@ static NSURLSessionAuthChallengeDisposition toNSURLSessionAuthChallengeDispositi
         if (auto* client = networkingTask->client()) {
             ASSERT(isMainThread());
             WebCore::ResourceResponse resourceResponse(response);
-            client->didReceiveResponse(response, ^(WebKit::ResponseDisposition responseDisposition)
+            client->didReceiveResponse(resourceResponse, ^(WebKit::ResponseDisposition responseDisposition)
                 {
                     completionHandler(toNSURLSessionResponseDisposition(responseDisposition));
                 }
@@ -183,13 +184,17 @@ NetworkSession::NetworkSession(Type type)
     m_sessionDelegate = adoptNS([[NetworkSessionDelegate alloc] initWithNetworkSession:*this]);
 
     NSURLSessionConfiguration *configuration = configurationForType(type);
+    // FIXME: Use SessionTracker to make sure the correct cookie storage is used once there is more than one NetworkSession.
+    if (auto* session = SessionTracker::session(WebCore::SessionID::defaultSessionID())) {
+        if (CFHTTPCookieStorageRef storage = session->cookieStorage().get())
+            configuration.HTTPCookieStorage = [[[NSHTTPCookieStorage alloc] _initWithCFHTTPCookieStorage:storage] autorelease];
+    }
     m_session = [NSURLSession sessionWithConfiguration:configuration delegate:static_cast<id>(m_sessionDelegate.get()) delegateQueue:[NSOperationQueue mainQueue]];
 }
 
-RefPtr<NetworkingDataTask> NetworkSession::createDataTaskWithRequest(const WebCore::ResourceRequest& request, NetworkSessionTaskClient& client)
+Ref<NetworkingDataTask> NetworkSession::createDataTaskWithRequest(const WebCore::ResourceRequest& request, NetworkSessionTaskClient& client)
 {
-    RefPtr<NetworkingDataTask> task = adoptRef(new NetworkingDataTask(*this, client, [m_session dataTaskWithRequest:request.nsURLRequest(WebCore::UpdateHTTPBody)]));
-    return task;
+    return adoptRef(*new NetworkingDataTask(*this, client, [m_session dataTaskWithRequest:request.nsURLRequest(WebCore::UpdateHTTPBody)]));
 }
 
 NetworkingDataTask* NetworkSession::dataTaskForIdentifier(uint64_t taskIdentifier)