| 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_service.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/location.h" | |
| 10 #include "base/memory/singleton.h" | |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "base/threading/thread_task_runner_handle.h" | |
| 13 #include "content/browser/geofencing/geofencing_provider.h" | |
| 14 #include "content/browser/geofencing/geofencing_registration_delegate.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 void RunSoon(const base::Closure& callback) { | |
| 23 if (!callback.is_null()) | |
| 24 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 struct GeofencingServiceImpl::Registration { | |
| 30 Registration(); | |
| 31 Registration(const blink::WebCircularGeofencingRegion& region, | |
| 32 int64_t geofencing_registration_id, | |
| 33 GeofencingRegistrationDelegate* delegate); | |
| 34 | |
| 35 blink::WebCircularGeofencingRegion region; | |
| 36 int64_t geofencing_registration_id; | |
| 37 GeofencingRegistrationDelegate* delegate; | |
| 38 | |
| 39 enum RegistrationState { | |
| 40 // In progress of being registered with provider. | |
| 41 STATE_REGISTERING, | |
| 42 // Currently registered with provider. | |
| 43 STATE_REGISTERED, | |
| 44 // In progress of being registered with provider, but should be unregistered | |
| 45 // and deleted. | |
| 46 STATE_SHOULD_UNREGISTER_AND_DELETE, | |
| 47 // Not currently registered with provider, but still an active registration. | |
| 48 STATE_UNREGISTERED | |
| 49 }; | |
| 50 RegistrationState state; | |
| 51 }; | |
| 52 | |
| 53 GeofencingServiceImpl::Registration::Registration() | |
| 54 : geofencing_registration_id(-1), | |
| 55 delegate(nullptr), | |
| 56 state(STATE_UNREGISTERED) { | |
| 57 } | |
| 58 | |
| 59 GeofencingServiceImpl::Registration::Registration( | |
| 60 const blink::WebCircularGeofencingRegion& region, | |
| 61 int64_t geofencing_registration_id, | |
| 62 GeofencingRegistrationDelegate* delegate) | |
| 63 : region(region), | |
| 64 geofencing_registration_id(geofencing_registration_id), | |
| 65 delegate(delegate), | |
| 66 state(STATE_REGISTERING) {} | |
| 67 | |
| 68 GeofencingServiceImpl::GeofencingServiceImpl() : next_registration_id_(0) { | |
| 69 } | |
| 70 | |
| 71 GeofencingServiceImpl::~GeofencingServiceImpl() { | |
| 72 } | |
| 73 | |
| 74 GeofencingServiceImpl* GeofencingServiceImpl::GetInstance() { | |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 76 return base::Singleton<GeofencingServiceImpl>::get(); | |
| 77 } | |
| 78 | |
| 79 bool GeofencingServiceImpl::IsServiceAvailable() { | |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 81 return EnsureProvider(); | |
| 82 } | |
| 83 | |
| 84 int64_t GeofencingServiceImpl::RegisterRegion( | |
| 85 const blink::WebCircularGeofencingRegion& region, | |
| 86 GeofencingRegistrationDelegate* delegate) { | |
| 87 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 88 | |
| 89 int64_t geofencing_registration_id = GetNextId(); | |
| 90 registrations_[geofencing_registration_id] = | |
| 91 Registration(region, geofencing_registration_id, delegate); | |
| 92 | |
| 93 if (!EnsureProvider()) { | |
| 94 RunSoon( | |
| 95 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, | |
| 96 base::Unretained(this), | |
| 97 geofencing_registration_id, | |
| 98 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE)); | |
| 99 return geofencing_registration_id; | |
| 100 } | |
| 101 | |
| 102 provider_->RegisterRegion( | |
| 103 geofencing_registration_id, | |
| 104 region, | |
| 105 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, | |
| 106 base::Unretained(this), | |
| 107 geofencing_registration_id)); | |
| 108 return geofencing_registration_id; | |
| 109 } | |
| 110 | |
| 111 void GeofencingServiceImpl::UnregisterRegion( | |
| 112 int64_t geofencing_registration_id) { | |
| 113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 114 | |
| 115 RegistrationsMap::iterator registration_iterator = | |
| 116 registrations_.find(geofencing_registration_id); | |
| 117 DCHECK(registration_iterator != registrations_.end()); | |
| 118 | |
| 119 if (!EnsureProvider()) | |
| 120 return; | |
| 121 | |
| 122 switch (registration_iterator->second.state) { | |
| 123 case Registration::STATE_REGISTERED: | |
| 124 provider_->UnregisterRegion(geofencing_registration_id); | |
| 125 // fallthru | |
| 126 case Registration::STATE_UNREGISTERED: | |
| 127 registrations_.erase(registration_iterator); | |
| 128 break; | |
| 129 case Registration::STATE_REGISTERING: | |
| 130 // Update state, NotifyRegistrationFinished will take care of actually | |
| 131 // unregistering. | |
| 132 registration_iterator->second.state = | |
| 133 Registration::STATE_SHOULD_UNREGISTER_AND_DELETE; | |
| 134 break; | |
| 135 case Registration::STATE_SHOULD_UNREGISTER_AND_DELETE: | |
| 136 // Should not happen. | |
| 137 NOTREACHED(); | |
| 138 break; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void GeofencingServiceImpl::SetProviderForTesting( | |
| 143 std::unique_ptr<GeofencingProvider> provider) { | |
| 144 DCHECK(!provider_.get()); | |
| 145 provider_ = std::move(provider); | |
| 146 } | |
| 147 | |
| 148 int GeofencingServiceImpl::RegistrationCountForTesting() { | |
| 149 return registrations_.size(); | |
| 150 } | |
| 151 | |
| 152 bool GeofencingServiceImpl::EnsureProvider() { | |
| 153 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 154 | |
| 155 if (!provider_) { | |
| 156 // TODO(mek): Create platform specific provider. | |
| 157 return false; | |
| 158 } | |
| 159 return true; | |
| 160 } | |
| 161 | |
| 162 int64_t GeofencingServiceImpl::GetNextId() { | |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 164 | |
| 165 return next_registration_id_++; | |
| 166 } | |
| 167 | |
| 168 void GeofencingServiceImpl::NotifyRegistrationFinished( | |
| 169 int64_t geofencing_registration_id, | |
| 170 GeofencingStatus status) { | |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 172 | |
| 173 RegistrationsMap::iterator registration_iterator = | |
| 174 registrations_.find(geofencing_registration_id); | |
| 175 DCHECK(registration_iterator != registrations_.end()); | |
| 176 DCHECK(registration_iterator->second.state == | |
| 177 Registration::STATE_REGISTERING || | |
| 178 registration_iterator->second.state == | |
| 179 Registration::STATE_SHOULD_UNREGISTER_AND_DELETE); | |
| 180 | |
| 181 if (registration_iterator->second.state == | |
| 182 Registration::STATE_SHOULD_UNREGISTER_AND_DELETE) { | |
| 183 // Don't call delegate, but unregister with provider if registration was | |
| 184 // succesfull. | |
| 185 if (status == GEOFENCING_STATUS_OK) { | |
| 186 provider_->UnregisterRegion(geofencing_registration_id); | |
| 187 } | |
| 188 registrations_.erase(registration_iterator); | |
| 189 return; | |
| 190 } | |
| 191 | |
| 192 // Normal case, mark as registered and call delegate. | |
| 193 registration_iterator->second.state = Registration::STATE_REGISTERED; | |
| 194 registration_iterator->second.delegate->RegistrationFinished( | |
| 195 geofencing_registration_id, status); | |
| 196 | |
| 197 if (status != GEOFENCING_STATUS_OK) { | |
| 198 // Registration failed, remove from our book-keeping. | |
| 199 registrations_.erase(registration_iterator); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 } // namespace content | |
| OLD | NEW |