Chromium Code Reviews| 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 #include "content/browser/geolocation/geolocation_provider.h" | 5 #include "content/browser/geolocation/geolocation_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 9 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 10 #include "base/threading/thread_restrictions.h" | 13 #include "base/message_loop.h" |
| 11 #include "content/browser/geolocation/location_arbitrator.h" | 14 #include "content/browser/geolocation/location_arbitrator.h" |
| 15 #include "content/public/browser/browser_thread.h" | |
| 12 | 16 |
| 13 GeolocationProvider* GeolocationProvider::GetInstance() { | 17 using content::BrowserThread; |
| 14 return Singleton<GeolocationProvider>::get(); | |
| 15 } | |
| 16 | |
| 17 GeolocationProvider::GeolocationProvider() | |
| 18 : base::Thread("Geolocation"), | |
| 19 client_loop_(base::MessageLoopProxy::current()), | |
| 20 is_permission_granted_(false), | |
| 21 ignore_location_updates_(false), | |
| 22 arbitrator_(NULL) { | |
| 23 } | |
| 24 | |
| 25 GeolocationProvider::~GeolocationProvider() { | |
| 26 DCHECK(observers_.empty()); // observers must unregister. | |
| 27 Stop(); | |
| 28 DCHECK(!arbitrator_); | |
| 29 } | |
| 30 | 18 |
| 31 void GeolocationProvider::AddObserver(GeolocationObserver* observer, | 19 void GeolocationProvider::AddObserver(GeolocationObserver* observer, |
| 32 const GeolocationObserverOptions& update_options) { | 20 const GeolocationObserverOptions& update_options) { |
| 33 DCHECK(OnClientThread()); | 21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 34 observers_[observer] = update_options; | 22 observers_[observer] = update_options; |
| 35 OnObserversChanged(); | 23 OnClientsChanged(); |
| 36 if (position_.Validate() || | 24 if (position_.Validate() || |
| 37 position_.error_code != content::Geoposition::ERROR_CODE_NONE) | 25 position_.error_code != content::Geoposition::ERROR_CODE_NONE) |
| 38 observer->OnLocationUpdate(position_); | 26 observer->OnLocationUpdate(position_); |
| 39 } | 27 } |
| 40 | 28 |
| 41 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) { | 29 bool GeolocationProvider::RemoveObserver(GeolocationObserver* observer) { |
| 42 DCHECK(OnClientThread()); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 size_t remove = observers_.erase(observer); | 31 size_t removed = observers_.erase(observer); |
| 44 OnObserversChanged(); | 32 if (removed) |
| 45 return remove > 0; | 33 OnClientsChanged(); |
| 34 return removed > 0; | |
| 46 } | 35 } |
| 47 | 36 |
| 48 void GeolocationProvider::OnObserversChanged() { | 37 void GeolocationProvider::RequestCallback( |
| 49 DCHECK(OnClientThread()); | 38 const content::GeolocationUpdateCallback& callback) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 40 callbacks_.push_back(callback); | |
| 41 OnClientsChanged(); | |
| 42 OnPermissionGranted(); | |
| 43 } | |
| 44 | |
| 45 void GeolocationProvider::OnPermissionGranted() { | |
| 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 47 bool was_permission_granted = is_permission_granted_; | |
| 48 is_permission_granted_ = true; | |
|
jam
2012/05/03 06:17:49
this looks like a process wide security variable.
bartfab (slow)
2012/05/03 08:41:15
No. Website permissions are handled elsewhere. The
| |
| 49 if (IsRunning() && !was_permission_granted) | |
| 50 InformProvidersPermissionGranted(); | |
| 51 } | |
| 52 | |
| 53 bool GeolocationProvider::HasPermissionBeenGranted() const { | |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 55 return is_permission_granted_; | |
| 56 } | |
| 57 | |
| 58 void GeolocationProvider::OnLocationUpdate( | |
| 59 const content::Geoposition& position) { | |
| 60 DCHECK(OnGeolocationThread()); | |
| 61 // Will be true only in testing. | |
| 62 if (ignore_location_updates_) | |
| 63 return; | |
| 64 BrowserThread::PostTask(BrowserThread::IO, | |
| 65 FROM_HERE, | |
| 66 base::Bind(&GeolocationProvider::NotifyClients, | |
| 67 base::Unretained(this), position)); | |
| 68 } | |
| 69 | |
| 70 void GeolocationProvider::OverrideLocationForTesting( | |
| 71 const content::Geoposition& position) { | |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 73 position_ = position; | |
| 74 ignore_location_updates_ = true; | |
| 75 NotifyClients(position); | |
| 76 } | |
| 77 | |
| 78 GeolocationProvider* GeolocationProvider::GetInstance() { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 80 return Singleton<GeolocationProvider>::get(); | |
| 81 } | |
| 82 | |
| 83 GeolocationProvider::GeolocationProvider() | |
| 84 : base::Thread("Geolocation"), | |
| 85 is_permission_granted_(false), | |
| 86 ignore_location_updates_(false), | |
| 87 arbitrator_(NULL) { | |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 89 } | |
| 90 | |
| 91 GeolocationProvider::~GeolocationProvider() { | |
| 92 // All observers should have unregistered before this singleton is destructed. | |
| 93 DCHECK(observers_.empty()); | |
| 94 Stop(); | |
| 95 DCHECK(!arbitrator_); | |
| 96 } | |
| 97 | |
| 98 bool GeolocationProvider::OnGeolocationThread() const { | |
| 99 return MessageLoop::current() == message_loop(); | |
| 100 } | |
| 101 | |
| 102 void GeolocationProvider::OnClientsChanged() { | |
| 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 50 base::Closure task; | 104 base::Closure task; |
| 51 if (observers_.empty()) { | 105 if (observers_.empty() && callbacks_.empty()) { |
| 52 DCHECK(IsRunning()); | 106 DCHECK(IsRunning()); |
| 53 task = base::Bind(&GeolocationProvider::StopProviders, | 107 task = base::Bind(&GeolocationProvider::StopProviders, |
| 54 base::Unretained(this)); | 108 base::Unretained(this)); |
| 55 } else { | 109 } else { |
| 56 if (!IsRunning()) { | 110 if (!IsRunning()) { |
| 57 Start(); | 111 Start(); |
| 58 if (HasPermissionBeenGranted()) | 112 if (HasPermissionBeenGranted()) |
| 59 InformProvidersPermissionGranted(); | 113 InformProvidersPermissionGranted(); |
| 60 } | 114 } |
| 115 // Determine a set of options that satisfies all clients. | |
| 116 GeolocationObserverOptions options = | |
| 117 GeolocationObserverOptions::Collapse(observers_); | |
| 118 // For callbacks, high accuracy position information is always requested. | |
| 119 if (!callbacks_.empty()) | |
| 120 options.Collapse(GeolocationObserverOptions(true)); | |
| 61 | 121 |
| 62 // The high accuracy requirement may have changed. | 122 // Send the current options to the providers as they may have changed. |
| 63 task = base::Bind(&GeolocationProvider::StartProviders, | 123 task = base::Bind(&GeolocationProvider::StartProviders, |
| 64 base::Unretained(this), | 124 base::Unretained(this), |
| 65 GeolocationObserverOptions::Collapse(observers_)); | 125 options); |
| 66 } | 126 } |
| 67 | 127 |
| 68 message_loop()->PostTask(FROM_HERE, task); | 128 message_loop()->PostTask(FROM_HERE, task); |
| 69 } | 129 } |
| 70 | 130 |
| 71 void GeolocationProvider::NotifyObservers( | 131 void GeolocationProvider::StopProviders() { |
| 72 const content::Geoposition& position) { | 132 DCHECK(OnGeolocationThread()); |
| 73 DCHECK(OnClientThread()); | 133 DCHECK(arbitrator_); |
| 74 DCHECK(position.Validate() || | 134 arbitrator_->StopProviders(); |
| 75 position.error_code != content::Geoposition::ERROR_CODE_NONE); | |
| 76 position_ = position; | |
| 77 ObserverMap::const_iterator it = observers_.begin(); | |
| 78 while (it != observers_.end()) { | |
| 79 // Advance iterator before callback to guard against synchronous unregister. | |
| 80 GeolocationObserver* observer = it->first; | |
| 81 ++it; | |
| 82 observer->OnLocationUpdate(position_); | |
| 83 } | |
| 84 } | 135 } |
| 85 | 136 |
| 86 void GeolocationProvider::StartProviders( | 137 void GeolocationProvider::StartProviders( |
| 87 const GeolocationObserverOptions& options) { | 138 const GeolocationObserverOptions& options) { |
| 88 DCHECK(OnGeolocationThread()); | 139 DCHECK(OnGeolocationThread()); |
| 89 DCHECK(arbitrator_); | 140 DCHECK(arbitrator_); |
| 90 arbitrator_->StartProviders(options); | 141 arbitrator_->StartProviders(options); |
| 91 } | 142 } |
| 92 | 143 |
| 93 void GeolocationProvider::StopProviders() { | |
| 94 DCHECK(OnGeolocationThread()); | |
| 95 DCHECK(arbitrator_); | |
| 96 arbitrator_->StopProviders(); | |
| 97 } | |
| 98 | |
| 99 void GeolocationProvider::OnPermissionGranted() { | |
| 100 DCHECK(OnClientThread()); | |
| 101 is_permission_granted_ = true; | |
| 102 if (IsRunning()) | |
| 103 InformProvidersPermissionGranted(); | |
| 104 } | |
| 105 | |
| 106 void GeolocationProvider::InformProvidersPermissionGranted() { | 144 void GeolocationProvider::InformProvidersPermissionGranted() { |
| 107 DCHECK(IsRunning()); | 145 DCHECK(IsRunning()); |
| 108 if (!OnGeolocationThread()) { | 146 if (!OnGeolocationThread()) { |
| 109 message_loop()->PostTask( | 147 message_loop()->PostTask( |
| 110 FROM_HERE, | 148 FROM_HERE, |
| 111 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, | 149 base::Bind(&GeolocationProvider::InformProvidersPermissionGranted, |
| 112 base::Unretained(this))); | 150 base::Unretained(this))); |
| 113 return; | 151 return; |
| 114 } | 152 } |
| 115 DCHECK(OnGeolocationThread()); | 153 DCHECK(OnGeolocationThread()); |
| 116 DCHECK(arbitrator_); | 154 DCHECK(arbitrator_); |
| 117 arbitrator_->OnPermissionGranted(); | 155 arbitrator_->OnPermissionGranted(); |
| 118 } | 156 } |
| 119 | 157 |
| 158 void GeolocationProvider::NotifyClients(const content::Geoposition& position) { | |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 160 DCHECK(position.Validate() || | |
| 161 position.error_code != content::Geoposition::ERROR_CODE_NONE); | |
| 162 position_ = position; | |
| 163 ObserverMap::const_iterator it = observers_.begin(); | |
| 164 while (it != observers_.end()) { | |
| 165 // Advance iterator before calling the observer to guard against synchronous | |
| 166 // unregister. | |
| 167 GeolocationObserver* observer = it->first; | |
| 168 ++it; | |
| 169 observer->OnLocationUpdate(position_); | |
| 170 } | |
| 171 if (!callbacks_.empty()) { | |
| 172 for (CallbackList::iterator it = callbacks_.begin(); | |
| 173 it != callbacks_.end(); | |
| 174 ++it) | |
| 175 it->Run(position); | |
| 176 callbacks_.clear(); | |
| 177 OnClientsChanged(); | |
| 178 } | |
| 179 } | |
| 180 | |
| 120 void GeolocationProvider::Init() { | 181 void GeolocationProvider::Init() { |
| 121 DCHECK(OnGeolocationThread()); | 182 DCHECK(OnGeolocationThread()); |
| 122 DCHECK(!arbitrator_); | 183 DCHECK(!arbitrator_); |
| 123 arbitrator_ = GeolocationArbitrator::Create(this); | 184 arbitrator_ = GeolocationArbitrator::Create(this); |
| 124 } | 185 } |
| 125 | 186 |
| 126 void GeolocationProvider::CleanUp() { | 187 void GeolocationProvider::CleanUp() { |
| 127 DCHECK(OnGeolocationThread()); | 188 DCHECK(OnGeolocationThread()); |
| 128 delete arbitrator_; | 189 delete arbitrator_; |
| 129 arbitrator_ = NULL; | 190 arbitrator_ = NULL; |
| 130 } | 191 } |
| 131 | |
| 132 void GeolocationProvider::OnLocationUpdate( | |
| 133 const content::Geoposition& position) { | |
| 134 DCHECK(OnGeolocationThread()); | |
| 135 // Will be true only in testing. | |
| 136 if (ignore_location_updates_) | |
| 137 return; | |
| 138 client_loop_->PostTask( | |
| 139 FROM_HERE, | |
| 140 base::Bind(&GeolocationProvider::NotifyObservers, | |
| 141 base::Unretained(this), position)); | |
| 142 } | |
| 143 | |
| 144 void GeolocationProvider::OverrideLocationForTesting( | |
| 145 const content::Geoposition& position) { | |
| 146 DCHECK(OnClientThread()); | |
| 147 position_ = position; | |
| 148 ignore_location_updates_ = true; | |
| 149 NotifyObservers(position); | |
| 150 } | |
| 151 | |
| 152 bool GeolocationProvider::HasPermissionBeenGranted() const { | |
| 153 DCHECK(OnClientThread()); | |
| 154 return is_permission_granted_; | |
| 155 } | |
| 156 | |
| 157 bool GeolocationProvider::OnClientThread() const { | |
| 158 return client_loop_->BelongsToCurrentThread(); | |
| 159 } | |
| 160 | |
| 161 bool GeolocationProvider::OnGeolocationThread() const { | |
| 162 return MessageLoop::current() == message_loop(); | |
| 163 } | |
| OLD | NEW |