2
* Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
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 Computer, 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.
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.
29
#import <WebKit/WebDownload.h>
31
#import <Foundation/NSURLAuthenticationChallenge.h>
32
#import <Foundation/NSURLDownload.h>
33
#import <JavaScriptCore/Assertions.h>
34
#import <WebKit/WebPanelAuthenticationHandler.h>
36
#import "WebTypesInternal.h"
38
@class NSURLConnectionDelegateProxy;
40
// FIXME: The following are NSURLDownload SPI - it would be nice to not have to override them at
41
// some point in the future
42
@interface NSURLDownload (WebDownloadCapability)
43
- (id)_initWithLoadingConnection:(NSURLConnection *)connection
44
request:(NSURLRequest *)request
45
response:(NSURLResponse *)response
47
proxy:(NSURLConnectionDelegateProxy *)proxy;
48
- (id)_initWithRequest:(NSURLRequest *)request
50
directory:(NSString *)directory;
53
@interface WebDownloadInternal : NSObject
59
- (void)setRealDelegate:(id)rd;
63
@implementation WebDownloadInternal
67
[realDelegate release];
71
- (void)setRealDelegate:(id)rd
74
[realDelegate release];
78
- (BOOL)respondsToSelector:(SEL)selector
80
if (selector == @selector(downloadDidBegin:) ||
81
selector == @selector(download:willSendRequest:redirectResponse:) ||
82
selector == @selector(download:didReceiveResponse:) ||
83
selector == @selector(download:didReceiveDataOfLength:) ||
84
selector == @selector(download:shouldDecodeSourceDataOfMIMEType:) ||
85
selector == @selector(download:decideDestinationWithSuggestedFilename:) ||
86
selector == @selector(download:didCreateDestination:) ||
87
selector == @selector(downloadDidFinish:) ||
88
selector == @selector(download:didFailWithError:) ||
89
selector == @selector(download:shouldBeginChildDownloadOfSource:delegate:) ||
90
selector == @selector(download:didBeginChildDownload:)) {
91
return [realDelegate respondsToSelector:selector];
94
return [super respondsToSelector:selector];
97
- (void)downloadDidBegin:(NSURLDownload *)download
99
[realDelegate downloadDidBegin:download];
102
- (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
104
return [realDelegate download:download willSendRequest:request redirectResponse:redirectResponse];
107
- (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
109
if ([realDelegate respondsToSelector:@selector(download:didReceiveAuthenticationChallenge:)]) {
110
[realDelegate download:download didReceiveAuthenticationChallenge:challenge];
112
NSWindow *window = nil;
113
if ([realDelegate respondsToSelector:@selector(downloadWindowForAuthenticationSheet:)]) {
114
window = [realDelegate downloadWindowForAuthenticationSheet:(WebDownload *)download];
117
[[WebPanelAuthenticationHandler sharedHandler] startAuthentication:challenge window:window];
121
- (void)download:(NSURLDownload *)download didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
123
if ([realDelegate respondsToSelector:@selector(download:didCancelAuthenticationChallenge:)]) {
124
[realDelegate download:download didCancelAuthenticationChallenge:challenge];
126
[[WebPanelAuthenticationHandler sharedHandler] cancelAuthentication:challenge];
130
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
132
[realDelegate download:download didReceiveResponse:response];
135
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
137
[realDelegate download:download didReceiveDataOfLength:length];
140
- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
142
return [realDelegate download:download shouldDecodeSourceDataOfMIMEType:encodingType];
145
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
147
[realDelegate download:download decideDestinationWithSuggestedFilename:filename];
150
- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
152
[realDelegate download:download didCreateDestination:path];
155
- (void)downloadDidFinish:(NSURLDownload *)download
157
[realDelegate downloadDidFinish:download];
160
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
162
[realDelegate download:download didFailWithError:error];
165
- (NSURLRequest *)download:(NSURLDownload *)download shouldBeginChildDownloadOfSource:(NSURLRequest *)child delegate:(id *)childDelegate
167
return [realDelegate download:download shouldBeginChildDownloadOfSource:child delegate:childDelegate];
170
- (void)download:(NSURLDownload *)parent didBeginChildDownload:(NSURLDownload *)child
172
[realDelegate download:parent didBeginChildDownload:child];
177
@implementation WebDownload
179
- (void)_setRealDelegate:(id)delegate
181
if (_webInternal == nil) {
182
_webInternal = [[WebDownloadInternal alloc] init];
183
[_webInternal setRealDelegate:delegate];
185
ASSERT(_webInternal == delegate);
193
// _webInternal can be set up before init by _setRealDelegate
194
if (_webInternal == nil) {
195
_webInternal = [[WebDownloadInternal alloc] init];
203
[_webInternal release];
207
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate
209
[self _setRealDelegate:delegate];
210
return [super initWithRequest:request delegate:_webInternal];
213
- (id)_initWithLoadingConnection:(NSURLConnection *)connection
214
request:(NSURLRequest *)request
215
response:(NSURLResponse *)response
216
delegate:(id)delegate
217
proxy:(NSURLConnectionDelegateProxy *)proxy
219
[self _setRealDelegate:delegate];
220
return [super _initWithLoadingConnection:connection request:request response:response delegate:_webInternal proxy:proxy];
223
- (id)_initWithRequest:(NSURLRequest *)request
224
delegate:(id)delegate
225
directory:(NSString *)directory
227
[self _setRealDelegate:delegate];
228
return [super _initWithRequest:request delegate:_webInternal directory:directory];
231
- (void)connection:(NSURLConnection *)connection willStopBufferingData:(NSData *)data
233
// NSURLConnection calls this method even if it is not implemented.
234
// This happens because NSURLConnection caches the results of respondsToSelector.
235
// Those results become invalid when the delegate of NSURLConnectionDelegateProxy is changed.
236
// This is a workaround since this problem needs to be fixed in NSURLConnectionDelegateProxy.
237
// <rdar://problem/3913270> NSURLConnection calls unimplemented delegate method in WebDownload