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 <Foundation/Foundation.h> | 5 #import <Foundation/Foundation.h> |
6 #import <assert.h> | |
Mark Mentovai
2016/07/19 21:07:40
<assert.h> gets #included, not #imported. We gener
| |
6 | 7 |
7 #import "DownloadDelegate.h" | |
8 #import "Downloader.h" | 8 #import "Downloader.h" |
9 #import "NetworkCommunication.h" | 9 #import "NetworkCommunication.h" |
10 #import "OmahaXMLParser.h" | 10 #import "OmahaXMLParser.h" |
11 | 11 |
12 @implementation Downloader | 12 @implementation Downloader |
13 | 13 |
14 @synthesize delegate = delegate_; | |
15 | |
14 // TODO: make this overrideable with commandline argument? or enviro variable | 16 // TODO: make this overrideable with commandline argument? or enviro variable |
15 + (NSString*)getDownloadsFilePath { | 17 + (NSString*)getDownloadsFilePath { |
16 NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains( | 18 NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains( |
17 NSDownloadsDirectory, NSUserDomainMask, YES); | 19 NSDownloadsDirectory, NSUserDomainMask, YES); |
18 NSString* filePathToDownloads = [downloadPaths objectAtIndex:0]; | 20 NSString* filePathToDownloads = [downloadPaths objectAtIndex:0]; |
19 NSArray* filenameComposition = @[ filePathToDownloads, @"GoogleChrome.dmg" ]; | 21 NSArray* filenameComposition = @[ filePathToDownloads, @"GoogleChrome.dmg" ]; |
20 NSString* completeFilePath = | 22 NSString* completeFilePath = |
21 [NSString pathWithComponents:filenameComposition]; | 23 [NSString pathWithComponents:filenameComposition]; |
22 return completeFilePath; | 24 return completeFilePath; |
23 } | 25 } |
(...skipping 28 matching lines...) Expand all Loading... | |
52 NSMutableArray* completeURLs = | 54 NSMutableArray* completeURLs = |
53 [self appendFilename:parser.chromeImageFilename toURLs:incompleteURLs]; | 55 [self appendFilename:parser.chromeImageFilename toURLs:incompleteURLs]; |
54 | 56 |
55 NSURL* chromeURL = [completeURLs firstObject]; | 57 NSURL* chromeURL = [completeURLs firstObject]; |
56 return chromeURL; | 58 return chromeURL; |
57 } | 59 } |
58 | 60 |
59 // Downloads contents of chromeURL to downloads folders and delegates the work | 61 // Downloads contents of chromeURL to downloads folders and delegates the work |
60 // to the DownloadDelegate class. | 62 // to the DownloadDelegate class. |
61 - (BOOL)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL { | 63 - (BOOL)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL { |
62 DownloadDelegate* delegate = [[DownloadDelegate alloc] init]; | |
63 NetworkCommunication* downloadTask = | 64 NetworkCommunication* downloadTask = |
64 [[NetworkCommunication alloc] initWithDelegate:delegate]; | 65 [[NetworkCommunication alloc] initWithDelegate:self]; |
65 | 66 |
66 // TODO: What if file already exists? | 67 // TODO: What if file already exists? |
67 [downloadTask createRequestWithUrlAsString:[chromeURL absoluteString] | 68 [downloadTask createRequestWithUrlAsString:[chromeURL absoluteString] |
68 andXMLBody:nil]; | 69 andXMLBody:nil]; |
69 [downloadTask sendDownloadRequest]; | 70 [downloadTask sendDownloadRequest]; |
70 | 71 |
71 NSFileManager* manager = [[NSFileManager alloc] init]; | 72 NSFileManager* manager = [[NSFileManager alloc] init]; |
72 return [manager fileExistsAtPath:[Downloader getDownloadsFilePath]]; | 73 return [manager fileExistsAtPath:[Downloader getDownloadsFilePath]]; |
73 } | 74 } |
74 | 75 |
75 // Pieces together the getting the URL portion and downloading the contents of | 76 // Pieces together the getting the URL portion and downloading the contents of |
76 // URL portion. | 77 // URL portion. |
77 - (BOOL)downloadChromeImageToDownloadsDirectory:(NSData*)omahaResponseXML { | 78 - (BOOL)downloadChromeImageToDownloadsDirectory:(NSData*)omahaResponseXML { |
78 NSURL* chromeURL = [self getChromeImageURLFromOmahaResponse:omahaResponseXML]; | 79 NSURL* chromeURL = [self getChromeImageURLFromOmahaResponse:omahaResponseXML]; |
79 BOOL writeWasSuccessful = | 80 BOOL writeWasSuccessful = |
80 [self writeChromeImageToDownloadsDirectory:chromeURL]; | 81 [self writeChromeImageToDownloadsDirectory:chromeURL]; |
81 return writeWasSuccessful; | 82 return writeWasSuccessful; |
82 } | 83 } |
83 | 84 |
85 // Skeleton of delegate method to provide download progress updates. | |
86 // TODO: Make use of (totalBytesWritten/totalBytesExpectedToWrite)*100 | |
87 // to generate download progress percentage. | |
88 - (void)URLSession:(NSURLSession*)session | |
89 downloadTask:(NSURLSessionDownloadTask*)downloadTask | |
90 didWriteData:(int64_t)bytesWritten | |
91 totalBytesWritten:(int64_t)totalBytesWritten | |
92 totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite { | |
93 } | |
94 | |
95 // Delegate method to move downloaded disk image to user's Download directory. | |
96 - (void)URLSession:(NSURLSession*)session | |
97 downloadTask:(NSURLSessionDownloadTask*)downloadTask | |
98 didFinishDownloadingToURL:(NSURL*)location { | |
99 assert([location isFileURL]); | |
100 NSFileManager* manager = [[NSFileManager alloc] init]; | |
101 NSURL* downloadsDirectory = | |
102 [[NSURL alloc] initFileURLWithPath:[Downloader getDownloadsFilePath]]; | |
103 if ([manager fileExistsAtPath:location.path]) { | |
104 [manager moveItemAtURL:location toURL:downloadsDirectory error:nil]; | |
105 } else { | |
106 // TODO: Error Handling | |
107 } | |
108 } | |
109 | |
110 - (void)URLSession:(NSURLSession*)session | |
111 task:(NSURLSessionTask*)task | |
112 didCompleteWithError:(NSError*)error { | |
113 // TODO: Error Handling | |
114 | |
115 [delegate_ unpackDMG]; | |
116 } | |
117 | |
84 @end | 118 @end |
OLD | NEW |