bd77d387ce5436a1a2cbbfce04b6f1daf3385a86
[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/AuthenticationCF.h>
33 #import <WebCore/AuthenticationMac.h>
34 #import <WebCore/Credential.h>
35 #import <WebCore/CredentialStorage.h>
36 #import <WebCore/ProtectionSpace.h>
37 #import <WebKitLegacy/WebPanelAuthenticationHandler.h>
38 #import <pal/spi/cocoa/NSURLDownloadSPI.h>
39 #import <wtf/Assertions.h>
40
41 #import "WebTypesInternal.h"
42
43 #if USE(CFURLCONNECTION)
44 #import <CFNetwork/CFNetwork.h>
45 #import <CFNetwork/CFURLConnection.h>
46 #endif
47
48 using namespace WebCore;
49
50 @class NSURLConnectionDelegateProxy;
51
52 // FIXME: The following are NSURLDownload SPI - it would be nice to not have to override them at 
53 // some point in the future
54 @interface NSURLDownload (WebDownloadCapability)
55 - (id)_initWithLoadingConnection:(NSURLConnection *)connection
56                          request:(NSURLRequest *)request
57                         response:(NSURLResponse *)response
58                         delegate:(id)delegate
59                            proxy:(NSURLConnectionDelegateProxy *)proxy;
60 - (id)_initWithRequest:(NSURLRequest *)request
61               delegate:(id)delegate
62              directory:(NSString *)directory;
63
64 #if USE(CFURLCONNECTION)
65 - (id)_initWithLoadingCFURLConnection:(CFURLConnectionRef)connection request:(CFURLRequestRef)request response:(CFURLResponseRef)response delegate:(id)delegate proxy:(NSURLConnectionDelegateProxy *)proxy;
66 #endif
67
68 @end
69
70 @interface WebDownloadInternal : NSObject <NSURLDownloadDelegate>
71 {
72 @public
73     id realDelegate;
74 }
75
76 - (void)setRealDelegate:(id)rd;
77
78 @end
79
80 @implementation WebDownloadInternal
81
82 - (void)dealloc
83 {
84     [realDelegate release];
85     [super dealloc];
86 }
87
88 - (void)setRealDelegate:(id)rd
89 {
90     [rd retain];
91     [realDelegate release];
92     realDelegate = rd;
93 }
94
95 - (BOOL)respondsToSelector:(SEL)selector
96 {
97     if (selector == @selector(downloadDidBegin:) ||
98         selector == @selector(download:willSendRequest:redirectResponse:) ||
99         selector == @selector(download:didReceiveResponse:) ||
100         selector == @selector(download:didReceiveDataOfLength:) ||
101         selector == @selector(download:shouldDecodeSourceDataOfMIMEType:) ||
102         selector == @selector(download:decideDestinationWithSuggestedFilename:) ||
103         selector == @selector(download:didCreateDestination:) ||
104         selector == @selector(downloadDidFinish:) ||
105         selector == @selector(download:didFailWithError:)) {
106         return [realDelegate respondsToSelector:selector];
107     }
108
109     return [super respondsToSelector:selector];
110 }
111
112 - (void)downloadDidBegin:(NSURLDownload *)download
113 {
114     [realDelegate downloadDidBegin:download];
115 }
116
117 - (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
118 {
119     return [realDelegate download:download willSendRequest:request redirectResponse:redirectResponse];
120 }
121
122 - (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
123 {
124 #if !PLATFORM(IOS)
125     // Try previously stored credential first.
126     if (![challenge previousFailureCount]) {
127         NSURLCredential *credential = CredentialStorage::defaultCredentialStorage().get(emptyString(), ProtectionSpace([challenge protectionSpace])).nsCredential();
128         if (credential) {
129             [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
130             return;
131         }
132     }
133
134     if ([realDelegate respondsToSelector:@selector(download:didReceiveAuthenticationChallenge:)]) {
135         [realDelegate download:download didReceiveAuthenticationChallenge:challenge];
136     } else {
137         NSWindow *window = nil;
138         if ([realDelegate respondsToSelector:@selector(downloadWindowForAuthenticationSheet:)]) {
139             window = [realDelegate downloadWindowForAuthenticationSheet:(WebDownload *)download];
140         }
141
142         [[WebPanelAuthenticationHandler sharedHandler] startAuthentication:challenge window:window];
143     }
144 #endif
145 }
146
147 - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
148 {
149     [realDelegate download:download didReceiveResponse:response];
150 }
151
152 - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
153 {
154     [realDelegate download:download didReceiveDataOfLength:length];
155 }
156
157 - (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
158 {
159     return [realDelegate download:download shouldDecodeSourceDataOfMIMEType:encodingType];
160 }
161
162 - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
163 {
164     [realDelegate download:download decideDestinationWithSuggestedFilename:filename];
165 }
166
167 - (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
168 {
169     [realDelegate download:download didCreateDestination:path];
170 }
171
172 - (void)downloadDidFinish:(NSURLDownload *)download
173 {
174     [realDelegate downloadDidFinish:download];
175 }
176
177 - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
178 {
179     [realDelegate download:download didFailWithError:error];
180 }
181
182 @end
183
184 @implementation WebDownload
185
186 - (void)_setRealDelegate:(id)delegate
187 {
188     if (_webInternal == nil) {
189         _webInternal = [[WebDownloadInternal alloc] init];
190         [_webInternal setRealDelegate:delegate];
191     } else {
192         ASSERT(_webInternal == delegate);
193     }
194 }
195
196 - (id)init
197 {
198     self = [super init];
199     if (self != nil) {
200         // _webInternal can be set up before init by _setRealDelegate
201         if (_webInternal == nil) {
202             _webInternal = [[WebDownloadInternal alloc] init];
203         }
204     }
205     return self;
206 }
207
208 - (void)dealloc
209 {
210     [_webInternal release];
211     [super dealloc];
212 }
213
214 - (id)initWithRequest:(NSURLRequest *)request delegate:(id<NSURLDownloadDelegate>)delegate
215 {
216     [self _setRealDelegate:delegate];
217     return [super initWithRequest:request delegate:_webInternal];
218 }
219
220 - (id)_initWithLoadingConnection:(NSURLConnection *)connection
221                          request:(NSURLRequest *)request
222                         response:(NSURLResponse *)response
223                         delegate:(id)delegate
224                            proxy:(NSURLConnectionDelegateProxy *)proxy
225 {
226     [self _setRealDelegate:delegate];
227     return [super _initWithLoadingConnection:connection request:request response:response delegate:_webInternal proxy:proxy];
228 }
229
230 #if USE(CFURLCONNECTION)
231 - (id)_initWithLoadingCFURLConnection:(CFURLConnectionRef)connection request:(CFURLRequestRef)request response:(CFURLResponseRef)response delegate:(id)delegate proxy:(NSURLConnectionDelegateProxy *)proxy
232 {
233     [self _setRealDelegate:delegate];
234     return [super _initWithLoadingCFURLConnection:connection request:request response:response delegate:_webInternal proxy:proxy];
235 }
236 #endif
237
238 - (id)_initWithRequest:(NSURLRequest *)request
239               delegate:(id)delegate
240              directory:(NSString *)directory
241 {
242     [self _setRealDelegate:delegate];
243     return [super _initWithRequest:request delegate:_webInternal directory:directory];
244 }
245
246 @end