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_GEOLOCATION_PROVIDER_H_ | 5 #ifndef CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |
6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ | 6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "content/browser/geolocation/geolocation_observer.h" | 12 #include "content/browser/geolocation/geolocation_observer.h" |
13 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
14 #include "content/common/geoposition.h" | 14 #include "content/common/geoposition.h" |
15 | 15 |
16 class GeolocationArbitrator; | 16 class GeolocationArbitrator; |
17 | 17 |
18 template<typename Type> | 18 template<typename Type> |
19 struct DefaultSingletonTraits; | 19 struct DefaultSingletonTraits; |
20 | 20 |
21 // This is the main API to the geolocation subsystem. The application | 21 // This is the main API to the geolocation subsystem. The application will hold |
22 // will hold a single instance of this class, and can register multiple | 22 // a single instance of this class and can register multiple observers which |
23 // observers which will be notified of location updates. Underlying location | 23 // will be notified of location updates. The application must instantiate the |
24 // arbitrator will only be enabled whilst there is at least one observer | 24 // GeolocationProvider on the IO thread and must communicate with it on the same |
25 // registered. | 25 // thread. |
26 // The underlying location arbitrator will only be enabled whilst there is at | |
27 // least one observer registered. The arbitrator and the location providers it | |
28 // uses run on a separate Geolocation thread. | |
26 class CONTENT_EXPORT GeolocationProvider | 29 class CONTENT_EXPORT GeolocationProvider |
27 : public base::Thread, public GeolocationObserver { | 30 : public base::Thread, public GeolocationObserver { |
28 public: | 31 public: |
29 GeolocationProvider(); | 32 // The GeolocationObserverOptions passed are used as a 'hint' for the provider |
30 | 33 // preferences for this particular observer, however the observer could |
31 // Must be called from the same thread as the GeolocationProvider was created | 34 // receive callbacks for best available locations from any active provider |
32 // on. The GeolocationObserverOptions passed are used as a 'hint' for the | 35 // whilst it is registered. |
33 // provider preferences for this particular observer, however the observer | 36 // If an existing observer is added a second time, its options are updated |
34 // could receive callbacks for best available locations from any active | |
35 // provider whilst it is registered. | |
36 // If an existing observer is added a second time it's options are updated | |
37 // but only a single call to RemoveObserver() is required to remove it. | 37 // but only a single call to RemoveObserver() is required to remove it. |
38 void AddObserver(GeolocationObserver* delegate, | 38 void AddObserver(GeolocationObserver* delegate, |
39 const GeolocationObserverOptions& update_options); | 39 const GeolocationObserverOptions& update_options); |
40 | 40 |
41 // Remove a previously registered observer. No-op if not previously registered | 41 // Remove a previously registered observer. No-op if not previously registered |
42 // via AddObserver(). Returns true if the observer was removed. | 42 // via AddObserver(). Returns true if the observer was removed. |
43 bool RemoveObserver(GeolocationObserver* delegate); | 43 bool RemoveObserver(GeolocationObserver* delegate); |
44 | 44 |
45 void OnPermissionGranted(); | 45 void OnPermissionGranted(); |
46 bool HasPermissionBeenGranted() const; | 46 bool HasPermissionBeenGranted() const; |
47 | 47 |
48 // GeolocationObserver | 48 // GeolocationObserver |
49 virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE; | 49 virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE; |
50 | 50 |
51 // Gets a pointer to the singleton instance of the location relayer, which | 51 // Gets a pointer to the singleton instance of the location relayer, which |
52 // is in turn bound to the browser's global context objects. Ownership is NOT | 52 // is in turn bound to the browser's global context objects. This must only be |
53 // returned. | 53 // called on the IO thread so that the GeolocationProvider is always |
54 // instantiated on the same thread. Ownership is NOT returned. | |
54 static GeolocationProvider* GetInstance(); | 55 static GeolocationProvider* GetInstance(); |
55 | 56 |
56 typedef std::map<GeolocationObserver*, GeolocationObserverOptions> | 57 typedef std::map<GeolocationObserver*, GeolocationObserverOptions> |
57 ObserverMap; | 58 ObserverMap; |
58 | 59 |
59 private: | 60 protected: |
joth
2012/04/23 10:05:14
comment why protected. (derivation required for te
bartfab (slow)
2012/04/23 12:21:28
Done.
| |
60 friend struct DefaultSingletonTraits<GeolocationProvider>; | 61 friend struct DefaultSingletonTraits<GeolocationProvider>; |
62 GeolocationProvider(); | |
61 virtual ~GeolocationProvider(); | 63 virtual ~GeolocationProvider(); |
62 | 64 |
63 bool OnClientThread() const; | 65 private: |
66 static bool OnIOThread(); | |
64 bool OnGeolocationThread() const; | 67 bool OnGeolocationThread() const; |
65 | 68 |
66 // When the observer list changes, we may start the thread and the required | 69 // When the observer list changes, we may start the thread and the required |
67 // providers, or stop them. | 70 // providers, or stop them. |
68 void OnObserversChanged(); | 71 void OnObserversChanged(); |
69 | 72 |
70 // Stop the providers when there are no more observers. Note that once our | 73 // Stop the providers when there are no more observers. Note that once our |
71 // thread is started, we'll keep it alive (but with no pending messages). | 74 // thread is started, we'll keep it alive (but with no pending messages). |
72 void StopProviders(); | 75 void StopProviders(); |
73 | 76 |
74 // Starts or updates the observers' geolocation options | 77 // Starts or updates the observers' geolocation options |
75 // (delegates to arbitrator). | 78 // (delegates to arbitrator). |
76 void StartProviders(const GeolocationObserverOptions& options); | 79 void StartProviders(const GeolocationObserverOptions& options); |
77 | 80 |
78 // Update the providers on the geolocation thread, which must be running. | 81 // Update the providers on the geolocation thread, which must be running. |
79 void InformProvidersPermissionGranted(); | 82 void InformProvidersPermissionGranted(); |
80 | 83 |
81 // Notifies observers when a new position fix is available. | 84 // Notifies observers when a new position fix is available. |
82 void NotifyObservers(const Geoposition& position); | 85 void NotifyObservers(const Geoposition& position); |
83 | 86 |
84 // Thread | 87 // Thread |
85 virtual void Init() OVERRIDE; | 88 virtual void Init() OVERRIDE; |
86 virtual void CleanUp() OVERRIDE; | 89 virtual void CleanUp() OVERRIDE; |
87 | 90 |
88 scoped_refptr<base::MessageLoopProxy> client_loop_; | 91 // Only used on the IO thread |
89 | |
90 // Only used on client thread | |
91 ObserverMap observers_; | 92 ObserverMap observers_; |
92 bool is_permission_granted_; | 93 bool is_permission_granted_; |
93 Geoposition position_; | 94 Geoposition position_; |
94 | 95 |
95 // Only to be used on the geolocation thread. | 96 // Only to be used on the geolocation thread. |
96 GeolocationArbitrator* arbitrator_; | 97 GeolocationArbitrator* arbitrator_; |
97 | 98 |
98 DISALLOW_COPY_AND_ASSIGN(GeolocationProvider); | 99 DISALLOW_COPY_AND_ASSIGN(GeolocationProvider); |
99 }; | 100 }; |
100 | 101 |
101 #endif // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ | 102 #endif // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_H_ |
OLD | NEW |