OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 | |
8 @implementation Downloader | |
9 | |
10 @synthesize chromeDownloadURLs; | |
Mark Mentovai
2016/06/28 15:42:51
We synthesize things into instance variables with
| |
11 @synthesize chromeImageFilename; | |
12 | |
13 - (id)init { | |
14 if ((self = [super init])) { | |
15 chromeDownloadURLs = [[NSMutableArray alloc] init]; | |
16 } | |
17 return self; | |
18 } | |
19 | |
20 - (void)parseXML:(NSData*)omahaResponseXML { | |
21 NSData *response = omahaResponseXML; | |
22 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:response]; | |
23 [parser setDelegate:self]; | |
24 bool success = [parser parse]; | |
25 if (!success) { | |
26 NSError *err = [parser parserError]; | |
27 NSLog(@"%@",err); | |
28 } | |
29 } | |
30 | |
31 - (void)appendFilenameToURL { | |
32 int count = [chromeDownloadURLs count]; | |
Mark Mentovai
2016/06/28 15:42:50
-[NSArray count] returns type NSUInteger, not int.
| |
33 for (int i = 0; i < count; i++) { | |
Mark Mentovai
2016/06/28 15:42:51
But to just walk over a NSArray in Objective-C, yo
| |
34 NSString *fullFilename = [[chromeDownloadURLs objectAtIndex:i] | |
35 stringByAppendingString: chromeImageFilename]; | |
36 [chromeDownloadURLs replaceObjectAtIndex:i withObject: fullFilename]; | |
37 } | |
38 } | |
39 | |
40 // Method implementation for XMLParserDelegate. | |
41 // Will search the XML data for the tag "url" and the subsequent "codebase" attr ibute | |
Mark Mentovai
2016/06/28 15:42:50
Watch the 80-column limit
| |
42 | |
43 - (void)parser:(NSXMLParser *)parser | |
44 didStartElement:(NSString *)elementName | |
45 namespaceURI:(NSString *)namespaceURI | |
46 qualifiedName:(NSString *)qName | |
47 attributes:(NSDictionary *)attributeDict { | |
48 if ([elementName isEqualToString:@"url"]) { | |
49 NSString *extractedURL = [attributeDict objectForKey:@"codebase"]; | |
50 [chromeDownloadURLs addObject: extractedURL]; | |
51 } | |
52 if ([elementName isEqualToString:@"package"]) { | |
53 chromeImageFilename = [attributeDict objectForKey:@"name"]; | |
54 } | |
55 } | |
56 | |
57 | |
58 - (NSURL*)getChromeImageURL: (NSData*)chromeImageAsData { | |
59 [self parseXML:chromeImageAsData]; | |
60 [self appendFilenameToURL]; | |
Mark Mentovai
2016/06/28 15:42:50
If the parse failed, chromeImageFilename might not
| |
61 | |
62 NSArray *downloadLinks = [NSArray arrayWithArray:chromeDownloadURLs]; | |
63 if(!downloadLinks || [downloadLinks count] < 1) { | |
64 NSLog(@"There were no download links found."); | |
65 return nil; | |
66 } | |
67 NSString *chromeURLString = [downloadLinks firstObject]; | |
68 | |
69 return [NSURL URLWithString: chromeURLString]; | |
70 } | |
71 | |
72 //TODO(ivan): make this not suck using [NSURLSession downloadTaskWithURL:] inste ad 👍. | |
Mark Mentovai
2016/06/28 15:42:51
I like the emoji!
| |
73 - (NSData*)downloadChromeAsData: (NSURL*)chromeURL { | |
74 return [NSData dataWithContentsOfURL:chromeURL]; | |
75 } | |
76 | |
77 - (NSString*)getDownloadsFilePath { | |
78 NSArray *downloadPaths = NSSearchPathForDirectoriesInDomains( | |
79 NSDownloadsDirect ory, NSUserDomainMask, YES); | |
80 NSString *filePathToDownloads = [downloadPaths objectAtIndex:0]; | |
81 NSArray *filenameComposition = @[filePathToDownloads, @"GoogleChrome.dmg"]; | |
82 NSString *completeFilePath = [NSString pathWithComponents: filenameComposition ]; | |
83 return completeFilePath; | |
84 } | |
85 | |
86 - (void)writeChromeImageToDownloadsDirectory: (NSData*)chromeImageAsData { | |
87 if (chromeImageAsData) { | |
88 NSString *filePath = [self getDownloadsFilePath]; | |
89 [chromeImageAsData writeToFile:filePath atomically:YES]; | |
Mark Mentovai
2016/06/28 15:42:50
What if it already exists, you overwrite it?
We’l
| |
90 | |
91 NSLog(@"File was downloaded to %@.", filePath); | |
92 } else { | |
93 NSLog(@"Error downloading file."); | |
94 } | |
95 } | |
96 | |
97 - (void)downloadChromeImageToDownloadsDirectory: (NSData*)responseXMLData { | |
98 NSURL *chromeURL = [self getChromeImageURL: responseXMLData]; | |
99 NSData *chromeImageData = [self downloadChromeAsData: chromeURL]; | |
100 [self writeChromeImageToDownloadsDirectory: chromeImageData]; | |
101 } | |
102 | |
103 @end | |
OLD | NEW |