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