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 "content/browser/geofencing/geofencing_service.h" | 10 #include "content/browser/geofencing/geofencing_service.h" |
| 11 #include "content/browser/geofencing/mock_geofencing_service.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/storage_partition.h" | 15 #include "content/public/browser/storage_partition.h" |
15 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
16 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" | 17 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" |
17 #include "url/gurl.h" | 18 #include "url/gurl.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 85 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
85 service_ = GeofencingServiceImpl::GetInstance(); | 86 service_ = GeofencingServiceImpl::GetInstance(); |
86 } | 87 } |
87 | 88 |
88 void GeofencingManager::ShutdownOnIO() { | 89 void GeofencingManager::ShutdownOnIO() { |
89 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
90 // Clean up all registrations with the |GeofencingService|. | 91 // Clean up all registrations with the |GeofencingService|. |
91 // TODO(mek): This will need to change to support geofence registrations that | 92 // TODO(mek): This will need to change to support geofence registrations that |
92 // outlive the browser, although removing the references to this | 93 // outlive the browser, although removing the references to this |
93 // |GeofencingManager| from the |GeofencingService| will still be needed. | 94 // |GeofencingManager| from the |GeofencingService| will still be needed. |
94 for (const auto& registration : registrations_by_id_) { | 95 for (const auto& registration : registrations_by_id_) |
95 service_->UnregisterRegion(registration.first); | 96 service_->UnregisterRegion(registration.first); |
96 } | |
97 } | 97 } |
98 | 98 |
99 void GeofencingManager::RegisterRegion( | 99 void GeofencingManager::RegisterRegion( |
100 int64 service_worker_registration_id, | 100 int64 service_worker_registration_id, |
101 const std::string& region_id, | 101 const std::string& region_id, |
102 const blink::WebCircularGeofencingRegion& region, | 102 const blink::WebCircularGeofencingRegion& region, |
103 const StatusCallback& callback) { | 103 const StatusCallback& callback) { |
104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
105 | 105 |
106 // TODO(mek): Validate region_id and region. | 106 // TODO(mek): Validate region_id and region. |
(...skipping 18 matching lines...) Expand all Loading... |
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, | 135 AddRegistration(service_worker_registration_id, service_worker_origin, |
136 service_worker_origin, | 136 region_id, region, callback, |
137 region_id, | |
138 region, | |
139 callback, | |
140 service_->RegisterRegion(region, this)); | 137 service_->RegisterRegion(region, this)); |
141 } | 138 } |
142 | 139 |
143 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, | 140 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
144 const std::string& region_id, | 141 const std::string& region_id, |
145 const StatusCallback& callback) { | 142 const StatusCallback& callback) { |
146 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 143 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
147 | 144 |
148 // TODO(mek): Validate region_id. | 145 // TODO(mek): Validate region_id. |
149 | 146 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 registrations_.find(service_worker_registration_id); | 207 registrations_.find(service_worker_registration_id); |
211 if (registrations == registrations_.end()) | 208 if (registrations == registrations_.end()) |
212 return GEOFENCING_STATUS_OK; | 209 return GEOFENCING_STATUS_OK; |
213 for (const auto& registration : registrations->second) { | 210 for (const auto& registration : registrations->second) { |
214 if (registration.second.is_active()) | 211 if (registration.second.is_active()) |
215 (*result)[registration.first] = registration.second.region; | 212 (*result)[registration.first] = registration.second.region; |
216 } | 213 } |
217 return GEOFENCING_STATUS_OK; | 214 return GEOFENCING_STATUS_OK; |
218 } | 215 } |
219 | 216 |
| 217 void GeofencingManager::SetMockProvider(GeofencingMockState mock_state) { |
| 218 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 219 |
| 220 // TODO(mek): It would be nice if enabling the mock geofencing service |
| 221 // wouldn't completely delete all existing registrations but instead just |
| 222 // somehow keep them around but inactive. |
| 223 // For now mocking is only used in layout tests, so just clearing all |
| 224 // registrations works good enough. |
| 225 for (const auto& registration : registrations_by_id_) |
| 226 service_->UnregisterRegion(registration.first); |
| 227 registrations_by_id_.clear(); |
| 228 registrations_.clear(); |
| 229 |
| 230 // Then set or reset the mock service for the service worker. |
| 231 if (mock_state == GeofencingMockState::NONE) { |
| 232 service_ = GeofencingServiceImpl::GetInstance(); |
| 233 mock_service_.reset(); |
| 234 } else { |
| 235 mock_service_.reset(new MockGeofencingService( |
| 236 mock_state != GeofencingMockState::SERVICE_UNAVAILABLE)); |
| 237 service_ = mock_service_.get(); |
| 238 } |
| 239 } |
| 240 |
| 241 void GeofencingManager::SetMockPosition(double latitude, double longitude) { |
| 242 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 243 if (!mock_service_) |
| 244 return; |
| 245 mock_service_->SetMockPosition(latitude, longitude); |
| 246 } |
| 247 |
220 void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id, | 248 void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id, |
221 GeofencingStatus status) { | 249 GeofencingStatus status) { |
222 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 250 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
223 Registration* registration = FindRegistrationById(geofencing_registration_id); | 251 Registration* registration = FindRegistrationById(geofencing_registration_id); |
224 DCHECK(registration); | 252 DCHECK(registration); |
225 DCHECK(!registration->is_active()); | 253 DCHECK(!registration->is_active()); |
226 registration->registration_callback.Run(status); | 254 registration->registration_callback.Run(status); |
227 registration->registration_callback.Reset(); | 255 registration->registration_callback.Reset(); |
228 | 256 |
229 // If the registration wasn't succesful, remove it from our storage. | 257 // If the registration wasn't succesful, remove it from our storage. |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 } | 390 } |
363 | 391 |
364 void GeofencingManager::DeliverGeofencingEventEnd( | 392 void GeofencingManager::DeliverGeofencingEventEnd( |
365 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 393 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
366 ServiceWorkerStatusCode service_worker_status) { | 394 ServiceWorkerStatusCode service_worker_status) { |
367 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 395 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
368 // TODO(mek): log/check result. | 396 // TODO(mek): log/check result. |
369 } | 397 } |
370 | 398 |
371 } // namespace content | 399 } // namespace content |
OLD | NEW |