OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/browser/geolocation/CLLocation+OmniboxGeolocation.h" | 5 #import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h" |
6 | 6 |
7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
8 | 8 |
9 #include "base/mac/scoped_nsobject.h" | 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 // The key needed for objc_setAssociatedObject. Any value will do, because the | 15 // The key needed for objc_setAssociatedObject. Any value will do, because the |
14 // address is the key. | 16 // address is the key. |
15 static char g_acquisitionIntervalKey = 'k'; | 17 static char g_acquisitionIntervalKey = 'k'; |
16 | 18 |
17 // Number of seconds before a location is no longer considered to be fresh | 19 // Number of seconds before a location is no longer considered to be fresh |
18 // enough to use for an Omnibox query. | 20 // enough to use for an Omnibox query. |
19 const NSTimeInterval kLocationIsFreshAge = 24.0 * 60.0 * 60.0; // 24 hours | 21 const NSTimeInterval kLocationIsFreshAge = 24.0 * 60.0 * 60.0; // 24 hours |
20 | 22 |
21 // Number of seconds before we will try to refresh the device location. | 23 // Number of seconds before we will try to refresh the device location. |
22 const NSTimeInterval kLocationShouldRefreshAge = 5.0 * 60.0; // 5 minutes | 24 const NSTimeInterval kLocationShouldRefreshAge = 5.0 * 60.0; // 5 minutes |
23 | 25 |
24 } // namespace | 26 } // namespace |
25 | 27 |
26 @implementation CLLocation (OmniboxGeolocation) | 28 @implementation CLLocation (OmniboxGeolocation) |
27 | 29 |
28 - (NSTimeInterval)cr_acquisitionInterval { | 30 - (NSTimeInterval)cr_acquisitionInterval { |
29 NSNumber* interval = | 31 NSNumber* interval = |
30 objc_getAssociatedObject(self, &g_acquisitionIntervalKey); | 32 objc_getAssociatedObject(self, &g_acquisitionIntervalKey); |
31 return [interval doubleValue]; | 33 return [interval doubleValue]; |
32 } | 34 } |
33 | 35 |
34 - (void)cr_setAcquisitionInterval:(NSTimeInterval)interval { | 36 - (void)cr_setAcquisitionInterval:(NSTimeInterval)interval { |
35 base::scoped_nsobject<NSNumber> boxedInterval( | 37 NSNumber* boxedInterval = [[NSNumber alloc] initWithDouble:interval]; |
36 [[NSNumber alloc] initWithDouble:interval]); | 38 objc_setAssociatedObject(self, &g_acquisitionIntervalKey, boxedInterval, |
37 objc_setAssociatedObject(self, &g_acquisitionIntervalKey, boxedInterval.get(), | |
38 OBJC_ASSOCIATION_RETAIN); | 39 OBJC_ASSOCIATION_RETAIN); |
39 } | 40 } |
40 | 41 |
41 - (BOOL)cr_isFreshEnough { | 42 - (BOOL)cr_isFreshEnough { |
42 NSTimeInterval age = -[self.timestamp timeIntervalSinceNow]; | 43 NSTimeInterval age = -[self.timestamp timeIntervalSinceNow]; |
43 return (age >= 0) && (age <= kLocationIsFreshAge); | 44 return (age >= 0) && (age <= kLocationIsFreshAge); |
44 } | 45 } |
45 | 46 |
46 - (BOOL)cr_shouldRefresh { | 47 - (BOOL)cr_shouldRefresh { |
47 NSTimeInterval age = -[self.timestamp timeIntervalSinceNow]; | 48 NSTimeInterval age = -[self.timestamp timeIntervalSinceNow]; |
48 // Note: if age < 0 (the location is from the future), don't believe it. | 49 // Note: if age < 0 (the location is from the future), don't believe it. |
49 return (age < 0) || (age > kLocationShouldRefreshAge); | 50 return (age < 0) || (age > kLocationShouldRefreshAge); |
50 } | 51 } |
51 | 52 |
52 @end | 53 @end |
OLD | NEW |