[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / geolocation / location_provider.h
blob67b124aead745ef8e5342956bbdd9fb8bf3869e4
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 // A location provider provides position information from a particular source
6 // (GPS, network etc).
7 //
8 // This file declares a base class to be used by all location providers.
9 // Primarily, this class declares interface methods to be implemented by
10 // derived classes.
12 #ifndef CONTENT_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_
13 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_
15 #include <map>
17 #include "base/string16.h"
18 #include "base/threading/non_thread_safe.h"
19 #include "content/common/content_export.h"
21 class GURL;
23 namespace net {
24 class URLRequestContextGetter;
27 namespace content {
28 class AccessTokenStore;
29 struct Geoposition;
31 // The base class used by all location providers.
32 class CONTENT_EXPORT LocationProviderBase
33 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
34 public:
35 // Clients of the location provider must implement this interface. All call-
36 // backs to this interface will happen in the context of the thread on which
37 // the location provider was created.
38 class CONTENT_EXPORT ListenerInterface {
39 public:
40 // Used to inform listener that a new position fix is available or that a
41 // fatal error has occurred. Providers should call this for new listeners
42 // as soon as a position is available.
43 virtual void LocationUpdateAvailable(LocationProviderBase* provider) = 0;
45 protected:
46 virtual ~ListenerInterface() {}
49 virtual ~LocationProviderBase();
51 // Registers a listener, which will be called back on
52 // ListenerInterface::LocationUpdateAvailable as soon as a position is
53 // available and again whenever a new position is available. Ref counts the
54 // listener to handle multiple calls to this method.
55 void RegisterListener(ListenerInterface* listener);
56 // Unregisters a listener. Unrefs the listener to handle multiple calls to
57 // this method. Once the ref count reaches zero, the listener is removed and
58 // once this method returns, no further calls to
59 // ListenerInterface::LocationUpdateAvailable will be made for this listener.
60 // It may block if a callback is in progress.
61 void UnregisterListener(ListenerInterface* listener);
63 // Interface methods
64 // StartProvider maybe called multiple times, e.g. to alter the
65 // |high_accuracy| setting. Returns false if a fatal error was encountered
66 // which prevented the provider from starting.
67 virtual bool StartProvider(bool high_accuracy) = 0;
68 virtual void StopProvider() = 0;
69 // Gets the current best position estimate.
70 virtual void GetPosition(Geoposition* position) = 0;
71 // Provides a hint to the provider that new location data is needed as soon
72 // as possible. Default implementation does nothing.
73 virtual void UpdatePosition() {}
74 // Delegated to the provider by GeolocationArbitrator. See the corresponding
75 // method on that class for more details.
76 virtual void OnPermissionGranted() {}
78 bool has_listeners() const;
80 protected:
81 LocationProviderBase();
83 // Inform listeners that a new position or error is available, using
84 // LocationUpdateAvailable.
85 void UpdateListeners();
87 private:
88 // The listeners registered to this provider. For each listener, we store a
89 // ref count.
90 typedef std::map<ListenerInterface*, int> ListenerMap;
91 ListenerMap listeners_;
94 // Factory functions for the various types of location provider to abstract
95 // over the platform-dependent implementations.
96 CONTENT_EXPORT LocationProviderBase* NewNetworkLocationProvider(
97 AccessTokenStore* access_token_store,
98 net::URLRequestContextGetter* context,
99 const GURL& url,
100 const string16& access_token);
101 LocationProviderBase* NewSystemLocationProvider();
103 } // namespace content
105 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_PROVIDER_H_