summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.mm
blob: 1449373d84271dae2de0ee428eabc032cd0c69bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ios/chrome/browser/geolocation/CLLocation+OmniboxGeolocation.h"

#import <objc/runtime.h>

#include "base/mac/scoped_nsobject.h"

namespace {

// The key needed for objc_setAssociatedObject. Any value will do, because the
// address is the key.
static char g_acquisitionIntervalKey = 'k';

// Number of seconds before a location is no longer considered to be fresh
// enough to use for an Omnibox query.
const NSTimeInterval kLocationIsFreshAge = 24.0 * 60.0 * 60.0;  // 24 hours

// Number of seconds before we will try to refresh the device location.
const NSTimeInterval kLocationShouldRefreshAge = 5.0 * 60.0;  // 5 minutes

}  // namespace

@implementation CLLocation (OmniboxGeolocation)

- (NSTimeInterval)cr_acquisitionInterval {
  NSNumber* interval =
      objc_getAssociatedObject(self, &g_acquisitionIntervalKey);
  return [interval doubleValue];
}

- (void)cr_setAcquisitionInterval:(NSTimeInterval)interval {
  base::scoped_nsobject<NSNumber> boxedInterval(
      [[NSNumber alloc] initWithDouble:interval]);
  objc_setAssociatedObject(self, &g_acquisitionIntervalKey, boxedInterval.get(),
                           OBJC_ASSOCIATION_RETAIN);
}

- (BOOL)cr_isFreshEnough {
  NSTimeInterval age = -[self.timestamp timeIntervalSinceNow];
  return (age >= 0) && (age <= kLocationIsFreshAge);
}

- (BOOL)cr_shouldRefresh {
  NSTimeInterval age = -[self.timestamp timeIntervalSinceNow];
  // Note: if age < 0 (the location is from the future), don't believe it.
  return (age < 0) || (age > kLocationShouldRefreshAge);
}

@end