Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / geolocation / location_arbitrator_impl.h
blob3ef7d5dfc6e382f588593d40fd0f4f87f72b3db2
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.
5 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
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/public/browser/access_token_store.h"
15 #include "content/public/browser/location_provider.h"
16 #include "content/public/common/geoposition.h"
17 #include "net/url_request/url_request_context_getter.h"
19 namespace net {
20 class URLRequestContextGetter;
23 namespace content {
24 class AccessTokenStore;
25 class LocationProvider;
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.
30 class CONTENT_EXPORT LocationArbitratorImpl : public LocationArbitrator {
31 public:
32 // Number of milliseconds newer a location provider has to be that it's worth
33 // switching to this location provider on the basis of it being fresher
34 // (regardles of relative accuracy). Public for tests.
35 static const int64 kFixStaleTimeoutMilliseconds;
37 typedef base::Callback<void(const Geoposition&)> LocationUpdateCallback;
39 explicit LocationArbitratorImpl(const LocationUpdateCallback& callback);
40 ~LocationArbitratorImpl() override;
42 static GURL DefaultNetworkProviderURL();
44 // LocationArbitrator
45 void StartProviders(bool use_high_accuracy) override;
46 void StopProviders() override;
47 void OnPermissionGranted() override;
48 bool HasPermissionBeenGranted() const override;
50 protected:
51 AccessTokenStore* GetAccessTokenStore();
53 // These functions are useful for injection of dependencies in derived
54 // testing classes.
55 virtual AccessTokenStore* NewAccessTokenStore();
56 virtual LocationProvider* NewNetworkLocationProvider(
57 AccessTokenStore* access_token_store,
58 net::URLRequestContextGetter* context,
59 const GURL& url,
60 const base::string16& access_token);
61 virtual LocationProvider* NewSystemLocationProvider();
62 virtual base::Time GetTimeNow() const;
64 private:
65 // Takes ownership of |provider| on entry; it will either be added to
66 // |providers_| or deleted on error (e.g. it fails to start).
67 void RegisterProvider(LocationProvider* provider);
68 void OnAccessTokenStoresLoaded(
69 AccessTokenStore::AccessTokenSet access_token_store,
70 net::URLRequestContextGetter* context_getter);
71 void DoStartProviders();
73 // Gets called when a provider has a new position.
74 void OnLocationUpdate(const LocationProvider* provider,
75 const Geoposition& new_position);
77 // Returns true if |new_position| is an improvement over |old_position|.
78 // Set |from_same_provider| to true if both the positions came from the same
79 // provider.
80 bool IsNewPositionBetter(const Geoposition& old_position,
81 const Geoposition& new_position,
82 bool from_same_provider) const;
84 scoped_refptr<AccessTokenStore> access_token_store_;
85 LocationUpdateCallback arbitrator_update_callback_;
86 LocationProvider::LocationProviderUpdateCallback provider_update_callback_;
87 ScopedVector<LocationProvider> providers_;
88 bool use_high_accuracy_;
89 // The provider which supplied the current |position_|
90 const LocationProvider* position_provider_;
91 bool is_permission_granted_;
92 // The current best estimate of our position.
93 Geoposition position_;
95 // Tracks whether providers should be running.
96 bool is_running_;
98 DISALLOW_COPY_AND_ASSIGN(LocationArbitratorImpl);
101 // Factory functions for the various types of location provider to abstract
102 // over the platform-dependent implementations.
103 LocationProvider* NewSystemLocationProvider();
105 } // namespace content
107 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_