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