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_dispatcher_host.h" | 5 #include "content/browser/geolocation/geolocation_dispatcher_host.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 int bridge_id, | 62 int bridge_id, |
63 const GURL& requesting_frame); | 63 const GURL& requesting_frame); |
64 void OnCancelPermissionRequest(int render_view_id, | 64 void OnCancelPermissionRequest(int render_view_id, |
65 int bridge_id, | 65 int bridge_id, |
66 const GURL& requesting_frame); | 66 const GURL& requesting_frame); |
67 void OnStartUpdating(int render_view_id, | 67 void OnStartUpdating(int render_view_id, |
68 const GURL& requesting_frame, | 68 const GURL& requesting_frame, |
69 bool enable_high_accuracy); | 69 bool enable_high_accuracy); |
70 void OnStopUpdating(int render_view_id); | 70 void OnStopUpdating(int render_view_id); |
71 | 71 |
72 virtual void PauseOrResume(int render_view_id, bool should_pause) OVERRIDE; | |
73 | |
72 // Updates the |geolocation_provider_| with the currently required update | 74 // Updates the |geolocation_provider_| with the currently required update |
73 // options, based on |renderer_high_accuracy_|. | 75 // options. |
74 void RefreshHighAccuracy(); | 76 void RefreshGeolocationOptions(); |
75 | 77 |
76 void OnLocationUpdate(const Geoposition& position); | 78 void OnLocationUpdate(const Geoposition& position); |
77 | 79 |
80 bool AreAllRenderersPaused() const; | |
81 | |
78 int render_process_id_; | 82 int render_process_id_; |
79 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | 83 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; |
80 | 84 |
81 // Iterated when sending location updates to renderer processes. The fan out | 85 struct RendererGeolocationOptions { |
82 // to individual bridge IDs happens renderer side, in order to minimize | 86 bool high_accuracy; |
83 // context switches. | 87 bool is_paused; |
88 }; | |
89 | |
90 // Used to keep track of the renderers in this process that are using | |
91 // geolocation and the options associated with them. The map is iterated | |
92 // when a location update is available and the fan out to individual bridge | |
93 // IDs happens renderer side, in order to minimize context switches. | |
84 // Only used on the IO thread. | 94 // Only used on the IO thread. |
85 std::set<int> geolocation_renderer_ids_; | 95 std::map<int, RendererGeolocationOptions> geolocation_renderers_; |
86 // Maps renderer_id to whether high accuracy is requested for this particular | 96 |
87 // bridge. | |
88 std::map<int, bool> renderer_high_accuracy_; | |
89 // Only set whilst we are registered with the geolocation provider. | 97 // Only set whilst we are registered with the geolocation provider. |
90 GeolocationProviderImpl* geolocation_provider_; | 98 GeolocationProviderImpl* geolocation_provider_; |
91 | 99 |
92 GeolocationProviderImpl::LocationUpdateCallback callback_; | 100 GeolocationProviderImpl::LocationUpdateCallback callback_; |
93 | 101 |
94 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | 102 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); |
95 }; | 103 }; |
96 | 104 |
97 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | 105 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( |
98 int render_process_id, | 106 int render_process_id, |
(...skipping 26 matching lines...) Expand all Loading... | |
125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 133 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
126 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 134 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
127 IPC_MESSAGE_UNHANDLED(handled = false) | 135 IPC_MESSAGE_UNHANDLED(handled = false) |
128 IPC_END_MESSAGE_MAP() | 136 IPC_END_MESSAGE_MAP() |
129 return handled; | 137 return handled; |
130 } | 138 } |
131 | 139 |
132 void GeolocationDispatcherHostImpl::OnLocationUpdate( | 140 void GeolocationDispatcherHostImpl::OnLocationUpdate( |
133 const Geoposition& geoposition) { | 141 const Geoposition& geoposition) { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
135 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | 143 for (std::map<int, RendererGeolocationOptions>::iterator it = |
136 it != geolocation_renderer_ids_.end(); ++it) { | 144 geolocation_renderers_.begin(); |
137 Send(new GeolocationMsg_PositionUpdated(*it, geoposition)); | 145 it != geolocation_renderers_.end(); ++it) { |
146 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); | |
138 } | 147 } |
139 } | 148 } |
140 | 149 |
141 void GeolocationDispatcherHostImpl::OnRequestPermission( | 150 void GeolocationDispatcherHostImpl::OnRequestPermission( |
142 int render_view_id, | 151 int render_view_id, |
143 int bridge_id, | 152 int bridge_id, |
144 const GURL& requesting_frame) { | 153 const GURL& requesting_frame) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
146 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 155 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
147 << render_view_id << ":" << bridge_id; | 156 << render_view_id << ":" << bridge_id; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 const GURL& requesting_frame, | 190 const GURL& requesting_frame, |
182 bool enable_high_accuracy) { | 191 bool enable_high_accuracy) { |
183 // StartUpdating() can be invoked as a result of high-accuracy mode | 192 // StartUpdating() can be invoked as a result of high-accuracy mode |
184 // being enabled / disabled. No need to record the dispatcher again. | 193 // being enabled / disabled. No need to record the dispatcher again. |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 195 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
187 << render_view_id; | 196 << render_view_id; |
188 UMA_HISTOGRAM_BOOLEAN( | 197 UMA_HISTOGRAM_BOOLEAN( |
189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 198 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
190 enable_high_accuracy); | 199 enable_high_accuracy); |
191 if (!geolocation_renderer_ids_.count(render_view_id)) | 200 RendererGeolocationOptions opts = { enable_high_accuracy, false }; |
192 geolocation_renderer_ids_.insert(render_view_id); | 201 geolocation_renderers_[render_view_id] = opts; |
193 | 202 RefreshGeolocationOptions(); |
194 renderer_high_accuracy_[render_view_id] = enable_high_accuracy; | |
195 RefreshHighAccuracy(); | |
196 } | 203 } |
197 | 204 |
198 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | 205 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
200 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 207 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
201 << render_view_id; | 208 << render_view_id; |
202 if (renderer_high_accuracy_.erase(render_view_id)) | 209 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); |
203 RefreshHighAccuracy(); | 210 geolocation_renderers_.erase(render_view_id); |
204 | 211 RefreshGeolocationOptions(); |
205 DCHECK_EQ(1U, geolocation_renderer_ids_.count(render_view_id)); | |
206 geolocation_renderer_ids_.erase(render_view_id); | |
207 } | 212 } |
208 | 213 |
209 void GeolocationDispatcherHostImpl::RefreshHighAccuracy() { | 214 void GeolocationDispatcherHostImpl::PauseOrResume(int render_view_id, |
215 bool should_pause) { | |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
211 if (renderer_high_accuracy_.empty()) { | 217 if (geolocation_renderers_.find(render_view_id) == |
218 geolocation_renderers_.end()) | |
219 return; | |
220 | |
221 RendererGeolocationOptions* opts = | |
222 &(geolocation_renderers_[render_view_id]); | |
joth
2013/11/08 20:41:14
nit: capture the find() result in an iterator abov
benm (inactive)
2013/11/08 21:10:28
Done.
| |
223 if (opts->is_paused != should_pause) { | |
224 opts->is_paused = should_pause; | |
225 RefreshGeolocationOptions(); | |
226 } | |
227 } | |
228 | |
229 bool GeolocationDispatcherHostImpl::AreAllRenderersPaused() const { | |
230 std::map<int, RendererGeolocationOptions>::const_iterator i = | |
231 geolocation_renderers_.begin(); | |
232 for (; i != geolocation_renderers_.end(); ++i) { | |
233 if (!i->second.is_paused) | |
234 return false; | |
235 } | |
236 return true; | |
237 } | |
238 | |
239 void GeolocationDispatcherHostImpl::RefreshGeolocationOptions() { | |
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
241 if (AreAllRenderersPaused()) { | |
joth
2013/11/08 20:41:14
Do we really need to special case this AreAllRende
benm (inactive)
2013/11/08 21:10:28
Gotcha, this makes sense. Sorry think I missed thi
| |
212 if (geolocation_provider_) { | 242 if (geolocation_provider_) { |
213 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 243 geolocation_provider_->RemoveLocationUpdateCallback(callback_); |
214 geolocation_provider_ = NULL; | 244 geolocation_provider_ = NULL; |
215 } | 245 } |
216 } else { | 246 } else { |
217 if (!geolocation_provider_) | 247 if (!geolocation_provider_) |
218 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); | 248 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); |
219 // Re-add to re-establish our options, in case they changed. | 249 // Re-add to re-establish our options, in case they changed. |
220 bool use_high_accuracy = false; | 250 bool use_high_accuracy = false; |
221 std::map<int, bool>::iterator i = renderer_high_accuracy_.begin(); | 251 std::map<int, RendererGeolocationOptions>::iterator i = |
222 for (; i != renderer_high_accuracy_.end(); ++i) { | 252 geolocation_renderers_.begin(); |
223 if (i->second) { | 253 for (; i != geolocation_renderers_.end(); ++i) { |
254 if (i->second.high_accuracy) { | |
224 use_high_accuracy = true; | 255 use_high_accuracy = true; |
225 break; | 256 break; |
226 } | 257 } |
227 } | 258 } |
228 geolocation_provider_->AddLocationUpdateCallback( | 259 geolocation_provider_->AddLocationUpdateCallback( |
229 callback_, use_high_accuracy); | 260 callback_, use_high_accuracy); |
230 } | 261 } |
231 } | 262 } |
232 } // namespace | 263 } // namespace |
233 | 264 |
234 | 265 |
235 // GeolocationDispatcherHost -------------------------------------------------- | 266 // GeolocationDispatcherHost -------------------------------------------------- |
236 | 267 |
237 // static | 268 // static |
238 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | 269 GeolocationDispatcherHost* GeolocationDispatcherHost::New( |
239 int render_process_id, | 270 int render_process_id, |
240 GeolocationPermissionContext* geolocation_permission_context) { | 271 GeolocationPermissionContext* geolocation_permission_context) { |
241 return new GeolocationDispatcherHostImpl( | 272 return new GeolocationDispatcherHostImpl( |
242 render_process_id, | 273 render_process_id, |
243 geolocation_permission_context); | 274 geolocation_permission_context); |
244 } | 275 } |
245 | 276 |
246 GeolocationDispatcherHost::GeolocationDispatcherHost() { | 277 GeolocationDispatcherHost::GeolocationDispatcherHost() { |
247 } | 278 } |
248 | 279 |
249 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 280 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
250 } | 281 } |
251 | 282 |
252 } // namespace content | 283 } // namespace content |
OLD | NEW |