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