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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 class GeolocationDispatcherHostImpl : public GeolocationDispatcherHost { | 48 class GeolocationDispatcherHostImpl : public GeolocationDispatcherHost { |
49 public: | 49 public: |
50 GeolocationDispatcherHostImpl( | 50 GeolocationDispatcherHostImpl( |
51 int render_process_id, | 51 int render_process_id, |
52 GeolocationPermissionContext* geolocation_permission_context); | 52 GeolocationPermissionContext* geolocation_permission_context); |
53 | 53 |
54 // GeolocationDispatcherHost | 54 // GeolocationDispatcherHost |
55 virtual bool OnMessageReceived(const IPC::Message& msg, | 55 virtual bool OnMessageReceived(const IPC::Message& msg, |
56 bool* msg_was_ok) OVERRIDE; | 56 bool* msg_was_ok) OVERRIDE; |
| 57 virtual bool IsGeolocationActive() OVERRIDE; |
57 | 58 |
58 private: | 59 private: |
59 virtual ~GeolocationDispatcherHostImpl(); | 60 virtual ~GeolocationDispatcherHostImpl(); |
60 | 61 |
61 void OnRequestPermission(int render_view_id, | 62 void OnRequestPermission(int render_view_id, |
62 int bridge_id, | 63 int bridge_id, |
63 const GURL& requesting_frame); | 64 const GURL& requesting_frame); |
64 void OnCancelPermissionRequest(int render_view_id, | 65 void OnCancelPermissionRequest(int render_view_id, |
65 int bridge_id, | 66 int bridge_id, |
66 const GURL& requesting_frame); | 67 const GURL& requesting_frame); |
67 void OnStartUpdating(int render_view_id, | 68 void OnStartUpdating(int render_view_id, |
68 const GURL& requesting_frame, | 69 const GURL& requesting_frame, |
69 bool enable_high_accuracy); | 70 bool enable_high_accuracy); |
70 void OnStopUpdating(int render_view_id); | 71 void OnStopUpdating(int render_view_id); |
71 | 72 |
| 73 |
| 74 virtual void PauseOrResume(int render_view_id, bool should_pause) OVERRIDE; |
| 75 |
72 // Updates the |geolocation_provider_| with the currently required update | 76 // Updates the |geolocation_provider_| with the currently required update |
73 // options, based on |renderer_high_accuracy_|. | 77 // options. |
74 void RefreshHighAccuracy(); | 78 void RefreshGeolocationOptions(); |
75 | 79 |
76 void OnLocationUpdate(const Geoposition& position); | 80 void OnLocationUpdate(const Geoposition& position); |
77 | 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 std::set<int> pending_paused_geolocation_renderers_; |
87 // bridge. | 97 |
88 std::map<int, bool> renderer_high_accuracy_; | |
89 // Only set whilst we are registered with the geolocation provider. | 98 // Only set whilst we are registered with the geolocation provider. |
90 GeolocationProviderImpl* geolocation_provider_; | 99 GeolocationProviderImpl* geolocation_provider_; |
91 | 100 |
92 GeolocationProviderImpl::LocationUpdateCallback callback_; | 101 GeolocationProviderImpl::LocationUpdateCallback callback_; |
93 | 102 |
94 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); | 103 DISALLOW_COPY_AND_ASSIGN(GeolocationDispatcherHostImpl); |
95 }; | 104 }; |
96 | 105 |
97 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( | 106 GeolocationDispatcherHostImpl::GeolocationDispatcherHostImpl( |
98 int render_process_id, | 107 int render_process_id, |
99 GeolocationPermissionContext* geolocation_permission_context) | 108 GeolocationPermissionContext* geolocation_permission_context) |
100 : render_process_id_(render_process_id), | 109 : render_process_id_(render_process_id), |
101 geolocation_permission_context_(geolocation_permission_context), | 110 geolocation_permission_context_(geolocation_permission_context), |
102 geolocation_provider_(NULL) { | 111 geolocation_provider_(NULL) { |
103 callback_ = base::Bind( | 112 callback_ = base::Bind( |
104 &GeolocationDispatcherHostImpl::OnLocationUpdate, base::Unretained(this)); | 113 &GeolocationDispatcherHostImpl::OnLocationUpdate, base::Unretained(this)); |
105 // This is initialized by ResourceMessageFilter. Do not add any non-trivial | 114 // This is initialized by ResourceMessageFilter. Do not add any non-trivial |
106 // initialization here, defer to OnRegisterBridge which is triggered whenever | 115 // initialization here, defer to OnRegisterBridge which is triggered whenever |
107 // a javascript geolocation object is actually initialized. | 116 // a javascript geolocation object is actually initialized. |
108 } | 117 } |
109 | 118 |
110 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { | 119 GeolocationDispatcherHostImpl::~GeolocationDispatcherHostImpl() { |
111 if (geolocation_provider_) | 120 if (geolocation_provider_) { |
112 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 121 BrowserThread::PostTask( |
| 122 BrowserThread::IO, FROM_HERE, |
| 123 base::Bind( |
| 124 base::IgnoreResult( |
| 125 &GeolocationProvider::RemoveLocationUpdateCallback), |
| 126 base::Unretained(geolocation_provider_), callback_)); |
| 127 } |
113 } | 128 } |
114 | 129 |
115 bool GeolocationDispatcherHostImpl::OnMessageReceived( | 130 bool GeolocationDispatcherHostImpl::OnMessageReceived( |
116 const IPC::Message& msg, bool* msg_was_ok) { | 131 const IPC::Message& msg, bool* msg_was_ok) { |
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
118 *msg_was_ok = true; | 133 *msg_was_ok = true; |
119 bool handled = true; | 134 bool handled = true; |
120 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) | 135 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) |
121 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, | 136 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, |
122 OnCancelPermissionRequest) | 137 OnCancelPermissionRequest) |
123 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, | 138 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, |
124 OnRequestPermission) | 139 OnRequestPermission) |
125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) | 140 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) |
126 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) | 141 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) |
127 IPC_MESSAGE_UNHANDLED(handled = false) | 142 IPC_MESSAGE_UNHANDLED(handled = false) |
128 IPC_END_MESSAGE_MAP() | 143 IPC_END_MESSAGE_MAP() |
129 return handled; | 144 return handled; |
130 } | 145 } |
131 | 146 |
132 void GeolocationDispatcherHostImpl::OnLocationUpdate( | 147 void GeolocationDispatcherHostImpl::OnLocationUpdate( |
133 const Geoposition& geoposition) { | 148 const Geoposition& geoposition) { |
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
135 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); | 150 for (std::map<int, RendererGeolocationOptions>::iterator it = |
136 it != geolocation_renderer_ids_.end(); ++it) { | 151 geolocation_renderers_.begin(); |
137 Send(new GeolocationMsg_PositionUpdated(*it, geoposition)); | 152 it != geolocation_renderers_.end(); ++it) { |
| 153 if (!(it->second.is_paused)) |
| 154 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition)); |
138 } | 155 } |
139 } | 156 } |
140 | 157 |
141 void GeolocationDispatcherHostImpl::OnRequestPermission( | 158 void GeolocationDispatcherHostImpl::OnRequestPermission( |
142 int render_view_id, | 159 int render_view_id, |
143 int bridge_id, | 160 int bridge_id, |
144 const GURL& requesting_frame) { | 161 const GURL& requesting_frame) { |
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
146 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 163 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
147 << render_view_id << ":" << bridge_id; | 164 << render_view_id << ":" << bridge_id; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 const GURL& requesting_frame, | 198 const GURL& requesting_frame, |
182 bool enable_high_accuracy) { | 199 bool enable_high_accuracy) { |
183 // StartUpdating() can be invoked as a result of high-accuracy mode | 200 // StartUpdating() can be invoked as a result of high-accuracy mode |
184 // being enabled / disabled. No need to record the dispatcher again. | 201 // being enabled / disabled. No need to record the dispatcher again. |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 203 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
187 << render_view_id; | 204 << render_view_id; |
188 UMA_HISTOGRAM_BOOLEAN( | 205 UMA_HISTOGRAM_BOOLEAN( |
189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", | 206 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", |
190 enable_high_accuracy); | 207 enable_high_accuracy); |
191 if (!geolocation_renderer_ids_.count(render_view_id)) | |
192 geolocation_renderer_ids_.insert(render_view_id); | |
193 | 208 |
194 renderer_high_accuracy_[render_view_id] = enable_high_accuracy; | 209 std::map<int, RendererGeolocationOptions>::iterator it = |
195 RefreshHighAccuracy(); | 210 geolocation_renderers_.find(render_view_id); |
| 211 if (it == geolocation_renderers_.end()) { |
| 212 bool should_start_paused = false; |
| 213 if (pending_paused_geolocation_renderers_.erase(render_view_id) == 1) { |
| 214 should_start_paused = true; |
| 215 } |
| 216 RendererGeolocationOptions opts = { |
| 217 enable_high_accuracy, |
| 218 should_start_paused |
| 219 }; |
| 220 geolocation_renderers_[render_view_id] = opts; |
| 221 } else { |
| 222 RendererGeolocationOptions* opts = &(it->second); |
| 223 opts->high_accuracy = enable_high_accuracy; |
| 224 } |
| 225 RefreshGeolocationOptions(); |
196 } | 226 } |
197 | 227 |
198 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { | 228 void GeolocationDispatcherHostImpl::OnStopUpdating(int render_view_id) { |
199 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
200 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" | 230 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" |
201 << render_view_id; | 231 << render_view_id; |
202 if (renderer_high_accuracy_.erase(render_view_id)) | 232 DCHECK_EQ(1U, geolocation_renderers_.count(render_view_id)); |
203 RefreshHighAccuracy(); | 233 geolocation_renderers_.erase(render_view_id); |
204 | 234 RefreshGeolocationOptions(); |
205 DCHECK_EQ(1U, geolocation_renderer_ids_.count(render_view_id)); | |
206 geolocation_renderer_ids_.erase(render_view_id); | |
207 } | 235 } |
208 | 236 |
209 void GeolocationDispatcherHostImpl::RefreshHighAccuracy() { | 237 void GeolocationDispatcherHostImpl::PauseOrResume(int render_view_id, |
| 238 bool should_pause) { |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
211 if (renderer_high_accuracy_.empty()) { | 240 std::map<int, RendererGeolocationOptions>::iterator it = |
212 if (geolocation_provider_) { | 241 geolocation_renderers_.find(render_view_id); |
213 geolocation_provider_->RemoveLocationUpdateCallback(callback_); | 242 if (it == geolocation_renderers_.end()) { |
214 geolocation_provider_ = NULL; | 243 // This renderer is not using geolocation yet, but if it does before |
| 244 // we get a call to resume, we should start it up in the paused state. |
| 245 if (should_pause) { |
| 246 pending_paused_geolocation_renderers_.insert(render_view_id); |
| 247 } else { |
| 248 pending_paused_geolocation_renderers_.erase(render_view_id); |
215 } | 249 } |
216 } else { | 250 } else { |
| 251 RendererGeolocationOptions* opts = &(it->second); |
| 252 if (opts->is_paused != should_pause) |
| 253 opts->is_paused = should_pause; |
| 254 RefreshGeolocationOptions(); |
| 255 } |
| 256 } |
| 257 |
| 258 void GeolocationDispatcherHostImpl::RefreshGeolocationOptions() { |
| 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 260 |
| 261 bool needs_updates = false; |
| 262 bool use_high_accuracy = false; |
| 263 std::map<int, RendererGeolocationOptions>::const_iterator i = |
| 264 geolocation_renderers_.begin(); |
| 265 for (; i != geolocation_renderers_.end(); ++i) { |
| 266 needs_updates |= !(i->second.is_paused); |
| 267 use_high_accuracy |= i->second.high_accuracy; |
| 268 if (needs_updates && use_high_accuracy) |
| 269 break; |
| 270 } |
| 271 if (needs_updates) { |
217 if (!geolocation_provider_) | 272 if (!geolocation_provider_) |
218 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); | 273 geolocation_provider_ = GeolocationProviderImpl::GetInstance(); |
219 // Re-add to re-establish our options, in case they changed. | 274 // 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( | 275 geolocation_provider_->AddLocationUpdateCallback( |
229 callback_, use_high_accuracy); | 276 callback_, use_high_accuracy); |
| 277 } else { |
| 278 if (geolocation_provider_) |
| 279 geolocation_provider_->RemoveLocationUpdateCallback(callback_); |
| 280 geolocation_provider_ = NULL; |
230 } | 281 } |
231 } | 282 } |
| 283 |
| 284 bool GeolocationDispatcherHostImpl::IsGeolocationActive() { |
| 285 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 286 std::map<int, RendererGeolocationOptions>::const_iterator i = |
| 287 geolocation_renderers_.begin(); |
| 288 if (i == geolocation_renderers_.end()) |
| 289 return false; |
| 290 |
| 291 for (; i != geolocation_renderers_.end(); ++i) |
| 292 if (!(i->second.is_paused)) return true; |
| 293 return false; |
| 294 } |
232 } // namespace | 295 } // namespace |
233 | 296 |
234 | 297 |
235 // GeolocationDispatcherHost -------------------------------------------------- | 298 // GeolocationDispatcherHost -------------------------------------------------- |
236 | 299 |
237 // static | 300 // static |
238 GeolocationDispatcherHost* GeolocationDispatcherHost::New( | 301 GeolocationDispatcherHost* GeolocationDispatcherHost::New( |
239 int render_process_id, | 302 int render_process_id, |
240 GeolocationPermissionContext* geolocation_permission_context) { | 303 GeolocationPermissionContext* geolocation_permission_context) { |
241 return new GeolocationDispatcherHostImpl( | 304 return new GeolocationDispatcherHostImpl( |
242 render_process_id, | 305 render_process_id, |
243 geolocation_permission_context); | 306 geolocation_permission_context); |
244 } | 307 } |
245 | 308 |
246 GeolocationDispatcherHost::GeolocationDispatcherHost() { | 309 GeolocationDispatcherHost::GeolocationDispatcherHost() { |
247 } | 310 } |
248 | 311 |
249 GeolocationDispatcherHost::~GeolocationDispatcherHost() { | 312 GeolocationDispatcherHost::~GeolocationDispatcherHost() { |
250 } | 313 } |
251 | 314 |
252 } // namespace content | 315 } // namespace content |
OLD | NEW |