| 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> | |
| 8 #include <set> | |
| 9 #include <utility> | 7 #include <utility> |
| 10 | 8 |
| 11 #include "base/bind.h" | 9 #include "base/bind.h" |
| 12 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 13 #include "content/browser/geolocation/geolocation_provider_impl.h" | |
| 14 #include "content/browser/renderer_host/render_message_filter.h" | 11 #include "content/browser/renderer_host/render_message_filter.h" |
| 15 #include "content/browser/renderer_host/render_process_host_impl.h" | 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 16 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 17 #include "content/public/browser/geolocation_permission_context.h" | 14 #include "content/public/browser/geolocation_permission_context.h" |
| 18 #include "content/public/common/geoposition.h" | 15 #include "content/public/common/geoposition.h" |
| 19 #include "content/common/geolocation_messages.h" | 16 #include "content/common/geolocation_messages.h" |
| 20 | 17 |
| 21 namespace content { | 18 namespace content { |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 render_view_host->Send( | 78 render_view_host->Send( |
| 82 new GeolocationMsg_PermissionSet(render_view_id, bridge_id, allowed)); | 79 new GeolocationMsg_PermissionSet(render_view_id, bridge_id, allowed)); |
| 83 | 80 |
| 84 if (allowed) { | 81 if (allowed) { |
| 85 BrowserThread::PostTask( | 82 BrowserThread::PostTask( |
| 86 BrowserThread::IO, FROM_HERE, | 83 BrowserThread::IO, FROM_HERE, |
| 87 base::Bind(&NotifyGeolocationProviderPermissionGranted)); | 84 base::Bind(&NotifyGeolocationProviderPermissionGranted)); |
| 88 } | 85 } |
| 89 } | 86 } |
| 90 | 87 |
| 91 class GeolocationDispatcherHostImpl : public GeolocationDispatcherHost { | 88 } // namespace |
| 92 public: | |
| 93 GeolocationDispatcherHostImpl( | |
| 94 int render_process_id, | |
| 95 GeolocationPermissionContext* geolocation_permission_context); | |
| 96 | 89 |
| 97 // GeolocationDispatcherHost | 90 GeolocationDispatcherHost::GeolocationDispatcherHost( |
| 98 virtual bool OnMessageReceived(const IPC::Message& msg, | |
| 99 bool* msg_was_ok) OVERRIDE; | |
| 100 | |
| 101 private: | |
| 102 virtual ~GeolocationDispatcherHostImpl(); | |
| 103 | |
| 104 void OnRequestPermission(int render_view_id, | |
| 105 int bridge_id, | |
| 106 const GURL& requesting_frame, | |
| 107 bool user_gesture); | |
| 108 void OnCancelPermissionRequest(int render_view_id, | |
| 109 int bridge_id, | |
| 110 const GURL& requesting_frame); | |
| 111 void OnStartUpdating(int render_view_id, | |
| 112 const GURL& requesting_frame, | |
| 113 bool enable_high_accuracy); | |
| 114 void OnStopUpdating(int render_view_id); | |
| 115 | |
| 116 | |
| 117 virtual void PauseOrResume(int render_view_id, bool should_pause) OVERRIDE; | |
| 118 | |
| 119 // Updates the |geolocation_provider_| with the currently required update | |
| 120 // options. | |
| 121 void RefreshGeolocationOptions(); | |
| 122 | |
| 123 void OnLocationUpdate(const Geoposition& position); | |
| 124 | |
| 125 int render_process_id_; | |
| 126 scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; | |
| 127 | |
| 128 struct RendererGeolocationOptions { | |
| 129 bool high_accuracy; | |
| 130 bool is_paused; | |
| 131 }; | |
| 132 | |
| 133 // Used to keep track of the renderers in this process that are using | |
| 134 // geolocation and the options associated with them. The map is iterated | |
| 135 // when a location update is available and the fan out to individual bridge | |
| 136 // IDs happens renderer side, in order to minimize context switches. | |
| 137 // Only used on the IO thread. | |
| 138 std::map<int, RendererGeolocationOptions> geolocation_renderers_; | |
| 139 | |
| 140 // Used by Android WebView to support that case that a renderer is in the | |
| 141 // 'paused' state but not yet using geolocation. If the renderer does start | |
| 142 // using geolocation while paused, we move from this set into | |
| 143 // |geolocation_renderers_|. If the renderer doesn't end up wanting to use | |
| 144 // geolocation while 'paused' then we remove from this set. A renderer id | |
| 145 // can exist only in this set or |geolocation_renderers_|, never both. | |
| 146 std::set<int> pending_paused_geolocation_renderers_; | |
| 147 | |
| 148 // Only set whilst we are registered with the geolocation provider. | |
| 149 GeolocationProviderImpl* geolocation_provider_; | |
| 150 | |
| 151 GeolocationProviderImpl::LocationUpdateCallback callback_; | |
| 152 | |
| 153 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | |
| 154 }; | |
| 155 | |
| 156 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | |
| 157 int render_process_id, | 91 int render_process_id, |
| 158 GeolocationPermissionContext* geolocation_permission_context) | 92 GeolocationPermissionContext* geolocation_permission_context) |
| 159 : render_process_id_(render_process_id), | 93 : BrowserMessageFilter(GeolocationMsgStart), |
| 94 render_process_id_(render_process_id), |
| 160 geolocation_permission_context_(geolocation_permission_context), | 95 geolocation_permission_context_(geolocation_permission_context), |
| 161 geolocation_provider_(NULL) { | 96 geolocation_provider_(NULL) { |
| 162 callback_ = base::Bind( | 97 callback_ = base::Bind( |
| 163 &GeolocationDispatcherHostImpl::OnLocationUpdate, base::Unretained(this)); | 98 &GeolocationDispatcherHost::OnLocationUpdate, base::Unretained(this)); |
| 164 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | 99 // This is initialized by ResourceMessageFilter. Do not add any non-trivial |
| 165 // initialization here, defer to OnRegisterBridge which is triggered whenever | 100 // initialization here, defer to OnRegisterBridge which is triggered whenever |
| 166 // a javascript geolocation object is actually initialized. | 101 // a javascript geolocation object is actually initialized. |
| 167 } | 102 } |
| 168 | 103 |
| 169 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { | 104 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 171 if (geolocation_provider_) | 106 if (geolocation_provider_) |
| 172 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 107 geolocation_provider_->RemoveLocationUpdateCallback(callback_); |
| 173 } | 108 } |
| 174 | 109 |
| 175 bool GeolocationDispatcherHostImpl::OnMessageReceived( | 110 bool GeolocationDispatcherHost::OnMessageReceived( |
| 176 const IPC::Message& msg, bool* msg_was_ok) { | 111 const IPC::Message& msg, bool* msg_was_ok) { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 178 *msg_was_ok = true; | 113 *msg_was_ok = true; |
| 179 bool handled = true; | 114 bool handled = true; |
| 180 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) | 115 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHost, msg, *msg_was_ok) |
| 181 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 116 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
| 182 OnCancelPermissionRequest) | 117 OnCancelPermissionRequest) |
| 183 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 118 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
| 184 OnRequestPermission) | 119 OnRequestPermission) |
| 185 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 120 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
| 186 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 121 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
| 187 IPC_MESSAGE_UNHANDLED(handled = false) | 122 IPC_MESSAGE_UNHANDLED(handled = false) |
| 188 IPC_END_MESSAGE_MAP() | 123 IPC_END_MESSAGE_MAP() |
| 189 return handled; | 124 return handled; |
| 190 } | 125 } |
| 191 | 126 |
| 192 void GeolocationDispatcherHostImpl::OnLocationUpdate( | 127 void GeolocationDispatcherHost::OnLocationUpdate( |
| 193 const Geoposition& geoposition) { | 128 const Geoposition& geoposition) { |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 195 RecordGeopositionErrorCode(geoposition.error_code); | 130 RecordGeopositionErrorCode(geoposition.error_code); |
| 196 for (std::map<int, RendererGeolocationOptions>::iterator it = | 131 for (std::map<int, RendererGeolocationOptions>::iterator it = |
| 197 geolocation_renderers_.begin(); | 132 geolocation_renderers_.begin(); |
| 198 it != geolocation_renderers_.end(); ++it) { | 133 it != geolocation_renderers_.end(); ++it) { |
| 199 if (!(it->second.is_paused)) | 134 if (!(it->second.is_paused)) |
| 200 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); | 135 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); |
| 201 } | 136 } |
| 202 } | 137 } |
| 203 | 138 |
| 204 void GeolocationDispatcherHostImpl::OnRequestPermission( | 139 void GeolocationDispatcherHost::OnRequestPermission( |
| 205 int render_view_id, | 140 int render_view_id, |
| 206 int bridge_id, | 141 int bridge_id, |
| 207 const GURL& requesting_frame, | 142 const GURL& requesting_frame, |
| 208 bool user_gesture) { | 143 bool user_gesture) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 210 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 145 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
| 211 << render_view_id << ":" << bridge_id; | 146 << render_view_id << ":" << bridge_id; |
| 212 if (geolocation_permission_context_.get()) { | 147 if (geolocation_permission_context_.get()) { |
| 213 geolocation_permission_context_->RequestGeolocationPermission( | 148 geolocation_permission_context_->RequestGeolocationPermission( |
| 214 render_process_id_, | 149 render_process_id_, |
| 215 render_view_id, | 150 render_view_id, |
| 216 bridge_id, | 151 bridge_id, |
| 217 requesting_frame, | 152 requesting_frame, |
| 218 user_gesture, | 153 user_gesture, |
| 219 base::Bind(&SendGeolocationPermissionResponse, | 154 base::Bind(&SendGeolocationPermissionResponse, |
| 220 render_process_id_, | 155 render_process_id_, |
| 221 render_view_id, | 156 render_view_id, |
| 222 bridge_id)); | 157 bridge_id)); |
| 223 } else { | 158 } else { |
| 224 BrowserThread::PostTask( | 159 BrowserThread::PostTask( |
| 225 BrowserThread::UI, FROM_HERE, | 160 BrowserThread::UI, FROM_HERE, |
| 226 base::Bind(&SendGeolocationPermissionResponse, render_process_id_, | 161 base::Bind(&SendGeolocationPermissionResponse, render_process_id_, |
| 227 render_view_id, bridge_id, true)); | 162 render_view_id, bridge_id, true)); |
| 228 } | 163 } |
| 229 } | 164 } |
| 230 | 165 |
| 231 void GeolocationDispatcherHostImpl::OnCancelPermissionRequest( | 166 void GeolocationDispatcherHost::OnCancelPermissionRequest( |
| 232 int render_view_id, | 167 int render_view_id, |
| 233 int bridge_id, | 168 int bridge_id, |
| 234 const GURL& requesting_frame) { | 169 const GURL& requesting_frame) { |
| 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 236 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 171 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
| 237 << render_view_id << ":" << bridge_id; | 172 << render_view_id << ":" << bridge_id; |
| 238 if (geolocation_permission_context_.get()) { | 173 if (geolocation_permission_context_.get()) { |
| 239 geolocation_permission_context_->CancelGeolocationPermissionRequest( | 174 geolocation_permission_context_->CancelGeolocationPermissionRequest( |
| 240 render_process_id_, render_view_id, bridge_id, requesting_frame); | 175 render_process_id_, render_view_id, bridge_id, requesting_frame); |
| 241 } | 176 } |
| 242 } | 177 } |
| 243 | 178 |
| 244 void GeolocationDispatcherHostImpl::OnStartUpdating( | 179 void GeolocationDispatcherHost::OnStartUpdating( |
| 245 int render_view_id, | 180 int render_view_id, |
| 246 const GURL& requesting_frame, | 181 const GURL& requesting_frame, |
| 247 bool enable_high_accuracy) { | 182 bool enable_high_accuracy) { |
| 248 // StartUpdating() can be invoked as a result of high-accuracy mode | 183 // StartUpdating() can be invoked as a result of high-accuracy mode |
| 249 // being enabled / disabled. No need to record the dispatcher again. | 184 // being enabled / disabled. No need to record the dispatcher again. |
| 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 251 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
| 252 << render_view_id; | 187 << render_view_id; |
| 253 UMA_HISTOGRAM_BOOLEAN( | 188 UMA_HISTOGRAM_BOOLEAN( |
| 254 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
| (...skipping 10 matching lines...) Expand all Loading... |
| 265 enable_high_accuracy, | 200 enable_high_accuracy, |
| 266 should_start_paused | 201 should_start_paused |
| 267 }; | 202 }; |
| 268 geolocation_renderers_[render_view_id] = opts; | 203 geolocation_renderers_[render_view_id] = opts; |
| 269 } else { | 204 } else { |
| 270 it->second.high_accuracy = enable_high_accuracy; | 205 it->second.high_accuracy = enable_high_accuracy; |
| 271 } | 206 } |
| 272 RefreshGeolocationOptions(); | 207 RefreshGeolocationOptions(); |
| 273 } | 208 } |
| 274 | 209 |
| 275 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | 210 void GeolocationDispatcherHost::OnStopUpdating(int render_view_id) { |
| 276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 277 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 212 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
| 278 << render_view_id; | 213 << render_view_id; |
| 279 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); | 214 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); |
| 280 geolocation_renderers_.erase(render_view_id); | 215 geolocation_renderers_.erase(render_view_id); |
| 281 RefreshGeolocationOptions(); | 216 RefreshGeolocationOptions(); |
| 282 } | 217 } |
| 283 | 218 |
| 284 void GeolocationDispatcherHostImpl::PauseOrResume(int render_view_id, | 219 void GeolocationDispatcherHost::PauseOrResume(int render_view_id, |
| 285 bool should_pause) { | 220 bool should_pause) { |
| 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 287 std::map<int, RendererGeolocationOptions>::iterator it = | 222 std::map<int, RendererGeolocationOptions>::iterator it = |
| 288 geolocation_renderers_.find(render_view_id); | 223 geolocation_renderers_.find(render_view_id); |
| 289 if (it == geolocation_renderers_.end()) { | 224 if (it == geolocation_renderers_.end()) { |
| 290 // This renderer is not using geolocation yet, but if it does before | 225 // This renderer is not using geolocation yet, but if it does before |
| 291 // we get a call to resume, we should start it up in the paused state. | 226 // we get a call to resume, we should start it up in the paused state. |
| 292 if (should_pause) { | 227 if (should_pause) { |
| 293 pending_paused_geolocation_renderers_.insert(render_view_id); | 228 pending_paused_geolocation_renderers_.insert(render_view_id); |
| 294 } else { | 229 } else { |
| 295 pending_paused_geolocation_renderers_.erase(render_view_id); | 230 pending_paused_geolocation_renderers_.erase(render_view_id); |
| 296 } | 231 } |
| 297 } else { | 232 } else { |
| 298 RendererGeolocationOptions* opts = &(it->second); | 233 RendererGeolocationOptions* opts = &(it->second); |
| 299 if (opts->is_paused != should_pause) | 234 if (opts->is_paused != should_pause) |
| 300 opts->is_paused = should_pause; | 235 opts->is_paused = should_pause; |
| 301 RefreshGeolocationOptions(); | 236 RefreshGeolocationOptions(); |
| 302 } | 237 } |
| 303 } | 238 } |
| 304 | 239 |
| 305 void GeolocationDispatcherHostImpl::RefreshGeolocationOptions() { | 240 void GeolocationDispatcherHost::RefreshGeolocationOptions() { |
| 306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 307 | 242 |
| 308 bool needs_updates = false; | 243 bool needs_updates = false; |
| 309 bool use_high_accuracy = false; | 244 bool use_high_accuracy = false; |
| 310 std::map<int, RendererGeolocationOptions>::const_iterator i = | 245 std::map<int, RendererGeolocationOptions>::const_iterator i = |
| 311 geolocation_renderers_.begin(); | 246 geolocation_renderers_.begin(); |
| 312 for (; i != geolocation_renderers_.end(); ++i) { | 247 for (; i != geolocation_renderers_.end(); ++i) { |
| 313 needs_updates |= !(i->second.is_paused); | 248 needs_updates |= !(i->second.is_paused); |
| 314 use_high_accuracy |= i->second.high_accuracy; | 249 use_high_accuracy |= i->second.high_accuracy; |
| 315 if (needs_updates && use_high_accuracy) | 250 if (needs_updates && use_high_accuracy) |
| 316 break; | 251 break; |
| 317 } | 252 } |
| 318 if (needs_updates) { | 253 if (needs_updates) { |
| 319 if (!geolocation_provider_) | 254 if (!geolocation_provider_) |
| 320 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); | 255 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); |
| 321 // Re-add to re-establish our options, in case they changed. | 256 // Re-add to re-establish our options, in case they changed. |
| 322 geolocation_provider_->AddLocationUpdateCallback( | 257 geolocation_provider_->AddLocationUpdateCallback( |
| 323 callback_, use_high_accuracy); | 258 callback_, use_high_accuracy); |
| 324 } else { | 259 } else { |
| 325 if (geolocation_provider_) | 260 if (geolocation_provider_) |
| 326 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 261 geolocation_provider_->RemoveLocationUpdateCallback(callback_); |
| 327 geolocation_provider_ = NULL; | 262 geolocation_provider_ = NULL; |
| 328 } | 263 } |
| 329 } | 264 } |
| 330 | 265 |
| 331 } // namespace | |
| 332 | |
| 333 | |
| 334 // GeolocationDispatcherHost -------------------------------------------------- | |
| 335 | |
| 336 // static | |
| 337 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | |
| 338 int render_process_id, | |
| 339 GeolocationPermissionContext* geolocation_permission_context) { | |
| 340 return new GeolocationDispatcherHostImpl( | |
| 341 render_process_id, | |
| 342 geolocation_permission_context); | |
| 343 } | |
| 344 | |
| 345 GeolocationDispatcherHost::GeolocationDispatcherHost() | |
| 346 : BrowserMessageFilter(GeolocationMsgStart) { | |
| 347 } | |
| 348 | |
| 349 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | |
| 350 } | |
| 351 | |
| 352 } // namespace content | 266 } // namespace content |
| OLD | NEW |