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 // Updates the |geolocation_provider_| with the currently required update | 72 void OnPauseOrResume(int render_view_id, bool should_pause); |
73 | |
74 // Updates the |location_arbitrator_| with the currently required update | |
benm (inactive)
2013/11/07 21:07:50
The geolocation_provider/arbitrator comment change
| |
73 // options, based on |renderer_high_accuracy_|. | 75 // options, based on |renderer_high_accuracy_|. |
74 void RefreshHighAccuracy(); | 76 void RefreshHighAccuracy(); |
75 | 77 |
76 void OnLocationUpdate(const Geoposition& position); | 78 void OnLocationUpdate(const Geoposition& position); |
77 | 79 |
78 int render_process_id_; | 80 int render_process_id_; |
79 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | 81 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; |
80 | 82 |
83 struct RendererGeolocationOptions { | |
84 bool high_accuracy; | |
85 bool is_paused; | |
86 }; | |
81 // Iterated when sending location updates to renderer processes. The fan out | 87 // Iterated when sending location updates to renderer processes. The fan out |
82 // to individual bridge IDs happens renderer side, in order to minimize | 88 // to individual bridge IDs happens renderer side, in order to minimize |
83 // context switches. | 89 // context switches. |
84 // Only used on the IO thread. | 90 // Only used on the IO thread. |
joth
2013/11/07 21:52:00
(update comment)
| |
85 std::set<int> geolocation_renderer_ids_; | 91 std::map<int, RendererGeolocationOptions> geolocation_renderers_; |
86 // Maps renderer_id to whether high accuracy is requested for this particular | 92 |
87 // bridge. | 93 // Only set whilst we are registered with the arbitrator. |
88 std::map<int, bool> renderer_high_accuracy_; | |
89 // Only set whilst we are registered with the geolocation provider. | |
90 GeolocationProviderImpl* geolocation_provider_; | 94 GeolocationProviderImpl* geolocation_provider_; |
91 | 95 |
92 GeolocationProviderImpl::LocationUpdateCallback callback_; | 96 GeolocationProviderImpl::LocationUpdateCallback callback_; |
93 | 97 |
94 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | 98 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); |
95 }; | 99 }; |
96 | 100 |
97 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | 101 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( |
98 int render_process_id, | 102 int render_process_id, |
99 GeolocationPermissionContext* geolocation_permission_context) | 103 GeolocationPermissionContext* geolocation_permission_context) |
(...skipping 17 matching lines...) Expand all Loading... | |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
118 *msg_was_ok = true; | 122 *msg_was_ok = true; |
119 bool handled = true; | 123 bool handled = true; |
120 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) | 124 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) |
121 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
122 OnCancelPermissionRequest) | 126 OnCancelPermissionRequest) |
123 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 127 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
124 OnRequestPermission) | 128 OnRequestPermission) |
125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 129 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
126 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 130 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
131 IPC_MESSAGE_HANDLER(GeolocationHostMsg_PauseOrResume, OnPauseOrResume) | |
127 IPC_MESSAGE_UNHANDLED(handled = false) | 132 IPC_MESSAGE_UNHANDLED(handled = false) |
128 IPC_END_MESSAGE_MAP() | 133 IPC_END_MESSAGE_MAP() |
129 return handled; | 134 return handled; |
130 } | 135 } |
131 | 136 |
132 void GeolocationDispatcherHostImpl::OnLocationUpdate( | 137 void GeolocationDispatcherHostImpl::OnLocationUpdate( |
133 const Geoposition& geoposition) { | 138 const Geoposition& geoposition) { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
135 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | 140 for (std::map<int, RendererGeolocationOptions>::iterator it = |
136 it != geolocation_renderer_ids_.end(); ++it) { | 141 geolocation_renderers_.begin(); |
137 Send(new GeolocationMsg_PositionUpdated(*it, geoposition)); | 142 it != geolocation_renderers_.end(); ++it) { |
143 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); | |
138 } | 144 } |
139 } | 145 } |
140 | 146 |
141 void GeolocationDispatcherHostImpl::OnRequestPermission( | 147 void GeolocationDispatcherHostImpl::OnRequestPermission( |
142 int render_view_id, | 148 int render_view_id, |
143 int bridge_id, | 149 int bridge_id, |
144 const GURL& requesting_frame) { | 150 const GURL& requesting_frame) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 151 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
146 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 152 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
147 << render_view_id << ":" << bridge_id; | 153 << render_view_id << ":" << bridge_id; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 const GURL& requesting_frame, | 187 const GURL& requesting_frame, |
182 bool enable_high_accuracy) { | 188 bool enable_high_accuracy) { |
183 // StartUpdating() can be invoked as a result of high-accuracy mode | 189 // StartUpdating() can be invoked as a result of high-accuracy mode |
184 // being enabled / disabled. No need to record the dispatcher again. | 190 // being enabled / disabled. No need to record the dispatcher again. |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 192 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
187 << render_view_id; | 193 << render_view_id; |
188 UMA_HISTOGRAM_BOOLEAN( | 194 UMA_HISTOGRAM_BOOLEAN( |
189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 195 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
190 enable_high_accuracy); | 196 enable_high_accuracy); |
191 if (!geolocation_renderer_ids_.count(render_view_id)) | 197 RendererGeolocationOptions opts = { enable_high_accuracy, false }; |
192 geolocation_renderer_ids_.insert(render_view_id); | 198 geolocation_renderers_[render_view_id] = opts; |
joth
2013/11/07 21:52:00
if we were previously paused we should now unpause
| |
193 | |
194 renderer_high_accuracy_[render_view_id] = enable_high_accuracy; | |
195 RefreshHighAccuracy(); | 199 RefreshHighAccuracy(); |
196 } | 200 } |
197 | 201 |
198 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | 202 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
200 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 204 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
201 << render_view_id; | 205 << render_view_id; |
202 if (renderer_high_accuracy_.erase(render_view_id)) | 206 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); |
203 RefreshHighAccuracy(); | 207 geolocation_renderers_.erase(render_view_id); |
joth
2013/11/07 21:52:00
if we just erased the only unpaused provider is sh
| |
208 RefreshHighAccuracy(); | |
209 } | |
204 | 210 |
205 DCHECK_EQ(1U, geolocation_renderer_ids_.count(render_view_id)); | 211 void GeolocationDispatcherHostImpl::OnPauseOrResume(int render_view_id, |
206 geolocation_renderer_ids_.erase(render_view_id); | 212 bool should_pause) { |
213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
214 if (geolocation_renderers_.find(render_view_id) == | |
215 geolocation_renderers_.end()) | |
216 return; | |
217 | |
218 if (should_pause) { | |
219 RendererGeolocationOptions* opts = | |
220 &(geolocation_renderers_[render_view_id]); | |
221 opts->is_paused = true; | |
222 | |
223 bool pause_all = true; | |
224 std::map<int, RendererGeolocationOptions>::iterator i = | |
225 geolocation_renderers_.begin(); | |
226 for (; i != geolocation_renderers_.end(); ++i) { | |
227 if (!i->second.is_paused) { | |
228 pause_all = false; | |
229 break; | |
230 } | |
231 } | |
232 if (pause_all) | |
233 GeolocationProvider::GetInstance()->PauseAllLocationUpdateCallbacks(); | |
234 } else { | |
235 RendererGeolocationOptions* opts = | |
236 &(geolocation_renderers_[render_view_id]); | |
237 opts->is_paused = false; | |
238 GeolocationProvider::GetInstance()->ResumeAllLocationUpdateCallbacks(); | |
239 } | |
207 } | 240 } |
208 | 241 |
209 void GeolocationDispatcherHostImpl::RefreshHighAccuracy() { | 242 void GeolocationDispatcherHostImpl::RefreshHighAccuracy() { |
joth
2013/11/07 21:52:00
this should probably be RefeshProviederOptions and
| |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
211 if (renderer_high_accuracy_.empty()) { | 244 if (geolocation_renderers_.empty()) { |
212 if (geolocation_provider_) { | 245 if (geolocation_provider_) { |
213 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 246 geolocation_provider_->RemoveLocationUpdateCallback(callback_); |
joth
2013/11/07 21:52:00
can we just use RemoveLocationUpdateCallback inste
joth
2013/11/07 21:58:15
Actually my change above is needed otherwise this
| |
214 geolocation_provider_ = NULL; | 247 geolocation_provider_ = NULL; |
215 } | 248 } |
216 } else { | 249 } else { |
217 if (!geolocation_provider_) | 250 if (!geolocation_provider_) |
218 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); | 251 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); |
219 // Re-add to re-establish our options, in case they changed. | 252 // Re-add to re-establish our options, in case they changed. |
220 bool use_high_accuracy = false; | 253 bool use_high_accuracy = false; |
221 std::map<int, bool>::iterator i = renderer_high_accuracy_.begin(); | 254 std::map<int, RendererGeolocationOptions>::iterator i = |
222 for (; i != renderer_high_accuracy_.end(); ++i) { | 255 geolocation_renderers_.begin(); |
223 if (i->second) { | 256 for (; i != geolocation_renderers_.end(); ++i) { |
257 if (i->second.high_accuracy) { | |
224 use_high_accuracy = true; | 258 use_high_accuracy = true; |
225 break; | 259 break; |
226 } | 260 } |
227 } | 261 } |
228 geolocation_provider_->AddLocationUpdateCallback( | 262 geolocation_provider_->AddLocationUpdateCallback( |
229 callback_, use_high_accuracy); | 263 callback_, use_high_accuracy); |
230 } | 264 } |
231 } | 265 } |
232 } // namespace | 266 } // namespace |
233 | 267 |
234 | 268 |
235 // GeolocationDispatcherHost -------------------------------------------------- | 269 // GeolocationDispatcherHost -------------------------------------------------- |
236 | 270 |
237 // static | 271 // static |
238 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | 272 GeolocationDispatcherHost* GeolocationDispatcherHost::New( |
239 int render_process_id, | 273 int render_process_id, |
240 GeolocationPermissionContext* geolocation_permission_context) { | 274 GeolocationPermissionContext* geolocation_permission_context) { |
241 return new GeolocationDispatcherHostImpl( | 275 return new GeolocationDispatcherHostImpl( |
242 render_process_id, | 276 render_process_id, |
243 geolocation_permission_context); | 277 geolocation_permission_context); |
244 } | 278 } |
245 | 279 |
246 GeolocationDispatcherHost::GeolocationDispatcherHost() { | 280 GeolocationDispatcherHost::GeolocationDispatcherHost() { |
247 } | 281 } |
248 | 282 |
249 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 283 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
250 } | 284 } |
251 | 285 |
252 } // namespace content | 286 } // namespace content |
OLD | NEW |