summaryrefslogtreecommitdiffstats
path: root/Source/WebKit2/WebProcess/Downloads/mac/DownloadMac.mm
blob: aacdf0be4d3e96dc0b0b0ef4ce0b7d72a1f724ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#import "config.h"
#import "Download.h"

#import <WebCore/AuthenticationMac.h>
#import <WebCore/BackForwardController.h>
#import <WebCore/HistoryItem.h>
#import <WebCore/NotImplemented.h>
#import <WebCore/Page.h>
#import <WebCore/ResourceHandle.h>
#import <WebCore/ResourceResponse.h>
#import "DataReference.h"
#import "WebPage.h"

@interface NSURLDownload (WebNSURLDownloadDetails)
+(id)_downloadWithLoadingConnection:(NSURLConnection *)connection
                            request:(NSURLRequest *)request
                           response:(NSURLResponse *)r
                           delegate:(id)delegate
                              proxy:(id)proxy;
- (void)_setOriginatingURL:(NSURL *)originatingURL;
@end

@interface WKDownloadAsDelegate : NSObject <NSURLDownloadDelegate> {
    WebKit::Download* _download;
}
- (id)initWithDownload:(WebKit::Download*)download;
- (void)invalidate;
@end

using namespace WebCore;

namespace WebKit {

static KURL originatingURLFromBackForwardList(WebPage *webPage)
{
    if (!webPage)
        return KURL();

    Page* page = webPage->corePage();
    if (!page)
        return KURL();

    KURL originalURL;
    int backCount = page->backForward()->backCount();
    for (int backIndex = 0; backIndex <= backCount; backIndex++) {
        // FIXME: At one point we had code here to check a "was user gesture" flag.
        // Do we need to restore that logic?
        HistoryItem* historyItem = page->backForward()->itemAtIndex(-backIndex);
        if (!historyItem)
            continue;

        originalURL = historyItem->originalURL(); 
        if (!originalURL.isNull()) 
            return originalURL;
    }

    return KURL();
}

static void setOriginalURLForDownload(WebPage *webPage, NSURLDownload *download, const ResourceRequest& initialRequest)
{
    KURL originalURL;
    
    // If there was no referrer, don't traverse the back/forward history
    // since this download was initiated directly. <rdar://problem/5294691>
    if (!initialRequest.httpReferrer().isNull()) {
        // find the first item in the history that was originated by the user
        originalURL = originatingURLFromBackForwardList(webPage);
    }

    if (originalURL.isNull())
        originalURL = initialRequest.url();

    NSURL *originalNSURL = originalURL;

    NSString *scheme = [originalNSURL scheme];
    NSString *host = [originalNSURL host];
    if (scheme && host && [scheme length] && [host length]) {
        NSNumber *port = [originalNSURL port];
        if (port && [port intValue] < 0)
            port = nil;
        RetainPtr<NSString> hostOnlyURLString;
        if (port)
            hostOnlyURLString.adoptNS([[NSString alloc] initWithFormat:@"%@://%@:%d", scheme, host, [port intValue]]);
        else
            hostOnlyURLString.adoptNS([[NSString alloc] initWithFormat:@"%@://%@", scheme, host]);

        RetainPtr<NSURL> hostOnlyURL = [[NSURL alloc] initWithString:hostOnlyURLString.get()];

        ASSERT([download respondsToSelector:@selector(_setOriginatingURL:)]);
        [download _setOriginatingURL:hostOnlyURL.get()];
    }
}

void Download::start(WebPage* initiatingPage)
{
    ASSERT(!m_nsURLDownload);
    ASSERT(!m_delegate);

    m_delegate.adoptNS([[WKDownloadAsDelegate alloc] initWithDownload:this]);
    m_nsURLDownload.adoptNS([[NSURLDownload alloc] initWithRequest:m_request.nsURLRequest() delegate:m_delegate.get()]);

    // FIXME: Allow this to be changed by the client.
    [m_nsURLDownload.get() setDeletesFileUponFailure:NO];

    setOriginalURLForDownload(initiatingPage, m_nsURLDownload.get(), m_request);
}

void Download::startWithHandle(WebPage* initiatingPage, ResourceHandle* handle, const ResourceRequest& initialRequest, const ResourceResponse& response)
{
    ASSERT(!m_nsURLDownload);
    ASSERT(!m_delegate);

    id proxy = handle->releaseProxy();
    ASSERT(proxy);

    m_delegate.adoptNS([[WKDownloadAsDelegate alloc] initWithDownload:this]);
    m_nsURLDownload = [NSURLDownload _downloadWithLoadingConnection:handle->connection()
                                                            request:m_request.nsURLRequest()
                                                           response:response.nsURLResponse()
                                                            delegate:m_delegate.get()
                                                               proxy:proxy];

    // FIXME: Allow this to be changed by the client.
    [m_nsURLDownload.get() setDeletesFileUponFailure:NO];
                                                            
    setOriginalURLForDownload(initiatingPage, m_nsURLDownload.get(), initialRequest);
}

void Download::cancel()
{
    [m_nsURLDownload.get() cancel];

    RetainPtr<NSData> resumeData = [m_nsURLDownload.get() resumeData];
    didCancel(CoreIPC::DataReference(reinterpret_cast<const uint8_t*>([resumeData.get() bytes]), [resumeData.get() length]));
}

void Download::platformInvalidate()
{
    ASSERT(m_nsURLDownload);
    ASSERT(m_delegate);

    [m_delegate.get() invalidate];
    m_delegate = nullptr;
    m_nsURLDownload = nullptr;
}

void Download::didDecideDestination(const String& destination, bool allowOverwrite)
{
}

void Download::platformDidFinish()
{
}

void Download::receivedCredential(const AuthenticationChallenge& authenticationChallenge, const Credential& credential)
{
    [authenticationChallenge.sender() useCredential:mac(credential) forAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
}

void Download::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& authenticationChallenge)
{
    [authenticationChallenge.sender() continueWithoutCredentialForAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
}

void Download::receivedCancellation(const AuthenticationChallenge& authenticationChallenge)
{
    [authenticationChallenge.sender() cancelAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
}

} // namespace WebKit

@implementation WKDownloadAsDelegate

- (id)initWithDownload:(WebKit::Download*)download
{
    self = [super init];
    if (!self)
        return nil;

    _download = download;
    return self;
}

- (void)invalidate
{
    _download = 0;
}

- (void)downloadDidBegin:(NSURLDownload *)download
{
    if (_download)
        _download->didStart();
}

- (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
{
    return request;
}

- (BOOL)download:(NSURLDownload *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    // FIXME: Implement.
    notImplemented();
    return NO;
}

- (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if (_download)
        _download->didReceiveAuthenticationChallenge(core(challenge));
}

- (void)download:(NSURLDownload *)download didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    // FIXME: Implement.
    notImplemented();
}

- (BOOL)downloadShouldUseCredentialStorage:(NSURLDownload *)download
{
    return NO;
}

- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{
    if (_download)
        _download->didReceiveResponse(response);
}

- (void)download:(NSURLDownload *)download willResumeWithResponse:(NSURLResponse *)response fromByte:(long long)startingByte
{
    // FIXME: Implement.
    notImplemented();
}

- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
{
    if (_download)
        _download->didReceiveData(length);
}

- (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
{
    if (_download)
        return _download->shouldDecodeSourceDataOfMIMEType(encodingType);

    return YES;
}

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
    String destination;
    bool allowOverwrite;
    if (_download)
        destination = _download->decideDestinationWithSuggestedFilename(filename, allowOverwrite);

    if (!destination.isNull())
        [download setDestination:destination allowOverwrite:allowOverwrite];
}

- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
{
    if (_download)
        _download->didCreateDestination(path);
}

- (void)downloadDidFinish:(NSURLDownload *)download
{
    if (_download)
        _download->didFinish();
}

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
    if (!_download)
        return;

    RetainPtr<NSData> resumeData = [download resumeData];
    CoreIPC::DataReference dataReference(reinterpret_cast<const uint8_t*>([resumeData.get() bytes]), [resumeData.get() length]);

    _download->didFail(error, dataReference);
}

@end