CCDownloader-apple.mm 24.6 KB
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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
/****************************************************************************
 Copyright (c) 2015-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.

 http://www.cocos2d-x.org

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "network/CCDownloader-apple.h"

#include "network/CCDownloader.h"
#include "base/ccUTF8.h"
#include <queue>

////////////////////////////////////////////////////////////////////////////////
//  OC Classes Declaration
#import <Foundation/Foundation.h>

// this wrapper used to wrap C++ class DownloadTask into NSMutableDictionary
@interface DownloadTaskWrapper : NSObject
{
    std::shared_ptr<const cocos2d::network::DownloadTask> _task;
    NSMutableArray *_dataArray;
}
// temp vars for dataTask: didReceivedData callback
@property (nonatomic) int64_t bytesReceived;
@property (nonatomic) int64_t totalBytesReceived;

-(id)init:(std::shared_ptr<const cocos2d::network::DownloadTask>&)t;
-(const cocos2d::network::DownloadTask *)get;
-(void) addData:(NSData*) data;
-(int64_t) transferDataToBuffer:(void*)buffer lengthOfBuffer:(int64_t)len;

@end

@interface DownloaderAppleImpl : NSObject <NSURLSessionDataDelegate, NSURLSessionDownloadDelegate>
{
    const cocos2d::network::DownloaderApple *_outer;
    cocos2d::network::DownloaderHints _hints;
    std::queue<NSURLSessionTask*> _taskQueue;
}
@property (nonatomic, strong) NSURLSession *downloadSession;
@property (nonatomic, strong) NSMutableDictionary *taskDict;    // ocTask: DownloadTaskWrapper

-(id)init:(const cocos2d::network::DownloaderApple *)o hints:(const cocos2d::network::DownloaderHints&) hints;
-(const cocos2d::network::DownloaderHints&)getHints;
-(NSURLSessionDataTask *)createDataTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task;
-(NSURLSessionDownloadTask *)createFileTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task;
-(void)doDestroy;

@end

////////////////////////////////////////////////////////////////////////////////
//  C++ Classes Implementation

namespace cocos2d { namespace network {

    struct DownloadTaskApple : public IDownloadTask
    {
        DownloadTaskApple()
        : dataTask(nil)
        , downloadTask(nil)
        {
            DLLOG("Construct DownloadTaskApple %p", this);
        }

        virtual ~DownloadTaskApple()
        {
            DLLOG("Destruct DownloadTaskApple %p", this);
        }

        NSURLSessionDataTask *dataTask;
        NSURLSessionDownloadTask *downloadTask;
    };
#define DeclareDownloaderImplVar DownloaderAppleImpl *impl = (__bridge DownloaderAppleImpl *)_impl
    // the _impl's type is id, we should convert it to subclass before call it's methods
    DownloaderApple::DownloaderApple(const DownloaderHints& hints)
    : _impl(nil)
    {
        DLLOG("Construct DownloaderApple %p", this);
        _impl = (__bridge void*)[[DownloaderAppleImpl alloc] init: this hints:hints];
    }

    DownloaderApple::~DownloaderApple()
    {
        DeclareDownloaderImplVar;
        [impl doDestroy];
        DLLOG("Destruct DownloaderApple %p", this);
    }
    IDownloadTask *DownloaderApple::createCoTask(std::shared_ptr<const DownloadTask>& task)
    {
        DownloadTaskApple* coTask = new (std::nothrow) DownloadTaskApple();
        DeclareDownloaderImplVar;
        if (task->storagePath.length())
        {
            coTask->downloadTask = [impl createFileTask:task];
        }
        else
        {
            coTask->dataTask = [impl createDataTask:task];
        }
        return coTask;
    }
}}  // namespace cocos2d::network

////////////////////////////////////////////////////////////////////////////////
//  OC Classes Implementation
@implementation DownloadTaskWrapper

- (id)init: (std::shared_ptr<const cocos2d::network::DownloadTask>&)t
{
    DLLOG("Construct DonloadTaskWrapper %p", self);
    _dataArray = [NSMutableArray arrayWithCapacity:8];
    [_dataArray retain];
    _task = t;
    return self;
}

-(const cocos2d::network::DownloadTask *)get
{
    return _task.get();
}

-(void) addData:(NSData*) data
{
    [_dataArray addObject:data];
    self.bytesReceived += data.length;
    self.totalBytesReceived += data.length;
}

-(int64_t) transferDataToBuffer:(void*)buffer lengthOfBuffer:(int64_t)len
{
    int64_t bytesReceived = 0;
    int receivedDataObject = 0;

    __block char *p = (char *)buffer;
    for (NSData* data in _dataArray)
    {
        // check
        if (bytesReceived + data.length > len)
        {
            break;
        }

        // copy data
        [data enumerateByteRangesUsingBlock:^(const void *bytes,
                                              NSRange byteRange,
                                              BOOL *stop)
         {
             memcpy(p, bytes, byteRange.length);
             p += byteRange.length;
             *stop = NO;
         }];

        // accumulate
        bytesReceived += data.length;
        ++receivedDataObject;
    }

    // remove receivedNSDataObject from dataArray
    [_dataArray removeObjectsInRange:NSMakeRange(0, receivedDataObject)];
    self.bytesReceived -= bytesReceived;
    return bytesReceived;
}

-(void)dealloc
{
    [_dataArray release];
    [super dealloc];
    DLLOG("Destruct DownloadTaskWrapper %p", self);
}

@end

@implementation DownloaderAppleImpl

- (id)init: (const cocos2d::network::DownloaderApple*)o hints:(const cocos2d::network::DownloaderHints&) hints
{
    DLLOG("Construct DownloaderAppleImpl %p", self);
    // save outer task ref
    _outer = o;
    _hints = hints;

    // create task dictionary
    self.taskDict = [NSMutableDictionary dictionary];

    // create download session
    NSURLSessionConfiguration *defaultConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    self.downloadSession = [NSURLSession sessionWithConfiguration:defaultConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];
//    self.downloadSession.sessionDescription = kCurrentSession;
    return self;
}

-(const cocos2d::network::DownloaderHints&)getHints
{
    return _hints;
}

-(NSURLSessionDataTask *)createDataTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task
{
    const char *urlStr = task->requestURL.c_str();
    DLLOG("DownloaderAppleImpl createDataTask: %s", urlStr);
    NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:urlStr]];
    NSURLRequest *request = nil;
    if (_hints.timeoutInSeconds > 0)
    {
        request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:(NSTimeInterval)_hints.timeoutInSeconds];
    }
    else
    {
        request = [NSURLRequest requestWithURL:url];
    }
    NSURLSessionDataTask *ocTask = [self.downloadSession dataTaskWithRequest:request];
    DownloadTaskWrapper* taskWrapper = [[DownloadTaskWrapper alloc] init:task];
    [self.taskDict setObject:taskWrapper forKey:ocTask];
    [taskWrapper release];

    if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
        [ocTask resume];
        _taskQueue.push(nil);
    } else {
        _taskQueue.push(ocTask);
    }
    return ocTask;
};

-(NSURLSessionDownloadTask *)createFileTask:(std::shared_ptr<const cocos2d::network::DownloadTask>&) task
{
    const char *urlStr = task->requestURL.c_str();
    DLLOG("DownloaderAppleImpl createFileTask: %s", urlStr);
    NSURL *url = [NSURL URLWithString:[NSString stringWithUTF8String:urlStr]];
    NSURLRequest *request = nil;
    if (_hints.timeoutInSeconds > 0)
    {
        request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:(NSTimeInterval)_hints.timeoutInSeconds];
    }
    else
    {
        request = [NSURLRequest requestWithURL:url];
    }
    NSString *tempFilePath = [NSString stringWithFormat:@"%s%s", task->storagePath.c_str(), _hints.tempFileNameSuffix.c_str()];
    NSData *resumeData = [NSData dataWithContentsOfFile:tempFilePath];
    NSURLSessionDownloadTask *ocTask = nil;
    if (resumeData)
    {
        ocTask = [self.downloadSession downloadTaskWithResumeData:resumeData];
    }
    else
    {
        ocTask = [self.downloadSession downloadTaskWithRequest:request];
    }

    DownloadTaskWrapper* taskWrapper = [[DownloadTaskWrapper alloc] init:task];
    [self.taskDict setObject:taskWrapper forKey:ocTask];
    [taskWrapper release];

    if (_taskQueue.size() < _hints.countOfMaxProcessingTasks) {
        [ocTask resume];
        _taskQueue.push(nil);
    } else {
        _taskQueue.push(ocTask);
    }
    return ocTask;
};

-(void)doDestroy
{
    // cancel all download task
    NSEnumerator * enumeratorKey = [self.taskDict keyEnumerator];
    for (NSURLSessionDownloadTask *task in enumeratorKey)
    {
        DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];

        // no resume support for a data task
        std::string storagePath = [wrapper get]->storagePath;
        if(storagePath.length() == 0) {
            [task cancel];
        }
        else {
            [task cancelByProducingResumeData:^(NSData *resumeData) {
                if (resumeData)
                {
                    NSString *tempFilePath = [NSString stringWithFormat:@"%s%s", storagePath.c_str(), _hints.tempFileNameSuffix.c_str()];
                    NSString *tempFileDir = [tempFilePath stringByDeletingLastPathComponent];
                    NSFileManager *fileManager = [NSFileManager defaultManager];
                    BOOL isDir = false;
                    if ([fileManager fileExistsAtPath:tempFileDir isDirectory:&isDir])
                    {
                        if (NO == isDir)
                        {
                            // TODO: the directory is a file, not a directory, how to echo to developer?
                            DLLOG("DownloaderAppleImpl temp dir is a file!");
                            return;
                        }
                    }
                    else
                    {
                        NSURL *tempFileURL = [NSURL fileURLWithPath:tempFileDir];
                        if (NO == [fileManager createDirectoryAtURL:tempFileURL withIntermediateDirectories:YES attributes:nil error:nil])
                        {
                            // create directory failed
                            DLLOG("DownloaderAppleImpl create temp dir failed");
                            return;
                        }
                    }

                    [resumeData writeToFile:tempFilePath atomically:YES];
                }
            }];
        }
    }
    _outer = nullptr;
    
    while(!_taskQueue.empty())
        _taskQueue.pop();
    
    [self.downloadSession invalidateAndCancel];
    [self release];
}

-(void)dealloc
{
    DLLOG("Destruct DownloaderAppleImpl %p", self);
    self.downloadSession = nil;
    [super dealloc];
}
#pragma mark - NSURLSessionTaskDelegate methods

//@optional

/* An HTTP request is attempting to perform a redirection to a different
 * URL. You must invoke the completion routine to allow the
 * redirection, allow the redirection with a modified request, or
 * pass nil to the completionHandler to cause the body of the redirection
 * response to be delivered as the payload of this request. The default
 * is to follow redirections.
 *
 * For tasks in background sessions, redirections will always be followed and this method will not be called.
 */
//- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
//willPerformHTTPRedirection:(NSHTTPURLResponse *)response
//        newRequest:(NSURLRequest *)request
// completionHandler:(void (^)(NSURLRequest *))completionHandler;

/* The task has received a request specific authentication challenge.
 * If this delegate is not implemented, the session specific authentication challenge
 * will *NOT* be called and the behavior will be the same as using the default handling
 * disposition.
 */
//- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
//didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
// completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler;

/* Sent if a task requires a new, unopened body stream.  This may be
 * necessary when authentication has failed for any request that
 * involves a body stream.
 */
//- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
// needNewBodyStream:(void (^)(NSInputStream *bodyStream))completionHandler;

/* Sent periodically to notify the delegate of upload progress.  This
 * information is also available as properties of the task.
 */
//- (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
//                                 didSendBodyData:(int64_t)bytesSent
//                                  totalBytesSent:(int64_t)totalBytesSent
//                        totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend;

/* Sent as the last message related to a specific task.  Error may be
 * nil, which implies that no error occurred and this task is complete.
 */
- (void)URLSession:(NSURLSession *)session task :(NSURLSessionTask *)task
                            didCompleteWithError:(NSError *)error
{
    DLLOG("DownloaderAppleImpl task: \"%s\" didCompleteWithError: %d errDesc: %s"
          , [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]
          , (error ? (int)error.code: 0)
          , [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding]);

    // clean wrapper C++ object
    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:task];

    if(_outer)
    {
        if(error)
        {
            std::vector<unsigned char> buf; // just a placeholder
            _outer->onTaskFinish(*[wrapper get],
                             cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
                             (int)error.code,
                             [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding],
                             buf);
        }
        else if (![wrapper get]->storagePath.length())
        {
            // call onTaskFinish for a data task
            // (for a file download task, callback is called in didFinishDownloadingToURL)
            std::string errorString;

            const int64_t buflen = [wrapper totalBytesReceived];
            std::vector<unsigned char> data((size_t)buflen);
            char* buf = (char*)data.data();
            [wrapper transferDataToBuffer:buf lengthOfBuffer:buflen];

            _outer->onTaskFinish(*[wrapper get],
                                 cocos2d::network::DownloadTask::ERROR_NO_ERROR,
                                 0,
                                 errorString,
                                 data);
        }
        else
        {
            NSInteger statusCode = ((NSHTTPURLResponse*)task.response).statusCode;

            // Check for error status code
            if (statusCode >= 400)
            {
                std::vector<unsigned char> buf; // just a placeholder
                const char *originalURL = [task.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding];
                std::string errorMessage = cocos2d::StringUtils::format("Downloader: Failed to download %s with status code (%d)", originalURL, (int)statusCode);

                _outer->onTaskFinish(*[wrapper get],
                                     cocos2d::network::DownloadTask::ERROR_IMPL_INTERNAL,
                                     0,
                                     errorMessage,
                                     buf);
            }
        }
    }
    [self.taskDict removeObjectForKey:task];

    while (!_taskQueue.empty() && _taskQueue.front() == nil) {
        _taskQueue.pop();
    }
    if (!_taskQueue.empty()) {
        [_taskQueue.front() resume];
        _taskQueue.pop();
    }
}

