Chromium Code Reviews| Index: chrome/installer/mac/app/downloader.m |
| diff --git a/chrome/installer/mac/app/downloader.m b/chrome/installer/mac/app/downloader.m |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..45f12bb72e050fdf85376c2eae3049f0ffb2f2bc |
| --- /dev/null |
| +++ b/chrome/installer/mac/app/downloader.m |
| @@ -0,0 +1,103 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import <Foundation/Foundation.h> |
| +#import "downloader.h" |
| + |
| +@implementation Downloader |
| + |
| +@synthesize chromeDownloadURLs; |
|
Mark Mentovai
2016/06/28 15:42:51
We synthesize things into instance variables with
|
| +@synthesize chromeImageFilename; |
| + |
| +- (id)init { |
| + if ((self = [super init])) { |
| + chromeDownloadURLs = [[NSMutableArray alloc] init]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)parseXML:(NSData*)omahaResponseXML { |
| + NSData *response = omahaResponseXML; |
| + NSXMLParser *parser = [[NSXMLParser alloc] initWithData:response]; |
| + [parser setDelegate:self]; |
| + bool success = [parser parse]; |
| + if (!success) { |
| + NSError *err = [parser parserError]; |
| + NSLog(@"%@",err); |
| + } |
| +} |
| + |
| +- (void)appendFilenameToURL { |
| + int count = [chromeDownloadURLs count]; |
|
Mark Mentovai
2016/06/28 15:42:50
-[NSArray count] returns type NSUInteger, not int.
|
| + for (int i = 0; i < count; i++) { |
|
Mark Mentovai
2016/06/28 15:42:51
But to just walk over a NSArray in Objective-C, yo
|
| + NSString *fullFilename = [[chromeDownloadURLs objectAtIndex:i] |
| + stringByAppendingString: chromeImageFilename]; |
| + [chromeDownloadURLs replaceObjectAtIndex:i withObject: fullFilename]; |
| + } |
| +} |
| + |
| +// Method implementation for XMLParserDelegate. |
| +// Will search the XML data for the tag "url" and the subsequent "codebase" attribute |
|
Mark Mentovai
2016/06/28 15:42:50
Watch the 80-column limit
|
| + |
| +- (void)parser:(NSXMLParser *)parser |
| +didStartElement:(NSString *)elementName |
| + namespaceURI:(NSString *)namespaceURI |
| + qualifiedName:(NSString *)qName |
| + attributes:(NSDictionary *)attributeDict { |
| + if ([elementName isEqualToString:@"url"]) { |
| + NSString *extractedURL = [attributeDict objectForKey:@"codebase"]; |
| + [chromeDownloadURLs addObject: extractedURL]; |
| + } |
| + if ([elementName isEqualToString:@"package"]) { |
| + chromeImageFilename = [attributeDict objectForKey:@"name"]; |
| + } |
| +} |
| + |
| + |
| +- (NSURL*)getChromeImageURL: (NSData*)chromeImageAsData { |
| + [self parseXML:chromeImageAsData]; |
| + [self appendFilenameToURL]; |
|
Mark Mentovai
2016/06/28 15:42:50
If the parse failed, chromeImageFilename might not
|
| + |
| + NSArray *downloadLinks = [NSArray arrayWithArray:chromeDownloadURLs]; |
| + if(!downloadLinks || [downloadLinks count] < 1) { |
| + NSLog(@"There were no download links found."); |
| + return nil; |
| + } |
| + NSString *chromeURLString = [downloadLinks firstObject]; |
| + |
| + return [NSURL URLWithString: chromeURLString]; |
| +} |
| + |
| +//TODO(ivan): make this not suck using [NSURLSession downloadTaskWithURL:] instead 👍. |
|
Mark Mentovai
2016/06/28 15:42:51
I like the emoji!
|
| +- (NSData*)downloadChromeAsData: (NSURL*)chromeURL { |
| + return [NSData dataWithContentsOfURL:chromeURL]; |
| +} |
| + |
| +- (NSString*)getDownloadsFilePath { |
| + NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains( |
| + NSDownloadsDirectory, NSUserDomainMask, YES); |
| + NSString *filePathToDownloads = [downloadPaths objectAtIndex:0]; |
| + NSArray *filenameComposition = @[filePathToDownloads, @"GoogleChrome.dmg"]; |
| + NSString *completeFilePath = [NSString pathWithComponents: filenameComposition]; |
| + return completeFilePath; |
| +} |
| + |
| +- (void)writeChromeImageToDownloadsDirectory: (NSData*)chromeImageAsData { |
| + if (chromeImageAsData) { |
| + NSString *filePath = [self getDownloadsFilePath]; |
| + [chromeImageAsData writeToFile:filePath atomically:YES]; |
|
Mark Mentovai
2016/06/28 15:42:50
What if it already exists, you overwrite it?
We’l
|
| + |
| + NSLog(@"File was downloaded to %@.", filePath); |
| + } else { |
| + NSLog(@"Error downloading file."); |
| + } |
| +} |
| + |
| +- (void)downloadChromeImageToDownloadsDirectory: (NSData*)responseXMLData { |
| + NSURL *chromeURL = [self getChromeImageURL: responseXMLData]; |
| + NSData *chromeImageData = [self downloadChromeAsData: chromeURL]; |
| + [self writeChromeImageToDownloadsDirectory: chromeImageData]; |
| +} |
| + |
| +@end |