Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: content/browser/geolocation/geolocation_dispatcher_host.cc

Issue 65273002: Add a mechanism to pause and resume geolocation requests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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() {
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
111 if (geolocation_provider_) 127 if (geolocation_provider_)
112 geolocation_provider_->RemoveLocationUpdateCallback(callback_); 128 geolocation_provider_->RemoveLocationUpdateCallback(callback_);
113 } 129 }
114 130
115 bool GeolocationDispatcherHostImpl::OnMessageReceived( 131 bool GeolocationDispatcherHostImpl::OnMessageReceived(
116 const IPC::Message& msg, bool* msg_was_ok) { 132 const IPC::Message& msg, bool* msg_was_ok) {
117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
118 *msg_was_ok = true; 134 *msg_was_ok = true;
119 bool handled = true; 135 bool handled = true;
120 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok) 136 IPC_BEGIN_MESSAGE_MAP_EX(GeolocationDispatcherHostImpl, msg, *msg_was_ok)
121 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest, 137 IPC_MESSAGE_HANDLER(GeolocationHostMsg_CancelPermissionRequest,
122 OnCancelPermissionRequest) 138 OnCancelPermissionRequest)
123 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission, 139 IPC_MESSAGE_HANDLER(GeolocationHostMsg_RequestPermission,
124 OnRequestPermission) 140 OnRequestPermission)
125 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating) 141 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StartUpdating, OnStartUpdating)
126 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating) 142 IPC_MESSAGE_HANDLER(GeolocationHostMsg_StopUpdating, OnStopUpdating)
127 IPC_MESSAGE_UNHANDLED(handled = false) 143 IPC_MESSAGE_UNHANDLED(handled = false)
128 IPC_END_MESSAGE_MAP() 144 IPC_END_MESSAGE_MAP()
129 return handled; 145 return handled;
130 } 146 }
131 147
132 void GeolocationDispatcherHostImpl::OnLocationUpdate( 148 void GeolocationDispatcherHostImpl::OnLocationUpdate(
133 const Geoposition& geoposition) { 149 const Geoposition& geoposition) {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
135 for (std::set<int>::iterator it = geolocation_renderer_ids_.begin(); 151 for (std::map<int, RendererGeolocationOptions>::iterator it =
136 it != geolocation_renderer_ids_.end(); ++it) { 152 geolocation_renderers_.begin();
137 Send(new GeolocationMsg_PositionUpdated(*it, geoposition)); 153 it != geolocation_renderers_.end(); ++it) {
154 if (!(it->second.is_paused))
155 Send(new GeolocationMsg_PositionUpdated(it->first, geoposition));
138 } 156 }
139 } 157 }
140 158
141 void GeolocationDispatcherHostImpl::OnRequestPermission( 159 void GeolocationDispatcherHostImpl::OnRequestPermission(
142 int render_view_id, 160 int render_view_id,
143 int bridge_id, 161 int bridge_id,
144 const GURL& requesting_frame) { 162 const GURL& requesting_frame) {
145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
146 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" 164 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":"
147 << render_view_id << ":" << bridge_id; 165 << render_view_id << ":" << bridge_id;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 const GURL& requesting_frame, 199 const GURL& requesting_frame,
182 bool enable_high_accuracy) { 200 bool enable_high_accuracy) {
183 // StartUpdating() can be invoked as a result of high-accuracy mode 201 // StartUpdating() can be invoked as a result of high-accuracy mode
184 // being enabled / disabled. No need to record the dispatcher again. 202 // being enabled / disabled. No need to record the dispatcher again.
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
186 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":" 204 DVLOG(1) << __FUNCTION__ << " " << render_process_id_ << ":"
187 << render_view_id; 205 << render_view_id;
188 UMA_HISTOGRAM_BOOLEAN( 206 UMA_HISTOGRAM_BOOLEAN(
189 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy", 207 "Geolocation.GeolocationDispatcherHostImpl.EnableHighAccuracy",
190 enable_high_accuracy); 208 enable_high_accuracy);
191 if (!geolocation_renderer_ids_.count(render_view_id))
192 geolocation_renderer_ids_.insert(render_view_id);
193 209
194 renderer_high_accuracy_[render_view_id] = enable_high_accuracy; 210 std::map<int, RendererGeolocationOptions>::iterator it =
195 RefreshHighAccuracy(); 211 geolocation_renderers_.find(render_view_id);
212 if (it == geolocation_renderers_.end()) {
213 bool should_start_paused = false;
214 if (pending_paused_geolocation_renderers_.erase(render_view_id) == 1) {
215 should_start_paused = true;
216 }
217 RendererGeolocationOptions opts = {
218 enable_high_accuracy,
219 should_start_paused
220 };
221 geolocation_renderers_[render_view_id] = opts;
222 } else {
223 it->second.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
232 } // namespace 284 } // namespace
233 285
234 286
235 // GeolocationDispatcherHost -------------------------------------------------- 287 // GeolocationDispatcherHost --------------------------------------------------
236 288
237 // static 289 // static
238 GeolocationDispatcherHost* GeolocationDispatcherHost::New( 290 GeolocationDispatcherHost* GeolocationDispatcherHost::New(
239 int render_process_id, 291 int render_process_id,
240 GeolocationPermissionContext* geolocation_permission_context) { 292 GeolocationPermissionContext* geolocation_permission_context) {
241 return new GeolocationDispatcherHostImpl( 293 return new GeolocationDispatcherHostImpl(
242 render_process_id, 294 render_process_id,
243 geolocation_permission_context); 295 geolocation_permission_context);
244 } 296 }
245 297
246 GeolocationDispatcherHost::GeolocationDispatcherHost() { 298 GeolocationDispatcherHost::GeolocationDispatcherHost() {
247 } 299 }
248 300
249 GeolocationDispatcherHost::~GeolocationDispatcherHost() { 301 GeolocationDispatcherHost::~GeolocationDispatcherHost() {
250 } 302 }
251 303
252 } // namespace content 304 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geolocation/geolocation_dispatcher_host.h ('k') | content/browser/geolocation/location_api_adapter_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698