Index: content/browser/geofencing/geofencing_manager.cc |
diff --git a/content/browser/geofencing/geofencing_manager.cc b/content/browser/geofencing/geofencing_manager.cc |
index 6899c73067a2c4be63108f24e00cf05413ac0a0c..224266d12e715def237def44e09f882ff189079d 100644 |
--- a/content/browser/geofencing/geofencing_manager.cc |
+++ b/content/browser/geofencing/geofencing_manager.cc |
@@ -8,6 +8,7 @@ |
#include "base/callback.h" |
#include "content/browser/geofencing/geofencing_service.h" |
+#include "content/browser/geofencing/mock_geofencing_service.h" |
#include "content/browser/service_worker/service_worker_context_wrapper.h" |
#include "content/public/browser/browser_context.h" |
#include "content/public/browser/browser_thread.h" |
@@ -91,8 +92,10 @@ void GeofencingManager::ShutdownOnIO() { |
// TODO(mek): This will need to change to support geofence registrations that |
// outlive the browser, although removing the references to this |
// |GeofencingManager| from the |GeofencingService| will still be needed. |
- for (const auto& registration : registrations_by_id_) { |
- service_->UnregisterRegion(registration.first); |
+ for (const auto& service_worker : registrations_) { |
+ GeofencingService* service = ServiceForServiceWorker(service_worker.first); |
+ for (const auto& registration : service_worker.second) |
+ service->UnregisterRegion(registration.second.geofencing_registration_id); |
} |
} |
@@ -120,7 +123,9 @@ void GeofencingManager::RegisterRegion( |
service_worker_origin = service_worker_registration->pattern().GetOrigin(); |
} |
- if (!service_->IsServiceAvailable()) { |
+ GeofencingService* service = |
+ ServiceForServiceWorker(service_worker_registration_id); |
+ if (!service->IsServiceAvailable()) { |
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
return; |
} |
@@ -132,12 +137,9 @@ void GeofencingManager::RegisterRegion( |
return; |
} |
- AddRegistration(service_worker_registration_id, |
- service_worker_origin, |
- region_id, |
- region, |
- callback, |
- service_->RegisterRegion(region, this)); |
+ AddRegistration(service_worker_registration_id, service_worker_origin, |
+ region_id, region, callback, |
+ service->RegisterRegion(region, this)); |
} |
void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
@@ -159,7 +161,9 @@ void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
} |
} |
- if (!service_->IsServiceAvailable()) { |
+ GeofencingService* service = |
+ ServiceForServiceWorker(service_worker_registration_id); |
+ if (!service->IsServiceAvailable()) { |
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
return; |
} |
@@ -178,7 +182,7 @@ void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
return; |
} |
- service_->UnregisterRegion(registration->geofencing_registration_id); |
+ service->UnregisterRegion(registration->geofencing_registration_id); |
ClearRegistration(registration); |
callback.Run(GEOFENCING_STATUS_OK); |
} |
@@ -200,7 +204,9 @@ GeofencingStatus GeofencingManager::GetRegisteredRegions( |
} |
} |
- if (!service_->IsServiceAvailable()) { |
+ GeofencingService* service = |
+ ServiceForServiceWorker(service_worker_registration_id); |
+ if (!service->IsServiceAvailable()) { |
return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; |
} |
@@ -217,6 +223,32 @@ GeofencingStatus GeofencingManager::GetRegisteredRegions( |
return GEOFENCING_STATUS_OK; |
} |
+void GeofencingManager::SetMockProvider(int64 service_worker_registration_id, |
+ GeofencingMockState mock_state) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ // First erase all current registration for this service worker. |
+ CleanUpForServiceWorker(service_worker_registration_id); |
+ |
+ // Then set or reset the mock service for the service worker. |
+ if (mock_state == GeofencingMockState::NONE) { |
+ mock_services_.erase(service_worker_registration_id); |
+ } else { |
+ mock_services_[service_worker_registration_id] = |
+ make_linked_ptr(new MockGeofencingService( |
+ mock_state != GeofencingMockState::SERVICE_UNAVAILABLE)); |
+ } |
+} |
+ |
+void GeofencingManager::SetMockPosition(int64 service_worker_registration_id, |
+ double latitude, |
+ double longitude) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ auto it = mock_services_.find(service_worker_registration_id); |
+ if (it == mock_services_.end()) |
+ return; |
+ it->second->SetMockPosition(latitude, longitude); |
+} |
+ |
void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id, |
GeofencingStatus status) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
@@ -241,6 +273,14 @@ void GeofencingManager::RegionExited(int64 geofencing_registration_id) { |
geofencing_registration_id); |
} |
+GeofencingService* GeofencingManager::ServiceForServiceWorker( |
+ int64 service_worker_registration_id) { |
+ auto it = mock_services_.find(service_worker_registration_id); |
+ if (it == mock_services_.end()) |
+ return service_; |
+ return it->second.get(); |
+} |
+ |
GeofencingManager::Registration* GeofencingManager::FindRegistration( |
int64 service_worker_registration_id, |
const std::string& region_id) { |
@@ -300,6 +340,27 @@ void GeofencingManager::ClearRegistration(Registration* registration) { |
registrations_.erase(registrations_iterator); |
} |
+void GeofencingManager::CleanUpForServiceWorker( |
+ int64 service_worker_registration_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ ServiceWorkerRegistrationsMap::iterator registrations_iterator = |
+ registrations_.find(service_worker_registration_id); |
+ if (registrations_iterator == registrations_.end()) |
+ return; |
+ |
+ GeofencingService* service = |
+ ServiceForServiceWorker(service_worker_registration_id); |
+ for (auto it = registrations_iterator->second.begin(); |
+ !registrations_iterator->second.empty();) { |
+ int geofencing_registration_id = it->second.geofencing_registration_id; |
+ service->UnregisterRegion(geofencing_registration_id); |
+ registrations_by_id_.erase(geofencing_registration_id); |
+ auto old_it = it; |
+ ++it; |
+ registrations_iterator->second.erase(old_it); |
+ } |
+} |
+ |
void GeofencingManager::DispatchGeofencingEvent( |
blink::WebGeofencingEventType event_type, |
int64 geofencing_registration_id) { |