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..6239441af18196aa5e590707e05de8eb130738b7 |
--- /dev/null |
+++ b/chrome/installer/mac/app/downloader.m |
@@ -0,0 +1,88 @@ |
+// 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> |
+#import "downloader.h" |
+#import "parser.h" |
+#import "NetworkCommunication.h" |
+#import "DownloadDelegate.h" |
+ |
+@implementation Downloader |
+ |
++ (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; |
+} |
+ |
+// 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]]; |
+ } |
+ 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 = [[NSError alloc] init]; |
+ |
+ 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){ |
+ NSLog(@"Unsuccesful write to Downloads."); |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+@end |