OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/geofencing/geofencing_manager.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/callback.h" |
| 10 #include "base/memory/singleton.h" |
| 11 #include "content/browser/geofencing/geofencing_provider.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 14 #include "url/gurl.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 struct GeofencingManager::RegistrationKey { |
| 19 RegistrationKey(BrowserContext* browser_context, |
| 20 int64 service_worker_registration_id, |
| 21 const GURL& service_worker_origin, |
| 22 const std::string& region_id); |
| 23 |
| 24 BrowserContext* browser_context; |
| 25 |
| 26 int64 service_worker_registration_id; |
| 27 GURL service_worker_origin; |
| 28 |
| 29 std::string region_id; |
| 30 }; |
| 31 |
| 32 GeofencingManager::RegistrationKey::RegistrationKey( |
| 33 BrowserContext* browser_context, |
| 34 int64 service_worker_registration_id, |
| 35 const GURL& service_worker_origin, |
| 36 const std::string& region_id) |
| 37 : browser_context(browser_context), |
| 38 service_worker_registration_id(service_worker_registration_id), |
| 39 service_worker_origin(service_worker_origin), |
| 40 region_id(region_id) { |
| 41 } |
| 42 |
| 43 struct GeofencingManager::Registration { |
| 44 Registration(); |
| 45 Registration(const RegistrationKey& key, |
| 46 const blink::WebCircularGeofencingRegion& region); |
| 47 |
| 48 RegistrationKey key; |
| 49 blink::WebCircularGeofencingRegion region; |
| 50 |
| 51 // Registration id as returned by the GeofencingProvider, set to -1 if not |
| 52 // currently registered with the provider. |
| 53 int registration_id; |
| 54 |
| 55 // Flag to indicate if this registration has completed, and thus should be |
| 56 // included in calls to GetRegisteredRegions. |
| 57 bool is_active; |
| 58 }; |
| 59 |
| 60 GeofencingManager::Registration::Registration() : key(nullptr, -1, GURL(), "") { |
| 61 } |
| 62 |
| 63 GeofencingManager::Registration::Registration( |
| 64 const RegistrationKey& key, |
| 65 const blink::WebCircularGeofencingRegion& region) |
| 66 : key(key), region(region), registration_id(-1), is_active(false) { |
| 67 } |
| 68 |
| 69 class GeofencingManager::RegistrationMatches { |
| 70 public: |
| 71 RegistrationMatches(const RegistrationKey& key) : key_(key) {} |
| 72 |
| 73 bool operator()(const Registration& registration) { |
| 74 return registration.key.browser_context == key_.browser_context && |
| 75 registration.key.service_worker_registration_id == |
| 76 key_.service_worker_registration_id && |
| 77 registration.key.service_worker_origin == |
| 78 key_.service_worker_origin && |
| 79 registration.key.region_id == key_.region_id; |
| 80 } |
| 81 |
| 82 private: |
| 83 const RegistrationKey& key_; |
| 84 }; |
| 85 |
| 86 GeofencingManager::GeofencingManager() { |
| 87 } |
| 88 |
| 89 GeofencingManager::~GeofencingManager() { |
| 90 } |
| 91 |
| 92 GeofencingManager* GeofencingManager::GetInstance() { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 return Singleton<GeofencingManager>::get(); |
| 95 } |
| 96 |
| 97 void GeofencingManager::RegisterRegion( |
| 98 BrowserContext* browser_context, |
| 99 int64 service_worker_registration_id, |
| 100 const GURL& service_worker_origin, |
| 101 const std::string& region_id, |
| 102 const blink::WebCircularGeofencingRegion& region, |
| 103 const StatusCallback& callback) { |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 105 |
| 106 // TODO(mek): Validate region_id and region. |
| 107 |
| 108 if (!provider_.get()) { |
| 109 callback.Run(GeofencingStatus:: |
| 110 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 111 return; |
| 112 } |
| 113 |
| 114 RegistrationKey key(browser_context, |
| 115 service_worker_registration_id, |
| 116 service_worker_origin, |
| 117 region_id); |
| 118 if (FindRegistration(key)) { |
| 119 // Already registered, return an error. |
| 120 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 121 return; |
| 122 } |
| 123 |
| 124 // Add registration, but don't mark it as active yet. This prevents duplicate |
| 125 // registrations. |
| 126 AddRegistration(key, region); |
| 127 |
| 128 // Register with provider. |
| 129 provider_->RegisterRegion( |
| 130 region, |
| 131 base::Bind(&GeofencingManager::RegisterRegionCompleted, |
| 132 base::Unretained(this), |
| 133 callback, |
| 134 key)); |
| 135 } |
| 136 |
| 137 void GeofencingManager::UnregisterRegion(BrowserContext* browser_context, |
| 138 int64 service_worker_registration_id, |
| 139 const GURL& service_worker_origin, |
| 140 const std::string& region_id, |
| 141 const StatusCallback& callback) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 |
| 144 // TODO(mek): Validate region_id. |
| 145 |
| 146 if (!provider_.get()) { |
| 147 callback.Run(GeofencingStatus:: |
| 148 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 149 return; |
| 150 } |
| 151 |
| 152 RegistrationKey key(browser_context, |
| 153 service_worker_registration_id, |
| 154 service_worker_origin, |
| 155 region_id); |
| 156 Registration* registration = FindRegistration(key); |
| 157 if (!registration) { |
| 158 // Not registered, return an error/ |
| 159 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 160 return; |
| 161 } |
| 162 |
| 163 if (!registration->is_active) { |
| 164 // Started registration, but not completed yet, error. |
| 165 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 166 return; |
| 167 } |
| 168 |
| 169 if (registration->registration_id != -1) { |
| 170 provider_->UnregisterRegion(registration->registration_id); |
| 171 } |
| 172 ClearRegistration(key); |
| 173 callback.Run(GeofencingStatus::GEOFENCING_STATUS_OK); |
| 174 } |
| 175 |
| 176 GeofencingStatus GeofencingManager::GetRegisteredRegions( |
| 177 BrowserContext* browser_context, |
| 178 int64 service_worker_registration_id, |
| 179 const GURL& service_worker_origin, |
| 180 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 182 CHECK(result); |
| 183 |
| 184 if (!provider_.get()) { |
| 185 return GeofencingStatus:: |
| 186 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; |
| 187 } |
| 188 |
| 189 // Populate result, filtering out inactive registrations. |
| 190 result->clear(); |
| 191 for (const auto& registration : registrations_) { |
| 192 if (registration.key.browser_context == browser_context && |
| 193 registration.key.service_worker_registration_id == |
| 194 service_worker_registration_id && |
| 195 registration.key.service_worker_origin == service_worker_origin && |
| 196 registration.is_active) { |
| 197 (*result)[registration.key.region_id] = registration.region; |
| 198 } |
| 199 } |
| 200 return GeofencingStatus::GEOFENCING_STATUS_OK; |
| 201 } |
| 202 |
| 203 void GeofencingManager::RegisterRegionCompleted(const StatusCallback& callback, |
| 204 const RegistrationKey& key, |
| 205 GeofencingStatus status, |
| 206 int registration_id) { |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 208 if (status != GEOFENCING_STATUS_OK) { |
| 209 ClearRegistration(key); |
| 210 callback.Run(status); |
| 211 return; |
| 212 } |
| 213 |
| 214 Registration* registration = FindRegistration(key); |
| 215 DCHECK(registration); |
| 216 registration->registration_id = registration_id; |
| 217 registration->is_active = true; |
| 218 callback.Run(GeofencingStatus::GEOFENCING_STATUS_OK); |
| 219 } |
| 220 |
| 221 void GeofencingManager::SetProviderForTests( |
| 222 scoped_ptr<GeofencingProvider> provider) { |
| 223 DCHECK(!provider_.get()); |
| 224 provider_ = provider.Pass(); |
| 225 } |
| 226 |
| 227 GeofencingManager::Registration* GeofencingManager::FindRegistration( |
| 228 const RegistrationKey& key) { |
| 229 std::vector<Registration>::iterator it = std::find_if( |
| 230 registrations_.begin(), registrations_.end(), RegistrationMatches(key)); |
| 231 if (it == registrations_.end()) |
| 232 return nullptr; |
| 233 return &*it; |
| 234 } |
| 235 |
| 236 GeofencingManager::Registration& GeofencingManager::AddRegistration( |
| 237 const RegistrationKey& key, |
| 238 const blink::WebCircularGeofencingRegion& region) { |
| 239 DCHECK(!FindRegistration(key)); |
| 240 registrations_.push_back(Registration(key, region)); |
| 241 return registrations_.back(); |
| 242 } |
| 243 |
| 244 void GeofencingManager::ClearRegistration(const RegistrationKey& key) { |
| 245 std::vector<Registration>::iterator it = std::find_if( |
| 246 registrations_.begin(), registrations_.end(), RegistrationMatches(key)); |
| 247 if (it == registrations_.end()) |
| 248 return; |
| 249 registrations_.erase(it); |
| 250 } |
| 251 |
| 252 } // namespace content |
OLD | NEW |