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 "base/callback.h" |
| 8 #include "base/memory/singleton.h" |
| 9 #include "content/browser/geofencing/geofencing_provider.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 struct GeofencingManager::Registration { |
| 16 Registration() : registration_id(-1), is_active(false) {} |
| 17 explicit Registration(const blink::WebCircularGeofencingRegion& region); |
| 18 |
| 19 blink::WebCircularGeofencingRegion region; |
| 20 |
| 21 // Registration id as returned by the GeofencingProvider, set to -1 if not |
| 22 // currently registered with the provider. |
| 23 int registration_id; |
| 24 |
| 25 // Flag to indicate if this registration has completed, and thus should be |
| 26 // included in calls to GetRegisteredRegions. |
| 27 bool is_active; |
| 28 }; |
| 29 |
| 30 GeofencingManager::Registration::Registration( |
| 31 const blink::WebCircularGeofencingRegion& region) |
| 32 : region(region), registration_id(-1), is_active(false) { |
| 33 } |
| 34 |
| 35 GeofencingManager::GeofencingManager() { |
| 36 } |
| 37 |
| 38 GeofencingManager::~GeofencingManager() { |
| 39 } |
| 40 |
| 41 GeofencingManager* GeofencingManager::GetInstance() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 43 return Singleton<GeofencingManager>::get(); |
| 44 } |
| 45 |
| 46 void GeofencingManager::RegisterRegion( |
| 47 int64 service_worker_registration_id, |
| 48 const std::string& region_id, |
| 49 const blink::WebCircularGeofencingRegion& region, |
| 50 const StatusCallback& callback) { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 52 |
| 53 // TODO(mek): Validate region_id and region. |
| 54 |
| 55 if (!provider_.get()) { |
| 56 callback.Run(GeofencingStatus:: |
| 57 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 58 return; |
| 59 } |
| 60 |
| 61 if (FindRegistration(service_worker_registration_id, region_id)) { |
| 62 // Already registered, return an error. |
| 63 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 64 return; |
| 65 } |
| 66 |
| 67 // Add registration, but don't mark it as active yet. This prevents duplicate |
| 68 // registrations. |
| 69 AddRegistration(service_worker_registration_id, region_id, region); |
| 70 |
| 71 // Register with provider. |
| 72 provider_->RegisterRegion( |
| 73 region, |
| 74 base::Bind(&GeofencingManager::RegisterRegionCompleted, |
| 75 base::Unretained(this), |
| 76 callback, |
| 77 service_worker_registration_id, |
| 78 region_id)); |
| 79 } |
| 80 |
| 81 void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id, |
| 82 const std::string& region_id, |
| 83 const StatusCallback& callback) { |
| 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 85 |
| 86 // TODO(mek): Validate region_id. |
| 87 |
| 88 if (!provider_.get()) { |
| 89 callback.Run(GeofencingStatus:: |
| 90 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE); |
| 91 return; |
| 92 } |
| 93 |
| 94 Registration* registration = |
| 95 FindRegistration(service_worker_registration_id, region_id); |
| 96 if (!registration) { |
| 97 // Not registered, return an error/ |
| 98 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 99 return; |
| 100 } |
| 101 |
| 102 if (!registration->is_active) { |
| 103 // Started registration, but not completed yet, error. |
| 104 callback.Run(GeofencingStatus::GEOFENCING_STATUS_ERROR); |
| 105 return; |
| 106 } |
| 107 |
| 108 if (registration->registration_id != -1) { |
| 109 provider_->UnregisterRegion(registration->registration_id); |
| 110 } |
| 111 ClearRegistration(service_worker_registration_id, region_id); |
| 112 callback.Run(GeofencingStatus::GEOFENCING_STATUS_OK); |
| 113 } |
| 114 |
| 115 GeofencingStatus GeofencingManager::GetRegisteredRegions( |
| 116 int64 service_worker_registration_id, |
| 117 std::map<std::string, blink::WebCircularGeofencingRegion>* result) { |
| 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 119 CHECK(result); |
| 120 |
| 121 if (!provider_.get()) { |
| 122 return GeofencingStatus:: |
| 123 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE; |
| 124 } |
| 125 |
| 126 // populate result |
| 127 result->clear(); |
| 128 Registrations::iterator service_worker_it = |
| 129 registrations_.find(service_worker_registration_id); |
| 130 if (service_worker_it == registrations_.end()) |
| 131 return GeofencingStatus::GEOFENCING_STATUS_OK; |
| 132 for (IdRegistrationMap::iterator it = service_worker_it->second.begin(); |
| 133 it != service_worker_it->second.end(); |
| 134 ++it) { |
| 135 if (it->second.is_active) |
| 136 (*result)[it->first] = it->second.region; |
| 137 } |
| 138 return GeofencingStatus::GEOFENCING_STATUS_OK; |
| 139 } |
| 140 |
| 141 void GeofencingManager::RegisterRegionCompleted( |
| 142 const GeofencingManager::StatusCallback& callback, |
| 143 int64 service_worker_registration_id, |
| 144 const std::string& region_id, |
| 145 GeofencingStatus status, |
| 146 int registration_id) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 148 if (status != GEOFENCING_STATUS_OK) { |
| 149 ClearRegistration(service_worker_registration_id, region_id); |
| 150 callback.Run(status); |
| 151 return; |
| 152 } |
| 153 |
| 154 Registration* registration = |
| 155 FindRegistration(service_worker_registration_id, region_id); |
| 156 DCHECK(registration); |
| 157 registration->registration_id = registration_id; |
| 158 registration->is_active = true; |
| 159 callback.Run(GeofencingStatus::GEOFENCING_STATUS_OK); |
| 160 } |
| 161 |
| 162 void GeofencingManager::SetProviderForTests( |
| 163 scoped_ptr<GeofencingProvider> provider) { |
| 164 DCHECK(!provider_.get()); |
| 165 provider_ = provider.Pass(); |
| 166 } |
| 167 |
| 168 GeofencingManager::Registration* GeofencingManager::FindRegistration( |
| 169 int64 service_worker_registration_id, |
| 170 const std::string& region_id) { |
| 171 Registrations::iterator service_worker_it = |
| 172 registrations_.find(service_worker_registration_id); |
| 173 if (service_worker_it == registrations_.end()) |
| 174 return nullptr; |
| 175 IdRegistrationMap::iterator registration_it = |
| 176 service_worker_it->second.find(region_id); |
| 177 if (registration_it == service_worker_it->second.end()) |
| 178 return nullptr; |
| 179 return ®istration_it->second; |
| 180 } |
| 181 |
| 182 GeofencingManager::Registration& GeofencingManager::AddRegistration( |
| 183 int64 service_worker_registration_id, |
| 184 const std::string& region_id, |
| 185 const blink::WebCircularGeofencingRegion& region) { |
| 186 auto result = registrations_[service_worker_registration_id].insert( |
| 187 std::make_pair(region_id, Registration(region))); |
| 188 DCHECK(result.second); |
| 189 return result.first->second; |
| 190 } |
| 191 |
| 192 void GeofencingManager::ClearRegistration(int64 service_worker_registration_id, |
| 193 const std::string& region_id) { |
| 194 Registrations::iterator service_worker_it = |
| 195 registrations_.find(service_worker_registration_id); |
| 196 if (service_worker_it == registrations_.end()) |
| 197 return; |
| 198 service_worker_it->second.erase(region_id); |
| 199 if (service_worker_it->second.size() == 0) |
| 200 registrations_.erase(service_worker_it); |
| 201 } |
| 202 |
| 203 } // namespace content |
OLD | NEW |