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