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+OmniboxGeolocation.h"
7 #import <objc/runtime.h>
9 #include "base/mac/scoped_nsobject.h"
13 // The key needed for objc_setAssociatedObject. Any value will do, because the
14 // address is the key.
15 static char g_acquisitionIntervalKey = 'k';
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
21 // Number of seconds before we will try to refresh the device location.
22 const NSTimeInterval kLocationShouldRefreshAge = 5.0 * 60.0; // 5 minutes
26 @implementation CLLocation (OmniboxGeolocation)
28 - (NSTimeInterval)cr_acquisitionInterval {
30 objc_getAssociatedObject(self, &g_acquisitionIntervalKey);
31 return [interval doubleValue];
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);
41 - (BOOL)cr_isFreshEnough {
42 NSTimeInterval age = -[self.timestamp timeIntervalSinceNow];
43 return (age >= 0) && (age <= kLocationIsFreshAge);
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);