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_GEOLOCATION_PROVIDER_IMPL_H_
6 #define CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/threading/thread.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/geolocation_provider.h"
17 #include "content/public/common/geoposition.h"
20 template<typename Type
> struct DefaultSingletonTraits
;
24 class LocationArbitrator
;
26 class CONTENT_EXPORT GeolocationProviderImpl
27 : public NON_EXPORTED_BASE(GeolocationProvider
),
30 // GeolocationProvider implementation:
31 scoped_ptr
<GeolocationProvider::Subscription
> AddLocationUpdateCallback(
32 const LocationUpdateCallback
& callback
,
33 bool use_high_accuracy
) override
;
34 void UserDidOptIntoLocationServices() override
;
35 void OverrideLocationForTesting(const Geoposition
& position
) override
;
37 // Callback from the LocationArbitrator. Public for testing.
38 void OnLocationUpdate(const Geoposition
& position
);
40 // Gets a pointer to the singleton instance of the location relayer, which
41 // is in turn bound to the browser's global context objects. This must only be
42 // called on the UI thread so that the GeolocationProviderImpl is always
43 // instantiated on the same thread. Ownership is NOT returned.
44 static GeolocationProviderImpl
* GetInstance();
46 bool user_did_opt_into_location_services_for_testing() {
47 return user_did_opt_into_location_services_
;
51 friend struct base::DefaultSingletonTraits
<GeolocationProviderImpl
>;
52 GeolocationProviderImpl();
53 ~GeolocationProviderImpl() override
;
55 // Useful for injecting mock geolocation arbitrator in tests.
56 virtual LocationArbitrator
* CreateArbitrator();
59 bool OnGeolocationThread() const;
61 // Start and stop providers as needed when clients are added or removed.
62 void OnClientsChanged();
64 // Stops the providers when there are no more registered clients. Note that
65 // once the Geolocation thread is started, it will stay alive (but sitting
66 // idle without any pending messages).
69 // Starts the geolocation providers or updates their options (delegates to
71 void StartProviders(bool use_high_accuracy
);
73 // Updates the providers on the geolocation thread, which must be running.
74 void InformProvidersPermissionGranted();
76 // Notifies all registered clients that a position update is available.
77 void NotifyClients(const Geoposition
& position
);
81 void CleanUp() override
;
83 base::CallbackList
<void(const Geoposition
&)> high_accuracy_callbacks_
;
84 base::CallbackList
<void(const Geoposition
&)> low_accuracy_callbacks_
;
86 bool user_did_opt_into_location_services_
;
87 Geoposition position_
;
89 // True only in testing, where we want to use a custom position.
90 bool ignore_location_updates_
;
92 // Only to be used on the geolocation thread.
93 LocationArbitrator
* arbitrator_
;
95 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderImpl
);
98 } // namespace content
100 #endif // CONTENT_BROWSER_GEOLOCATION_GEOLOCATION_PROVIDER_IMPL_H_