OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #import "DownloadDelegate.h" |
| 8 #import "Downloader.h" |
| 9 #import "NetworkCommunication.h" |
| 10 #import "OmahaXMLParser.h" |
| 11 |
| 12 @implementation Downloader |
| 13 |
| 14 // TODO: make this overrideable with commandline argument? or enviro variable |
| 15 + (NSString*)getDownloadsFilePath { |
| 16 NSArray* downloadPaths = NSSearchPathForDirectoriesInDomains( |
| 17 NSDownloadsDirectory, NSUserDomainMask, YES); |
| 18 NSString* filePathToDownloads = [downloadPaths objectAtIndex:0]; |
| 19 NSArray* filenameComposition = @[ filePathToDownloads, @"GoogleChrome.dmg" ]; |
| 20 NSString* completeFilePath = |
| 21 [NSString pathWithComponents:filenameComposition]; |
| 22 return completeFilePath; |
| 23 } |
| 24 |
| 25 // The URLs extracted from parseXML are incomplete and need the filename (which |
| 26 // is the same for all the links) appended to the end. Iterates through |
| 27 // chromeIncompleteDownloadURLs_ and appends chromeImageFilename_ to each URL. |
| 28 - (NSMutableArray*)appendFilename:(NSString*)filename |
| 29 toURLs:(NSArray*)incompleteURLs { |
| 30 NSMutableArray* completeURLs = [[NSMutableArray alloc] init]; |
| 31 |
| 32 for (NSString* URL in incompleteURLs) { |
| 33 [completeURLs addObject:[NSURL URLWithString:filename |
| 34 relativeToURL:[NSURL URLWithString:URL]]]; |
| 35 } |
| 36 return completeURLs; |
| 37 } |
| 38 |
| 39 // Extract URLs and the filename from the omahaResponseXML. Then complete the |
| 40 // URLs by appending filename and returns the first complete URL. |
| 41 - (NSURL*)getChromeImageURLFromOmahaResponse:(NSData*)omahaResponseXML { |
| 42 NSError* err = nil; |
| 43 |
| 44 OmahaXMLParser* parser = [[OmahaXMLParser alloc] init]; |
| 45 NSArray* incompleteURLs = [parser parseXML:omahaResponseXML error:&err]; |
| 46 |
| 47 if ([incompleteURLs count] < 1) { |
| 48 // TODO: Error handling and implement way to verify URL is working. Error |
| 49 // information saved in "err". |
| 50 } |
| 51 |
| 52 NSMutableArray* completeURLs = |
| 53 [self appendFilename:parser.chromeImageFilename toURLs:incompleteURLs]; |
| 54 |
| 55 NSURL* chromeURL = [completeURLs firstObject]; |
| 56 return chromeURL; |
| 57 } |
| 58 |
| 59 // Downloads contents of chromeURL to downloads folders and delegates the work |
| 60 // to the DownloadDelegate class. |
| 61 - (BOOL)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL { |
| 62 DownloadDelegate* delegate = [[DownloadDelegate alloc] init]; |
| 63 NetworkCommunication* downloadTask = |
| 64 [[NetworkCommunication alloc] initWithDelegate:delegate]; |
| 65 |
| 66 // TODO: What if file already exists? |
| 67 [downloadTask createRequestWithUrlAsString:[chromeURL absoluteString] |
| 68 andXMLBody:nil]; |
| 69 [downloadTask sendDownloadRequest]; |
| 70 |
| 71 NSFileManager* manager = [[NSFileManager alloc] init]; |
| 72 return [manager fileExistsAtPath:[Downloader getDownloadsFilePath]]; |
| 73 } |
| 74 |
| 75 // Pieces together the getting the URL portion and downloading the contents of |
| 76 // URL portion. |
| 77 - (BOOL)downloadChromeImageToDownloadsDirectory:(NSData*)omahaResponseXML { |
| 78 NSURL* chromeURL = [self getChromeImageURLFromOmahaResponse:omahaResponseXML]; |
| 79 BOOL writeWasSuccessful = |
| 80 [self writeChromeImageToDownloadsDirectory:chromeURL]; |
| 81 return writeWasSuccessful; |
| 82 } |
| 83 |
| 84 @end |
OLD | NEW |