| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "Downloader.h" | 5 #import "Downloader.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 | 8 |
| 9 @implementation Downloader | 9 @implementation Downloader |
| 10 | 10 |
| 11 @synthesize delegate = delegate_; | 11 @synthesize delegate = delegate_; |
| 12 | 12 |
| 13 + (NSString*)getChromeDownloadFilePath { | |
| 14 NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains( | |
| 15 NSDownloadsDirectory, NSUserDomainMask, YES); | |
| 16 NSString* completeFilePath = [NSString | |
| 17 pathWithComponents:@[ [downloadPaths firstObject], @"GoogleChrome.dmg" ]]; | |
| 18 return completeFilePath; | |
| 19 } | |
| 20 | |
| 21 // Downloads contents of chromeURL to downloads folders and delegates the work | 13 // Downloads contents of chromeURL to downloads folders and delegates the work |
| 22 // to the DownloadDelegate class. | 14 // to the DownloadDelegate class. |
| 23 - (void)downloadChromeImageToDownloadsDirectory:(NSURL*)chromeImageURL { | 15 - (void)downloadChromeImageFrom:(NSURL*)chromeImageURL { |
| 24 NSURLSession* session = | 16 NSURLSession* session = |
| 25 [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration | 17 [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration |
| 26 defaultSessionConfiguration] | 18 defaultSessionConfiguration] |
| 27 delegate:self | 19 delegate:self |
| 28 delegateQueue:nil]; | 20 delegateQueue:nil]; |
| 29 [[session downloadTaskWithURL:chromeImageURL] resume]; | 21 [[session downloadTaskWithURL:chromeImageURL] resume]; |
| 30 [session finishTasksAndInvalidate]; | 22 [session finishTasksAndInvalidate]; |
| 31 } | 23 } |
| 32 | 24 |
| 33 // Provides updates to download progress. | 25 // Provides updates to download progress. |
| 34 - (void)URLSession:(NSURLSession*)session | 26 - (void)URLSession:(NSURLSession*)session |
| 35 downloadTask:(NSURLSessionDownloadTask*)downloadTask | 27 downloadTask:(NSURLSessionDownloadTask*)downloadTask |
| 36 didWriteData:(int64_t)bytesWritten | 28 didWriteData:(int64_t)bytesWritten |
| 37 totalBytesWritten:(int64_t)totalBytesWritten | 29 totalBytesWritten:(int64_t)totalBytesWritten |
| 38 totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { | 30 totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { |
| 39 double downloadProgressPercentage = | 31 double downloadProgressPercentage = |
| 40 (double)totalBytesWritten / totalBytesExpectedToWrite * 100.0; | 32 (double)totalBytesWritten / totalBytesExpectedToWrite * 100.0; |
| 41 [delegate_ didDownloadData:downloadProgressPercentage]; | 33 [delegate_ downloader:self percentProgress:downloadProgressPercentage]; |
| 42 } | 34 } |
| 43 | 35 |
| 44 // Delegate method to move downloaded disk image to user's Download directory. | 36 // Delegate method to move downloaded disk image to user's Download directory. |
| 45 - (void)URLSession:(NSURLSession*)session | 37 - (void)URLSession:(NSURLSession*)session |
| 46 downloadTask:(NSURLSessionDownloadTask*)downloadTask | 38 downloadTask:(NSURLSessionDownloadTask*)downloadTask |
| 47 didFinishDownloadingToURL:(NSURL*)location { | 39 didFinishDownloadingToURL:(NSURL*)location { |
| 48 assert([location isFileURL]); | 40 assert([location isFileURL]); |
| 49 NSFileManager* manager = [NSFileManager defaultManager]; | 41 [delegate_ downloader:self onSuccess:location]; |
| 50 NSURL* downloadsDirectory = | |
| 51 [NSURL fileURLWithPath:[Downloader getChromeDownloadFilePath]]; | |
| 52 NSError* fileManagerError = nil; | |
| 53 [manager moveItemAtURL:location | |
| 54 toURL:downloadsDirectory | |
| 55 error:&fileManagerError]; | |
| 56 if (fileManagerError) { | |
| 57 [delegate_ downloader:self onDownloadFailureWithError:fileManagerError]; | |
| 58 } | |
| 59 [delegate_ downloader:self onDownloadSuccess:location]; | |
| 60 } | 42 } |
| 61 | 43 |
| 62 - (void)URLSession:(NSURLSession*)session | 44 - (void)URLSession:(NSURLSession*)session |
| 63 task:(NSURLSessionTask*)task | 45 task:(NSURLSessionTask*)task |
| 64 didCompleteWithError:(NSError*)error { | 46 didCompleteWithError:(NSError*)error { |
| 65 if (error) { | 47 if (error) { |
| 66 [delegate_ downloader:self onDownloadFailureWithError:error]; | 48 [delegate_ downloader:self onFailure:error]; |
| 67 } | 49 } |
| 68 } | 50 } |
| 69 | 51 |
| 70 @end | 52 @end |
| OLD | NEW |