| 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 <Foundation/Foundation.h> |
| 6 | 6 |
| 7 #import "NetworkCommunication.h" | 7 #import "NetworkCommunication.h" |
| 8 | 8 |
| 9 @implementation NetworkCommunication : NSObject | 9 @implementation NetworkCommunication : NSObject |
| 10 | 10 |
| 11 @synthesize session = session_; | 11 @synthesize session = session_; |
| 12 @synthesize request = request_; | 12 @synthesize request = request_; |
| 13 @synthesize dataResponseHandler = dataResponseHandler_; | |
| 14 @synthesize downloadResponseHandler = downloadResponseHandler_; | |
| 15 | 13 |
| 16 - (id)init { | 14 - (id)init { |
| 17 return [self initWithDelegate:nil]; | 15 return [self initWithDelegate:nil]; |
| 18 } | 16 } |
| 19 | 17 |
| 20 - (id)initWithDelegate:(id)delegate { | 18 - (id)initWithDelegate:(id)delegate { |
| 21 if ((self = [super init])) { | 19 if ((self = [super init])) { |
| 22 NSURLSessionConfiguration* sessionConfig = | 20 NSURLSessionConfiguration* sessionConfig = |
| 23 [NSURLSessionConfiguration defaultSessionConfiguration]; | 21 [NSURLSessionConfiguration defaultSessionConfiguration]; |
| 24 session_ = [NSURLSession sessionWithConfiguration:sessionConfig | 22 session_ = [NSURLSession sessionWithConfiguration:sessionConfig |
| 25 delegate:delegate | 23 delegate:delegate |
| 26 delegateQueue:nil]; | 24 delegateQueue:nil]; |
| 27 } | 25 } |
| 28 return self; | 26 return self; |
| 29 } | 27 } |
| 30 | 28 |
| 31 - (NSMutableURLRequest*)createRequestWithUrlAsString:(NSString*)urlString | 29 - (NSMutableURLRequest*)createRequestWithUrlAsString:(NSString*)urlString |
| 32 andXMLBody:(NSXMLDocument*)body { | 30 andXMLBody:(NSXMLDocument*)body { |
| 33 NSURL* requestURL = [NSURL URLWithString:urlString]; | 31 NSURL* requestURL = [NSURL URLWithString:urlString]; |
| 34 request_ = [NSMutableURLRequest requestWithURL:requestURL]; | 32 request_ = [NSMutableURLRequest requestWithURL:requestURL]; |
| 35 if (body) { | 33 if (body) { |
| 36 [request_ addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; | 34 [request_ addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; |
| 37 NSData* requestBody = | 35 NSData* requestBody = |
| 38 [[body XMLString] dataUsingEncoding:NSUTF8StringEncoding]; | 36 [[body XMLString] dataUsingEncoding:NSUTF8StringEncoding]; |
| 39 request_.HTTPBody = requestBody; | 37 request_.HTTPBody = requestBody; |
| 40 } | 38 } |
| 41 return request_; | 39 return request_; |
| 42 } | 40 } |
| 43 | 41 |
| 44 - (void)sendDataRequestWithCompletionHandler: | 42 - (void)sendDataRequest { |
| 45 (DataTaskCompletionHandler)completionHandler { | 43 NSURLSessionDataTask* dataTask = [session_ dataTaskWithRequest:request_]; |
| 46 dataResponseHandler_ = completionHandler; | |
| 47 NSURLSessionDataTask* dataTask = | |
| 48 [session_ dataTaskWithRequest:request_ | |
| 49 completionHandler:dataResponseHandler_]; | |
| 50 | |
| 51 [dataTask resume]; | 44 [dataTask resume]; |
| 52 } | 45 } |
| 53 | 46 |
| 54 - (void)sendDownloadRequest { | 47 - (void)sendDownloadRequest { |
| 55 NSURLSessionDownloadTask* downloadTask; | 48 NSURLSessionDownloadTask* downloadTask = |
| 56 if (downloadResponseHandler_) { | 49 [session_ downloadTaskWithRequest:request_]; |
| 57 downloadTask = [session_ downloadTaskWithRequest:request_ | |
| 58 completionHandler:downloadResponseHandler_]; | |
| 59 } else { | |
| 60 downloadTask = [session_ downloadTaskWithRequest:request_]; | |
| 61 } | |
| 62 [downloadTask resume]; | 50 [downloadTask resume]; |
| 63 } | 51 } |
| 64 | 52 |
| 65 @end | 53 @end |
| OLD | NEW |