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