OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 5 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ |
6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ |
7 | 7 |
8 #include "base/memory/scoped_vector.h" | |
9 #include "base/string16.h" | |
10 #include "base/time.h" | |
11 #include "content/browser/geolocation/location_provider.h" | |
12 #include "content/browser/geolocation/geolocation_observer.h" | |
13 #include "content/common/content_export.h" | 8 #include "content/common/content_export.h" |
14 #include "content/public/browser/access_token_store.h" | |
15 #include "content/public/common/geoposition.h" | |
16 #include "net/url_request/url_request_context_getter.h" | |
17 | |
18 namespace net { | |
19 class URLRequestContextGetter; | |
20 } | |
21 | 9 |
22 namespace content { | 10 namespace content { |
23 class AccessTokenStore; | 11 |
24 class LocationProviderBase; | 12 struct GeolocationObserverOptions; |
25 | 13 |
26 // This class is responsible for handling updates from multiple underlying | 14 // This class is responsible for handling updates from multiple underlying |
27 // providers and resolving them to a single 'best' location fix at any given | 15 // providers and resolving them to a single 'best' location fix at any given |
28 // moment. | 16 // moment. |
29 class CONTENT_EXPORT GeolocationArbitrator | 17 class CONTENT_EXPORT GeolocationArbitrator { |
30 : public LocationProviderBase::ListenerInterface { | 18 public: |
31 public: | 19 virtual ~GeolocationArbitrator() {}; |
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; | |
36 | |
37 explicit GeolocationArbitrator(GeolocationObserver* observer); | |
38 virtual ~GeolocationArbitrator(); | |
39 | |
40 static GURL DefaultNetworkProviderURL(); | |
41 | 20 |
42 // See more details in geolocation_provider. | 21 // See more details in geolocation_provider. |
43 void StartProviders(const GeolocationObserverOptions& options); | 22 virtual void StartProviders(const GeolocationObserverOptions& options) = 0; |
44 void StopProviders(); | 23 virtual void StopProviders() = 0; |
45 | 24 |
46 // Called everytime permission is granted to a page for using geolocation. | 25 // Called everytime permission is granted to a page for using geolocation. |
47 // This may either be through explicit user action (e.g. responding to the | 26 // This may either be through explicit user action (e.g. responding to the |
48 // infobar prompt) or inferred from a persisted site permission. | 27 // infobar prompt) or inferred from a persisted site permission. |
49 // The arbitrator will inform all providers of this, which may in turn use | 28 // The arbitrator will inform all providers of this, which may in turn use |
50 // this information to modify their internal policy. | 29 // this information to modify their internal policy. |
51 void OnPermissionGranted(); | 30 virtual void OnPermissionGranted() = 0; |
52 | 31 |
53 // Returns true if this arbitrator has received at least one call to | 32 // Returns true if this arbitrator has received at least one call to |
54 // OnPermissionGranted(). | 33 // OnPermissionGranted(). |
55 bool HasPermissionBeenGranted() const; | 34 virtual bool HasPermissionBeenGranted() const = 0; |
56 | |
57 // ListenerInterface | |
58 virtual void LocationUpdateAvailable(LocationProviderBase* provider) OVERRIDE; | |
59 | |
60 protected: | |
61 | |
62 AccessTokenStore* GetAccessTokenStore(); | |
63 | |
64 // These functions are useful for injection of dependencies in derived | |
65 // testing classes. | |
66 virtual AccessTokenStore* NewAccessTokenStore(); | |
67 virtual LocationProviderBase* NewNetworkLocationProvider( | |
68 AccessTokenStore* access_token_store, | |
69 net::URLRequestContextGetter* context, | |
70 const GURL& url, | |
71 const string16& access_token); | |
72 virtual LocationProviderBase* NewSystemLocationProvider(); | |
73 virtual base::Time GetTimeNow() const; | |
74 | |
75 private: | |
76 // Takes ownership of |provider| on entry; it will either be added to | |
77 // |providers_| or deleted on error (e.g. it fails to start). | |
78 void RegisterProvider(LocationProviderBase* provider); | |
79 void OnAccessTokenStoresLoaded( | |
80 AccessTokenStore::AccessTokenSet access_token_store, | |
81 net::URLRequestContextGetter* context_getter); | |
82 void DoStartProviders(); | |
83 // Returns true if |new_position| is an improvement over |old_position|. | |
84 // Set |from_same_provider| to true if both the positions came from the same | |
85 // provider. | |
86 bool IsNewPositionBetter(const Geoposition& old_position, | |
87 const Geoposition& new_position, | |
88 bool from_same_provider) const; | |
89 | |
90 scoped_refptr<AccessTokenStore> access_token_store_; | |
91 GeolocationObserver* observer_; | |
92 ScopedVector<LocationProviderBase> providers_; | |
93 GeolocationObserverOptions current_provider_options_; | |
94 // The provider which supplied the current |position_| | |
95 const LocationProviderBase* position_provider_; | |
96 bool is_permission_granted_; | |
97 // The current best estimate of our position. | |
98 Geoposition position_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(GeolocationArbitrator); | |
101 }; | 35 }; |
102 | 36 |
103 } // namespace content | 37 } // namespace content |
104 | 38 |
105 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 39 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_ |
OLD | NEW |