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> |
| 6 |
| 7 #include "OmahaCommunication.h" |
| 8 |
| 9 @implementation OmahaCommunication : NSObject |
| 10 |
| 11 @synthesize requestXMLBody = requestXMLBody_; |
| 12 @synthesize sessionHelper = sessionHelper_; |
| 13 |
| 14 - (id)init { |
| 15 return [self initWithBody:[[NSXMLDocument alloc] init]]; |
| 16 } |
| 17 |
| 18 - (id)initWithBody:(NSXMLDocument*)xmlBody { |
| 19 if ((self = [super init])) { |
| 20 sessionHelper_ = [[NetworkCommunication alloc] init]; |
| 21 requestXMLBody_ = xmlBody; |
| 22 [self createOmahaRequest]; |
| 23 } |
| 24 return self; |
| 25 } |
| 26 |
| 27 - (NSURLRequest*)createOmahaRequest { |
| 28 // TODO: turn this string to a comand-line flag |
| 29 NSMutableURLRequest* request = [sessionHelper_ |
| 30 createRequestWithUrlAsString:@"https://tools.google.com/service/update2" |
| 31 andXMLBody:requestXMLBody_]; |
| 32 request.HTTPMethod = @"POST"; |
| 33 return request; |
| 34 } |
| 35 |
| 36 - (void)sendRequestWithBlock:(OmahaRequestCompletionHandler)block { |
| 37 DataTaskCompletionHandler cHandler = |
| 38 ^(NSData* _Nullable data, NSURLResponse* _Nullable response, |
| 39 NSError* _Nullable error) { |
| 40 if (error) { |
| 41 NSLog(@"%@", error); |
| 42 block(data, error); |
| 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 } |
| 58 |
| 59 @end |
OLD | NEW |