Add a separate class for networking related storage
[WebKit-https.git] / Source / WebKit2 / WebProcess / Downloads / mac / DownloadMac.mm
1 /*
2  * Copyright (C) 2010, 2011 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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "Download.h"
28
29 #import <WebCore/AuthenticationChallenge.h>
30 #import <WebCore/AuthenticationMac.h>
31 #import <WebCore/NotImplemented.h>
32 #import <WebCore/ResourceHandle.h>
33 #import <WebCore/ResourceResponse.h>
34 #import "DataReference.h"
35 #import "WebPage.h"
36
37 @interface NSURLDownload (WebNSURLDownloadDetails)
38 +(id)_downloadWithLoadingConnection:(NSURLConnection *)connection
39                             request:(NSURLRequest *)request
40                            response:(NSURLResponse *)r
41                            delegate:(id)delegate
42                               proxy:(id)proxy;
43 @end
44
45 @interface WKDownloadAsDelegate : NSObject <NSURLDownloadDelegate> {
46     WebKit::Download* _download;
47 }
48 - (id)initWithDownload:(WebKit::Download*)download;
49 - (void)invalidate;
50 @end
51
52 using namespace WebCore;
53
54 namespace WebKit {
55
56 void Download::start()
57 {
58     ASSERT(!m_nsURLDownload);
59     ASSERT(!m_delegate);
60
61     m_delegate.adoptNS([[WKDownloadAsDelegate alloc] initWithDownload:this]);
62     m_nsURLDownload.adoptNS([[NSURLDownload alloc] initWithRequest:m_request.nsURLRequest() delegate:m_delegate.get()]);
63
64     // FIXME: Allow this to be changed by the client.
65     [m_nsURLDownload.get() setDeletesFileUponFailure:NO];
66 }
67
68 void Download::startWithHandle(ResourceHandle* handle, const ResourceResponse& response)
69 {
70     ASSERT(!m_nsURLDownload);
71     ASSERT(!m_delegate);
72
73     id proxy = handle->releaseProxy();
74     ASSERT(proxy);
75
76     m_delegate.adoptNS([[WKDownloadAsDelegate alloc] initWithDownload:this]);
77     m_nsURLDownload = [NSURLDownload _downloadWithLoadingConnection:handle->connection()
78                                                             request:m_request.nsURLRequest()
79                                                            response:response.nsURLResponse()
80                                                             delegate:m_delegate.get()
81                                                                proxy:proxy];
82
83     // FIXME: Allow this to be changed by the client.
84     [m_nsURLDownload.get() setDeletesFileUponFailure:NO];
85 }
86
87 void Download::cancel()
88 {
89     [m_nsURLDownload.get() cancel];
90
91     RetainPtr<NSData> resumeData = [m_nsURLDownload.get() resumeData];
92     didCancel(CoreIPC::DataReference(reinterpret_cast<const uint8_t*>([resumeData.get() bytes]), [resumeData.get() length]));
93 }
94
95 void Download::platformInvalidate()
96 {
97     ASSERT(m_nsURLDownload);
98     ASSERT(m_delegate);
99
100     [m_delegate.get() invalidate];
101     m_delegate = nullptr;
102     m_nsURLDownload = nullptr;
103 }
104
105 void Download::didDecideDestination(const String& destination, bool allowOverwrite)
106 {
107 }
108
109 void Download::platformDidFinish()
110 {
111 }
112
113 void Download::receivedCredential(const AuthenticationChallenge& authenticationChallenge, const Credential& credential)
114 {
115     [authenticationChallenge.sender() useCredential:mac(credential) forAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
116 }
117
118 void Download::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& authenticationChallenge)
119 {
120     [authenticationChallenge.sender() continueWithoutCredentialForAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
121 }
122
123 void Download::receivedCancellation(const AuthenticationChallenge& authenticationChallenge)
124 {
125     [authenticationChallenge.sender() cancelAuthenticationChallenge:authenticationChallenge.nsURLAuthenticationChallenge()];
126 }
127
128 } // namespace WebKit
129
130 @implementation WKDownloadAsDelegate
131
132 - (id)initWithDownload:(WebKit::Download*)download
133 {
134     self = [super init];
135     if (!self)
136         return nil;
137
138     _download = download;
139     return self;
140 }
141
142 - (void)invalidate
143 {
144     _download = 0;
145 }
146
147 - (void)downloadDidBegin:(NSURLDownload *)download
148 {
149     if (_download)
150         _download->didStart();
151 }
152
153 - (NSURLRequest *)download:(NSURLDownload *)download willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse
154 {
155     return request;
156 }
157
158 - (BOOL)download:(NSURLDownload *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
159 {
160     // FIXME: Implement.
161     notImplemented();
162     return NO;
163 }
164
165 - (void)download:(NSURLDownload *)download didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
166 {
167     if (_download)
168         _download->didReceiveAuthenticationChallenge(core(challenge));
169 }
170
171 - (void)download:(NSURLDownload *)download didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
172 {
173     // FIXME: Implement.
174     notImplemented();
175 }
176
177 - (BOOL)downloadShouldUseCredentialStorage:(NSURLDownload *)download
178 {
179     return NO;
180 }
181
182 - (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
183 {
184     if (_download)
185         _download->didReceiveResponse(response);
186 }
187
188 - (void)download:(NSURLDownload *)download willResumeWithResponse:(NSURLResponse *)response fromByte:(long long)startingByte
189 {
190     // FIXME: Implement.
191     notImplemented();
192 }
193
194 - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)length
195 {
196     if (_download)
197         _download->didReceiveData(length);
198 }
199
200 - (BOOL)download:(NSURLDownload *)download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType
201 {
202     if (_download)
203         return _download->shouldDecodeSourceDataOfMIMEType(encodingType);
204
205     return YES;
206 }
207
208 - (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
209 {
210     String destination;
211     bool allowOverwrite;
212     if (_download)
213         destination = _download->decideDestinationWithSuggestedFilename(filename, allowOverwrite);
214
215     if (!destination.isNull())
216         [download setDestination:destination allowOverwrite:allowOverwrite];
217 }
218
219 - (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
220 {
221     if (_download)
222         _download->didCreateDestination(path);
223 }
224
225 - (void)downloadDidFinish:(NSURLDownload *)download
226 {
227     if (_download)
228         _download->didFinish();
229 }
230
231 - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
232 {
233     if (!_download)
234         return;
235
236     RetainPtr<NSData> resumeData = [download resumeData];
237     CoreIPC::DataReference dataReference(reinterpret_cast<const uint8_t*>([resumeData.get() bytes]), [resumeData.get() length]);
238
239     _download->didFail(error, dataReference);
240 }
241
242 @end