Remove Cocoa CFURLConnection loading code
[WebKit-https.git] / Source / WebCore / platform / network / mac / AuthenticationMac.mm
1 /*
2  * Copyright (C) 2007 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. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25 #import "config.h"
26 #import "AuthenticationMac.h"
27
28 #import "AuthenticationChallenge.h"
29 #import "AuthenticationClient.h"
30 #import "Credential.h"
31 #import <Foundation/NSURLAuthenticationChallenge.h>
32 #import <Foundation/NSURLProtectionSpace.h>
33
34 using namespace WebCore;
35
36 @interface WebCoreAuthenticationClientAsChallengeSender : NSObject <NSURLAuthenticationChallengeSender>
37 {
38     AuthenticationClient* m_client;
39 }
40 - (id)initWithAuthenticationClient:(AuthenticationClient*)client;
41 - (AuthenticationClient*)client;
42 - (void)detachClient;
43 @end
44
45 @implementation WebCoreAuthenticationClientAsChallengeSender
46
47 - (id)initWithAuthenticationClient:(AuthenticationClient*)client
48 {
49     self = [self init];
50     if (!self)
51         return nil;
52     m_client = client;
53     return self;
54 }
55
56 - (AuthenticationClient*)client
57 {
58     return m_client;
59 }
60
61 - (void)detachClient
62 {
63     m_client = 0;
64 }
65
66 - (void)performDefaultHandlingForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
67 {
68     if (m_client)
69         m_client->receivedRequestToPerformDefaultHandling(core(challenge));
70 }
71
72 - (void)rejectProtectionSpaceAndContinueWithChallenge:(NSURLAuthenticationChallenge *)challenge
73 {
74     if (m_client)
75         m_client->receivedChallengeRejection(core(challenge));
76 }
77
78 - (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
79 {
80     if (m_client)
81         m_client->receivedCredential(core(challenge), Credential(credential));
82 }
83
84 - (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
85 {
86     if (m_client)
87         m_client->receivedRequestToContinueWithoutCredential(core(challenge));
88 }
89
90 - (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
91 {
92     if (m_client)
93         m_client->receivedCancellation(core(challenge));
94 }
95
96 @end
97
98 namespace WebCore {
99
100 AuthenticationChallenge::AuthenticationChallenge(const ProtectionSpace& protectionSpace,
101                                                  const Credential& proposedCredential,
102                                                  unsigned previousFailureCount,
103                                                  const ResourceResponse& response,
104                                                  const ResourceError& error)
105     : AuthenticationChallengeBase(protectionSpace,
106                                   proposedCredential,
107                                   previousFailureCount,
108                                   response,
109                                   error)
110 {
111 }
112
113 AuthenticationChallenge::AuthenticationChallenge(NSURLAuthenticationChallenge *challenge)
114     : AuthenticationChallengeBase(ProtectionSpace([challenge protectionSpace]),
115                                   Credential([challenge proposedCredential]),
116                                   [challenge previousFailureCount],
117                                   [challenge failureResponse],
118                                   [challenge error])
119     , m_sender([challenge sender])
120     , m_nsChallenge(challenge)
121 {
122 }
123
124 void AuthenticationChallenge::setAuthenticationClient(AuthenticationClient* client)
125 {
126     if (client) {
127         m_sender = adoptNS([[WebCoreAuthenticationClientAsChallengeSender alloc] initWithAuthenticationClient:client]);
128         if (m_nsChallenge)
129             m_nsChallenge = adoptNS([[NSURLAuthenticationChallenge alloc] initWithAuthenticationChallenge:m_nsChallenge.get() sender:m_sender.get()]);
130     } else {
131         if ([m_sender.get() isMemberOfClass:[WebCoreAuthenticationClientAsChallengeSender class]])
132             [(WebCoreAuthenticationClientAsChallengeSender *)m_sender.get() detachClient];
133     }
134 }
135
136 AuthenticationClient* AuthenticationChallenge::authenticationClient() const
137 {
138     if ([m_sender.get() isMemberOfClass:[WebCoreAuthenticationClientAsChallengeSender class]])
139         return [static_cast<WebCoreAuthenticationClientAsChallengeSender*>(m_sender.get()) client];
140     
141     return nullptr;
142 }
143
144 bool AuthenticationChallenge::platformCompare(const AuthenticationChallenge& a, const AuthenticationChallenge& b)
145 {
146     if (a.sender() != b.sender())
147         return false;
148         
149     if (a.nsURLAuthenticationChallenge() != b.nsURLAuthenticationChallenge())
150         return false;
151
152     return true;
153 }
154
155 NSURLAuthenticationChallenge *mac(const AuthenticationChallenge& coreChallenge)
156 {
157     if (coreChallenge.nsURLAuthenticationChallenge())
158         return coreChallenge.nsURLAuthenticationChallenge();
159         
160     return [[[NSURLAuthenticationChallenge alloc] initWithProtectionSpace:coreChallenge.protectionSpace().nsSpace() proposedCredential:coreChallenge.proposedCredential().nsCredential() previousFailureCount:coreChallenge.previousFailureCount() failureResponse:coreChallenge.failureResponse().nsURLResponse() error:coreChallenge.error() sender:coreChallenge.sender()] autorelease];
161 }
162
163 AuthenticationChallenge core(NSURLAuthenticationChallenge *macChallenge)
164 {
165     return AuthenticationChallenge(macChallenge);
166 }
167
168 } // namespace WebCore