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.
5 #import "ios/chrome/browser/geolocation/CLLocation+XGeoHeader.h"
7 #include "base/basictypes.h"
8 #import "third_party/google_toolbox_for_mac/src/Foundation/GTMStringEncoding.h"
10 NSString* const kGMOLocationDescriptorFormat =
11 @"role: CURRENT_LOCATION\n"
12 @"producer: DEVICE_LOCATION\n"
16 @" latitude_e7: %.f\n"
17 @" longitude_e7: %.f\n"
20 @implementation CLLocation (XGeoHeader)
22 - (NSString*)cr_serializeStringToWebSafeBase64String:(NSString*)data {
23 GTMStringEncoding* encoder =
24 [GTMStringEncoding rfc4648Base64WebsafeStringEncoding];
26 [encoder encode:[data dataUsingEncoding:NSUTF8StringEncoding]];
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];
39 const int64 kSecondsToMicroseconds = 1000000;
40 return (int64)(seconds * kSecondsToMicroseconds);
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
48 - (long)cr_accuracyInMillimeters {
49 const long kMetersToMillimeters = 1000;
50 if (self.horizontalAccuracy > 0) {
51 return (long)(self.horizontalAccuracy * kMetersToMillimeters);
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)];
66 - (NSString*)cr_xGeoString {
67 NSString* locationDescriptor = [self cr_locationDescriptor];
68 // The "a" indicates that it is an ASCII proto.
70 stringWithFormat:@"a %@", [self cr_serializeStringToWebSafeBase64String: