Chromium Code Reviews| 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 "delegate.h" | |
| 10 | |
| 11 @implementation Downloader | |
| 12 | |
| 13 // The URLs extracted from parseXML are incomplete and need the filename (which | |
| 14 // is the same for all the links) appended to the end. Iterates through | |
| 15 // chromeIncompleteDownloadURLs_ and appends chromeImageFilename_ to each URL. | |
| 16 - (NSMutableArray*)appendFilename:(NSString*)filename | |
| 17 toURLs:(NSArray*)incompleteURLs { | |
| 18 NSMutableArray* completeURLs = [[NSMutableArray alloc] init]; | |
| 19 | |
| 20 for (NSString* URL in incompleteURLs) { | |
| 21 [completeURLs addObject: [NSString stringWithFormat:@"%@%@",URL,filename]]; | |
|
Elly Fong-Jones
2016/07/06 15:22:02
no space after : (in the rest of this file, too)
| |
| 22 } | |
| 23 return completeURLs; | |
| 24 } | |
| 25 | |
| 26 // Calls parseXML to extract URLs and the filename. Then calls appenFilename to | |
|
Elly Fong-Jones
2016/07/06 15:22:02
It's better for the comment to describe what the f
| |
| 27 // complete the URLs and returns the first complete URL. | |
| 28 - (NSURL*)parseOhamaResponseForChromeImageURLAndFilename:(NSData*)omahaResponseX ML { | |
|
Elly Fong-Jones
2016/07/06 15:22:02
getChromeImageURLFromOmahaResponse:?
| |
| 29 Parser* parser = [[Parser alloc] initWithXML: omahaResponseXML]; | |
| 30 [parser parseXML]; | |
| 31 | |
| 32 NSMutableArray* completeURLs = | |
| 33 [self appendFilename:parser.chromeImageFilename | |
| 34 toURLs:parser.chromeIncompleteDownloadURLs]; | |
| 35 // TODO: Error handling and implement way to verify URL is working | |
| 36 if(!completeURLs || [completeURLs count] < 1) { | |
| 37 NSLog(@"There were no download links found."); | |
| 38 return nil; | |
| 39 } | |
| 40 NSString* chromeURLString = [completeURLs firstObject]; | |
| 41 return [NSURL URLWithString:chromeURLString]; | |
| 42 } | |
| 43 | |
| 44 // Downloads contents of chromeURL to downloads folders and delegates the work | |
| 45 // to the DownloadDelegate class. | |
| 46 - (void)writeChromeImageToDownloadsDirectory:(NSURL*)chromeURL { | |
| 47 DownloadDelegate *delegate = [[DownloadDelegate alloc] init]; | |
| 48 NetworkCommunication* downloadTask = [[NetworkCommunication alloc] | |
| 49 initWithDelegate: delegate]; | |
| 50 | |
| 51 // TODO: What if file already exists? | |
| 52 [downloadTask createRequestWithURLasString:[NSString | |
| 53 stringWithFormat:@"%@",chromeURL] andXMLBody: nil]; | |
| 54 [downloadTask sendDownloadRequest]; | |
| 55 | |
| 56 NSLog(@"File was downloaded to Downloads."); | |
| 57 } | |
| 58 | |
| 59 // Pieces together the getting the URL portion and downloading the contents of | |
| 60 // URL portion. | |
| 61 - (void)downloadChromeImageToDownloadsDirectory: (NSData*)responseXMLData { | |
| 62 NSURL* chromeURL = [self | |
| 63 parseOhamaResponseForChromeImageURLAndFilename: responseXMLData]; | |
| 64 [self writeChromeImageToDownloadsDirectory: chromeURL]; | |
| 65 } | |
| 66 | |
| 67 @end | |
| OLD | NEW |