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" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 device_.reset([device retain]); | 66 device_.reset([device retain]); |
67 } | 67 } |
68 return self; | 68 return self; |
69 } | 69 } |
70 | 70 |
71 - (instancetype)init { | 71 - (instancetype)init { |
72 NOTREACHED(); | 72 NOTREACHED(); |
73 return nil; | 73 return nil; |
74 } | 74 } |
75 | 75 |
| 76 - (NSURL*)requestURL { |
| 77 return [device_ requestURL]; |
| 78 } |
| 79 |
76 - (void)cancel { | 80 - (void)cancel { |
77 [urlSessionTask_ cancel]; | 81 [urlSessionTask_ cancel]; |
78 block_.reset(); | 82 block_.reset(); |
79 } | 83 } |
80 | 84 |
81 - (void)start:(physical_web::RequestFinishedBlock)block { | 85 - (void)start:(physical_web::RequestFinishedBlock)block { |
82 block_.reset([block copy]); | 86 block_.reset([block copy]); |
83 data_.reset([[NSMutableData alloc] init]); | 87 data_.reset([[NSMutableData alloc] init]); |
84 | 88 |
85 // Creates the HTTP post request. | 89 // Creates the HTTP post request. |
86 base::scoped_nsobject<NSURLComponents> components( | 90 base::scoped_nsobject<NSURLComponents> components( |
87 [[NSURLComponents alloc] initWithString:kMetadataServiceUrl]); | 91 [[NSURLComponents alloc] initWithString:kMetadataServiceUrl]); |
88 NSString* apiKey = | 92 NSString* apiKey = |
89 [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()]; | 93 [NSString stringWithUTF8String:google_apis::GetAPIKey().c_str()]; |
90 [components | 94 [components |
91 setQueryItems:@[ [NSURLQueryItem queryItemWithName:kKeyQueryItemName | 95 setQueryItems:@[ [NSURLQueryItem queryItemWithName:kKeyQueryItemName |
92 value:apiKey] ]]; | 96 value:apiKey] ]]; |
93 NSURL* url = [components URL]; | 97 NSURL* url = [components URL]; |
94 request_.reset([[NSMutableURLRequest requestWithURL:url] retain]); | 98 request_.reset([[NSMutableURLRequest requestWithURL:url] retain]); |
95 [request_ setHTTPMethod:kHTTPPOSTRequestMethod]; | 99 [request_ setHTTPMethod:kHTTPPOSTRequestMethod]; |
96 | 100 |
97 // body of the POST request. | 101 // body of the POST request. |
98 NSDictionary* jsonBody = | 102 NSDictionary* jsonBody = |
99 @{ kUrlsKey : @[ @{kUrlKey : [[device_ url] absoluteString]} ] }; | 103 @{ kUrlsKey : @[ @{kUrlKey : [[device_ requestURL] absoluteString]} ] }; |
100 [request_ setHTTPBody:[NSJSONSerialization dataWithJSONObject:jsonBody | 104 [request_ setHTTPBody:[NSJSONSerialization dataWithJSONObject:jsonBody |
101 options:0 | 105 options:0 |
102 error:NULL]]; | 106 error:NULL]]; |
103 [request_ setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; | 107 [request_ setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; |
104 [request_ setValue:base::SysUTF8ToNSString(GetUserAgent()) | 108 [request_ setValue:base::SysUTF8ToNSString(GetUserAgent()) |
105 forHTTPHeaderField:@"User-Agent"]; | 109 forHTTPHeaderField:@"User-Agent"]; |
106 | 110 |
107 // Set the Accept-Language header from the locale language code. This may | 111 // Set the Accept-Language header from the locale language code. This may |
108 // cause us to fetch metadata for the wrong region in languages such as | 112 // cause us to fetch metadata for the wrong region in languages such as |
109 // Chinese that vary significantly between regions. | 113 // Chinese that vary significantly between regions. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 block_.get()(nil, error); | 229 block_.get()(nil, error); |
226 } | 230 } |
227 return; | 231 return; |
228 } | 232 } |
229 | 233 |
230 // Read optional fields. | 234 // Read optional fields. |
231 NSString* iconString = base::mac::ObjCCast<NSString>(pageInfo[kIconKey]); | 235 NSString* iconString = base::mac::ObjCCast<NSString>(pageInfo[kIconKey]); |
232 NSString* description = | 236 NSString* description = |
233 base::mac::ObjCCast<NSString>(pageInfo[kDescriptionKey]); | 237 base::mac::ObjCCast<NSString>(pageInfo[kDescriptionKey]); |
234 NSString* title = base::mac::ObjCCast<NSString>(pageInfo[kTitleKey]); | 238 NSString* title = base::mac::ObjCCast<NSString>(pageInfo[kTitleKey]); |
235 NSURL* scannedUrl = | |
236 scannedUrlString ? [NSURL URLWithString:scannedUrlString] : nil; | |
237 NSURL* resolvedUrl = | 239 NSURL* resolvedUrl = |
238 resolvedUrlString ? [NSURL URLWithString:resolvedUrlString] : nil; | 240 resolvedUrlString ? [NSURL URLWithString:resolvedUrlString] : nil; |
239 NSURL* icon = iconString ? [NSURL URLWithString:iconString] : nil; | 241 NSURL* icon = iconString ? [NSURL URLWithString:iconString] : nil; |
240 base::scoped_nsobject<PhysicalWebDevice> device([[PhysicalWebDevice alloc] | 242 base::scoped_nsobject<PhysicalWebDevice> device([[PhysicalWebDevice alloc] |
241 initWithURL:resolvedUrl | 243 initWithURL:resolvedUrl |
242 requestURL:scannedUrl | 244 requestURL:[device_ requestURL] |
243 icon:icon | 245 icon:icon |
244 title:title | 246 title:title |
245 description:description | 247 description:description |
246 transmitPower:[device_ transmitPower] | 248 transmitPower:[device_ transmitPower] |
247 rssi:[device_ rssi] | 249 rssi:[device_ rssi] |
248 rank:physical_web::kMaxRank]); | 250 rank:physical_web::kMaxRank |
| 251 scanTimestamp:[device_ scanTimestamp]]); |
249 if (block_.get() != nil) { | 252 if (block_.get() != nil) { |
250 block_.get()(device, nil); | 253 block_.get()(device, nil); |
251 } | 254 } |
252 } | 255 } |
253 } | 256 } |
254 | 257 |
255 @end | 258 @end |
OLD | NEW |