| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 | 91 |
| 89 void GeofencingManager::RegisterRegion( | 92 void GeofencingManager::RegisterRegion( |
| 90 int64 service_worker_registration_id, | 93 int64 service_worker_registration_id, |
| 91 const std::string& region_id, | 94 const std::string& region_id, |
| 92 const blink::WebCircularGeofencingRegion& region, | 95 const blink::WebCircularGeofencingRegion& region, |
| 93 const StatusCallback& callback) { | 96 const StatusCallback& callback) { |
| 94 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 95 | 98 |
| 96 // TODO(mek): Validate region_id and region. | 99 // TODO(mek): Validate region_id and region. |
| 97 | 100 |
| 101 // Look up service worker. In unit tests |service_worker_context_| might not |
| 102 // be set. |
| 103 GURL service_worker_origin; |
| 104 if (service_worker_context_.get()) { |
| 105 ServiceWorkerRegistration* service_worker_registration = |
| 106 service_worker_context_->context()->GetLiveRegistration( |
| 107 service_worker_registration_id); |
| 108 if (!service_worker_registration) { |
| 109 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER); |
| 110 return; |
| 111 } |
| 112 |
| 113 service_worker_origin = service_worker_registration->pattern().GetOrigin(); |
| 114 } |
| 115 |
| 98 if (!service_->IsServiceAvailable()) { | 116 if (!service_->IsServiceAvailable()) { |
| 99 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); | 117 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 100 return; | 118 return; |
| 101 } | 119 } |
| 102 | 120 |
| 103 if (FindRegistration(service_worker_registration_id, region_id)) { | 121 if (FindRegistration(service_worker_registration_id, region_id)) { |
| 104 // Already registered, return an error. | 122 // Already registered, return an error. |
| 105 callback.Run(GEOFENCING_STATUS_ERROR); | 123 callback.Run(GEOFENCING_STATUS_ERROR); |
| 106 return; | 124 return; |
| 107 } | 125 } |
| 108 | 126 |
| 109 AddRegistration(service_worker_registration_id, | 127 AddRegistration(service_worker_registration_id, |
| 128 service_worker_origin, |
| 110 region_id, | 129 region_id, |
| 111 region, | 130 region, |
| 112 callback, | 131 callback, |
| 113 service_->RegisterRegion(region, this)); | 132 service_->RegisterRegion(region, this)); |
| 114 } | 133 } |
| 115 | 134 |
| 116 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, | 135 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
| 117 const std::string& region_id, | 136 const std::string& region_id, |
| 118 const StatusCallback& callback) { | 137 const StatusCallback& callback) { |
| 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 120 | 139 |
| 121 // TODO(mek): Validate region_id. | 140 // TODO(mek): Validate region_id. |
| 122 | 141 |
| 142 // Look up service worker. In unit tests |service_worker_context_| might not |
| 143 // be set. |
| 144 if (service_worker_context_.get()) { |
| 145 ServiceWorkerRegistration* service_worker_registration = |
| 146 service_worker_context_->context()->GetLiveRegistration( |
| 147 service_worker_registration_id); |
| 148 if (!service_worker_registration) { |
| 149 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER); |
| 150 return; |
| 151 } |
| 152 } |
| 153 |
| 123 if (!service_->IsServiceAvailable()) { | 154 if (!service_->IsServiceAvailable()) { |
| 124 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); | 155 callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 125 return; | 156 return; |
| 126 } | 157 } |
| 127 | 158 |
| 128 Registration* registration = | 159 Registration* registration = |
| 129 FindRegistration(service_worker_registration_id, region_id); | 160 FindRegistration(service_worker_registration_id, region_id); |
| 130 if (!registration) { | 161 if (!registration) { |
| 131 // Not registered, return an error. | 162 // Not registered, return an error. |
| 132 callback.Run(GEOFENCING_STATUS_ERROR); | 163 callback.Run(GEOFENCING_STATUS_ERROR); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 143 ClearRegistration(registration); | 174 ClearRegistration(registration); |
| 144 callback.Run(GEOFENCING_STATUS_OK); | 175 callback.Run(GEOFENCING_STATUS_OK); |
| 145 } | 176 } |
| 146 | 177 |
| 147 GeofencingStatus GeofencingManager::GetRegisteredRegions( | 178 GeofencingStatus GeofencingManager::GetRegisteredRegions( |
| 148 int64 service_worker_registration_id, | 179 int64 service_worker_registration_id, |
| 149 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { | 180 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 CHECK(result); | 182 CHECK(result); |
| 152 | 183 |
| 184 // Look up service worker. In unit tests |service_worker_context_| might not |
| 185 // be set. |
| 186 if (service_worker_context_.get()) { |
| 187 ServiceWorkerRegistration* service_worker_registration = |
| 188 service_worker_context_->context()->GetLiveRegistration( |
| 189 service_worker_registration_id); |
| 190 if (!service_worker_registration) { |
| 191 return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER; |
| 192 } |
| 193 } |
| 194 |
| 153 if (!service_->IsServiceAvailable()) { | 195 if (!service_->IsServiceAvailable()) { |
| 154 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; | 196 return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; |
| 155 } | 197 } |
| 156 | 198 |
| 157 // Populate result, filtering out inactive registrations. | 199 // Populate result, filtering out inactive registrations. |
| 158 result->clear(); | 200 result->clear(); |
| 159 ServiceWorkerRegistrationsMap::iterator registrations = | 201 ServiceWorkerRegistrationsMap::iterator registrations = |
| 160 registrations_.find(service_worker_registration_id); | 202 registrations_.find(service_worker_registration_id); |
| 161 if (registrations == registrations_.end()) | 203 if (registrations == registrations_.end()) |
| 162 return GEOFENCING_STATUS_OK; | 204 return GEOFENCING_STATUS_OK; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 int64 geofencing_registration_id) { | 243 int64 geofencing_registration_id) { |
| 202 RegistrationIdRegistrationMap::iterator registration_iterator = | 244 RegistrationIdRegistrationMap::iterator registration_iterator = |
| 203 registrations_by_id_.find(geofencing_registration_id); | 245 registrations_by_id_.find(geofencing_registration_id); |
| 204 if (registration_iterator == registrations_by_id_.end()) | 246 if (registration_iterator == registrations_by_id_.end()) |
| 205 return nullptr; | 247 return nullptr; |
| 206 return ®istration_iterator->second->second; | 248 return ®istration_iterator->second->second; |
| 207 } | 249 } |
| 208 | 250 |
| 209 GeofencingManager::Registration& GeofencingManager::AddRegistration( | 251 GeofencingManager::Registration& GeofencingManager::AddRegistration( |
| 210 int64 service_worker_registration_id, | 252 int64 service_worker_registration_id, |
| 253 const GURL& service_worker_origin, |
| 211 const std::string& region_id, | 254 const std::string& region_id, |
| 212 const blink::WebCircularGeofencingRegion& region, | 255 const blink::WebCircularGeofencingRegion& region, |
| 213 const StatusCallback& callback, | 256 const StatusCallback& callback, |
| 214 int64 geofencing_registration_id) { | 257 int64 geofencing_registration_id) { |
| 215 DCHECK(!FindRegistration(service_worker_registration_id, region_id)); | 258 DCHECK(!FindRegistration(service_worker_registration_id, region_id)); |
| 216 RegionIdRegistrationMap::iterator registration = | 259 RegionIdRegistrationMap::iterator registration = |
| 217 registrations_[service_worker_registration_id] | 260 registrations_[service_worker_registration_id] |
| 218 .insert(std::make_pair(region_id, | 261 .insert(std::make_pair(region_id, |
| 219 Registration(service_worker_registration_id, | 262 Registration(service_worker_registration_id, |
| 263 service_worker_origin, |
| 220 region_id, | 264 region_id, |
| 221 region, | 265 region, |
| 222 callback, | 266 callback, |
| 223 geofencing_registration_id))) | 267 geofencing_registration_id))) |
| 224 .first; | 268 .first; |
| 225 registrations_by_id_[geofencing_registration_id] = registration; | 269 registrations_by_id_[geofencing_registration_id] = registration; |
| 226 return registration->second; | 270 return registration->second; |
| 227 } | 271 } |
| 228 | 272 |
| 229 void GeofencingManager::ClearRegistration(Registration* registration) { | 273 void GeofencingManager::ClearRegistration(Registration* registration) { |
| 230 registrations_by_id_.erase(registration->geofencing_registration_id); | 274 registrations_by_id_.erase(registration->geofencing_registration_id); |
| 231 ServiceWorkerRegistrationsMap::iterator registrations_iterator = | 275 ServiceWorkerRegistrationsMap::iterator registrations_iterator = |
| 232 registrations_.find(registration->service_worker_registration_id); | 276 registrations_.find(registration->service_worker_registration_id); |
| 233 DCHECK(registrations_iterator != registrations_.end()); | 277 DCHECK(registrations_iterator != registrations_.end()); |
| 234 registrations_iterator->second.erase(registration->region_id); | 278 registrations_iterator->second.erase(registration->region_id); |
| 235 if (registrations_iterator->second.empty()) | 279 if (registrations_iterator->second.empty()) |
| 236 registrations_.erase(registrations_iterator); | 280 registrations_.erase(registrations_iterator); |
| 237 } | 281 } |
| 238 | 282 |
| 239 } // namespace content | 283 } // namespace content |
| OLD | NEW |