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