| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 5 #ifndef CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ |
| 6 #define CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 6 #define CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ |
| 7 | 7 |
| 8 #include "base/ref_counted.h" |
| 9 |
| 8 class AccessTokenStore; | 10 class AccessTokenStore; |
| 9 class URLRequestContextGetter; | 11 class URLRequestContextGetter; |
| 10 struct Geoposition; | 12 struct Geoposition; |
| 11 | 13 |
| 12 // This is the main API to the geolocaiton subsystem. Typically the application | 14 // This is the main API to the geolocaiton subsystem. Typically the application |
| 13 // will hold a single instance of this class, and can register multiple | 15 // will hold a single instance of this class, and can register multiple |
| 14 // observers which will be notified of location updates. Underlying location | 16 // observers which will be notified of location updates. Underlying location |
| 15 // provider(s) will only be enabled whilst there is at least one observer | 17 // provider(s) will only be enabled whilst there is at least one observer |
| 16 // registered. | 18 // registered. |
| 17 // This class is responsible for handling updates from multiple underlying | 19 // This class is responsible for handling updates from multiple underlying |
| 18 // providers and resolving them to a single 'best' location fix at any given | 20 // providers and resolving them to a single 'best' location fix at any given |
| 19 // moment. | 21 // moment. |
| 20 class GeolocationArbitrator { | 22 class GeolocationArbitrator : public base::RefCounted<GeolocationArbitrator> { |
| 21 public: | 23 public: |
| 22 // Creates and returns a new instance of the location arbitrator. Will use | 24 // Creates and returns a new instance of the location arbitrator. Allows |
| 23 // the default access token store implementation bound to Chrome Prefs. | 25 // injection of the access token store and url getter context, for testing. |
| 24 static GeolocationArbitrator* Create(URLRequestContextGetter* context_getter); | |
| 25 // Creates and returns a new instance of the location arbitrator. As above | |
| 26 // but allows injectino of the access token store, for testing. | |
| 27 static GeolocationArbitrator* Create( | 26 static GeolocationArbitrator* Create( |
| 28 AccessTokenStore* access_token_store, | 27 AccessTokenStore* access_token_store, |
| 29 URLRequestContextGetter* context_getter); | 28 URLRequestContextGetter* context_getter); |
| 30 | 29 |
| 30 // Gets a pointer to the singleton instance of the location arbitrator, which |
| 31 // is in turn bound to the browser's global context objects. Ownership is NOT |
| 32 // returned. |
| 33 static GeolocationArbitrator* GetInstance(); |
| 34 |
| 31 class Delegate { | 35 class Delegate { |
| 32 public: | 36 public: |
| 33 // This will be called whenever the 'best available' location is updated, | 37 // This will be called whenever the 'best available' location is updated, |
| 34 // or when an error is encountered meaning no location data will be | 38 // or when an error is encountered meaning no location data will be |
| 35 // available in the forseeable future. | 39 // available in the forseeable future. |
| 36 virtual void OnLocationUpdate(const Geoposition& position) = 0; | 40 virtual void OnLocationUpdate(const Geoposition& position) = 0; |
| 37 | 41 |
| 38 protected: | 42 protected: |
| 39 virtual ~Delegate() {} | 43 virtual ~Delegate() {} |
| 40 }; | 44 }; |
| 41 struct UpdateOptions { | 45 struct UpdateOptions { |
| 42 UpdateOptions() : use_high_accuracy(false) {} | 46 UpdateOptions() : use_high_accuracy(false) {} |
| 43 explicit UpdateOptions(bool high_accuracy) | 47 explicit UpdateOptions(bool high_accuracy) |
| 44 : use_high_accuracy(high_accuracy) {} | 48 : use_high_accuracy(high_accuracy) {} |
| 45 bool use_high_accuracy; | 49 bool use_high_accuracy; |
| 46 }; | 50 }; |
| 47 | 51 |
| 48 virtual ~GeolocationArbitrator(); | |
| 49 | |
| 50 // Must be called from the same thread as the arbitrator was created on. | 52 // Must be called from the same thread as the arbitrator was created on. |
| 51 // The update options passed are used as a 'hint' for the provider preferences | 53 // The update options passed are used as a 'hint' for the provider preferences |
| 52 // for this particular observer, however the delegate could receive callbacks | 54 // for this particular observer, however the delegate could receive callbacks |
| 53 // for best available locations from any active provider whilst it is | 55 // for best available locations from any active provider whilst it is |
| 54 // registerd. | 56 // registerd. |
| 55 // If an existing delegate is added a second time it's options are updated | 57 // If an existing delegate is added a second time it's options are updated |
| 56 // but only a single call to RemoveObserver() is required to remove it. | 58 // but only a single call to RemoveObserver() is required to remove it. |
| 57 virtual void AddObserver(Delegate* delegate, | 59 virtual void AddObserver(Delegate* delegate, |
| 58 const UpdateOptions& update_options) = 0; | 60 const UpdateOptions& update_options) = 0; |
| 59 // Remove a previously registered observer. No-op if not previously registered | 61 // Remove a previously registered observer. No-op if not previously registered |
| 60 // via AddObserver(). Returns true if the observer was removed. | 62 // via AddObserver(). Returns true if the observer was removed. |
| 61 virtual bool RemoveObserver(Delegate* delegate) = 0; | 63 virtual bool RemoveObserver(Delegate* delegate) = 0; |
| 62 | 64 |
| 63 // TODO(joth): This is a stop-gap for testing; once we have decoupled | 65 // TODO(joth): This is a stop-gap for testing; once we have decoupled |
| 64 // provider factory we should extract mock creation from the arbitrator. | 66 // provider factory we should extract mock creation from the arbitrator. |
| 65 virtual void SetUseMockProvider(bool use_mock) = 0; | 67 static void SetUseMockProvider(bool use_mock); |
| 68 |
| 69 protected: |
| 70 friend class base::RefCounted<GeolocationArbitrator>; |
| 71 GeolocationArbitrator(); |
| 72 // RefCounted object; no not delete directly. |
| 73 virtual ~GeolocationArbitrator(); |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(GeolocationArbitrator); |
| 66 }; | 77 }; |
| 67 | 78 |
| 68 #endif // CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ | 79 #endif // CHROME_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_H_ |
| OLD | NEW |