OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ios/chrome/common/physical_web/physical_web_request.h" | 5 #import "ios/chrome/common/physical_web/physical_web_request.h" |
6 | 6 |
7 #include "base/ios/weak_nsobject.h" | 7 #include "base/ios/weak_nsobject.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
10 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
14 #include "components/version_info/version_info.h" | 14 #include "components/version_info/version_info.h" |
15 #include "google_apis/google_api_keys.h" | 15 #include "google_apis/google_api_keys.h" |
16 #import "ios/chrome/common/physical_web/physical_web_device.h" | 16 #import "ios/chrome/common/physical_web/physical_web_device.h" |
17 #import "ios/chrome/common/physical_web/physical_web_types.h" | 17 #import "ios/chrome/common/physical_web/physical_web_types.h" |
18 #include "ios/web/public/user_agent.h" | 18 #include "ios/web/public/user_agent.h" |
19 | 19 |
| 20 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 21 #error "This file requires ARC support." |
| 22 #endif |
| 23 |
20 typedef void (^SessionCompletionProceduralBlock)(NSData* data, | 24 typedef void (^SessionCompletionProceduralBlock)(NSData* data, |
21 NSURLResponse* response, | 25 NSURLResponse* response, |
22 NSError* error); | 26 NSError* error); |
23 | 27 |
24 namespace { | 28 namespace { |
25 | 29 |
26 NSString* const kUrlsKey = @"urls"; | 30 NSString* const kUrlsKey = @"urls"; |
27 NSString* const kUrlKey = @"url"; | 31 NSString* const kUrlKey = @"url"; |
28 NSString* const kResultsKey = @"results"; | 32 NSString* const kResultsKey = @"results"; |
29 NSString* const kPageInfoKey = @"pageInfo"; | 33 NSString* const kPageInfoKey = @"pageInfo"; |
(...skipping 26 matching lines...) Expand all Loading... |
56 base::scoped_nsobject<PhysicalWebDevice> device_; | 60 base::scoped_nsobject<PhysicalWebDevice> device_; |
57 base::scoped_nsobject<NSMutableURLRequest> request_; | 61 base::scoped_nsobject<NSMutableURLRequest> request_; |
58 base::scoped_nsobject<NSURLSessionDataTask> urlSessionTask_; | 62 base::scoped_nsobject<NSURLSessionDataTask> urlSessionTask_; |
59 base::scoped_nsobject<NSMutableData> data_; | 63 base::scoped_nsobject<NSMutableData> data_; |
60 base::scoped_nsobject<NSDate> startDate_; | 64 base::scoped_nsobject<NSDate> startDate_; |
61 } | 65 } |
62 | 66 |
63 - (instancetype)initWithDevice:(PhysicalWebDevice*)device { | 67 - (instancetype)initWithDevice:(PhysicalWebDevice*)device { |
64 self = [super init]; | 68 self = [super init]; |
65 if (self) { | 69 if (self) { |
66 device_.reset([device retain]); | 70 device_.reset(device); |
67 } | 71 } |
68 return self; | 72 return self; |
69 } | 73 } |
70 | 74 |
71 - (instancetype)init { | 75 - (instancetype)init { |
72 NOTREACHED(); | 76 NOTREACHED(); |
73 return nil; | 77 return nil; |
74 } | 78 } |
75 | 79 |
76 - (NSURL*)requestURL { | 80 - (NSURL*)requestURL { |
(...skipping 11 matching lines...) Expand all Loading... |
88 | 92 |
89 // Creates the HTTP post request. | 93 // Creates the HTTP post request. |
90 base::scoped_nsobject<NSURLComponents> components( | 94 base::scoped_nsobject<NSURLComponents> components( |
91 [[NSURLComponents alloc] initWithString:kMetadataServiceUrl]); | 95 [[NSURLComponents alloc] initWithString:kMetadataServiceUrl]); |
92 NSString* apiKey = | 96 NSString* apiKey = |
93 [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()]; | 97 [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()]; |
94 [components | 98 [components |
95 setQueryItems:@[ [NSURLQueryItem queryItemWithName:kKeyQueryItemName | 99 setQueryItems:@[ [NSURLQueryItem queryItemWithName:kKeyQueryItemName |
96 value:apiKey] ]]; | 100 value:apiKey] ]]; |
97 NSURL* url = [components URL]; | 101 NSURL* url = [components URL]; |
98 request_.reset([[NSMutableURLRequest requestWithURL:url] retain]); | 102 request_.reset([NSMutableURLRequest requestWithURL:url]); |
99 [request_ setHTTPMethod:kHTTPPOSTRequestMethod]; | 103 [request_ setHTTPMethod:kHTTPPOSTRequestMethod]; |
100 | 104 |
101 // body of the POST request. | 105 // body of the POST request. |
102 NSDictionary* jsonBody = | 106 NSDictionary* jsonBody = |
103 @{ kUrlsKey : @[ @{kUrlKey : [[device_ requestURL] absoluteString]} ] }; | 107 @{ kUrlsKey : @[ @{kUrlKey : [[device_ requestURL] absoluteString]} ] }; |
104 [request_ setHTTPBody:[NSJSONSerialization dataWithJSONObject:jsonBody | 108 [request_ setHTTPBody:[NSJSONSerialization dataWithJSONObject:jsonBody |
105 options:0 | 109 options:0 |
106 error:NULL]]; | 110 error:NULL]]; |
107 [request_ setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | 111 [request_ setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; |
108 [request_ setValue:base::SysUTF8ToNSString(GetUserAgent()) | 112 [request_ setValue:base::SysUTF8ToNSString(GetUserAgent()) |
109 forHTTPHeaderField:@"User-Agent"]; | 113 forHTTPHeaderField:@"User-Agent"]; |
110 | 114 |
111 // Set the Accept-Language header from the locale language code. This may | 115 // Set the Accept-Language header from the locale language code. This may |
112 // cause us to fetch metadata for the wrong region in languages such as | 116 // cause us to fetch metadata for the wrong region in languages such as |
113 // Chinese that vary significantly between regions. | 117 // Chinese that vary significantly between regions. |
114 // TODO(mattreynolds): Use the same Accept-Language string as WKWebView. | 118 // TODO(mattreynolds): Use the same Accept-Language string as WKWebView. |
115 NSString* acceptLanguage = | 119 NSString* acceptLanguage = |
116 [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]; | 120 [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]; |
117 [request_ setValue:acceptLanguage forHTTPHeaderField:@"Acccept-Language"]; | 121 [request_ setValue:acceptLanguage forHTTPHeaderField:@"Acccept-Language"]; |
118 | 122 |
119 startDate_.reset([[NSDate date] retain]); | 123 startDate_.reset([NSDate date]); |
120 // Starts the request. | 124 // Starts the request. |
121 NSURLSessionConfiguration* sessionConfiguration = | 125 NSURLSessionConfiguration* sessionConfiguration = |
122 [NSURLSessionConfiguration ephemeralSessionConfiguration]; | 126 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
123 sessionConfiguration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever; | 127 sessionConfiguration.HTTPCookieAcceptPolicy = NSHTTPCookieAcceptPolicyNever; |
124 NSURLSession* session = | 128 NSURLSession* session = |
125 [NSURLSession sessionWithConfiguration:sessionConfiguration | 129 [NSURLSession sessionWithConfiguration:sessionConfiguration |
126 delegate:nil | 130 delegate:nil |
127 delegateQueue:[NSOperationQueue mainQueue]]; | 131 delegateQueue:[NSOperationQueue mainQueue]]; |
128 base::WeakNSObject<PhysicalWebRequest> weakSelf(self); | 132 base::WeakNSObject<PhysicalWebRequest> weakSelf(self); |
129 SessionCompletionProceduralBlock completionHandler = | 133 SessionCompletionProceduralBlock completionHandler = |
130 ^(NSData* data, NSURLResponse* response, NSError* error) { | 134 ^(NSData* data, NSURLResponse* response, NSError* error) { |
131 base::scoped_nsobject<PhysicalWebRequest> strongSelf([weakSelf retain]); | 135 base::scoped_nsobject<PhysicalWebRequest> strongSelf(weakSelf); |
132 if (!strongSelf) { | 136 if (!strongSelf) { |
133 return; | 137 return; |
134 } | 138 } |
135 if (error) { | 139 if (error) { |
136 [strongSelf callBlockWithError:error jsonObject:nil]; | 140 [strongSelf callBlockWithError:error jsonObject:nil]; |
137 } else { | 141 } else { |
138 [strongSelf.get()->data_ appendData:data]; | 142 [strongSelf.get()->data_ appendData:data]; |
139 [strongSelf sessionDidFinishLoading]; | 143 [strongSelf sessionDidFinishLoading]; |
140 } | 144 } |
141 }; | 145 }; |
142 urlSessionTask_.reset([ | 146 urlSessionTask_.reset([session dataTaskWithRequest:request_ |
143 [session dataTaskWithRequest:request_ completionHandler:completionHandler] | 147 completionHandler:completionHandler]); |
144 retain]); | |
145 [urlSessionTask_ resume]; | 148 [urlSessionTask_ resume]; |
146 } | 149 } |
147 | 150 |
148 - (void)sessionDidFinishLoading { | 151 - (void)sessionDidFinishLoading { |
149 NSError* error = nil; | 152 NSError* error = nil; |
150 NSDictionary* jsonObject = | 153 NSDictionary* jsonObject = |
151 [NSJSONSerialization JSONObjectWithData:data_ options:0 error:&error]; | 154 [NSJSONSerialization JSONObjectWithData:data_ options:0 error:&error]; |
152 if (error != nil) { | 155 if (error != nil) { |
153 [self callBlockWithError:error jsonObject:nil]; | 156 [self callBlockWithError:error jsonObject:nil]; |
154 return; | 157 return; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 rssi:[device_ rssi] | 254 rssi:[device_ rssi] |
252 rank:physical_web::kMaxRank | 255 rank:physical_web::kMaxRank |
253 scanTimestamp:[device_ scanTimestamp]]); | 256 scanTimestamp:[device_ scanTimestamp]]); |
254 if (block_.get() != nil) { | 257 if (block_.get() != nil) { |
255 block_.get()(device, nil); | 258 block_.get()(device, nil); |
256 } | 259 } |
257 } | 260 } |
258 } | 261 } |
259 | 262 |
260 @end | 263 @end |
OLD | NEW |