OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "ios/chrome/browser/geolocation/CLLocation+XGeoHeader.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #import "third_party/google_toolbox_for_mac/src/Foundation/GTMStringEncoding.h" |
| 9 |
| 10 NSString* const kGMOLocationDescriptorFormat = |
| 11 @"role: CURRENT_LOCATION\n" |
| 12 @"producer: DEVICE_LOCATION\n" |
| 13 @"timestamp: %lld\n" |
| 14 @"radius: %ld\n" |
| 15 @"latlng <\n" |
| 16 @" latitude_e7: %.f\n" |
| 17 @" longitude_e7: %.f\n" |
| 18 @">"; |
| 19 |
| 20 @implementation CLLocation (XGeoHeader) |
| 21 |
| 22 - (NSString*)cr_serializeStringToWebSafeBase64String:(NSString*)data { |
| 23 GTMStringEncoding* encoder = |
| 24 [GTMStringEncoding rfc4648Base64WebsafeStringEncoding]; |
| 25 NSString* base64 = |
| 26 [encoder encode:[data dataUsingEncoding:NSUTF8StringEncoding]]; |
| 27 if (base64) { |
| 28 return base64; |
| 29 } else { |
| 30 return @""; |
| 31 } |
| 32 } |
| 33 |
| 34 // Returns the timestamp of this location in microseconds since the UNIX epoch. |
| 35 // Returns 0 if the timestamp is unavailable or invalid. |
| 36 - (int64)cr_timestampInMicroseconds { |
| 37 NSTimeInterval seconds = [self.timestamp timeIntervalSince1970]; |
| 38 if (seconds > 0) { |
| 39 const int64 kSecondsToMicroseconds = 1000000; |
| 40 return (int64)(seconds * kSecondsToMicroseconds); |
| 41 } |
| 42 return 0; |
| 43 } |
| 44 |
| 45 // Returns the horizontal accuracy radius of |location|. The smaller the value, |
| 46 // the more accurate the location. A value -1 is returned if accuracy is |
| 47 // unavailable. |
| 48 - (long)cr_accuracyInMillimeters { |
| 49 const long kMetersToMillimeters = 1000; |
| 50 if (self.horizontalAccuracy > 0) { |
| 51 return (long)(self.horizontalAccuracy * kMetersToMillimeters); |
| 52 } |
| 53 return -1L; |
| 54 } |
| 55 |
| 56 // Returns the LocationDescriptor as an ASCII proto. |
| 57 - (NSString*)cr_locationDescriptor { |
| 58 // Construct the location descriptor using its format string. |
| 59 return [NSString stringWithFormat:kGMOLocationDescriptorFormat, |
| 60 [self cr_timestampInMicroseconds], |
| 61 [self cr_accuracyInMillimeters], |
| 62 floor(self.coordinate.latitude * 1e7), |
| 63 floor(self.coordinate.longitude * 1e7)]; |
| 64 } |
| 65 |
| 66 - (NSString*)cr_xGeoString { |
| 67 NSString* locationDescriptor = [self cr_locationDescriptor]; |
| 68 // The "a" indicates that it is an ASCII proto. |
| 69 return [NSString |
| 70 stringWithFormat:@"a %@", [self cr_serializeStringToWebSafeBase64String: |
| 71 locationDescriptor]]; |
| 72 } |
| 73 |
| 74 @end |
OLD | NEW |