#pragma mark - NSURLSessionDataDelegate methods
//@optional
/* The task has received a response and no further messages will be
 * received until the completion block is called. The disposition
 * allows you to cancel a request or to turn a data task into a
 * download task. This delegate message is optional - if you do not
 * implement it, you can get the response as a property of the task.
 *
 * This method will not be called for background upload tasks (which cannot be converted to download tasks).
 */
//- (void)URLSession:(NSURLSession *)session dataTask :(NSURLSessionDataTask *)dataTask
//                                  didReceiveResponse:(NSURLResponse *)response
//                                   completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
//{
//    DLLOG("DownloaderAppleImpl dataTask: response:%s", [response.description cStringUsingEncoding:NSUTF8StringEncoding]);
//    completionHandler(NSURLSessionResponseAllow);
//}

/* Notification that a data task has become a download task.  No
 * future messages will be sent to the data task.
 */
//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
//didBecomeDownloadTask:(NSURLSessionDownloadTask *)downloadTask;

/* Sent when data is available for the delegate to consume.  It is
 * assumed that the delegate will retain and not copy the data.  As
 * the data may be discontiguous, you should use
 * [NSData enumerateByteRangesUsingBlock:] to access it.
 */
- (void)URLSession:(NSURLSession *)session dataTask :(NSURLSessionDataTask *)dataTask
                                      didReceiveData:(NSData *)data
{
    DLLOG("DownloaderAppleImpl dataTask: \"%s\" didReceiveDataLen %d",
          [dataTask.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding],
          (int)data.length);
    if (nullptr == _outer)
    {
        return;
    }
    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:dataTask];
    [wrapper addData:data];

    std::function<int64_t(void *, int64_t)> transferDataToBuffer =
    [wrapper](void *buffer, int64_t bufLen)->int64_t
    {
        return [wrapper transferDataToBuffer:buffer lengthOfBuffer: bufLen];
    };

    _outer->onTaskProgress(*[wrapper get],
                          wrapper.bytesReceived,
                          wrapper.totalBytesReceived,
                          dataTask.countOfBytesExpectedToReceive,
                          transferDataToBuffer);
}

/* Invoke the completion routine with a valid NSCachedURLResponse to
 * allow the resulting data to be cached, or pass nil to prevent
 * caching. Note that there is no guarantee that caching will be
 * attempted for a given resource, and you should not rely on this
 * message to receive the resource data.
 */
//- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
// willCacheResponse:(NSCachedURLResponse *)proposedResponse
// completionHandler:(void (^)(NSCachedURLResponse *cachedResponse))completionHandler;

#pragma mark - NSURLSessionDownloadDelegate methods

/* Sent when a download task that has completed a download.  The delegate should
 * copy or move the file at the given location to a new location as it will be
 * removed when the delegate message returns. URLSession:task:didCompleteWithError: will
 * still be called.
 */
- (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTask *)downloadTask
                               didFinishDownloadingToURL:(NSURL *)location
{
    DLLOG("DownloaderAppleImpl downloadTask: \"%s\" didFinishDownloadingToURL %s",
          [downloadTask.originalRequest.URL.absoluteString cStringUsingEncoding:NSUTF8StringEncoding],
          [location.absoluteString cStringUsingEncoding:NSUTF8StringEncoding]);
    if (nullptr == _outer)
    {
        return;
    }

    // On iOS 9 a response with status code 4xx(Client Error) or 5xx(Server Error)
    // might end up calling this delegate method, saving the error message to the storage path
    // and treating this download task as a successful one, so we need to check the status code here
    NSInteger statusCode = ((NSHTTPURLResponse*)downloadTask.response).statusCode;
    if (statusCode >= 400)
    {
        return;
    }

    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:downloadTask];
    const char * storagePath = [wrapper get]->storagePath.c_str();
    NSString *destPath = [NSString stringWithUTF8String:storagePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *destURL = nil;

    do
    {
        if ([destPath hasPrefix:@"file://"])
        {
            break;
        }

        if ('/' == [destPath characterAtIndex:0])
        {
            destURL = [NSURL fileURLWithPath:destPath];
            break;
        }

        // relative path, store to user domain default
        NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
        NSURL *documentsDirectory = URLs[0];
        destURL = [documentsDirectory URLByAppendingPathComponent:destPath];
    } while (0);

    // Make sure we overwrite anything that's already there
    [fileManager removeItemAtURL:destURL error:NULL];

    // copy file to dest location
    int errorCode = cocos2d::network::DownloadTask::ERROR_NO_ERROR;
    int errorCodeInternal = 0;
    std::string errorString;

    NSError *error = nil;
    if ([fileManager copyItemAtURL:location toURL:destURL error:&error])
    {
        // success, remove temp file if it exist
        if (_hints.tempFileNameSuffix.length())
        {
            NSString *tempStr = [[destURL absoluteString] stringByAppendingFormat:@"%s", _hints.tempFileNameSuffix.c_str()];
            NSURL *tempDestUrl = [NSURL URLWithString:tempStr];
            [fileManager removeItemAtURL:tempDestUrl error:NULL];
        }
    }
    else
    {
        errorCode = cocos2d::network::DownloadTask::ERROR_FILE_OP_FAILED;
        if (error)
        {
            errorCodeInternal = (int)error.code;
            errorString = [error.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding];
        }
    }

    std::vector<unsigned char> buf; // just a placeholder
    _outer->onTaskFinish(*[wrapper get], errorCode, errorCodeInternal, errorString, buf);
}

