Chromium Code Reviews| OLD | NEW | 
|---|---|
| 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 "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" | 
| 11 #include "content/browser/geofencing/geofencing_provider.h" | 11 #include "content/browser/geofencing/geofencing_provider.h" | 
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 13 #include "content/public/browser/browser_context.h" | |
| 12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" | 
| 15 #include "content/public/browser/storage_partition.h" | |
| 13 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 
| 17 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" | |
| 14 #include "url/gurl.h" | 18 #include "url/gurl.h" | 
| 15 | 19 | 
| 16 namespace content { | 20 namespace content { | 
| 17 | 21 | 
| 22 namespace { | |
| 23 | |
| 24 scoped_refptr<ServiceWorkerContextWrapper> FindServiceWorkerContextOnUIThread( | |
| 25 BrowserContext* browser_context, | |
| 26 const GURL& service_worker_origin) { | |
| 27 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 28 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite( | |
| 
 
michaeln
2014/10/14 02:20:40
I don't see how this can be correct?
An app embed
 
Marijn Kruisselbrink
2014/10/14 19:16:55
Ah yes, you're right of course. That also means th
 
Marijn Kruisselbrink
2014/10/15 23:55:15
Okay, I did this refactoring of GeofencingManager
 
 | |
| 29 browser_context, service_worker_origin); | |
| 30 return static_cast<ServiceWorkerContextWrapper*>( | |
| 31 partition->GetServiceWorkerContext()); | |
| 32 } | |
| 33 | |
| 34 void FindServiceWorkerRegistration( | |
| 35 int64 service_worker_registration_id, | |
| 36 const GURL& service_worker_origin, | |
| 37 const ServiceWorkerStorage::FindRegistrationCallback& callback, | |
| 38 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 40 service_worker_context->context()->storage()->FindRegistrationForId( | |
| 41 service_worker_registration_id, service_worker_origin, callback); | |
| 42 } | |
| 43 | |
| 44 void DeliverGeofencingEventEnd( | |
| 45 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | |
| 46 ServiceWorkerStatusCode service_worker_status) { | |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 48 // TODO(mek): log/check result. | |
| 49 } | |
| 50 | |
| 51 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type, | |
| 52 const std::string& region_id, | |
| 53 const blink::WebCircularGeofencingRegion& region, | |
| 54 ServiceWorkerStatusCode service_worker_status, | |
| 55 const scoped_refptr<ServiceWorkerRegistration>& | |
| 56 service_worker_registration) { | |
| 57 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 58 if (service_worker_status == SERVICE_WORKER_OK) { | |
| 59 // Hold on to the service worker registration in the callback to keep it | |
| 60 // alive until the callback dies. Otherwise the registration could be | |
| 61 // released when this method returns - before the event is delivered to the | |
| 62 // service worker. | |
| 63 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | |
| 64 base::Bind(&DeliverGeofencingEventEnd, service_worker_registration); | |
| 65 service_worker_registration->active_version()->DispatchGeofencingEvent( | |
| 
 
michaeln
2014/10/14 02:20:40
need to be more defensive about active_version() b
 
Marijn Kruisselbrink
2014/10/14 19:16:55
Hmm, okay... So what I think should happen if a ge
 
falken
2014/10/15 01:59:34
I haven't been closely following...
There's a few
 
Marijn Kruisselbrink
2014/10/15 23:55:15
I'm not sure, I think generally events are only de
 
falken
2014/10/16 05:18:18
TODO sounds good. We sometimes dispatch events to
 
 | |
| 66 dispatch_event_callback, event_type, region_id, region); | |
| 67 } else { | |
| 68 // TODO(mek): no SW, cleanup/logging | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 18 struct GeofencingManager::RegistrationKey { | 74 struct GeofencingManager::RegistrationKey { | 
| 19 RegistrationKey(BrowserContext* browser_context, | 75 RegistrationKey(BrowserContext* browser_context, | 
| 20 int64 service_worker_registration_id, | 76 int64 service_worker_registration_id, | 
| 21 const GURL& service_worker_origin, | 77 const GURL& service_worker_origin, | 
| 22 const std::string& region_id); | 78 const std::string& region_id); | 
| 23 | 79 | 
| 24 BrowserContext* browser_context; | 80 BrowserContext* browser_context; | 
| 25 | 81 | 
| 26 int64 service_worker_registration_id; | 82 int64 service_worker_registration_id; | 
| 27 GURL service_worker_origin; | 83 GURL service_worker_origin; | 
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 registration.key.service_worker_registration_id == | 249 registration.key.service_worker_registration_id == | 
| 194 service_worker_registration_id && | 250 service_worker_registration_id && | 
| 195 registration.key.service_worker_origin == service_worker_origin && | 251 registration.key.service_worker_origin == service_worker_origin && | 
| 196 registration.is_active) { | 252 registration.is_active) { | 
| 197 (*result)[registration.key.region_id] = registration.region; | 253 (*result)[registration.key.region_id] = registration.region; | 
| 198 } | 254 } | 
| 199 } | 255 } | 
| 200 return GeofencingStatus::GEOFENCING_STATUS_OK; | 256 return GeofencingStatus::GEOFENCING_STATUS_OK; | 
| 201 } | 257 } | 
| 202 | 258 | 
| 259 void GeofencingManager::RegionEntered(int provider_id) { | |
| 260 DispatchGeofencingEvent(blink::WebGeofencingEventTypeEnter, provider_id); | |
| 261 } | |
| 262 | |
| 263 void GeofencingManager::RegionExited(int provider_id) { | |
| 264 DispatchGeofencingEvent(blink::WebGeofencingEventTypeLeave, provider_id); | |
| 265 } | |
| 266 | |
| 203 void GeofencingManager::RegisterRegionCompleted(const StatusCallback& callback, | 267 void GeofencingManager::RegisterRegionCompleted(const StatusCallback& callback, | 
| 204 const RegistrationKey& key, | 268 const RegistrationKey& key, | 
| 205 GeofencingStatus status, | 269 GeofencingStatus status, | 
| 206 int registration_id) { | 270 int registration_id) { | 
| 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 271 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 208 if (status != GEOFENCING_STATUS_OK) { | 272 if (status != GEOFENCING_STATUS_OK) { | 
| 209 ClearRegistration(key); | 273 ClearRegistration(key); | 
| 210 callback.Run(status); | 274 callback.Run(status); | 
| 211 return; | 275 return; | 
| 212 } | 276 } | 
| (...skipping 29 matching lines...) Expand all Loading... | |
| 242 } | 306 } | 
| 243 | 307 | 
| 244 void GeofencingManager::ClearRegistration(const RegistrationKey& key) { | 308 void GeofencingManager::ClearRegistration(const RegistrationKey& key) { | 
| 245 std::vector<Registration>::iterator it = std::find_if( | 309 std::vector<Registration>::iterator it = std::find_if( | 
| 246 registrations_.begin(), registrations_.end(), RegistrationMatches(key)); | 310 registrations_.begin(), registrations_.end(), RegistrationMatches(key)); | 
| 247 if (it == registrations_.end()) | 311 if (it == registrations_.end()) | 
| 248 return; | 312 return; | 
| 249 registrations_.erase(it); | 313 registrations_.erase(it); | 
| 250 } | 314 } | 
| 251 | 315 | 
| 316 GeofencingManager::Registration* | |
| 317 GeofencingManager::FindRegistrationByProviderId(int provider_id) { | |
| 318 for (Registration& registration : registrations_) { | |
| 319 if (registration.registration_id == provider_id) | |
| 320 return ®istration; | |
| 321 } | |
| 322 return nullptr; | |
| 323 } | |
| 324 | |
| 325 void GeofencingManager::DispatchGeofencingEvent( | |
| 326 blink::WebGeofencingEventType event_type, | |
| 327 int provider_id) { | |
| 328 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 329 Registration* registration = FindRegistrationByProviderId(provider_id); | |
| 330 if (!registration || registration->key.service_worker_registration_id < 0) | |
| 331 return; | |
| 332 | |
| 333 BrowserThread::PostTaskAndReplyWithResult( | |
| 334 BrowserThread::UI, | |
| 335 FROM_HERE, | |
| 336 base::Bind(&FindServiceWorkerContextOnUIThread, | |
| 337 registration->key.browser_context, | |
| 338 registration->key.service_worker_origin), | |
| 339 base::Bind(&FindServiceWorkerRegistration, | |
| 340 registration->key.service_worker_registration_id, | |
| 341 registration->key.service_worker_origin, | |
| 342 base::Bind(&DeliverGeofencingEvent, | |
| 343 event_type, | |
| 344 registration->key.region_id, | |
| 345 registration->region))); | |
| 346 } | |
| 347 | |
| 252 } // namespace content | 348 } // namespace content | 
| OLD | NEW |