OLD | NEW |
(Empty) | |
| 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 <stdint.h> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/callback_forward.h" |
| 12 #include "base/cancelable_callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/strings/string16.h" |
| 16 #include "base/time/time.h" |
| 17 #include "content/browser/geolocation/location_arbitrator.h" |
| 18 #include "content/common/content_export.h" |
| 19 #include "content/public/browser/access_token_store.h" |
| 20 #include "content/public/browser/geolocation_provider.h" |
| 21 #include "content/public/browser/location_provider.h" |
| 22 #include "content/public/common/geoposition.h" |
| 23 #include "net/url_request/url_request_context_getter.h" |
| 24 |
| 25 namespace net { |
| 26 class URLRequestContextGetter; |
| 27 } |
| 28 |
| 29 namespace content { |
| 30 class AccessTokenStore; |
| 31 class GeolocationDelegate; |
| 32 class LocationProvider; |
| 33 |
| 34 // This class is responsible for handling updates from multiple underlying |
| 35 // providers and resolving them to a single 'best' location fix at any given |
| 36 // moment. |
| 37 class CONTENT_EXPORT LocationArbitratorImpl : public LocationArbitrator { |
| 38 public: |
| 39 // Number of milliseconds newer a location provider has to be that it's worth |
| 40 // switching to this location provider on the basis of it being fresher |
| 41 // (regardles of relative accuracy). Public for tests. |
| 42 static const int64_t kFixStaleTimeoutMilliseconds; |
| 43 |
| 44 typedef base::Callback<void(const Geoposition&)> LocationUpdateCallback; |
| 45 |
| 46 LocationArbitratorImpl(const LocationUpdateCallback& callback, |
| 47 GeolocationDelegate* delegate); |
| 48 ~LocationArbitratorImpl() override; |
| 49 |
| 50 static GURL DefaultNetworkProviderURL(); |
| 51 |
| 52 // LocationArbitrator |
| 53 void StartProviders(bool enable_high_accuracy) override; |
| 54 void StopProviders() override; |
| 55 void OnPermissionGranted() override; |
| 56 bool HasPermissionBeenGranted() const override; |
| 57 |
| 58 protected: |
| 59 // These functions are useful for injection of dependencies in derived |
| 60 // testing classes. |
| 61 virtual scoped_refptr<AccessTokenStore> NewAccessTokenStore(); |
| 62 virtual std::unique_ptr<LocationProvider> NewNetworkLocationProvider( |
| 63 const scoped_refptr<AccessTokenStore>& access_token_store, |
| 64 const scoped_refptr<net::URLRequestContextGetter>& context, |
| 65 const GURL& url, |
| 66 const base::string16& access_token); |
| 67 virtual std::unique_ptr<LocationProvider> NewSystemLocationProvider(); |
| 68 virtual base::Time GetTimeNow() const; |
| 69 |
| 70 private: |
| 71 friend class TestingLocationArbitrator; |
| 72 |
| 73 scoped_refptr<AccessTokenStore> GetAccessTokenStore(); |
| 74 |
| 75 // Provider will either be added to |providers_| or |
| 76 // deleted on error (e.g. it fails to start). |
| 77 void RegisterProvider(std::unique_ptr<LocationProvider> provider); |
| 78 |
| 79 void RegisterSystemProvider(); |
| 80 void OnAccessTokenStoresLoaded( |
| 81 AccessTokenStore::AccessTokenMap access_token_map, |
| 82 const scoped_refptr<net::URLRequestContextGetter>& context_getter); |
| 83 void DoStartProviders(); |
| 84 |
| 85 // Gets called when a provider has a new position. |
| 86 void OnLocationUpdate(const LocationProvider* provider, |
| 87 const Geoposition& new_position); |
| 88 |
| 89 // Returns true if |new_position| is an improvement over |old_position|. |
| 90 // Set |from_same_provider| to true if both the positions came from the same |
| 91 // provider. |
| 92 bool IsNewPositionBetter(const Geoposition& old_position, |
| 93 const Geoposition& new_position, |
| 94 bool from_same_provider) const; |
| 95 |
| 96 GeolocationDelegate* delegate_; |
| 97 |
| 98 scoped_refptr<AccessTokenStore> access_token_store_; |
| 99 LocationUpdateCallback arbitrator_update_callback_; |
| 100 LocationProvider::LocationProviderUpdateCallback provider_update_callback_; |
| 101 |
| 102 // The CancelableCallback will prevent OnAccessTokenStoresLoaded from being |
| 103 // called multiple times by calling Reset() at the time of binding. |
| 104 base::CancelableCallback<void( |
| 105 AccessTokenStore::AccessTokenMap, |
| 106 const scoped_refptr<net::URLRequestContextGetter>&)> |
| 107 token_store_callback_; |
| 108 std::vector<std::unique_ptr<LocationProvider>> providers_; |
| 109 bool enable_high_accuracy_; |
| 110 // The provider which supplied the current |position_| |
| 111 const LocationProvider* position_provider_; |
| 112 bool is_permission_granted_; |
| 113 // The current best estimate of our position. |
| 114 Geoposition position_; |
| 115 |
| 116 // Tracks whether providers should be running. |
| 117 bool is_running_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(LocationArbitratorImpl); |
| 120 }; |
| 121 |
| 122 // Factory functions for the various types of location provider to abstract |
| 123 // over the platform-dependent implementations. |
| 124 std::unique_ptr<LocationProvider> NewSystemLocationProvider(); |
| 125 |
| 126 } // namespace content |
| 127 |
| 128 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ |
OLD | NEW |