1 /*
2 * Copyright (C) 2007 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "WebKitDLL.h"
28 #include "WebDownload.h"
29
30 #include "CString.h"
31 #include "DefaultDownloadDelegate.h"
32 #include "MarshallingHelpers.h"
33 #include "WebError.h"
34 #include "WebKit.h"
35 #include "WebKitLogging.h"
36 #include "WebMutableURLRequest.h"
37 #include "WebURLAuthenticationChallenge.h"
38 #include "WebURLCredential.h"
39 #include "WebURLResponse.h"
40
41 #include <wtf/platform.h>
42
43 #include <io.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46
47 #pragma warning(push, 0)
48 #include <WebCore/AuthenticationCF.h>
49 #include <WebCore/BString.h>
50 #include <WebCore/ResourceError.h>
51 #include <WebCore/ResourceHandle.h>
52 #include <WebCore/ResourceRequest.h>
53 #include <WebCore/ResourceResponse.h>
54 #include <wtf/CurrentTime.h>
55 #pragma warning(pop)
56
57 using namespace WebCore;
58
59 // CFURLDownload Callbacks ----------------------------------------------------------------
60 static void didStartCallback(CFURLDownloadRef download, const void *clientInfo);
61 static CFURLRequestRef willSendRequestCallback(CFURLDownloadRef download, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo);
62 static void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef download, CFURLAuthChallengeRef challenge, const void *clientInfo);
63 static void didReceiveResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, const void *clientInfo);
64 static void willResumeWithResponseCallback(CFURLDownloadRef download, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo);
65 static void didReceiveDataCallback(CFURLDownloadRef download, CFIndex length, const void *clientInfo);
66 static Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef download, CFStringRef encodingType, const void *clientInfo);
67 static void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef download, CFStringRef objectName, const void *clientInfo);
68 static void didCreateDestinationCallback(CFURLDownloadRef download, CFURLRef path, const void *clientInfo);
69 static void didFinishCallback(CFURLDownloadRef download, const void *clientInfo);
70 static void didFailCallback(CFURLDownloadRef download, CFErrorRef error, const void *clientInfo);
71
init(ResourceHandle * handle,const ResourceRequest & request,const ResourceResponse & response,IWebDownloadDelegate * delegate)72 void WebDownload::init(ResourceHandle* handle, const ResourceRequest& request, const ResourceResponse& response, IWebDownloadDelegate* delegate)
73 {
74 m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
75 CFURLConnectionRef connection = handle->connection();
76 if (!connection) {
77 LOG_ERROR("WebDownload::WebDownload(ResourceHandle*,...) called with an inactive ResourceHandle");
78 return;
79 }
80
81 CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
82 didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
83 decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
84
85 m_request.adoptRef(WebMutableURLRequest::createInstance(request));
86 m_download.adoptCF(CFURLDownloadCreateAndStartWithLoadingConnection(0, connection, request.cfURLRequest(), response.cfURLResponse(), &client));
87
88 // It is possible for CFURLDownloadCreateAndStartWithLoadingConnection() to fail if the passed in CFURLConnection is not in a "downloadable state"
89 // However, we should never hit that case
90 if (!m_download) {
91 ASSERT_NOT_REACHED();
92 LOG_ERROR("WebDownload - Failed to create WebDownload from existing connection (%s)", request.url().string().utf8().data());
93 } else
94 LOG(Download, "WebDownload - Created WebDownload %p from existing connection (%s)", this, request.url().string().utf8().data());
95
96 // The CFURLDownload either starts successfully and retains the CFURLConnection,
97 // or it fails to creating and we have a now-useless connection with a dangling ref.
98 // Either way, we need to release the connection to balance out ref counts
99 handle->releaseConnectionForDownload();
100 CFRelease(connection);
101 }
102
init(const KURL & url,IWebDownloadDelegate * delegate)103 void WebDownload::init(const KURL& url, IWebDownloadDelegate* delegate)
104 {
105 m_delegate = delegate ? delegate : DefaultDownloadDelegate::sharedInstance();
106 LOG_ERROR("Delegate is %p", m_delegate.get());
107
108 ResourceRequest request(url);
109 CFURLRequestRef cfRequest = request.cfURLRequest();
110
111 CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
112 didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
113 decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
114 m_request.adoptRef(WebMutableURLRequest::createInstance(request));
115 m_download.adoptCF(CFURLDownloadCreate(0, cfRequest, &client));
116
117 CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
118 CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
119
120 LOG(Download, "WebDownload - Initialized download of url %s in WebDownload %p", url.string().utf8().data(), this);
121 }
122
123 // IWebDownload -------------------------------------------------------------------
124
initWithRequest(IWebURLRequest * request,IWebDownloadDelegate * delegate)125 HRESULT STDMETHODCALLTYPE WebDownload::initWithRequest(
126 /* [in] */ IWebURLRequest* request,
127 /* [in] */ IWebDownloadDelegate* delegate)
128 {
129 COMPtr<WebMutableURLRequest> webRequest;
130 if (!request || FAILED(request->QueryInterface(&webRequest))) {
131 LOG(Download, "WebDownload - initWithRequest failed - not a WebMutableURLRequest");
132 return E_FAIL;
133 }
134
135 if (!delegate)
136 return E_FAIL;
137 m_delegate = delegate;
138 LOG(Download, "Delegate is %p", m_delegate.get());
139
140 RetainPtr<CFURLRequestRef> cfRequest = webRequest->resourceRequest().cfURLRequest();
141
142 CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
143 didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
144 decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
145 m_request.adoptRef(WebMutableURLRequest::createInstance(webRequest.get()));
146 m_download.adoptCF(CFURLDownloadCreate(0, cfRequest.get(), &client));
147
148 // If for some reason the download failed to create,
149 // we have particular cleanup to do
150 if (!m_download) {
151 m_request = 0;
152 return E_FAIL;
153 }
154
155 CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
156 CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
157
158 LOG(Download, "WebDownload - initWithRequest complete, started download of url %s", webRequest->resourceRequest().url().string().utf8().data());
159 return S_OK;
160 }
161
initToResumeWithBundle(BSTR bundlePath,IWebDownloadDelegate * delegate)162 HRESULT STDMETHODCALLTYPE WebDownload::initToResumeWithBundle(
163 /* [in] */ BSTR bundlePath,
164 /* [in] */ IWebDownloadDelegate* delegate)
165 {
166 LOG(Download, "Attempting resume of download bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
167
168 RetainPtr<CFDataRef> resumeData(AdoptCF, extractResumeDataFromBundle(String(bundlePath, SysStringLen(bundlePath))));
169
170 if (!resumeData)
171 return E_FAIL;
172
173 if (!delegate)
174 return E_FAIL;
175 m_delegate = delegate;
176 LOG(Download, "Delegate is %p", m_delegate.get());
177
178 CFURLDownloadClient client = {0, this, 0, 0, 0, didStartCallback, willSendRequestCallback, didReceiveAuthenticationChallengeCallback,
179 didReceiveResponseCallback, willResumeWithResponseCallback, didReceiveDataCallback, shouldDecodeDataOfMIMETypeCallback,
180 decideDestinationWithSuggestedObjectNameCallback, didCreateDestinationCallback, didFinishCallback, didFailCallback};
181
182 RetainPtr<CFURLRef> pathURL(AdoptCF, MarshallingHelpers::PathStringToFileCFURLRef(String(bundlePath, SysStringLen(bundlePath))));
183 ASSERT(pathURL);
184
185 m_download.adoptCF(CFURLDownloadCreateWithResumeData(0, resumeData.get(), pathURL.get(), &client));
186
187 if (!m_download) {
188 LOG(Download, "Failed to create CFURLDownloadRef for resume");
189 return E_FAIL;
190 }
191
192 m_bundlePath = String(bundlePath, SysStringLen(bundlePath));
193 // Attempt to remove the ".download" extension from the bundle for the final file destination
194 // Failing that, we clear m_destination and will ask the delegate later once the download starts
195 if (m_bundlePath.endsWith(bundleExtension(), false)) {
196 m_destination = m_bundlePath.copy();
197 m_destination.truncate(m_destination.length() - bundleExtension().length());
198 } else
199 m_destination = String();
200
201 CFURLDownloadScheduleWithCurrentMessageQueue(m_download.get());
202 CFURLDownloadScheduleDownloadWithRunLoop(m_download.get(), ResourceHandle::loaderRunLoop(), kCFRunLoopDefaultMode);
203
204 LOG(Download, "WebDownload - initWithRequest complete, resumed download of bundle %s", String(bundlePath, SysStringLen(bundlePath)).ascii().data());
205 return S_OK;
206 }
207
start()208 HRESULT STDMETHODCALLTYPE WebDownload::start()
209 {
210 LOG(Download, "WebDownload - Starting download (%p)", this);
211 if (!m_download)
212 return E_FAIL;
213
214 CFURLDownloadStart(m_download.get());
215 // FIXME: 4950477 - CFURLDownload neglects to make the didStart() client call upon starting the download.
216 // This is a somewhat critical call, so we'll fake it for now!
217 didStart();
218
219 return S_OK;
220 }
221
cancel()222 HRESULT STDMETHODCALLTYPE WebDownload::cancel()
223 {
224 LOG(Download, "WebDownload - Cancelling download (%p)", this);
225 if (!m_download)
226 return E_FAIL;
227
228 CFURLDownloadCancel(m_download.get());
229 m_download = 0;
230 return S_OK;
231 }
232
cancelForResume()233 HRESULT STDMETHODCALLTYPE WebDownload::cancelForResume()
234 {
235 LOG(Download, "WebDownload - Cancelling download (%p), writing resume information to file if possible", this);
236 ASSERT(m_download);
237 if (!m_download)
238 return E_FAIL;
239
240 HRESULT hr = S_OK;
241 RetainPtr<CFDataRef> resumeData;
242 if (m_destination.isEmpty()) {
243 CFURLDownloadCancel(m_download.get());
244 goto exit;
245 }
246
247 CFURLDownloadSetDeletesUponFailure(m_download.get(), false);
248 CFURLDownloadCancel(m_download.get());
249
250 resumeData = CFURLDownloadCopyResumeData(m_download.get());
251 if (!resumeData) {
252 LOG(Download, "WebDownload - Unable to create resume data for download (%p)", this);
253 goto exit;
254 }
255
256 appendResumeDataToBundle(resumeData.get(), m_bundlePath);
257
258 exit:
259 m_download = 0;
260 return hr;
261 }
262
deletesFileUponFailure(BOOL * result)263 HRESULT STDMETHODCALLTYPE WebDownload::deletesFileUponFailure(
264 /* [out, retval] */ BOOL* result)
265 {
266 if (!m_download)
267 return E_FAIL;
268 *result = CFURLDownloadDeletesUponFailure(m_download.get());
269 return S_OK;
270 }
271
setDeletesFileUponFailure(BOOL deletesFileUponFailure)272 HRESULT STDMETHODCALLTYPE WebDownload::setDeletesFileUponFailure(
273 /* [in] */ BOOL deletesFileUponFailure)
274 {
275 if (!m_download)
276 return E_FAIL;
277 CFURLDownloadSetDeletesUponFailure(m_download.get(), !!deletesFileUponFailure);
278 return S_OK;
279 }
280
setDestination(BSTR path,BOOL allowOverwrite)281 HRESULT STDMETHODCALLTYPE WebDownload::setDestination(
282 /* [in] */ BSTR path,
283 /* [in] */ BOOL allowOverwrite)
284 {
285 if (!m_download)
286 return E_FAIL;
287
288 m_destination = String(path, SysStringLen(path));
289 m_bundlePath = m_destination + bundleExtension();
290
291 CFURLRef pathURL = MarshallingHelpers::PathStringToFileCFURLRef(m_bundlePath);
292 CFURLDownloadSetDestination(m_download.get(), pathURL, !!allowOverwrite);
293 CFRelease(pathURL);
294
295 LOG(Download, "WebDownload - Set destination to %s", m_bundlePath.ascii().data());
296
297 return S_OK;
298 }
299
300 // IWebURLAuthenticationChallengeSender -------------------------------------------------------------------
301
cancelAuthenticationChallenge(IWebURLAuthenticationChallenge *)302 HRESULT STDMETHODCALLTYPE WebDownload::cancelAuthenticationChallenge(
303 /* [in] */ IWebURLAuthenticationChallenge*)
304 {
305 if (m_download) {
306 CFURLDownloadCancel(m_download.get());
307 m_download = 0;
308 }
309
310 // FIXME: Do we need a URL or description for this error code?
311 ResourceError error(String(WebURLErrorDomain), WebURLErrorUserCancelledAuthentication, "", "");
312 COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(error));
313 m_delegate->didFailWithError(this, webError.get());
314
315 return S_OK;
316 }
317
continueWithoutCredentialForAuthenticationChallenge(IWebURLAuthenticationChallenge * challenge)318 HRESULT STDMETHODCALLTYPE WebDownload::continueWithoutCredentialForAuthenticationChallenge(
319 /* [in] */ IWebURLAuthenticationChallenge* challenge)
320 {
321 COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
322 if (!webChallenge)
323 return E_NOINTERFACE;
324
325 if (m_download)
326 CFURLDownloadUseCredential(m_download.get(), 0, webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
327 return S_OK;
328 }
329
useCredential(IWebURLCredential * credential,IWebURLAuthenticationChallenge * challenge)330 HRESULT STDMETHODCALLTYPE WebDownload::useCredential(
331 /* [in] */ IWebURLCredential* credential,
332 /* [in] */ IWebURLAuthenticationChallenge* challenge)
333 {
334 COMPtr<WebURLAuthenticationChallenge> webChallenge(Query, challenge);
335 if (!webChallenge)
336 return E_NOINTERFACE;
337
338 COMPtr<WebURLCredential> webCredential(Query, credential);
339 if (!webCredential)
340 return E_NOINTERFACE;
341
342 RetainPtr<CFURLCredentialRef> cfCredential(AdoptCF, createCF(webCredential->credential()));
343
344 if (m_download)
345 CFURLDownloadUseCredential(m_download.get(), cfCredential.get(), webChallenge->authenticationChallenge().cfURLAuthChallengeRef());
346 return S_OK;
347 }
348
349 // CFURLDownload Callbacks -------------------------------------------------------------------
didStart()350 void WebDownload::didStart()
351 {
352 #ifndef NDEBUG
353 m_startTime = m_dataTime = currentTime();
354 m_received = 0;
355 LOG(Download, "DOWNLOAD - Started %p at %.3f seconds", this, m_startTime);
356 #endif
357 if (FAILED(m_delegate->didBegin(this)))
358 LOG_ERROR("DownloadDelegate->didBegin failed");
359 }
360
willSendRequest(CFURLRequestRef request,CFURLResponseRef response)361 CFURLRequestRef WebDownload::willSendRequest(CFURLRequestRef request, CFURLResponseRef response)
362 {
363 COMPtr<WebMutableURLRequest> webRequest(AdoptCOM, WebMutableURLRequest::createInstance(ResourceRequest(request)));
364 COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
365 COMPtr<IWebMutableURLRequest> finalRequest;
366
367 if (FAILED(m_delegate->willSendRequest(this, webRequest.get(), webResponse.get(), &finalRequest)))
368 LOG_ERROR("DownloadDelegate->willSendRequest failed");
369
370 if (!finalRequest)
371 return 0;
372
373 COMPtr<WebMutableURLRequest> finalWebRequest(AdoptCOM, WebMutableURLRequest::createInstance(finalRequest.get()));
374 m_request = finalWebRequest.get();
375 CFURLRequestRef result = finalWebRequest->resourceRequest().cfURLRequest();
376 CFRetain(result);
377 return result;
378 }
379
didReceiveAuthenticationChallenge(CFURLAuthChallengeRef challenge)380 void WebDownload::didReceiveAuthenticationChallenge(CFURLAuthChallengeRef challenge)
381 {
382 // Try previously stored credential first.
383 if (!CFURLAuthChallengeGetPreviousFailureCount(challenge)) {
384 CFURLCredentialRef credential = WebCoreCredentialStorage::get(CFURLAuthChallengeGetProtectionSpace(challenge));
385 if (credential) {
386 CFURLDownloadUseCredential(m_download.get(), credential, challenge);
387 return;
388 }
389 }
390
391 COMPtr<IWebURLAuthenticationChallenge> webChallenge(AdoptCOM,
392 WebURLAuthenticationChallenge::createInstance(AuthenticationChallenge(challenge, 0), this));
393
394 if (SUCCEEDED(m_delegate->didReceiveAuthenticationChallenge(this, webChallenge.get())))
395 return;
396
397 cancelAuthenticationChallenge(webChallenge.get());
398 }
399
didReceiveResponse(CFURLResponseRef response)400 void WebDownload::didReceiveResponse(CFURLResponseRef response)
401 {
402 COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
403 if (FAILED(m_delegate->didReceiveResponse(this, webResponse.get())))
404 LOG_ERROR("DownloadDelegate->didReceiveResponse failed");
405 }
406
willResumeWithResponse(CFURLResponseRef response,UInt64 fromByte)407 void WebDownload::willResumeWithResponse(CFURLResponseRef response, UInt64 fromByte)
408 {
409 COMPtr<WebURLResponse> webResponse(AdoptCOM, WebURLResponse::createInstance(ResourceResponse(response)));
410 if (FAILED(m_delegate->willResumeWithResponse(this, webResponse.get(), fromByte)))
411 LOG_ERROR("DownloadDelegate->willResumeWithResponse failed");
412 }
413
didReceiveData(CFIndex length)414 void WebDownload::didReceiveData(CFIndex length)
415 {
416 #ifndef NDEBUG
417 m_received += length;
418 double current = currentTime();
419 if (current - m_dataTime > 2.0)
420 LOG(Download, "DOWNLOAD - %p hanged for %.3f seconds - Received %i bytes for a total of %i", this, current - m_dataTime, length, m_received);
421 m_dataTime = current;
422 #endif
423 if (FAILED(m_delegate->didReceiveDataOfLength(this, length)))
424 LOG_ERROR("DownloadDelegate->didReceiveData failed");
425 }
426
shouldDecodeDataOfMIMEType(CFStringRef mimeType)427 bool WebDownload::shouldDecodeDataOfMIMEType(CFStringRef mimeType)
428 {
429 BOOL result;
430 if (FAILED(m_delegate->shouldDecodeSourceDataOfMIMEType(this, BString(mimeType), &result))) {
431 LOG_ERROR("DownloadDelegate->shouldDecodeSourceDataOfMIMEType failed");
432 return false;
433 }
434 return !!result;
435 }
436
decideDestinationWithSuggestedObjectName(CFStringRef name)437 void WebDownload::decideDestinationWithSuggestedObjectName(CFStringRef name)
438 {
439 if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, BString(name))))
440 LOG_ERROR("DownloadDelegate->decideDestinationWithSuggestedObjectName failed");
441 }
442
didCreateDestination(CFURLRef destination)443 void WebDownload::didCreateDestination(CFURLRef destination)
444 {
445 // The concept of the ".download bundle" is internal to the WebDownload, so therefore
446 // we try to mask the delegate from its existence as much as possible by telling it the final
447 // destination was created, when in reality the bundle was created
448
449 String createdDestination = MarshallingHelpers::FileCFURLRefToPathString(destination);
450
451 // At this point in receiving CFURLDownload callbacks, we should definitely have the bundle path stored locally
452 // and it should match with the file that CFURLDownload created
453 ASSERT(createdDestination == m_bundlePath);
454 // And we should also always have the final-destination stored
455 ASSERT(!m_destination.isEmpty());
456
457 BString path(m_destination);
458 if (FAILED(m_delegate->didCreateDestination(this, path)))
459 LOG_ERROR("DownloadDelegate->didCreateDestination failed");
460 }
461
didFinish()462 void WebDownload::didFinish()
463 {
464 #ifndef NDEBUG
465 LOG(Download, "DOWNLOAD - Finished %p after %i bytes and %.3f seconds", this, m_received, currentTime() - m_startTime);
466 #endif
467
468 ASSERT(!m_bundlePath.isEmpty() && !m_destination.isEmpty());
469 LOG(Download, "WebDownload - Moving file from bundle %s to destination %s", m_bundlePath.ascii().data(), m_destination.ascii().data());
470
471 // We try to rename the bundle to the final file name. If that fails, we give the delegate one more chance to chose
472 // the final file name, then we just leave it
473 if (!MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0)) {
474 LOG_ERROR("Failed to move bundle %s to %s on completion\nError - %i", m_bundlePath.ascii().data(), m_destination.ascii().data(), GetLastError());
475
476 bool reportBundlePathAsFinalPath = true;
477
478 BString destinationBSTR(m_destination.characters(), m_destination.length());
479 if (FAILED(m_delegate->decideDestinationWithSuggestedFilename(this, destinationBSTR)))
480 LOG_ERROR("delegate->decideDestinationWithSuggestedFilename() failed");
481
482 // The call to m_delegate->decideDestinationWithSuggestedFilename() should have changed our destination, so we'll try the move
483 // one last time.
484 if (!m_destination.isEmpty())
485 if (MoveFileEx(m_bundlePath.charactersWithNullTermination(), m_destination.charactersWithNullTermination(), 0))
486 reportBundlePathAsFinalPath = false;
487
488 // We either need to tell the delegate our final filename is the bundle filename, or is the file name they just told us to use
489 if (reportBundlePathAsFinalPath) {
490 BString bundleBSTR(m_bundlePath);
491 m_delegate->didCreateDestination(this, bundleBSTR);
492 } else {
493 BString finalDestinationBSTR = BString(m_destination);
494 m_delegate->didCreateDestination(this, finalDestinationBSTR);
495 }
496 }
497
498 // It's extremely likely the call to delegate->didFinish() will deref this, so lets not let that cause our destruction just yet
499 COMPtr<WebDownload> protect = this;
500 if (FAILED(m_delegate->didFinish(this)))
501 LOG_ERROR("DownloadDelegate->didFinish failed");
502
503 m_download = 0;
504 }
505
didFail(CFErrorRef error)506 void WebDownload::didFail(CFErrorRef error)
507 {
508 COMPtr<WebError> webError(AdoptCOM, WebError::createInstance(ResourceError(error)));
509 if (FAILED(m_delegate->didFailWithError(this, webError.get())))
510 LOG_ERROR("DownloadDelegate->didFailWithError failed");
511 }
512
513 // CFURLDownload Callbacks ----------------------------------------------------------------
didStartCallback(CFURLDownloadRef,const void * clientInfo)514 void didStartCallback(CFURLDownloadRef, const void *clientInfo)
515 { ((WebDownload*)clientInfo)->didStart(); }
516
willSendRequestCallback(CFURLDownloadRef,CFURLRequestRef request,CFURLResponseRef redirectionResponse,const void * clientInfo)517 CFURLRequestRef willSendRequestCallback(CFURLDownloadRef, CFURLRequestRef request, CFURLResponseRef redirectionResponse, const void *clientInfo)
518 { return ((WebDownload*)clientInfo)->willSendRequest(request, redirectionResponse); }
519
didReceiveAuthenticationChallengeCallback(CFURLDownloadRef,CFURLAuthChallengeRef challenge,const void * clientInfo)520 void didReceiveAuthenticationChallengeCallback(CFURLDownloadRef, CFURLAuthChallengeRef challenge, const void *clientInfo)
521 { ((WebDownload*)clientInfo)->didReceiveAuthenticationChallenge(challenge); }
522
didReceiveResponseCallback(CFURLDownloadRef,CFURLResponseRef response,const void * clientInfo)523 void didReceiveResponseCallback(CFURLDownloadRef, CFURLResponseRef response, const void *clientInfo)
524 { ((WebDownload*)clientInfo)->didReceiveResponse(response); }
525
willResumeWithResponseCallback(CFURLDownloadRef,CFURLResponseRef response,UInt64 startingByte,const void * clientInfo)526 void willResumeWithResponseCallback(CFURLDownloadRef, CFURLResponseRef response, UInt64 startingByte, const void *clientInfo)
527 { ((WebDownload*)clientInfo)->willResumeWithResponse(response, startingByte); }
528
didReceiveDataCallback(CFURLDownloadRef,CFIndex length,const void * clientInfo)529 void didReceiveDataCallback(CFURLDownloadRef, CFIndex length, const void *clientInfo)
530 { ((WebDownload*)clientInfo)->didReceiveData(length); }
531
shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef,CFStringRef encodingType,const void * clientInfo)532 Boolean shouldDecodeDataOfMIMETypeCallback(CFURLDownloadRef, CFStringRef encodingType, const void *clientInfo)
533 { return ((WebDownload*)clientInfo)->shouldDecodeDataOfMIMEType(encodingType); }
534
decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef,CFStringRef objectName,const void * clientInfo)535 void decideDestinationWithSuggestedObjectNameCallback(CFURLDownloadRef, CFStringRef objectName, const void *clientInfo)
536 { ((WebDownload*)clientInfo)->decideDestinationWithSuggestedObjectName(objectName); }
537
didCreateDestinationCallback(CFURLDownloadRef,CFURLRef path,const void * clientInfo)538 void didCreateDestinationCallback(CFURLDownloadRef, CFURLRef path, const void *clientInfo)
539 { ((WebDownload*)clientInfo)->didCreateDestination(path); }
540
didFinishCallback(CFURLDownloadRef,const void * clientInfo)541 void didFinishCallback(CFURLDownloadRef, const void *clientInfo)
542 { ((WebDownload*)clientInfo)->didFinish(); }
543
didFailCallback(CFURLDownloadRef,CFErrorRef error,const void * clientInfo)544 void didFailCallback(CFURLDownloadRef, CFErrorRef error, const void *clientInfo)
545 { ((WebDownload*)clientInfo)->didFail(error); }
546