Remove Cocoa CFURLConnection loading code
[WebKit-https.git] / Source / WebKitLegacy / mac / Misc / WebDownload.mm
1 /*
2  * Copyright (C) 2005 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  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import <WebKitLegacy/WebDownload.h>
30
31 #import <Foundation/NSURLAuthenticationChallenge.h>
32 #import <WebCore/AuthenticationMac.h>
33 #import <WebCore/Credential.h>
34 #import <WebCore/CredentialStorage.h>
35 #import <WebCore/ProtectionSpace.h>
36 #import <WebKitLegacy/WebPanelAuthenticationHandler.h>
37 #import <pal/spi/cocoa/NSURLDownloadSPI.h>
38 #import <wtf/Assertions.h>
39
40 #import "WebTypesInternal.h"
41
42 using namespace WebCore;
43
44 @class NSURLConnectionDelegateProxy;
45
46 // FIXME: The following are NSURLDownload SPI - it would be nice to not have to override them at 
47 // some point in the future
48 @interface NSURLDownload (WebDownloadCapability)
49 - (id)_initWithLoadingConnection:(NSURLConnection *)connection
50                          request:(NSURLRequest *)request
51                         response:(NSURLResponse *)response
52                         delegate:(id)delegate
53                            proxy:(NSURLConnectionDelegateProxy *)proxy;
54 - (id)_initWithRequest:(NSURLRequest *)request
55               delegate:(id)delegate
56              directory:(NSString *)directory;
57
58 @end
59
60 @interface WebDownloadInternal : NSObject <NSURLDownloadDelegate>
61 {
62 @public
63     id realDelegate;
64 }
65
66 - (void)setRealDelegate:(id)rd;
67
68 @end
69
70 @implementation WebDownloadInternal
71
72 - (void)dealloc
73 {
74     [realDelegate release];
75     [super dealloc];
76 }
77
78 - (void)setRealDelegate:(id)rd
79 {
80     [rd retain];
81     [realDelegate release];
82     realDelegate = rd;
83 }
84
85 - (BOOL)respondsToSelector:(SEL)selector
86 {
87     if (selector == @selector(downloadDidBegin:) ||
88         selector == @selector(download:willSendRequest:redirectResponse:) ||
89         selector == @selector(download:didReceiveResponse:) ||
90         selector == @selector(download:didReceiveDataOfLength:) ||
91         selector == @selector(download:shouldDecodeSourceDataOfMIMEType:) ||
92         selector == @selector(download:decideDestinationWithSuggestedFilename:) ||
93         selector == @selector(download:didCreateDestination:) ||
94         selector == @selector(downloadDidFinish:) ||
95         selector == @selector(download:didFailWithError:)) {
96         return [realDelegate respondsToSelector:selector];
97     }
98
99     return [super respondsToSelector:selector];
100 }
101
102 - (void)downloadDidBegin:(NSURLDownload *)download
103 {
104     [realDelegate downloadDidBegin:download];
105 }
106
107 - (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
108 {
109     return [realDelegate download:download willSendRequest:request redirectResponse:redirectResponse];
110 }
111
112 - (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
113 {
114 #if !PLATFORM(IOS)
115     // Try previously stored credential first.
116     if (![challenge previousFailureCount]) {
117         NSURLCredential *credential = CredentialStorage::defaultCredentialStorage().get(emptyString(), ProtectionSpace([challenge protectionSpace])).nsCredential();
118         if (credential) {
119             [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
120             return;
121         }
122     }
123
124     if ([realDelegate respondsToSelector:@selector(download:didReceiveAuthenticationChallenge:)]) {
125         [realDelegate download:download didReceiveAuthenticationChallenge:challenge];
126     } else {
127         NSWindow *window = nil;
128         if ([realDelegate respondsToSelector:@selector(downloadWindowForAuthenticationSheet:)]) {
129             window = [realDelegate downloadWindowForAuthenticationSheet:(WebDownload *)download];
130         }
131
132         [[WebPanelAuthenticationHandler sharedHandler] startAuthentication:challenge window:window];
133     }
134 #endif
135 }
136
137 - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
138 {
139     [realDelegate download:download didReceiveResponse:response];
140 }
141
142 - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
143 {
144     [realDelegate download:download didReceiveDataOfLength:length];
145 }
146
147 - (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
148 {
149     return [realDelegate download:download shouldDecodeSourceDataOfMIMEType:encodingType];
150 }
151
152 - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
153 {
154     [realDelegate download:download decideDestinationWithSuggestedFilename:filename];
155 }
156
157 - (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
158 {
159     [realDelegate download:download didCreateDestination:path];
160 }
161
162 - (void)downloadDidFinish:(NSURLDownload *)download
163 {
164     [realDelegate downloadDidFinish:download];
165 }
166
167 - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
168 {
169     [realDelegate download:download didFailWithError:error];
170 }
171
172 @end
173
174 @implementation WebDownload
175
176 - (void)_setRealDelegate:(id)delegate
177 {
178     if (_webInternal == nil) {
179         _webInternal = [[WebDownloadInternal alloc] init];
180         [_webInternal setRealDelegate:delegate];
181     } else {
182         ASSERT(_webInternal == delegate);
183     }
184 }
185
186 - (id)init
187 {
188     self = [super init];
189     if (self != nil) {
190         // _webInternal can be set up before init by _setRealDelegate
191         if (_webInternal == nil) {
192             _webInternal = [[WebDownloadInternal alloc] init];
193         }
194     }
195     return self;
196 }
197
198 - (void)dealloc
199 {
200     [_webInternal release];
201     [super dealloc];
202 }
203
204 - (id)initWithRequest:(NSURLRequest *)request delegate:(id<NSURLDownloadDelegate>)delegate
205 {
206     [self _setRealDelegate:delegate];
207     return [super initWithRequest:request delegate:_webInternal];
208 }
209
210 - (id)_initWithLoadingConnection:(NSURLConnection *)connection
211                          request:(NSURLRequest *)request
212                         response:(NSURLResponse *)response
213                         delegate:(id)delegate
214                            proxy:(NSURLConnectionDelegateProxy *)proxy
215 {
216     [self _setRealDelegate:delegate];
217     return [super _initWithLoadingConnection:connection request:request response:response delegate:_webInternal proxy:proxy];
218 }
219
220 - (id)_initWithRequest:(NSURLRequest *)request
221               delegate:(id)delegate
222              directory:(NSString *)directory
223 {
224     [self _setRealDelegate:delegate];
225     return [super _initWithRequest:request delegate:_webInternal directory:directory];
226 }
227
228 @end