[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / browser / geolocation / location_arbitrator_impl.h
blobe7b4d50f3f558801c4df1743cf969675aa4d8dfc
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_LOCATION_ARBITRATOR_IMPL_H_
6 #define CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_
8 #include "base/memory/scoped_vector.h"
9 #include "base/string16.h"
10 #include "base/time.h"
11 #include "content/browser/geolocation/geolocation_observer.h"
12 #include "content/browser/geolocation/location_arbitrator.h"
13 #include "content/browser/geolocation/location_provider.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/access_token_store.h"
16 #include "content/public/common/geoposition.h"
17 #include "net/url_request/url_request_context_getter.h"
19 namespace net {
20 class URLRequestContextGetter;
23 namespace content {
24 class AccessTokenStore;
25 class LocationProviderBase;
27 // This class is responsible for handling updates from multiple underlying
28 // providers and resolving them to a single 'best' location fix at any given
29 // moment.
30 class CONTENT_EXPORT GeolocationArbitratorImpl
31 : public GeolocationArbitrator,
32 public LocationProviderBase::ListenerInterface {
33 public:
34 // Number of milliseconds newer a location provider has to be that it's worth
35 // switching to this location provider on the basis of it being fresher
36 // (regardles of relative accuracy). Public for tests.
37 static const int64 kFixStaleTimeoutMilliseconds;
39 explicit GeolocationArbitratorImpl(GeolocationObserver* observer);
40 virtual ~GeolocationArbitratorImpl();
42 static GURL DefaultNetworkProviderURL();
44 // GeolocationArbitrator
45 virtual void StartProviders(const GeolocationObserverOptions& options)
46 OVERRIDE;
47 virtual void StopProviders() OVERRIDE;
48 virtual void OnPermissionGranted() OVERRIDE;
49 virtual bool HasPermissionBeenGranted() const OVERRIDE;
51 // ListenerInterface
52 virtual void LocationUpdateAvailable(LocationProviderBase* provider) OVERRIDE;
54 protected:
56 AccessTokenStore* GetAccessTokenStore();
58 // These functions are useful for injection of dependencies in derived
59 // testing classes.
60 virtual AccessTokenStore* NewAccessTokenStore();
61 virtual LocationProviderBase* NewNetworkLocationProvider(
62 AccessTokenStore* access_token_store,
63 net::URLRequestContextGetter* context,
64 const GURL& url,
65 const string16& access_token);
66 virtual LocationProviderBase* NewSystemLocationProvider();
67 virtual base::Time GetTimeNow() const;
69 private:
70 // Takes ownership of |provider| on entry; it will either be added to
71 // |providers_| or deleted on error (e.g. it fails to start).
72 void RegisterProvider(LocationProviderBase* provider);
73 void OnAccessTokenStoresLoaded(
74 AccessTokenStore::AccessTokenSet access_token_store,
75 net::URLRequestContextGetter* context_getter);
76 void DoStartProviders();
77 // Returns true if |new_position| is an improvement over |old_position|.
78 // Set |from_same_provider| to true if both the positions came from the same
79 // provider.
80 bool IsNewPositionBetter(const Geoposition& old_position,
81 const Geoposition& new_position,
82 bool from_same_provider) const;
84 scoped_refptr<AccessTokenStore> access_token_store_;
85 GeolocationObserver* observer_;
86 ScopedVector<LocationProviderBase> providers_;
87 GeolocationObserverOptions current_provider_options_;
88 // The provider which supplied the current |position_|
89 const LocationProviderBase* position_provider_;
90 bool is_permission_granted_;
91 // The current best estimate of our position.
92 Geoposition position_;
94 DISALLOW_COPY_AND_ASSIGN(GeolocationArbitratorImpl);
97 } // namespace content
99 #endif // CONTENT_BROWSER_GEOLOCATION_LOCATION_ARBITRATOR_IMPL_H_