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 <Foundation/Foundation.h> | 5 #import "OmahaCommunication.h" |
6 | 6 |
7 #include "OmahaCommunication.h" | 7 #import "OmahaXMLRequest.h" |
8 | 8 |
9 @implementation OmahaCommunication : NSObject | 9 @implementation OmahaCommunication |
10 | 10 |
11 @synthesize requestXMLBody = requestXMLBody_; | 11 @synthesize requestXMLBody = requestXMLBody_; |
12 @synthesize sessionHelper = sessionHelper_; | 12 @synthesize sessionHelper = sessionHelper_; |
13 @synthesize delegate = delegate_; | |
13 | 14 |
14 - (id)init { | 15 - (id)init { |
15 return [self initWithBody:[[NSXMLDocument alloc] init]]; | 16 return [self initWithBody:[OmahaXMLRequest createXMLRequestBody]]; |
16 } | 17 } |
17 | 18 |
18 - (id)initWithBody:(NSXMLDocument*)xmlBody { | 19 - (id)initWithBody:(NSXMLDocument*)xmlBody { |
19 if ((self = [super init])) { | 20 if ((self = [super init])) { |
20 sessionHelper_ = [[NetworkCommunication alloc] init]; | 21 sessionHelper_ = [[NetworkCommunication alloc] initWithDelegate:self]; |
21 requestXMLBody_ = xmlBody; | 22 requestXMLBody_ = xmlBody; |
22 [self createOmahaRequest]; | 23 [self createOmahaRequest]; |
23 } | 24 } |
24 return self; | 25 return self; |
25 } | 26 } |
26 | 27 |
27 - (NSURLRequest*)createOmahaRequest { | 28 - (NSURLRequest*)createOmahaRequest { |
28 // TODO: turn this string to a comand-line flag | 29 // TODO: turn this string to a comand-line flag |
29 NSMutableURLRequest* request = [sessionHelper_ | 30 NSMutableURLRequest* request = [sessionHelper_ |
30 createRequestWithUrlAsString:@"https://tools.google.com/service/update2" | 31 createRequestWithUrlAsString:@"https://tools.google.com/service/update2" |
31 andXMLBody:requestXMLBody_]; | 32 andXMLBody:requestXMLBody_]; |
32 request.HTTPMethod = @"POST"; | 33 request.HTTPMethod = @"POST"; |
33 return request; | 34 return request; |
34 } | 35 } |
35 | 36 |
36 - (void)sendRequestWithBlock:(OmahaRequestCompletionHandler)block { | 37 - (void)sendRequest { |
37 DataTaskCompletionHandler cHandler = | 38 [sessionHelper_ sendDataRequest]; |
38 ^(NSData* _Nullable data, NSURLResponse* _Nullable response, | 39 } |
39 NSError* _Nullable error) { | |
40 if (error) { | |
41 NSLog(@"%@", error); | |
42 block(data, error); | |
43 return; | |
44 } | |
45 | 40 |
46 NSHTTPURLResponse* HTTPResponse = (NSHTTPURLResponse*)response; | 41 - (void)URLSession:(NSURLSession*)session |
47 if (HTTPResponse.statusCode != 200) { | 42 dataTask:(NSURLSessionDataTask*)dataTask |
48 // TODO: make these logging statements more rare | 43 didReceiveData:(NSData*)data { |
49 NSLog(@"HTTP response: %ld", (unsigned long)HTTPResponse.statusCode); | 44 if (!_dataCollected) { |
50 } | 45 _dataCollected = [[NSMutableData alloc] initWithData:data]; |
46 } else { | |
47 [_dataCollected appendData:data]; | |
Sidney San Martín
2016/07/22 02:30:17
You might be better off just using -[NSURLSession
| |
48 } | |
49 } | |
51 | 50 |
52 block(data, error); | 51 - (void)URLSession:(NSURLSession*)session |
53 | 52 task:(NSURLSessionTask*)task |
54 }; | 53 didCompleteWithError:(NSError*)error { |
55 | 54 [delegate_ onOmahaSuccessWithData:_dataCollected]; |
Sidney San Martín
2016/07/22 02:30:17
The delegate should get the error, if there is one
| |
56 [sessionHelper_ sendDataRequestWithCompletionHandler:cHandler]; | |
57 } | 55 } |
58 | 56 |
59 @end | 57 @end |
OLD | NEW |