OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "OmahaCommunication.h" | 5 #import "OmahaCommunication.h" |
6 | 6 |
7 #import "OmahaXMLRequest.h" | 7 #import "OmahaXMLRequest.h" |
8 #import "OmahaXMLParser.h" | 8 #import "OmahaXMLParser.h" |
9 | 9 |
| 10 // TODO: turn this string to a command-line flag |
10 static NSString* const omahaURLPath = | 11 static NSString* const omahaURLPath = |
11 @"https://tools.google.com/service/update2"; | 12 @"https://tools.google.com/service/update2"; |
12 | 13 |
13 @interface NSURLSession () | 14 @interface NSURLSession () |
14 - (NSURLSessionDataTask*)dataTaskWithRequest:(NSURLRequest*)request | 15 - (NSURLSessionDataTask*)dataTaskWithRequest:(NSURLRequest*)request |
15 completionHandler: | 16 completionHandler: |
16 (void (^)(NSData* data, | 17 (void (^)(NSData* data, |
17 NSURLResponse* response, | 18 NSURLResponse* response, |
18 NSError* error))completionHandler; | 19 NSError* error))completionHandler; |
19 @end | 20 @end |
20 | 21 |
21 @implementation OmahaCommunication | 22 @implementation OmahaCommunication |
22 | 23 |
23 @synthesize requestXMLBody = requestXMLBody_; | 24 @synthesize requestXMLBody = requestXMLBody_; |
24 @synthesize delegate = delegate_; | 25 @synthesize delegate = delegate_; |
25 | 26 |
26 - (id)init { | 27 - (id)init { |
27 return [self initWithBody:[OmahaXMLRequest createXMLRequestBody]]; | 28 return [self initWithBody:[OmahaXMLRequest createXMLRequestBody]]; |
28 } | 29 } |
29 | 30 |
30 - (id)initWithBody:(NSXMLDocument*)xmlBody { | 31 - (id)initWithBody:(NSXMLDocument*)xmlBody { |
31 if ((self = [super init])) { | 32 if ((self = [super init])) { |
32 requestXMLBody_ = xmlBody; | 33 requestXMLBody_ = xmlBody; |
33 } | 34 } |
34 return self; | 35 return self; |
35 } | 36 } |
36 | 37 |
37 - (void)fetchDownloadURLs { | 38 - (void)fetchDownloadURLs { |
38 // TODO: turn this string to a command-line flag | 39 // Forming the request |
39 NSURL* requestURL = [NSURL URLWithString:omahaURLPath]; | 40 NSURL* requestURL = [NSURL URLWithString:omahaURLPath]; |
40 NSMutableURLRequest* request = | 41 NSMutableURLRequest* request = |
41 [NSMutableURLRequest requestWithURL:requestURL]; | 42 [NSMutableURLRequest requestWithURL:requestURL]; |
42 [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; | 43 [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; |
43 NSData* requestBody = | 44 NSData* requestBody = |
44 [[requestXMLBody_ XMLString] dataUsingEncoding:NSUTF8StringEncoding]; | 45 [[requestXMLBody_ XMLString] dataUsingEncoding:NSUTF8StringEncoding]; |
45 request.HTTPBody = requestBody; | 46 request.HTTPBody = requestBody; |
46 request.HTTPMethod = @"POST"; | 47 request.HTTPMethod = @"POST"; |
| 48 // Sending the request |
47 [[[NSURLSession sharedSession] | 49 [[[NSURLSession sharedSession] |
48 dataTaskWithRequest:request | 50 dataTaskWithRequest:request |
49 completionHandler:^(NSData* data, NSURLResponse* response, | 51 completionHandler:^(NSData* data, NSURLResponse* response, |
50 NSError* error) { | 52 NSError* error) { |
51 NSArray* completeURLs = nil; | 53 NSArray* completeURLs = nil; |
52 if (!error) { | 54 if (!error) { |
53 completeURLs = [OmahaXMLParser parseXML:data error:&error]; | 55 completeURLs = [OmahaXMLParser parseXML:data error:&error]; |
54 } | 56 } |
55 // Deals with errors both from the network error and the | 57 // Deals with errors both from the network error and the |
56 // parsing error, as the user only needs to know there was a problem | 58 // parsing error, as the user only needs to know there was a problem |
57 // talking with the Google Update server. | 59 // talking with the Google Update server. |
58 if (error) { | 60 if (error) { |
59 [delegate_ onOmahaFailureWithError:error]; | 61 [delegate_ omahaCommunication:self onOmahaFailureWithError:error]; |
60 } else { | 62 } else { |
61 [delegate_ onOmahaSuccessWithURLs:completeURLs]; | 63 [delegate_ omahaCommunication:self |
| 64 onOmahaSuccessWithURLs:completeURLs]; |
62 } | 65 } |
63 }] resume]; | 66 }] resume]; |
64 } | 67 } |
65 | 68 |
66 @end | 69 @end |
OLD | NEW |