1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/geofencing/geofencing_manager.h"
#include <algorithm>
#include "base/callback.h"
#include "content/browser/geofencing/geofencing_service.h"
#include "content/browser/geofencing/mock_geofencing_service.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
#include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
#include "url/gurl.h"
namespace content {
struct GeofencingManager::Registration {
Registration(int64 service_worker_registration_id,
const GURL& service_worker_origin,
const std::string& region_id,
const blink::WebCircularGeofencingRegion& region,
const StatusCallback& callback,
int64 geofencing_registration_id);
int64 service_worker_registration_id;
GURL service_worker_origin;
std::string region_id;
blink::WebCircularGeofencingRegion region;
// Registration ID as returned by the |GeofencingService|.
int64 geofencing_registration_id;
// Callback to call when registration is completed. This field is reset when
// registration is complete.
StatusCallback registration_callback;
// Returns true if registration has been completed, and thus should be
// included in calls to GetRegisteredRegions.
bool is_active() const { return registration_callback.is_null(); }
};
GeofencingManager::Registration::Registration(
int64 service_worker_registration_id,
const GURL& service_worker_origin,
const std::string& region_id,
const blink::WebCircularGeofencingRegion& region,
const GeofencingManager::StatusCallback& callback,
int64 geofencing_registration_id)
: service_worker_registration_id(service_worker_registration_id),
service_worker_origin(service_worker_origin),
region_id(region_id),
region(region),
geofencing_registration_id(geofencing_registration_id),
registration_callback(callback) {
}
GeofencingManager::GeofencingManager(
const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
: service_(nullptr), service_worker_context_(service_worker_context) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
GeofencingManager::~GeofencingManager() {
}
void GeofencingManager::Init() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&GeofencingManager::InitOnIO, this));
}
void GeofencingManager::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(BrowserThread::IO,
FROM_HERE,
base::Bind(&GeofencingManager::ShutdownOnIO, this));
}
void GeofencingManager::InitOnIO() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->AddObserver(this);
if (!service_)
service_ = GeofencingServiceImpl::GetInstance();
}
void GeofencingManager::ShutdownOnIO() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
service_worker_context_->RemoveObserver(this);
// Clean up all registrations with the |GeofencingService|.
// TODO(mek): This will need to change to support geofence registrations that
// outlive the browser, although removing the references to this
// |GeofencingManager| from the |GeofencingService| will still be needed.
for (const auto& registration : registrations_by_id_)
service_->UnregisterRegion(registration.first);
}
void GeofencingManager::RegisterRegion(
int64 service_worker_registration_id,
const std::string& region_id,
const blink::WebCircularGeofencingRegion& region,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(mek): Validate region_id and region.
// Look up service worker.
ServiceWorkerRegistration* service_worker_registration =
service_worker_context_->context()->GetLiveRegistration(
service_worker_registration_id);
if (!service_worker_registration) {
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
return;
}
GURL service_worker_origin =
service_worker_registration->pattern().GetOrigin();
if (!service_->IsServiceAvailable()) {
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
return;
}
if (FindRegistration(service_worker_registration_id, region_id)) {
// Already registered, return an error.
// TODO(mek): Use a more specific error code.
callback.Run(GEOFENCING_STATUS_ERROR);
return;
}
AddRegistration(service_worker_registration_id, service_worker_origin,
region_id, region, callback,
service_->RegisterRegion(region, this));
}
void GeofencingManager::UnregisterRegion(int64 service_worker_registration_id,
const std::string& region_id,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(mek): Validate region_id.
// Look up service worker.
ServiceWorkerRegistration* service_worker_registration =
service_worker_context_->context()->GetLiveRegistration(
service_worker_registration_id);
if (!service_worker_registration) {
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
return;
}
if (!service_->IsServiceAvailable()) {
callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
return;
}
Registration* registration =
FindRegistration(service_worker_registration_id, region_id);
if (!registration) {
// Not registered, return an error.
callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED);
return;
}
if (!registration->is_active()) {
// Started registration, but not completed yet, error.
callback.Run(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED);
return;
}
service_->UnregisterRegion(registration->geofencing_registration_id);
ClearRegistration(registration);
callback.Run(GEOFENCING_STATUS_OK);
}
GeofencingStatus GeofencingManager::GetRegisteredRegions(
int64 service_worker_registration_id,
std::map<std::string, blink::WebCircularGeofencingRegion>* result) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
CHECK(result);
// Look up service worker.
ServiceWorkerRegistration* service_worker_registration =
service_worker_context_->context()->GetLiveRegistration(
service_worker_registration_id);
if (!service_worker_registration) {
return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER;
}
if (!service_->IsServiceAvailable()) {
return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE;
}
// Populate result, filtering out inactive registrations.
result->clear();
ServiceWorkerRegistrationsMap::iterator registrations =
registrations_.find(service_worker_registration_id);
if (registrations == registrations_.end())
return GEOFENCING_STATUS_OK;
for (const auto& registration : registrations->second) {
if (registration.second.is_active())
(*result)[registration.first] = registration.second.region;
}
return GEOFENCING_STATUS_OK;
}
void GeofencingManager::SetMockProvider(GeofencingMockState mock_state) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(mek): It would be nice if enabling the mock geofencing service
// wouldn't completely delete all existing registrations but instead just
// somehow keep them around but inactive.
// For now mocking is only used in layout tests, so just clearing all
// registrations works good enough.
for (const auto& registration : registrations_by_id_)
service_->UnregisterRegion(registration.first);
registrations_by_id_.clear();
registrations_.clear();
// Then set or reset the mock service for the service worker.
if (mock_state == GeofencingMockState::NONE) {
service_ = GeofencingServiceImpl::GetInstance();
mock_service_.reset();
} else {
mock_service_.reset(new MockGeofencingService(
mock_state != GeofencingMockState::SERVICE_UNAVAILABLE));
service_ = mock_service_.get();
}
}
void GeofencingManager::SetMockPosition(double latitude, double longitude) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!mock_service_)
return;
mock_service_->SetMockPosition(latitude, longitude);
}
void GeofencingManager::OnRegistrationDeleted(
int64 service_worker_registration_id,
const GURL& pattern) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
CleanUpForServiceWorker(service_worker_registration_id);
}
void GeofencingManager::RegistrationFinished(int64 geofencing_registration_id,
GeofencingStatus status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
Registration* registration = FindRegistrationById(geofencing_registration_id);
DCHECK(registration);
DCHECK(!registration->is_active());
registration->registration_callback.Run(status);
registration->registration_callback.Reset();
// If the registration wasn't succesful, remove it from our storage.
if (status != GEOFENCING_STATUS_OK)
ClearRegistration(registration);
}
void GeofencingManager::RegionEntered(int64 geofencing_registration_id) {
DispatchGeofencingEvent(blink::WebGeofencingEventTypeEnter,
geofencing_registration_id);
}
void GeofencingManager::RegionExited(int64 geofencing_registration_id) {
DispatchGeofencingEvent(blink::WebGeofencingEventTypeLeave,
geofencing_registration_id);
}
GeofencingManager::Registration* GeofencingManager::FindRegistration(
int64 service_worker_registration_id,
const std::string& region_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerRegistrationsMap::iterator registrations_iterator =
registrations_.find(service_worker_registration_id);
if (registrations_iterator == registrations_.end())
return nullptr;
RegionIdRegistrationMap::iterator registration =
registrations_iterator->second.find(region_id);
if (registration == registrations_iterator->second.end())
return nullptr;
return ®istration->second;
}
GeofencingManager::Registration* GeofencingManager::FindRegistrationById(
int64 geofencing_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
RegistrationIdRegistrationMap::iterator registration_iterator =
registrations_by_id_.find(geofencing_registration_id);
if (registration_iterator == registrations_by_id_.end())
return nullptr;
return ®istration_iterator->second->second;
}
GeofencingManager::Registration& GeofencingManager::AddRegistration(
int64 service_worker_registration_id,
const GURL& service_worker_origin,
const std::string& region_id,
const blink::WebCircularGeofencingRegion& region,
const StatusCallback& callback,
int64 geofencing_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK(!FindRegistration(service_worker_registration_id, region_id));
RegionIdRegistrationMap::iterator registration =
registrations_[service_worker_registration_id]
.insert(std::make_pair(region_id,
Registration(service_worker_registration_id,
service_worker_origin,
region_id,
region,
callback,
geofencing_registration_id)))
.first;
registrations_by_id_[geofencing_registration_id] = registration;
return registration->second;
}
void GeofencingManager::ClearRegistration(Registration* registration) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
registrations_by_id_.erase(registration->geofencing_registration_id);
ServiceWorkerRegistrationsMap::iterator registrations_iterator =
registrations_.find(registration->service_worker_registration_id);
DCHECK(registrations_iterator != registrations_.end());
registrations_iterator->second.erase(registration->region_id);
if (registrations_iterator->second.empty())
registrations_.erase(registrations_iterator);
}
void GeofencingManager::CleanUpForServiceWorker(
int64 service_worker_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerRegistrationsMap::iterator registrations_iterator =
registrations_.find(service_worker_registration_id);
if (registrations_iterator == registrations_.end())
return;
for (const auto& registration : registrations_iterator->second) {
int geofencing_registration_id =
registration.second.geofencing_registration_id;
service_->UnregisterRegion(geofencing_registration_id);
registrations_by_id_.erase(geofencing_registration_id);
}
registrations_.erase(service_worker_registration_id);
}
void GeofencingManager::DispatchGeofencingEvent(
blink::WebGeofencingEventType event_type,
int64 geofencing_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
Registration* registration = FindRegistrationById(geofencing_registration_id);
if (!registration ||
registration->service_worker_registration_id ==
kInvalidServiceWorkerRegistrationId) {
// TODO(mek): Log/track these failures.
return;
}
service_worker_context_->context()->storage()->FindRegistrationForId(
registration->service_worker_registration_id,
registration->service_worker_origin,
base::Bind(&GeofencingManager::DeliverGeofencingEvent,
this,
event_type,
geofencing_registration_id));
}
void GeofencingManager::DeliverGeofencingEvent(
blink::WebGeofencingEventType event_type,
int64 geofencing_registration_id,
ServiceWorkerStatusCode service_worker_status,
const scoped_refptr<ServiceWorkerRegistration>&
service_worker_registration) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
Registration* registration = FindRegistrationById(geofencing_registration_id);
if (!registration) {
// Geofence got unregistered in the meantime, no longer need to deliver
// event.
return;
}
if (service_worker_status != SERVICE_WORKER_OK) {
// TODO(mek): SW no longer exists, somehow handle this.
return;
}
ServiceWorkerVersion* active_version =
service_worker_registration->active_version();
if (!active_version) {
// TODO(mek): No active version, potentially because one is still being
// installed. Handle this somehow.
return;
}
// Hold on to the service worker registration in the callback to keep it alive
// until the callback dies. Otherwise the registration could be released when
// this method returns - before the event is delivered to the service worker.
base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback =
base::Bind(&GeofencingManager::DeliverGeofencingEventEnd,
this,
service_worker_registration);
active_version->DispatchGeofencingEvent(dispatch_event_callback,
event_type,
registration->region_id,
registration->region);
}
void GeofencingManager::DeliverGeofencingEventEnd(
const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
ServiceWorkerStatusCode service_worker_status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(mek): log/check result.
}
} // namespace content
|