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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 virtual ~GeolocationDispatcherHostImpl(); | 59 virtual ~GeolocationDispatcherHostImpl(); |
60 | 60 |
61 void OnRequestPermission(int render_view_id, | 61 void OnRequestPermission(int render_view_id, |
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 | |
73 virtual void PauseOrResume(int render_view_id, bool should_pause) OVERRIDE; | |
74 | |
72 // Updates the |geolocation_provider_| with the currently required update | 75 // Updates the |geolocation_provider_| with the currently required update |
73 // options, based on |renderer_high_accuracy_|. | 76 // options. |
74 void RefreshHighAccuracy(); | 77 void RefreshGeolocationOptions(); |
75 | 78 |
76 void OnLocationUpdate(const Geoposition& position); | 79 void OnLocationUpdate(const Geoposition& position); |
77 | 80 |
78 int render_process_id_; | 81 int render_process_id_; |
79 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | 82 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; |
80 | 83 |
81 // Iterated when sending location updates to renderer processes. The fan out | 84 struct RendererGeolocationOptions { |
82 // to individual bridge IDs happens renderer side, in order to minimize | 85 bool high_accuracy; |
83 // context switches. | 86 bool is_paused; |
87 }; | |
88 | |
89 // Used to keep track of the renderers in this process that are using | |
90 // geolocation and the options associated with them. The map is iterated | |
91 // when a location update is available and the fan out to individual bridge | |
92 // IDs happens renderer side, in order to minimize context switches. | |
84 // Only used on the IO thread. | 93 // Only used on the IO thread. |
85 std::set<int> geolocation_renderer_ids_; | 94 std::map<int, RendererGeolocationOptions> geolocation_renderers_; |
86 // Maps renderer_id to whether high accuracy is requested for this particular | 95 |
87 // bridge. | 96 // Used by Android WebView to support that case that a renderer is in the |
88 std::map<int, bool> renderer_high_accuracy_; | 97 // 'paused' state but not yet using geolocation. If the renderer does start |
98 // using geolocation while paused, we move from this set into | |
99 // |geolocation_renderers_|. If the renderer doesn't end up wanting to use | |
100 // geolocation while 'paused' then we remove from this set. A renderer id | |
101 // can exist only in this set or |geolocation_renderers_|, never both. | |
102 std::set<int> pending_paused_geolocation_renderers_; | |
103 | |
89 // Only set whilst we are registered with the geolocation provider. | 104 // Only set whilst we are registered with the geolocation provider. |
90 GeolocationProviderImpl* geolocation_provider_; | 105 GeolocationProviderImpl* geolocation_provider_; |
91 | 106 |
92 GeolocationProviderImpl::LocationUpdateCallback callback_; | 107 GeolocationProviderImpl::LocationUpdateCallback callback_; |
93 | 108 |
94 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | 109 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); |
95 }; | 110 }; |
96 | 111 |
97 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | 112 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( |
98 int render_process_id, | 113 int render_process_id, |
99 GeolocationPermissionContext* geolocation_permission_context) | 114 GeolocationPermissionContext* geolocation_permission_context) |
100 : render_process_id_(render_process_id), | 115 : render_process_id_(render_process_id), |
101 geolocation_permission_context_(geolocation_permission_context), | 116 geolocation_permission_context_(geolocation_permission_context), |
102 geolocation_provider_(NULL) { | 117 geolocation_provider_(NULL) { |
103 callback_ = base::Bind( | 118 callback_ = base::Bind( |
104 &GeolocationDispatcherHostImpl::OnLocationUpdate, base::Unretained(this)); | 119 &GeolocationDispatcherHostImpl::OnLocationUpdate, base::Unretained(this)); |
105 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | 120 // This is initialized by ResourceMessageFilter. Do not add any non-trivial |
106 // initialization here, defer to OnRegisterBridge which is triggered whenever | 121 // initialization here, defer to OnRegisterBridge which is triggered whenever |
107 // a javascript geolocation object is actually initialized. | 122 // a javascript geolocation object is actually initialized. |
108 } | 123 } |
109 | 124 |
110 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { | 125 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { |
111 if (geolocation_provider_) | 126 if (geolocation_provider_) { |
112 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 127 BrowserThread::PostTask( |
128 BrowserThread::IO, FROM_HERE, | |
129 base::Bind( | |
130 base::IgnoreResult( | |
131 &GeolocationProvider::RemoveLocationUpdateCallback), | |
132 base::Unretained(geolocation_provider_), callback_)); | |
bulach
2013/12/11 13:29:27
I think GeolocationDispatcherHostImpl lives in the
benm (inactive)
2013/12/11 20:08:51
hmm... I need to double check this again, IIRC cal
| |
133 } | |
113 } | 134 } |
114 | 135 |
115 bool GeolocationDispatcherHostImpl::OnMessageReceived( | 136 bool GeolocationDispatcherHostImpl::OnMessageReceived( |
116 const IPC::Message& msg, bool* msg_was_ok) { | 137 const IPC::Message& msg, bool* msg_was_ok) { |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
118 *msg_was_ok = true; | 139 *msg_was_ok = true; |
119 bool handled = true; | 140 bool handled = true; |
120 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) | 141 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) |
121 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 142 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
122 OnCancelPermissionRequest) | 143 OnCancelPermissionRequest) |
123 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 144 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
124 OnRequestPermission) | 145 OnRequestPermission) |
125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 146 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
126 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 147 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
127 IPC_MESSAGE_UNHANDLED(handled = false) | 148 IPC_MESSAGE_UNHANDLED(handled = false) |
128 IPC_END_MESSAGE_MAP() | 149 IPC_END_MESSAGE_MAP() |
129 return handled; | 150 return handled; |
130 } | 151 } |
131 | 152 |
132 void GeolocationDispatcherHostImpl::OnLocationUpdate( | 153 void GeolocationDispatcherHostImpl::OnLocationUpdate( |
133 const Geoposition& geoposition) { | 154 const Geoposition& geoposition) { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
135 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | 156 for (std::map<int, RendererGeolocationOptions>::iterator it = |
136 it != geolocation_renderer_ids_.end(); ++it) { | 157 geolocation_renderers_.begin(); |
137 Send(new GeolocationMsg_PositionUpdated(*it, geoposition)); | 158 it != geolocation_renderers_.end(); ++it) { |
159 if (!(it->second.is_paused)) | |
160 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); | |
138 } | 161 } |
139 } | 162 } |
140 | 163 |
141 void GeolocationDispatcherHostImpl::OnRequestPermission( | 164 void GeolocationDispatcherHostImpl::OnRequestPermission( |
142 int render_view_id, | 165 int render_view_id, |
143 int bridge_id, | 166 int bridge_id, |
144 const GURL& requesting_frame) { | 167 const GURL& requesting_frame) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
146 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 169 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
147 << render_view_id << ":" << bridge_id; | 170 << render_view_id << ":" << bridge_id; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
181 const GURL& requesting_frame, | 204 const GURL& requesting_frame, |
182 bool enable_high_accuracy) { | 205 bool enable_high_accuracy) { |
183 // StartUpdating() can be invoked as a result of high-accuracy mode | 206 // StartUpdating() can be invoked as a result of high-accuracy mode |
184 // being enabled / disabled. No need to record the dispatcher again. | 207 // being enabled / disabled. No need to record the dispatcher again. |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 209 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
187 << render_view_id; | 210 << render_view_id; |
188 UMA_HISTOGRAM_BOOLEAN( | 211 UMA_HISTOGRAM_BOOLEAN( |
189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 212 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
190 enable_high_accuracy); | 213 enable_high_accuracy); |
191 if (!geolocation_renderer_ids_.count(render_view_id)) | |
192 geolocation_renderer_ids_.insert(render_view_id); | |
193 | 214 |
194 renderer_high_accuracy_[render_view_id] = enable_high_accuracy; | 215 std::map<int, RendererGeolocationOptions>::iterator it = |
195 RefreshHighAccuracy(); | 216 geolocation_renderers_.find(render_view_id); |
217 if (it == geolocation_renderers_.end()) { | |
218 bool should_start_paused = false; | |
219 if (pending_paused_geolocation_renderers_.erase(render_view_id) == 1) { | |
220 should_start_paused = true; | |
221 } | |
222 RendererGeolocationOptions opts = { | |
223 enable_high_accuracy, | |
224 should_start_paused | |
225 }; | |
226 geolocation_renderers_[render_view_id] = opts; | |
227 } else { | |
228 RendererGeolocationOptions* opts = &(it->second); | |
229 opts->high_accuracy = enable_high_accuracy; | |
bulach
2013/12/11 13:29:27
nit: unindent, but tbh, perhaps just:
it->second.
benm (inactive)
2013/12/11 20:08:51
Done.
| |
230 } | |
231 RefreshGeolocationOptions(); | |
196 } | 232 } |
197 | 233 |
198 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | 234 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
200 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 236 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
201 << render_view_id; | 237 << render_view_id; |
202 if (renderer_high_accuracy_.erase(render_view_id)) | 238 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); |
203 RefreshHighAccuracy(); | 239 geolocation_renderers_.erase(render_view_id); |
204 | 240 RefreshGeolocationOptions(); |
205 DCHECK_EQ(1U, geolocation_renderer_ids_.count(render_view_id)); | |
206 geolocation_renderer_ids_.erase(render_view_id); | |
207 } | 241 } |
208 | 242 |
209 void GeolocationDispatcherHostImpl::RefreshHighAccuracy() { | 243 void GeolocationDispatcherHostImpl::PauseOrResume(int render_view_id, |
244 bool should_pause) { | |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
211 if (renderer_high_accuracy_.empty()) { | 246 std::map<int, RendererGeolocationOptions>::iterator it = |
212 if (geolocation_provider_) { | 247 geolocation_renderers_.find(render_view_id); |
213 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 248 if (it == geolocation_renderers_.end()) { |
214 geolocation_provider_ = NULL; | 249 // This renderer is not using geolocation yet, but if it does before |
250 // we get a call to resume, we should start it up in the paused state. | |
251 if (should_pause) { | |
252 pending_paused_geolocation_renderers_.insert(render_view_id); | |
253 } else { | |
254 pending_paused_geolocation_renderers_.erase(render_view_id); | |
215 } | 255 } |
216 } else { | 256 } else { |
257 RendererGeolocationOptions* opts = &(it->second); | |
258 if (opts->is_paused != should_pause) | |
259 opts->is_paused = should_pause; | |
260 RefreshGeolocationOptions(); | |
261 } | |
262 } | |
263 | |
264 void GeolocationDispatcherHostImpl::RefreshGeolocationOptions() { | |
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
266 | |
267 bool needs_updates = false; | |
268 bool use_high_accuracy = false; | |
269 std::map<int, RendererGeolocationOptions>::const_iterator i = | |
270 geolocation_renderers_.begin(); | |
271 for (; i != geolocation_renderers_.end(); ++i) { | |
272 needs_updates |= !(i->second.is_paused); | |
273 use_high_accuracy |= i->second.high_accuracy; | |
274 if (needs_updates && use_high_accuracy) | |
275 break; | |
276 } | |
277 if (needs_updates) { | |
217 if (!geolocation_provider_) | 278 if (!geolocation_provider_) |
218 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); | 279 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); |
219 // Re-add to re-establish our options, in case they changed. | 280 // Re-add to re-establish our options, in case they changed. |
220 bool use_high_accuracy = false; | |
221 std::map<int, bool>::iterator i = renderer_high_accuracy_.begin(); | |
222 for (; i != renderer_high_accuracy_.end(); ++i) { | |
223 if (i->second) { | |
224 use_high_accuracy = true; | |
225 break; | |
226 } | |
227 } | |
228 geolocation_provider_->AddLocationUpdateCallback( | 281 geolocation_provider_->AddLocationUpdateCallback( |
229 callback_, use_high_accuracy); | 282 callback_, use_high_accuracy); |
283 } else { | |
284 if (geolocation_provider_) | |
285 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | |
286 geolocation_provider_ = NULL; | |
230 } | 287 } |
231 } | 288 } |
289 | |
232 } // namespace | 290 } // namespace |
233 | 291 |
234 | 292 |
235 // GeolocationDispatcherHost -------------------------------------------------- | 293 // GeolocationDispatcherHost -------------------------------------------------- |
236 | 294 |
237 // static | 295 // static |
238 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | 296 GeolocationDispatcherHost* GeolocationDispatcherHost::New( |
239 int render_process_id, | 297 int render_process_id, |
240 GeolocationPermissionContext* geolocation_permission_context) { | 298 GeolocationPermissionContext* geolocation_permission_context) { |
241 return new GeolocationDispatcherHostImpl( | 299 return new GeolocationDispatcherHostImpl( |
242 render_process_id, | 300 render_process_id, |
243 geolocation_permission_context); | 301 geolocation_permission_context); |
244 } | 302 } |
245 | 303 |
246 GeolocationDispatcherHost::GeolocationDispatcherHost() { | 304 GeolocationDispatcherHost::GeolocationDispatcherHost() { |
247 } | 305 } |
248 | 306 |
249 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 307 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
250 } | 308 } |
251 | 309 |
252 } // namespace content | 310 } // namespace content |
OLD | NEW |