| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/geolocation/location_arbitrator.h" |
| 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/non_thread_safe.h" |
| 11 #include "base/ref_counted.h" |
| 12 #include "base/scoped_ptr.h" |
| 13 #include "base/string_util.h" |
| 14 #include "chrome/browser/net/url_request_context_getter.h" |
| 15 #include "chrome/browser/geolocation/access_token_store.h" |
| 16 #include "chrome/browser/geolocation/geoposition.h" |
| 17 #include "chrome/browser/geolocation/location_provider.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char* kDefaultNetworkProviderUrl = "https://www.google.com/loc/json"; |
| 23 |
| 24 class GeolocationArbitratorImpl |
| 25 : public GeolocationArbitrator, |
| 26 public LocationProviderBase::ListenerInterface, |
| 27 public NonThreadSafe { |
| 28 public: |
| 29 GeolocationArbitratorImpl(AccessTokenStore* access_token_store, |
| 30 URLRequestContextGetter* context_getter); |
| 31 ~GeolocationArbitratorImpl(); |
| 32 |
| 33 // GeolocationArbitrator |
| 34 virtual void AddObserver(Delegate* delegate, |
| 35 const UpdateOptions& update_options); |
| 36 virtual void RemoveObserver(Delegate* delegate); |
| 37 virtual void SetUseMockProvider(bool use_mock); |
| 38 |
| 39 // ListenerInterface |
| 40 virtual void LocationUpdateAvailable(LocationProviderBase* provider); |
| 41 virtual void MovementDetected(LocationProviderBase* provider); |
| 42 |
| 43 private: |
| 44 void CreateProviders(); |
| 45 bool HasHighAccuracyObserver(); |
| 46 |
| 47 scoped_refptr<AccessTokenStore> access_token_store_; |
| 48 scoped_refptr<URLRequestContextGetter> context_getter_; |
| 49 |
| 50 const GURL default_url_; |
| 51 scoped_ptr<LocationProviderBase> provider_; |
| 52 |
| 53 typedef std::map<Delegate*, UpdateOptions> DelegateMap; |
| 54 DelegateMap observers_; |
| 55 bool use_mock_; |
| 56 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 60 GeolocationArbitrator* GeolocationArbitrator::New( |
| 61 AccessTokenStore* access_token_store, |
| 62 URLRequestContextGetter* context_getter) { |
| 63 return new GeolocationArbitratorImpl(access_token_store, context_getter); |
| 64 } |
| 65 |
| 66 GeolocationArbitratorImpl::GeolocationArbitratorImpl( |
| 67 AccessTokenStore* access_token_store, |
| 68 URLRequestContextGetter* context_getter) |
| 69 : access_token_store_(access_token_store), context_getter_(context_getter), |
| 70 default_url_(kDefaultNetworkProviderUrl), use_mock_(false) { |
| 71 DCHECK(default_url_.is_valid()); |
| 72 } |
| 73 |
| 74 GeolocationArbitratorImpl::~GeolocationArbitratorImpl() { |
| 75 DCHECK(CalledOnValidThread()); |
| 76 } |
| 77 |
| 78 void GeolocationArbitratorImpl::AddObserver( |
| 79 Delegate* delegate, const UpdateOptions& update_options) { |
| 80 DCHECK(CalledOnValidThread()); |
| 81 observers_[delegate] = update_options; |
| 82 CreateProviders(); |
| 83 } |
| 84 |
| 85 void GeolocationArbitratorImpl::RemoveObserver(Delegate* delegate) { |
| 86 DCHECK(CalledOnValidThread()); |
| 87 observers_.erase(delegate); |
| 88 if (observers_.empty()) { |
| 89 // TODO(joth): Delayed callback to linger before destroying the provider. |
| 90 provider_.reset(); |
| 91 } |
| 92 } |
| 93 |
| 94 void GeolocationArbitratorImpl::SetUseMockProvider(bool use_mock) { |
| 95 DCHECK(CalledOnValidThread()); |
| 96 DCHECK(provider_ == NULL); |
| 97 use_mock_ = use_mock; |
| 98 } |
| 99 |
| 100 void GeolocationArbitratorImpl::LocationUpdateAvailable( |
| 101 LocationProviderBase* provider) { |
| 102 DCHECK(CalledOnValidThread()); |
| 103 DCHECK(provider); |
| 104 Position position; |
| 105 provider->GetPosition(&position); |
| 106 // TODO(joth): Arbitrate. |
| 107 for (DelegateMap::const_iterator it = observers_.begin(); |
| 108 it != observers_.end(); ++it) { |
| 109 it->first->OnLocationUpdate(position); |
| 110 } |
| 111 } |
| 112 |
| 113 void GeolocationArbitratorImpl::MovementDetected( |
| 114 LocationProviderBase* provider) { |
| 115 DCHECK(CalledOnValidThread()); |
| 116 } |
| 117 |
| 118 void GeolocationArbitratorImpl::CreateProviders() { |
| 119 DCHECK(CalledOnValidThread()); |
| 120 DCHECK(!observers_.empty()); |
| 121 if (provider_ == NULL) { |
| 122 // TODO(joth): Check with GLS folks what hostname they'd like sent. |
| 123 provider_.reset(NewNetworkLocationProvider(access_token_store_.get(), |
| 124 context_getter_.get(), default_url_, ASCIIToUTF16("chromium.org"))); |
| 125 } |
| 126 // TODO(joth): Use high-accuracy to create conditionally create GPS provider. |
| 127 } |
| 128 |
| 129 bool GeolocationArbitratorImpl::HasHighAccuracyObserver() { |
| 130 DCHECK(CalledOnValidThread()); |
| 131 for (DelegateMap::const_iterator it = observers_.begin(); |
| 132 it != observers_.end(); ++it) { |
| 133 if (it->second.use_high_accuracy) |
| 134 return true; |
| 135 } |
| 136 return false; |
| 137 } |
| OLD | NEW |