1// Copyright (c) 2012 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#ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
6#define CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
7
8#include "base/callback_forward.h"
9#include "base/memory/scoped_vector.h"
10#include "base/strings/string16.h"
11#include "base/time/time.h"
12#include "content/browser/geolocation/location_arbitrator.h"
13#include "content/common/content_export.h"
14#include "content/port/browser/location_provider.h"
15#include "content/public/browser/access_token_store.h"
16#include "content/public/common/geoposition.h"
17#include "net/url_request/url_request_context_getter.h"
18
19namespace net {
20class URLRequestContextGetter;
21}
22
23namespace content {
24class AccessTokenStore;
25class LocationProvider;
26
27// This class is responsible for handling updates from multiple underlying
28// providers and resolving them to a single 'best' location fix at any given
29// moment.
30class CONTENT_EXPORT GeolocationArbitratorImpl
31    : public GeolocationArbitrator {
32 public:
33  // Number of milliseconds newer a location provider has to be that it's worth
34  // switching to this location provider on the basis of it being fresher
35  // (regardles of relative accuracy). Public for tests.
36  static const int64 kFixStaleTimeoutMilliseconds;
37
38  typedef base::Callback<void(const Geoposition&)> LocationUpdateCallback;
39
40  explicit GeolocationArbitratorImpl(const LocationUpdateCallback& callback);
41  virtual ~GeolocationArbitratorImpl();
42
43  static GURL DefaultNetworkProviderURL();
44
45  // GeolocationArbitrator
46  virtual void StartProviders(bool use_high_accuracy) OVERRIDE;
47  virtual void StopProviders() OVERRIDE;
48  virtual void OnPermissionGranted() OVERRIDE;
49  virtual bool HasPermissionBeenGranted() const OVERRIDE;
50
51 protected:
52  AccessTokenStore* GetAccessTokenStore();
53
54  // These functions are useful for injection of dependencies in derived
55  // testing classes.
56  virtual AccessTokenStore* NewAccessTokenStore();
57  virtual LocationProvider* NewNetworkLocationProvider(
58      AccessTokenStore* access_token_store,
59      net::URLRequestContextGetter* context,
60      const GURL& url,
61      const string16& access_token);
62  virtual LocationProvider* NewSystemLocationProvider();
63  virtual base::Time GetTimeNow() const;
64
65 private:
66  // Takes ownership of |provider| on entry; it will either be added to
67  // |providers_| or deleted on error (e.g. it fails to start).
68  void RegisterProvider(LocationProvider* provider);
69  void OnAccessTokenStoresLoaded(
70      AccessTokenStore::AccessTokenSet access_token_store,
71      net::URLRequestContextGetter* context_getter);
72  void DoStartProviders();
73
74  // The providers call this function when a new position is available.
75  void LocationUpdateAvailable(const LocationProvider* provider,
76                               const Geoposition& new_position);
77
78  // Returns true if |new_position| is an improvement over |old_position|.
79  // Set |from_same_provider| to true if both the positions came from the same
80  // provider.
81  bool IsNewPositionBetter(const Geoposition& old_position,
82                           const Geoposition& new_position,
83                           bool from_same_provider) const;
84
85  scoped_refptr<AccessTokenStore> access_token_store_;
86  LocationUpdateCallback callback_;
87  LocationProvider::LocationProviderUpdateCallback provider_callback_;
88  ScopedVector<LocationProvider> providers_;
89  bool use_high_accuracy_;
90  // The provider which supplied the current |position_|
91  const LocationProvider* position_provider_;
92  bool is_permission_granted_;
93  // The current best estimate of our position.
94  Geoposition position_;
95
96  // Tracks whether providers should be running.
97  bool is_running_;
98
99  DISALLOW_COPY_AND_ASSIGN(GeolocationArbitratorImpl);
100};
101
102// Factory functions for the various types of location provider to abstract
103// over the platform-dependent implementations.
104LocationProvider* NewSystemLocationProvider();
105
106}  // namespace content
107
108#endif  // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
109