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

Side by Side Diff: content/browser/geofencing/geofencing_manager.cc

Issue 788073007: Cleanup geofence registrations when a service worker is unregistered. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: slightly improve mock usage in test Created 5 years, 12 months 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/geofencing/geofencing_manager.h" 5 #include "content/browser/geofencing/geofencing_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "content/browser/geofencing/geofencing_service.h" 10 #include "content/browser/geofencing/geofencing_service.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 76
77 void GeofencingManager::Shutdown() { 77 void GeofencingManager::Shutdown() {
78 DCHECK_CURRENTLY_ON(BrowserThread::UI); 78 DCHECK_CURRENTLY_ON(BrowserThread::UI);
79 BrowserThread::PostTask(BrowserThread::IO, 79 BrowserThread::PostTask(BrowserThread::IO,
80 FROM_HERE, 80 FROM_HERE,
81 base::Bind(&GeofencingManager::ShutdownOnIO, this)); 81 base::Bind(&GeofencingManager::ShutdownOnIO, this));
82 } 82 }
83 83
84 void GeofencingManager::InitOnIO() { 84 void GeofencingManager::InitOnIO() {
85 DCHECK_CURRENTLY_ON(BrowserThread::IO); 85 DCHECK_CURRENTLY_ON(BrowserThread::IO);
86 service_ = GeofencingServiceImpl::GetInstance(); 86 service_worker_context_->AddObserver(this);
87 if (!service_)
88 service_ = GeofencingServiceImpl::GetInstance();
87 } 89 }
88 90
89 void GeofencingManager::ShutdownOnIO() { 91 void GeofencingManager::ShutdownOnIO() {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO); 92 DCHECK_CURRENTLY_ON(BrowserThread::IO);
93 service_worker_context_->RemoveObserver(this);
91 // Clean up all registrations with the |GeofencingService|. 94 // Clean up all registrations with the |GeofencingService|.
92 // TODO(mek): This will need to change to support geofence registrations that 95 // TODO(mek): This will need to change to support geofence registrations that
93 // outlive the browser, although removing the references to this 96 // outlive the browser, although removing the references to this
94 // |GeofencingManager| from the |GeofencingService| will still be needed. 97 // |GeofencingManager| from the |GeofencingService| will still be needed.
95 for (const auto& registration : registrations_by_id_) 98 for (const auto& registration : registrations_by_id_)
96 service_->UnregisterRegion(registration.first); 99 service_->UnregisterRegion(registration.first);
97 } 100 }
98 101
99 void GeofencingManager::RegisterRegion( 102 void GeofencingManager::RegisterRegion(
100 int64 service_worker_registration_id, 103 int64 service_worker_registration_id,
101 const std::string& region_id, 104 const std::string& region_id,
102 const blink::WebCircularGeofencingRegion& region, 105 const blink::WebCircularGeofencingRegion& region,
103 const StatusCallback& callback) { 106 const StatusCallback& callback) {
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); 107 DCHECK_CURRENTLY_ON(BrowserThread::IO);
105 108
106 // TODO(mek): Validate region_id and region. 109 // TODO(mek): Validate region_id and region.
107 110
108 // Look up service worker. In unit tests |service_worker_context_| might not 111 // Look up service worker.
109 // be set. 112 ServiceWorkerRegistration* service_worker_registration =
110 GURL service_worker_origin; 113 service_worker_context_->context()->GetLiveRegistration(
111 if (service_worker_context_.get()) { 114 service_worker_registration_id);
112 ServiceWorkerRegistration* service_worker_registration = 115 if (!service_worker_registration) {
113 service_worker_context_->context()->GetLiveRegistration( 116 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
114 service_worker_registration_id); 117 return;
115 if (!service_worker_registration) { 118 }
116 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
117 return;
118 }
119 119
120 service_worker_origin = service_worker_registration->pattern().GetOrigin(); 120 GURL service_worker_origin =
121 } 121 service_worker_registration->pattern().GetOrigin();
122 122
123 if (!service_->IsServiceAvailable()) { 123 if (!service_->IsServiceAvailable()) {
124 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); 124 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
125 return; 125 return;
126 } 126 }
127 127
128 if (FindRegistration(service_worker_registration_id, region_id)) { 128 if (FindRegistration(service_worker_registration_id, region_id)) {
129 // Already registered, return an error. 129 // Already registered, return an error.
130 // TODO(mek): Use a more specific error code. 130 // TODO(mek): Use a more specific error code.
131 callback.Run(GEOFENCING_STATUS_ERROR); 131 callback.Run(GEOFENCING_STATUS_ERROR);
132 return; 132 return;
133 } 133 }
134 134
135 AddRegistration(service_worker_registration_id, service_worker_origin, 135 AddRegistration(service_worker_registration_id, service_worker_origin,
136 region_id, region, callback, 136 region_id, region, callback,
137 service_->RegisterRegion(region, this)); 137 service_->RegisterRegion(region, this));
138 } 138 }
139 139
140 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, 140 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id,
141 const std::string& region_id, 141 const std::string& region_id,
142 const StatusCallback& callback) { 142 const StatusCallback& callback) {
143 DCHECK_CURRENTLY_ON(BrowserThread::IO); 143 DCHECK_CURRENTLY_ON(BrowserThread::IO);
144 144
145 // TODO(mek): Validate region_id. 145 // TODO(mek): Validate region_id.
146 146
147 // Look up service worker. In unit tests |service_worker_context_| might not 147 // Look up service worker.
148 // be set. 148 ServiceWorkerRegistration* service_worker_registration =
149 if (service_worker_context_.get()) { 149 service_worker_context_->context()->GetLiveRegistration(
150 ServiceWorkerRegistration* service_worker_registration = 150 service_worker_registration_id);
151 service_worker_context_->context()->GetLiveRegistration( 151 if (!service_worker_registration) {
152 service_worker_registration_id); 152 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
153 if (!service_worker_registration) { 153 return;
154 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
155 return;
156 }
157 } 154 }
158 155
159 if (!service_->IsServiceAvailable()) { 156 if (!service_->IsServiceAvailable()) {
160 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); 157 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
161 return; 158 return;
162 } 159 }
163 160
164 Registration* registration = 161 Registration* registration =
165 FindRegistration(service_worker_registration_id, region_id); 162 FindRegistration(service_worker_registration_id, region_id);
166 if (!registration) { 163 if (!registration) {
(...skipping 12 matching lines...) Expand all
179 ClearRegistration(registration); 176 ClearRegistration(registration);
180 callback.Run(GEOFENCING_STATUS_OK); 177 callback.Run(GEOFENCING_STATUS_OK);
181 } 178 }
182 179
183 GeofencingStatus GeofencingManager::GetRegisteredRegions( 180 GeofencingStatus GeofencingManager::GetRegisteredRegions(
184 int64 service_worker_registration_id, 181 int64 service_worker_registration_id,
185 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { 182 std::map<std::string, blink::WebCircularGeofencingRegion>* result) {
186 DCHECK_CURRENTLY_ON(BrowserThread::IO); 183 DCHECK_CURRENTLY_ON(BrowserThread::IO);
187 CHECK(result); 184 CHECK(result);
188 185
189 // Look up service worker. In unit tests |service_worker_context_| might not 186 // Look up service worker.
190 // be set. 187 ServiceWorkerRegistration* service_worker_registration =
191 if (service_worker_context_.get()) { 188 service_worker_context_->context()->GetLiveRegistration(
192 ServiceWorkerRegistration* service_worker_registration = 189 service_worker_registration_id);
193 service_worker_context_->context()->GetLiveRegistration( 190 if (!service_worker_registration) {
194 service_worker_registration_id); 191 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER;
195 if (!service_worker_registration) {
196 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER;
197 }
198 } 192 }
199 193
200 if (!service_->IsServiceAvailable()) { 194 if (!service_->IsServiceAvailable()) {
201 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; 195 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE;
202 } 196 }
203 197
204 // Populate result, filtering out inactive registrations. 198 // Populate result, filtering out inactive registrations.
205 result->clear(); 199 result->clear();
206 ServiceWorkerRegistrationsMap::iterator registrations = 200 ServiceWorkerRegistrationsMap::iterator registrations =
207 registrations_.find(service_worker_registration_id); 201 registrations_.find(service_worker_registration_id);
(...skipping 30 matching lines...) Expand all
238 } 232 }
239 } 233 }
240 234
241 void GeofencingManager::SetMockPosition(double latitude, double longitude) { 235 void GeofencingManager::SetMockPosition(double latitude, double longitude) {
242 DCHECK_CURRENTLY_ON(BrowserThread::IO); 236 DCHECK_CURRENTLY_ON(BrowserThread::IO);
243 if (!mock_service_) 237 if (!mock_service_)
244 return; 238 return;
245 mock_service_->SetMockPosition(latitude, longitude); 239 mock_service_->SetMockPosition(latitude, longitude);
246 } 240 }
247 241
242 void GeofencingManager::OnRegistrationDeleted(
243 int64 service_worker_registration_id,
244 const GURL& pattern) {
245 DCHECK_CURRENTLY_ON(BrowserThread::IO);
246 CleanUpForServiceWorker(service_worker_registration_id);
247 }
248
248 void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id, 249 void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id,
249 GeofencingStatus status) { 250 GeofencingStatus status) {
250 DCHECK_CURRENTLY_ON(BrowserThread::IO); 251 DCHECK_CURRENTLY_ON(BrowserThread::IO);
251 Registration* registration = FindRegistrationById(geofencing_registration_id); 252 Registration* registration = FindRegistrationById(geofencing_registration_id);
252 DCHECK(registration); 253 DCHECK(registration);
253 DCHECK(!registration->is_active()); 254 DCHECK(!registration->is_active());
254 registration->registration_callback.Run(status); 255 registration->registration_callback.Run(status);
255 registration->registration_callback.Reset(); 256 registration->registration_callback.Reset();
256 257
257 // If the registration wasn't succesful, remove it from our storage. 258 // If the registration wasn't succesful, remove it from our storage.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 DCHECK_CURRENTLY_ON(BrowserThread::IO); 322 DCHECK_CURRENTLY_ON(BrowserThread::IO);
322 registrations_by_id_.erase(registration->geofencing_registration_id); 323 registrations_by_id_.erase(registration->geofencing_registration_id);
323 ServiceWorkerRegistrationsMap::iterator registrations_iterator = 324 ServiceWorkerRegistrationsMap::iterator registrations_iterator =
324 registrations_.find(registration->service_worker_registration_id); 325 registrations_.find(registration->service_worker_registration_id);
325 DCHECK(registrations_iterator != registrations_.end()); 326 DCHECK(registrations_iterator != registrations_.end());
326 registrations_iterator->second.erase(registration->region_id); 327 registrations_iterator->second.erase(registration->region_id);
327 if (registrations_iterator->second.empty()) 328 if (registrations_iterator->second.empty())
328 registrations_.erase(registrations_iterator); 329 registrations_.erase(registrations_iterator);
329 } 330 }
330 331
332 void GeofencingManager::CleanUpForServiceWorker(
333 int64 service_worker_registration_id) {
michaeln 2015/01/09 22:58:41 id here can be 'invalid', not sure if you want to
Marijn Kruisselbrink 2015/01/09 23:13:15 I'm not sure if that's worth the extra code. It wo
334 DCHECK_CURRENTLY_ON(BrowserThread::IO);
335 ServiceWorkerRegistrationsMap::iterator registrations_iterator =
336 registrations_.find(service_worker_registration_id);
337 if (registrations_iterator == registrations_.end())
338 return;
339
340 for (const auto& registration : registrations_iterator->second) {
341 int geofencing_registration_id =
342 registration.second.geofencing_registration_id;
343 service_->UnregisterRegion(geofencing_registration_id);
344 registrations_by_id_.erase(geofencing_registration_id);
345 }
346 registrations_.erase(service_worker_registration_id);
347 }
348
331 void GeofencingManager::DispatchGeofencingEvent( 349 void GeofencingManager::DispatchGeofencingEvent(
332 blink::WebGeofencingEventType event_type, 350 blink::WebGeofencingEventType event_type,
333 int64 geofencing_registration_id) { 351 int64 geofencing_registration_id) {
334 DCHECK_CURRENTLY_ON(BrowserThread::IO); 352 DCHECK_CURRENTLY_ON(BrowserThread::IO);
335 Registration* registration = FindRegistrationById(geofencing_registration_id); 353 Registration* registration = FindRegistrationById(geofencing_registration_id);
336 if (!registration || 354 if (!registration ||
337 registration->service_worker_registration_id == 355 registration->service_worker_registration_id ==
338 kInvalidServiceWorkerRegistrationId) { 356 kInvalidServiceWorkerRegistrationId) {
339 // TODO(mek): Log/track these failures. 357 // TODO(mek): Log/track these failures.
340 return; 358 return;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } 408 }
391 409
392 void GeofencingManager::DeliverGeofencingEventEnd( 410 void GeofencingManager::DeliverGeofencingEventEnd(
393 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 411 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
394 ServiceWorkerStatusCode service_worker_status) { 412 ServiceWorkerStatusCode service_worker_status) {
395 DCHECK_CURRENTLY_ON(BrowserThread::IO); 413 DCHECK_CURRENTLY_ON(BrowserThread::IO);
396 // TODO(mek): log/check result. 414 // TODO(mek): log/check result.
397 } 415 }
398 416
399 } // namespace content 417 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698