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_ setDataResponseHandler:^(NSData* _Nullable data, |
38 ^(NSData* _Nullable data, NSURLResponse* _Nullable response, | 39 NSURLResponse* _Nullable response, |
39 NSError* _Nullable error) { | 40 NSError* _Nullable error) { |
40 if (error) { | 41 [delegate_ onOmahaSuccessWithResponseBody:data AndError:error]; |
41 NSLog(@"%@", error); | 42 }]; |
42 block(data, error); | 43 [sessionHelper_ sendDataRequest]; |
43 return; | |
44 } | |
45 | |
46 NSHTTPURLResponse* HTTPResponse = (NSHTTPURLResponse*)response; | |
47 if (HTTPResponse.statusCode != 200) { | |
48 // TODO: make these logging statements more rare | |
49 NSLog(@"HTTP response: %ld", (unsigned long)HTTPResponse.statusCode); | |
50 } | |
51 | |
52 block(data, error); | |
53 | |
54 }; | |
55 | |
56 [sessionHelper_ sendDataRequestWithCompletionHandler:cHandler]; | |
57 } | 44 } |
58 | 45 |
59 @end | 46 @end |
OLD | NEW |