// @optional
/* Sent periodically to notify the delegate of download progress. */
- (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTask *)downloadTask
                                            didWriteData:(int64_t)bytesWritten
                                       totalBytesWritten:(int64_t)totalBytesWritten
                               totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
//    NSLog(@"DownloaderAppleImpl downloadTask: \"%@\" received: %lld total: %lld", downloadTask.originalRequest.URL, totalBytesWritten, totalBytesExpectedToWrite);

    if (nullptr == _outer || totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown)
    {
        return;
    }
    DownloadTaskWrapper *wrapper = [self.taskDict objectForKey:downloadTask];

    std::function<int64_t(void *, int64_t)> transferDataToBuffer;   // just a placeholder
    _outer->onTaskProgress(*[wrapper get], bytesWritten, totalBytesWritten, totalBytesExpectedToWrite, transferDataToBuffer);
}

/* Sent when a download has been resumed. If a download failed with an
 * error, the -userInfo dictionary of the error will contain an
 * NSURLSessionDownloadTaskResumeData key, whose value is the resume
 * data.
 */
- (void)URLSession:(NSURLSession *)session downloadTask :(NSURLSessionDownloadTask *)downloadTask
                                       didResumeAtOffset:(int64_t)fileOffset
                                      expectedTotalBytes:(int64_t)expectedTotalBytes
{
    NSLog(@"[TODO]DownloaderAppleImpl downloadTask: \"%@\" didResumeAtOffset: %lld", downloadTask.originalRequest.URL, fileOffset);
    // 下载失败
//    self.downloadFail([self getDownloadRespose:XZDownloadFail identifier:self.identifier progress:0.00 downloadUrl:nil downloadSaveFileUrl:nil downloadData:nil downloadResult:@"下载失败"]);
}

@end