| 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/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 13 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 struct GeofencingManager::Registration { | 17 struct GeofencingManager::Registration { |
| 18 Registration(int64 service_worker_registration_id, | 18 Registration(int64 service_worker_registration_id, |
| 19 const GURL& service_worker_origin, |
| 19 const std::string& region_id, | 20 const std::string& region_id, |
| 20 const blink::WebCircularGeofencingRegion& region, | 21 const blink::WebCircularGeofencingRegion& region, |
| 21 const StatusCallback& callback, | 22 const StatusCallback& callback, |
| 22 int64 geofencing_registration_id); | 23 int64 geofencing_registration_id); |
| 23 | 24 |
| 24 int64 service_worker_registration_id; | 25 int64 service_worker_registration_id; |
| 26 GURL service_worker_origin; |
| 25 std::string region_id; | 27 std::string region_id; |
| 26 blink::WebCircularGeofencingRegion region; | 28 blink::WebCircularGeofencingRegion region; |
| 27 | 29 |
| 28 // Registration ID as returned by the |GeofencingService|. | 30 // Registration ID as returned by the |GeofencingService|. |
| 29 int64 geofencing_registration_id; | 31 int64 geofencing_registration_id; |
| 30 | 32 |
| 31 // Callback to call when registration is completed. This field is reset when | 33 // Callback to call when registration is completed. This field is reset when |
| 32 // registration is complete. | 34 // registration is complete. |
| 33 StatusCallback registration_callback; | 35 StatusCallback registration_callback; |
| 34 | 36 |
| 35 // Returns true if registration has been completed, and thus should be | 37 // Returns true if registration has been completed, and thus should be |
| 36 // included in calls to GetRegisteredRegions. | 38 // included in calls to GetRegisteredRegions. |
| 37 bool is_active() const { return registration_callback.is_null(); } | 39 bool is_active() const { return registration_callback.is_null(); } |
| 38 }; | 40 }; |
| 39 | 41 |
| 40 GeofencingManager::Registration::Registration( | 42 GeofencingManager::Registration::Registration( |
| 41 int64 service_worker_registration_id, | 43 int64 service_worker_registration_id, |
| 44 const GURL& service_worker_origin, |
| 42 const std::string& region_id, | 45 const std::string& region_id, |
| 43 const blink::WebCircularGeofencingRegion& region, | 46 const blink::WebCircularGeofencingRegion& region, |
| 44 const GeofencingManager::StatusCallback& callback, | 47 const GeofencingManager::StatusCallback& callback, |
| 45 int64 geofencing_registration_id) | 48 int64 geofencing_registration_id) |
| 46 : service_worker_registration_id(service_worker_registration_id), | 49 : service_worker_registration_id(service_worker_registration_id), |
| 47 region_id(region_id), | 50 region_id(region_id), |
| 48 region(region), | 51 region(region), |
| 49 geofencing_registration_id(geofencing_registration_id), | 52 geofencing_registration_id(geofencing_registration_id), |
| 50 registration_callback(callback) { | 53 registration_callback(callback) { |
| 51 } | 54 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 94 |
| 92 void GeofencingManager::RegisterRegion( | 95 void GeofencingManager::RegisterRegion( |
| 93 int64 service_worker_registration_id, | 96 int64 service_worker_registration_id, |
| 94 const std::string& region_id, | 97 const std::string& region_id, |
| 95 const blink::WebCircularGeofencingRegion& region, | 98 const blink::WebCircularGeofencingRegion& region, |
| 96 const StatusCallback& callback) { | 99 const StatusCallback& callback) { |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 100 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 98 | 101 |
| 99 // TODO(mek): Validate region_id and region. | 102 // TODO(mek): Validate region_id and region. |
| 100 | 103 |
| 104 // Look up service worker. In unit tests |service_worker_context_| might not |
| 105 // be set. |
| 106 GURL service_worker_origin; |
| 107 if (service_worker_context_.get()) { |
| 108 ServiceWorkerRegistration* service_worker_registration = |
| 109 service_worker_context_->context()->GetLiveRegistration( |
| 110 service_worker_registration_id); |
| 111 if (!service_worker_registration) { |
| 112 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER); |
| 113 return; |
| 114 } |
| 115 |
| 116 service_worker_origin = service_worker_registration->pattern().GetOrigin(); |
| 117 } |
| 118 |
| 101 if (!service_->IsServiceAvailable()) { | 119 if (!service_->IsServiceAvailable()) { |
| 102 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); | 120 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 103 return; | 121 return; |
| 104 } | 122 } |
| 105 | 123 |
| 106 if (FindRegistration(service_worker_registration_id, region_id)) { | 124 if (FindRegistration(service_worker_registration_id, region_id)) { |
| 107 // Already registered, return an error. | 125 // Already registered, return an error. |
| 108 // TODO(mek): Use a more specific error code. | 126 // TODO(mek): Use a more specific error code. |
| 109 callback.Run(GEOFENCING_STATUS_ERROR); | 127 callback.Run(GEOFENCING_STATUS_ERROR); |
| 110 return; | 128 return; |
| 111 } | 129 } |
| 112 | 130 |
| 113 AddRegistration(service_worker_registration_id, | 131 AddRegistration(service_worker_registration_id, |
| 132 service_worker_origin, |
| 114 region_id, | 133 region_id, |
| 115 region, | 134 region, |
| 116 callback, | 135 callback, |
| 117 service_->RegisterRegion(region, this)); | 136 service_->RegisterRegion(region, this)); |
| 118 } | 137 } |
| 119 | 138 |
| 120 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, | 139 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
| 121 const std::string& region_id, | 140 const std::string& region_id, |
| 122 const StatusCallback& callback) { | 141 const StatusCallback& callback) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 | 143 |
| 125 // TODO(mek): Validate region_id. | 144 // TODO(mek): Validate region_id. |
| 126 | 145 |
| 146 // Look up service worker. In unit tests |service_worker_context_| might not |
| 147 // be set. |
| 148 if (service_worker_context_.get()) { |
| 149 ServiceWorkerRegistration* service_worker_registration = |
| 150 service_worker_context_->context()->GetLiveRegistration( |
| 151 service_worker_registration_id); |
| 152 if (!service_worker_registration) { |
| 153 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER); |
| 154 return; |
| 155 } |
| 156 } |
| 157 |
| 127 if (!service_->IsServiceAvailable()) { | 158 if (!service_->IsServiceAvailable()) { |
| 128 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); | 159 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 129 return; | 160 return; |
| 130 } | 161 } |
| 131 | 162 |
| 132 Registration* registration = | 163 Registration* registration = |
| 133 FindRegistration(service_worker_registration_id, region_id); | 164 FindRegistration(service_worker_registration_id, region_id); |
| 134 if (!registration) { | 165 if (!registration) { |
| 135 // Not registered, return an error. | 166 // Not registered, return an error. |
| 136 callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED); | 167 callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 147 ClearRegistration(registration); | 178 ClearRegistration(registration); |
| 148 callback.Run(GEOFENCING_STATUS_OK); | 179 callback.Run(GEOFENCING_STATUS_OK); |
| 149 } | 180 } |
| 150 | 181 |
| 151 GeofencingStatus GeofencingManager::GetRegisteredRegions( | 182 GeofencingStatus GeofencingManager::GetRegisteredRegions( |
| 152 int64 service_worker_registration_id, | 183 int64 service_worker_registration_id, |
| 153 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { | 184 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { |
| 154 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 155 CHECK(result); | 186 CHECK(result); |
| 156 | 187 |
| 188 // Look up service worker. In unit tests |service_worker_context_| might not |
| 189 // be set. |
| 190 if (service_worker_context_.get()) { |
| 191 ServiceWorkerRegistration* service_worker_registration = |
| 192 service_worker_context_->context()->GetLiveRegistration( |
| 193 service_worker_registration_id); |
| 194 if (!service_worker_registration) { |
| 195 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER; |
| 196 } |
| 197 } |
| 198 |
| 157 if (!service_->IsServiceAvailable()) { | 199 if (!service_->IsServiceAvailable()) { |
| 158 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; | 200 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; |
| 159 } | 201 } |
| 160 | 202 |
| 161 // Populate result, filtering out inactive registrations. | 203 // Populate result, filtering out inactive registrations. |
| 162 result->clear(); | 204 result->clear(); |
| 163 ServiceWorkerRegistrationsMap::iterator registrations = | 205 ServiceWorkerRegistrationsMap::iterator registrations = |
| 164 registrations_.find(service_worker_registration_id); | 206 registrations_.find(service_worker_registration_id); |
| 165 if (registrations == registrations_.end()) | 207 if (registrations == registrations_.end()) |
| 166 return GEOFENCING_STATUS_OK; | 208 return GEOFENCING_STATUS_OK; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 247 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 206 RegistrationIdRegistrationMap::iterator registration_iterator = | 248 RegistrationIdRegistrationMap::iterator registration_iterator = |
| 207 registrations_by_id_.find(geofencing_registration_id); | 249 registrations_by_id_.find(geofencing_registration_id); |
| 208 if (registration_iterator == registrations_by_id_.end()) | 250 if (registration_iterator == registrations_by_id_.end()) |
| 209 return nullptr; | 251 return nullptr; |
| 210 return ®istration_iterator->second->second; | 252 return ®istration_iterator->second->second; |
| 211 } | 253 } |
| 212 | 254 |
| 213 GeofencingManager::Registration& GeofencingManager::AddRegistration( | 255 GeofencingManager::Registration& GeofencingManager::AddRegistration( |
| 214 int64 service_worker_registration_id, | 256 int64 service_worker_registration_id, |
| 257 const GURL& service_worker_origin, |
| 215 const std::string& region_id, | 258 const std::string& region_id, |
| 216 const blink::WebCircularGeofencingRegion& region, | 259 const blink::WebCircularGeofencingRegion& region, |
| 217 const StatusCallback& callback, | 260 const StatusCallback& callback, |
| 218 int64 geofencing_registration_id) { | 261 int64 geofencing_registration_id) { |
| 219 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 220 DCHECK(!FindRegistration(service_worker_registration_id, region_id)); | 263 DCHECK(!FindRegistration(service_worker_registration_id, region_id)); |
| 221 RegionIdRegistrationMap::iterator registration = | 264 RegionIdRegistrationMap::iterator registration = |
| 222 registrations_[service_worker_registration_id] | 265 registrations_[service_worker_registration_id] |
| 223 .insert(std::make_pair(region_id, | 266 .insert(std::make_pair(region_id, |
| 224 Registration(service_worker_registration_id, | 267 Registration(service_worker_registration_id, |
| 268 service_worker_origin, |
| 225 region_id, | 269 region_id, |
| 226 region, | 270 region, |
| 227 callback, | 271 callback, |
| 228 geofencing_registration_id))) | 272 geofencing_registration_id))) |
| 229 .first; | 273 .first; |
| 230 registrations_by_id_[geofencing_registration_id] = registration; | 274 registrations_by_id_[geofencing_registration_id] = registration; |
| 231 return registration->second; | 275 return registration->second; |
| 232 } | 276 } |
| 233 | 277 |
| 234 void GeofencingManager::ClearRegistration(Registration* registration) { | 278 void GeofencingManager::ClearRegistration(Registration* registration) { |
| 235 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 279 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 236 registrations_by_id_.erase(registration->geofencing_registration_id); | 280 registrations_by_id_.erase(registration->geofencing_registration_id); |
| 237 ServiceWorkerRegistrationsMap::iterator registrations_iterator = | 281 ServiceWorkerRegistrationsMap::iterator registrations_iterator = |
| 238 registrations_.find(registration->service_worker_registration_id); | 282 registrations_.find(registration->service_worker_registration_id); |
| 239 DCHECK(registrations_iterator != registrations_.end()); | 283 DCHECK(registrations_iterator != registrations_.end()); |
| 240 registrations_iterator->second.erase(registration->region_id); | 284 registrations_iterator->second.erase(registration->region_id); |
| 241 if (registrations_iterator->second.empty()) | 285 if (registrations_iterator->second.empty()) |
| 242 registrations_.erase(registrations_iterator); | 286 registrations_.erase(registrations_iterator); |
| 243 } | 287 } |
| 244 | 288 |
| 245 } // namespace content | 289 } // namespace content |
| OLD | NEW |