01770472787eff3c967c1d3edfd9c1f8045f4eef
[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/NetworkStorageSession.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 using namespace WebCore;
44
45 @interface WebDownloadInternal : NSObject <NSURLDownloadDelegate> {
46     id realDelegate;
47 }
48 - (void)setRealDelegate:(id)realDelegate;
49 @end
50
51 @implementation WebDownloadInternal
52
53 - (void)dealloc
54 {
55     [realDelegate release];
56     [super dealloc];
57 }
58
59 - (void)setRealDelegate:(id)rd
60 {
61     [rd retain];
62     [realDelegate release];
63     realDelegate = rd;
64 }
65
66 - (BOOL)respondsToSelector:(SEL)selector
67 {
68     if (selector == @selector(downloadDidBegin:) ||
69         selector == @selector(download:willSendRequest:redirectResponse:) ||
70         selector == @selector(download:didReceiveResponse:) ||
71         selector == @selector(download:didReceiveDataOfLength:) ||
72         selector == @selector(download:shouldDecodeSourceDataOfMIMEType:) ||
73         selector == @selector(download:decideDestinationWithSuggestedFilename:) ||
74         selector == @selector(download:didCreateDestination:) ||
75         selector == @selector(downloadDidFinish:) ||
76         selector == @selector(download:didFailWithError:)) {
77         return [realDelegate respondsToSelector:selector];
78     }
79
80     return [super respondsToSelector:selector];
81 }
82
83 - (void)downloadDidBegin:(NSURLDownload *)download
84 {
85     [realDelegate downloadDidBegin:download];
86 }
87
88 - (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
89 {
90     return [realDelegate download:download willSendRequest:request redirectResponse:redirectResponse];
91 }
92
93 - (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
94 {
95 #if !PLATFORM(IOS_FAMILY)
96     // Try previously stored credential first.
97     if (![challenge previousFailureCount]) {
98         NSURLCredential *credential = NetworkStorageSession::defaultStorageSession().credentialStorage().get(emptyString(), ProtectionSpace([challenge protectionSpace])).nsCredential();
99         if (credential) {
100             [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
101             return;
102         }
103     }
104
105     if ([realDelegate respondsToSelector:@selector(download:didReceiveAuthenticationChallenge:)]) {
106         [realDelegate download:download didReceiveAuthenticationChallenge:challenge];
107     } else {
108         NSWindow *window = nil;
109         if ([realDelegate respondsToSelector:@selector(downloadWindowForAuthenticationSheet:)]) {
110             window = [realDelegate downloadWindowForAuthenticationSheet:(WebDownload *)download];
111         }
112
113         [[WebPanelAuthenticationHandler sharedHandler] startAuthentication:challenge window:window];
114     }
115 #endif
116 }
117
118 - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
119 {
120     [realDelegate download:download didReceiveResponse:response];
121 }
122
123 - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
124 {
125     [realDelegate download:download didReceiveDataOfLength:length];
126 }
127
128 - (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
129 {
130     return [realDelegate download:download shouldDecodeSourceDataOfMIMEType:encodingType];
131 }
132
133 - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
134 {
135     [realDelegate download:download decideDestinationWithSuggestedFilename:filename];
136 }
137
138 - (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
139 {
140     [realDelegate download:download didCreateDestination:path];
141 }
142
143 - (void)downloadDidFinish:(NSURLDownload *)download
144 {
145     [realDelegate downloadDidFinish:download];
146 }
147
148 - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
149 {
150     [realDelegate download:download didFailWithError:error];
151 }
152
153 @end
154
155 @implementation WebDownload
156
157 - (void)_setRealDelegate:(id)delegate
158 {
159     if (_webInternal == nil) {
160         _webInternal = [[WebDownloadInternal alloc] init];
161         [_webInternal setRealDelegate:delegate];
162     } else {
163         ASSERT(_webInternal == delegate);
164     }
165 }
166
167 - (id)init
168 {
169     self = [super init];
170     if (self != nil) {
171         // _webInternal can be set up before init by _setRealDelegate
172         if (_webInternal == nil) {
173             _webInternal = [[WebDownloadInternal alloc] init];
174         }
175     }
176     return self;
177 }
178
179 - (void)dealloc
180 {
181     [_webInternal release];
182     [super dealloc];
183 }
184
185 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
186 - (id)initWithRequest:(NSURLRequest *)request delegate:(id<NSURLDownloadDelegate>)delegate
187 IGNORE_WARNINGS_END
188 {
189     [self _setRealDelegate:delegate];
190     return [super initWithRequest:request delegate:_webInternal];
191 }
192
193 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
194 - (id)_initWithLoadingConnection:(NSURLConnection *)connection
195                          request:(NSURLRequest *)request
196                         response:(NSURLResponse *)response
197                         delegate:(id)delegate
198                            proxy:(NSURLConnectionDelegateProxy *)proxy
199 IGNORE_WARNINGS_END
200 {
201     [self _setRealDelegate:delegate];
202     return [super _initWithLoadingConnection:connection request:request response:response delegate:_webInternal proxy:proxy];
203 }
204
205 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
206 - (id)_initWithRequest:(NSURLRequest *)request
207               delegate:(id)delegate
208              directory:(NSString *)directory
209 IGNORE_WARNINGS_END
210 {
211     [self _setRealDelegate:delegate];
212     return [super _initWithRequest:request delegate:_webInternal directory:directory];
213 }
214
215 @end