| 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_service.h" | 5 #include "content/browser/geofencing/geofencing_service.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "content/browser/geofencing/geofencing_provider.h" | 11 #include "content/browser/geofencing/geofencing_provider.h" |
| 12 #include "content/browser/geofencing/geofencing_registration_delegate.h" | 12 #include "content/browser/geofencing/geofencing_registration_delegate.h" |
| 13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" | 14 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 void RunSoon(const base::Closure& callback) { | 20 void RunSoon(const base::Closure& callback) { |
| 21 if (!callback.is_null()) | 21 if (!callback.is_null()) |
| 22 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | 22 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
| 23 } | 23 } |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 struct GeofencingServiceImpl::Registration { | 27 struct GeofencingServiceImpl::Registration { |
| 28 Registration(); | 28 Registration(); |
| 29 Registration(const blink::WebCircularGeofencingRegion& region, | 29 Registration(const blink::WebCircularGeofencingRegion& region, |
| 30 int64 geofencing_registration_id, | 30 int64_t geofencing_registration_id, |
| 31 GeofencingRegistrationDelegate* delegate); | 31 GeofencingRegistrationDelegate* delegate); |
| 32 | 32 |
| 33 blink::WebCircularGeofencingRegion region; | 33 blink::WebCircularGeofencingRegion region; |
| 34 int64 geofencing_registration_id; | 34 int64_t geofencing_registration_id; |
| 35 GeofencingRegistrationDelegate* delegate; | 35 GeofencingRegistrationDelegate* delegate; |
| 36 | 36 |
| 37 enum RegistrationState { | 37 enum RegistrationState { |
| 38 // In progress of being registered with provider. | 38 // In progress of being registered with provider. |
| 39 STATE_REGISTERING, | 39 STATE_REGISTERING, |
| 40 // Currently registered with provider. | 40 // Currently registered with provider. |
| 41 STATE_REGISTERED, | 41 STATE_REGISTERED, |
| 42 // In progress of being registered with provider, but should be unregistered | 42 // In progress of being registered with provider, but should be unregistered |
| 43 // and deleted. | 43 // and deleted. |
| 44 STATE_SHOULD_UNREGISTER_AND_DELETE, | 44 STATE_SHOULD_UNREGISTER_AND_DELETE, |
| 45 // Not currently registered with provider, but still an active registration. | 45 // Not currently registered with provider, but still an active registration. |
| 46 STATE_UNREGISTERED | 46 STATE_UNREGISTERED |
| 47 }; | 47 }; |
| 48 RegistrationState state; | 48 RegistrationState state; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 GeofencingServiceImpl::Registration::Registration() | 51 GeofencingServiceImpl::Registration::Registration() |
| 52 : geofencing_registration_id(-1), | 52 : geofencing_registration_id(-1), |
| 53 delegate(nullptr), | 53 delegate(nullptr), |
| 54 state(STATE_UNREGISTERED) { | 54 state(STATE_UNREGISTERED) { |
| 55 } | 55 } |
| 56 | 56 |
| 57 GeofencingServiceImpl::Registration::Registration( | 57 GeofencingServiceImpl::Registration::Registration( |
| 58 const blink::WebCircularGeofencingRegion& region, | 58 const blink::WebCircularGeofencingRegion& region, |
| 59 int64 geofencing_registration_id, | 59 int64_t geofencing_registration_id, |
| 60 GeofencingRegistrationDelegate* delegate) | 60 GeofencingRegistrationDelegate* delegate) |
| 61 : region(region), | 61 : region(region), |
| 62 geofencing_registration_id(geofencing_registration_id), | 62 geofencing_registration_id(geofencing_registration_id), |
| 63 delegate(delegate), | 63 delegate(delegate), |
| 64 state(STATE_REGISTERING) { | 64 state(STATE_REGISTERING) {} |
| 65 } | |
| 66 | 65 |
| 67 GeofencingServiceImpl::GeofencingServiceImpl() : next_registration_id_(0) { | 66 GeofencingServiceImpl::GeofencingServiceImpl() : next_registration_id_(0) { |
| 68 } | 67 } |
| 69 | 68 |
| 70 GeofencingServiceImpl::~GeofencingServiceImpl() { | 69 GeofencingServiceImpl::~GeofencingServiceImpl() { |
| 71 } | 70 } |
| 72 | 71 |
| 73 GeofencingServiceImpl* GeofencingServiceImpl::GetInstance() { | 72 GeofencingServiceImpl* GeofencingServiceImpl::GetInstance() { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 73 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 return base::Singleton<GeofencingServiceImpl>::get(); | 74 return base::Singleton<GeofencingServiceImpl>::get(); |
| 76 } | 75 } |
| 77 | 76 |
| 78 bool GeofencingServiceImpl::IsServiceAvailable() { | 77 bool GeofencingServiceImpl::IsServiceAvailable() { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 return EnsureProvider(); | 79 return EnsureProvider(); |
| 81 } | 80 } |
| 82 | 81 |
| 83 int64 GeofencingServiceImpl::RegisterRegion( | 82 int64_t GeofencingServiceImpl::RegisterRegion( |
| 84 const blink::WebCircularGeofencingRegion& region, | 83 const blink::WebCircularGeofencingRegion& region, |
| 85 GeofencingRegistrationDelegate* delegate) { | 84 GeofencingRegistrationDelegate* delegate) { |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 85 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 87 | 86 |
| 88 int64 geofencing_registration_id = GetNextId(); | 87 int64_t geofencing_registration_id = GetNextId(); |
| 89 registrations_[geofencing_registration_id] = | 88 registrations_[geofencing_registration_id] = |
| 90 Registration(region, geofencing_registration_id, delegate); | 89 Registration(region, geofencing_registration_id, delegate); |
| 91 | 90 |
| 92 if (!EnsureProvider()) { | 91 if (!EnsureProvider()) { |
| 93 RunSoon( | 92 RunSoon( |
| 94 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, | 93 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, |
| 95 base::Unretained(this), | 94 base::Unretained(this), |
| 96 geofencing_registration_id, | 95 geofencing_registration_id, |
| 97 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE)); | 96 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE)); |
| 98 return geofencing_registration_id; | 97 return geofencing_registration_id; |
| 99 } | 98 } |
| 100 | 99 |
| 101 provider_->RegisterRegion( | 100 provider_->RegisterRegion( |
| 102 geofencing_registration_id, | 101 geofencing_registration_id, |
| 103 region, | 102 region, |
| 104 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, | 103 base::Bind(&GeofencingServiceImpl::NotifyRegistrationFinished, |
| 105 base::Unretained(this), | 104 base::Unretained(this), |
| 106 geofencing_registration_id)); | 105 geofencing_registration_id)); |
| 107 return geofencing_registration_id; | 106 return geofencing_registration_id; |
| 108 } | 107 } |
| 109 | 108 |
| 110 void GeofencingServiceImpl::UnregisterRegion(int64 geofencing_registration_id) { | 109 void GeofencingServiceImpl::UnregisterRegion( |
| 110 int64_t geofencing_registration_id) { |
| 111 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 111 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 112 | 112 |
| 113 RegistrationsMap::iterator registration_iterator = | 113 RegistrationsMap::iterator registration_iterator = |
| 114 registrations_.find(geofencing_registration_id); | 114 registrations_.find(geofencing_registration_id); |
| 115 DCHECK(registration_iterator != registrations_.end()); | 115 DCHECK(registration_iterator != registrations_.end()); |
| 116 | 116 |
| 117 if (!EnsureProvider()) | 117 if (!EnsureProvider()) |
| 118 return; | 118 return; |
| 119 | 119 |
| 120 switch (registration_iterator->second.state) { | 120 switch (registration_iterator->second.state) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 150 bool GeofencingServiceImpl::EnsureProvider() { | 150 bool GeofencingServiceImpl::EnsureProvider() { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 152 | 152 |
| 153 if (!provider_) { | 153 if (!provider_) { |
| 154 // TODO(mek): Create platform specific provider. | 154 // TODO(mek): Create platform specific provider. |
| 155 return false; | 155 return false; |
| 156 } | 156 } |
| 157 return true; | 157 return true; |
| 158 } | 158 } |
| 159 | 159 |
| 160 int64 GeofencingServiceImpl::GetNextId() { | 160 int64_t GeofencingServiceImpl::GetNextId() { |
| 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 162 | 162 |
| 163 return next_registration_id_++; | 163 return next_registration_id_++; |
| 164 } | 164 } |
| 165 | 165 |
| 166 void GeofencingServiceImpl::NotifyRegistrationFinished( | 166 void GeofencingServiceImpl::NotifyRegistrationFinished( |
| 167 int64 geofencing_registration_id, | 167 int64_t geofencing_registration_id, |
| 168 GeofencingStatus status) { | 168 GeofencingStatus status) { |
| 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 169 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 170 | 170 |
| 171 RegistrationsMap::iterator registration_iterator = | 171 RegistrationsMap::iterator registration_iterator = |
| 172 registrations_.find(geofencing_registration_id); | 172 registrations_.find(geofencing_registration_id); |
| 173 DCHECK(registration_iterator != registrations_.end()); | 173 DCHECK(registration_iterator != registrations_.end()); |
| 174 DCHECK(registration_iterator->second.state == | 174 DCHECK(registration_iterator->second.state == |
| 175 Registration::STATE_REGISTERING || | 175 Registration::STATE_REGISTERING || |
| 176 registration_iterator->second.state == | 176 registration_iterator->second.state == |
| 177 Registration::STATE_SHOULD_UNREGISTER_AND_DELETE); | 177 Registration::STATE_SHOULD_UNREGISTER_AND_DELETE); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 192 registration_iterator->second.delegate->RegistrationFinished( | 192 registration_iterator->second.delegate->RegistrationFinished( |
| 193 geofencing_registration_id, status); | 193 geofencing_registration_id, status); |
| 194 | 194 |
| 195 if (status != GEOFENCING_STATUS_OK) { | 195 if (status != GEOFENCING_STATUS_OK) { |
| 196 // Registration failed, remove from our book-keeping. | 196 // Registration failed, remove from our book-keeping. |
| 197 registrations_.erase(registration_iterator); | 197 registrations_.erase(registration_iterator); |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 } // namespace content | 201 } // namespace content |
| OLD | NEW |