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..5e42602896ceea33bd6a9895cc050dcd458c98f9 |
| --- /dev/null |
| +++ b/chrome/installer/mac/app/downloader.m |
| @@ -0,0 +1,90 @@ |
| +// Copyright 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> |
|
Mark Mentovai
2016/07/11 17:56:16
#import "downloader.h"
(blank line)
#import <
|
| +#import "downloader.h" |
| +#import "parser.h" |
| +#import "NetworkCommunication.h" |
| +#import "DownloadDelegate.h" |
| + |
| +@implementation Downloader |
| + |
| +// TODO: make this overrideable with commandline argument? or enviro variable |
| ++ (NSString*)getDownloadsFilePath { |
| + NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains( |
| + NSDownloadsDirectory, NSUserDomainMask, YES); |
| + NSString* filePathToDownloads = [downloadPaths objectAtIndex:0]; |
| + NSArray* filenameComposition = @[ filePathToDownloads, @"GoogleChrome.dmg" ]; |
|
Elly Fong-Jones
2016/07/11 16:15:48
nit: no spaces around []
|
| + NSString* completeFilePath = |
| + [NSString pathWithComponents:filenameComposition]; |
| + return completeFilePath; |
| +} |
| + |
| +// The URLs extracted from parseXML are incomplete and need the filename (which |
| +// is the same for all the links) appended to the end. Iterates through |
| +// chromeIncompleteDownloadURLs_ and appends chromeImageFilename_ to each URL. |
| +- (NSMutableArray*)appendFilename:(NSString*)filename |
| + toURLs:(NSArray*)incompleteURLs { |
| + NSMutableArray* completeURLs = [[NSMutableArray alloc] init]; |
| + |
| + for (NSString* URL in incompleteURLs) { |
| + [completeURLs addObject:[NSString stringWithFormat:@"%@%@", URL, filename]]; |
|
Elly Fong-Jones
2016/07/11 16:15:48
[NSURL URLwithString:relativeToURL:] I think
|
| + } |
| + return completeURLs; |
| +} |
| + |
| +// Extract URLs and the filename from the omahaResponseXML. Then complete the |
| +// URLs by appending filename and returns the first complete URL. |
| +- (NSURL*)getChromeImageURLFromOmahaResponse:(NSData*)omahaResponseXML { |
| + NSError* err = nil; |
| + |
| + Parser* parser = [[Parser alloc] init]; |
| + NSArray* incompleteURLs = [parser parseXML:omahaResponseXML error:&err]; |
| + |
| + if ([incompleteURLs count] < 1) { |
| + // TODO: Error handling and implement way to verify URL is working. Error |
| + // information saved in "err". |
| + } |
| + |
| + NSMutableArray* completeURLs = |
| + [self appendFilename:parser.chromeImageFilename toURLs:incompleteURLs]; |
| + |
| + NSString* chromeURLString = [completeURLs firstObject]; |
| + |
| + return [NSURL URLWithString:chromeURLString]; |
| +} |
| + |
| +// Downloads contents of chromeURL to downloads folders and delegates the work |
| +// to the DownloadDelegate class. |
| +- (BOOL)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL { |
| + DownloadDelegate* delegate = [[DownloadDelegate alloc] init]; |
| + NetworkCommunication* downloadTask = |
| + [[NetworkCommunication alloc] initWithDelegate:delegate]; |
| + |
| + // TODO: What if file already exists? |
| + [downloadTask |
| + createRequestWithUrlAsString:[NSString stringWithFormat:@"%@", chromeURL] |
| + andXMLBody:nil]; |
| + [downloadTask sendDownloadRequest]; |
| + |
| + NSFileManager* manager = [[NSFileManager alloc] init]; |
| + if (![manager fileExistsAtPath:[Downloader getDownloadsFilePath]]) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// Pieces together the getting the URL portion and downloading the contents of |
| +// URL portion. |
| +- (BOOL)downloadChromeImageToDownloadsDirectory:(NSData*)omahaResponseXML { |
| + NSURL* chromeURL = [self getChromeImageURLFromOmahaResponse:omahaResponseXML]; |
| + BOOL writeWasSuccessful = |
| + [self writeChromeImageToDownloadsDirectory:chromeURL]; |
| + if (!writeWasSuccessful) { |
|
Elly Fong-Jones
2016/07/11 16:15:48
just return writeWasSuccessful :)
|
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +@end |