Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/background_sync/background_sync_service_impl.h" | 5 #include "content/browser/background_sync/background_sync_service_impl.h" |
| 6 | 6 |
| 7 #include "background_sync_registration_handle.h" | |
| 7 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/stl_util.h" | |
| 8 #include "content/browser/background_sync/background_sync_context_impl.h" | 10 #include "content/browser/background_sync/background_sync_context_impl.h" |
| 9 #include "content/browser/background_sync/background_sync_registration.h" | |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // TODO(iclelland): Move these converters to mojo::TypeConverter template | 17 // TODO(iclelland): Move these converters to mojo::TypeConverter template |
| 17 // specializations. | 18 // specializations. |
| 18 | 19 |
| 19 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( | 20 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
| 20 const SyncRegistrationPtr& in) { | 21 const SyncRegistrationPtr& in) { |
| 21 BackgroundSyncRegistrationOptions out; | 22 BackgroundSyncRegistrationOptions out; |
| 22 | 23 |
| 23 out.tag = in->tag; | 24 out.tag = in->tag; |
| 24 out.min_period = in->min_period_ms; | 25 out.min_period = in->min_period_ms; |
| 25 out.power_state = static_cast<SyncPowerState>(in->power_state); | 26 out.power_state = static_cast<SyncPowerState>(in->power_state); |
| 26 out.network_state = static_cast<SyncNetworkState>(in->network_state); | 27 out.network_state = static_cast<SyncNetworkState>(in->network_state); |
| 27 out.periodicity = static_cast<SyncPeriodicity>(in->periodicity); | 28 out.periodicity = static_cast<SyncPeriodicity>(in->periodicity); |
| 28 return out; | 29 return out; |
| 29 } | 30 } |
| 30 | 31 |
| 31 SyncRegistrationPtr ToMojoRegistration(const BackgroundSyncRegistration& in) { | 32 SyncRegistrationPtr ToMojoRegistration( |
| 33 const BackgroundSyncRegistrationHandle& in) { | |
| 32 SyncRegistrationPtr out(content::SyncRegistration::New()); | 34 SyncRegistrationPtr out(content::SyncRegistration::New()); |
| 33 out->id = in.id(); | 35 out->id = in.handle_id(); |
| 34 out->tag = in.options()->tag; | 36 out->tag = in.options()->tag; |
| 35 out->min_period_ms = in.options()->min_period; | 37 out->min_period_ms = in.options()->min_period; |
| 36 out->periodicity = static_cast<content::BackgroundSyncPeriodicity>( | 38 out->periodicity = static_cast<content::BackgroundSyncPeriodicity>( |
| 37 in.options()->periodicity); | 39 in.options()->periodicity); |
| 38 out->power_state = | 40 out->power_state = |
| 39 static_cast<content::BackgroundSyncPowerState>(in.options()->power_state); | 41 static_cast<content::BackgroundSyncPowerState>(in.options()->power_state); |
| 40 out->network_state = static_cast<content::BackgroundSyncNetworkState>( | 42 out->network_state = static_cast<content::BackgroundSyncNetworkState>( |
| 41 in.options()->network_state); | 43 in.options()->network_state); |
| 42 return out.Pass(); | 44 return out.Pass(); |
| 43 } | 45 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 83 |
| 82 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, | 84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, |
| 83 SyncPeriodicity::SYNC_PERIODIC); | 85 SyncPeriodicity::SYNC_PERIODIC); |
| 84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 85 SyncPeriodicity::SYNC_ONE_SHOT); | 87 SyncPeriodicity::SYNC_ONE_SHOT); |
| 86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, | 88 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, |
| 87 SyncPeriodicity::SYNC_ONE_SHOT); | 89 SyncPeriodicity::SYNC_ONE_SHOT); |
| 88 | 90 |
| 89 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { | 91 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 93 BackgroundSyncManager* background_sync_manager = | |
| 94 background_sync_context_->background_sync_manager(); | |
| 95 DCHECK(background_sync_manager); | |
|
michaeln
2015/08/21 02:39:24
You could put this larger expression inside the dc
jkarlin
2015/08/25 17:32:58
Done.
| |
| 91 } | 96 } |
| 92 | 97 |
| 93 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( | 98 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
| 94 BackgroundSyncContextImpl* background_sync_context, | 99 BackgroundSyncContextImpl* background_sync_context, |
| 95 mojo::InterfaceRequest<BackgroundSyncService> request) | 100 mojo::InterfaceRequest<BackgroundSyncService> request) |
| 96 : background_sync_context_(background_sync_context), | 101 : background_sync_context_(background_sync_context), |
| 97 binding_(this, request.Pass()), | 102 binding_(this, request.Pass()), |
| 98 weak_ptr_factory_(this) { | 103 weak_ptr_factory_(this) { |
| 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 100 DCHECK(background_sync_context); | 105 DCHECK(background_sync_context); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 122 DCHECK(background_sync_manager); | 127 DCHECK(background_sync_manager); |
| 123 background_sync_manager->Register( | 128 background_sync_manager->Register( |
| 124 sw_registration_id, mgr_options, | 129 sw_registration_id, mgr_options, |
| 125 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 130 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
| 126 weak_ptr_factory_.GetWeakPtr(), callback)); | 131 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 127 } | 132 } |
| 128 | 133 |
| 129 void BackgroundSyncServiceImpl::Unregister( | 134 void BackgroundSyncServiceImpl::Unregister( |
| 130 BackgroundSyncPeriodicity periodicity, | 135 BackgroundSyncPeriodicity periodicity, |
| 131 int64_t id, | 136 int64_t id, |
| 132 const mojo::String& tag, | |
| 133 int64_t sw_registration_id, | 137 int64_t sw_registration_id, |
| 134 const UnregisterCallback& callback) { | 138 const UnregisterCallback& callback) { |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 136 BackgroundSyncManager* background_sync_manager = | 140 BackgroundSyncManager* background_sync_manager = |
| 137 background_sync_context_->background_sync_manager(); | 141 background_sync_context_->background_sync_manager(); |
| 138 DCHECK(background_sync_manager); | 142 DCHECK(background_sync_manager); |
| 139 background_sync_manager->Unregister( | 143 |
| 140 sw_registration_id, tag, content::SYNC_ONE_SHOT, id, | 144 BackgroundSyncRegistrationHandle* registration = |
| 145 hosted_registrations_.Lookup(id); | |
| 146 if (!registration) { | |
| 147 callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 registration->Unregister( | |
| 152 sw_registration_id, | |
| 141 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, | 153 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
| 142 weak_ptr_factory_.GetWeakPtr(), callback)); | 154 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 143 } | 155 } |
| 144 | 156 |
| 145 void BackgroundSyncServiceImpl::GetRegistration( | 157 void BackgroundSyncServiceImpl::GetRegistration( |
| 146 BackgroundSyncPeriodicity periodicity, | 158 BackgroundSyncPeriodicity periodicity, |
| 147 const mojo::String& tag, | 159 const mojo::String& tag, |
| 148 int64_t sw_registration_id, | 160 int64_t sw_registration_id, |
| 149 const GetRegistrationCallback& callback) { | 161 const GetRegistrationCallback& callback) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 162 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 176 int64_t sw_registration_id, | 188 int64_t sw_registration_id, |
| 177 const GetPermissionStatusCallback& callback) { | 189 const GetPermissionStatusCallback& callback) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 | 191 |
| 180 // TODO(iclelland): Implement a real policy. This is a stub implementation. | 192 // TODO(iclelland): Implement a real policy. This is a stub implementation. |
| 181 // OneShot: crbug.com/482091 | 193 // OneShot: crbug.com/482091 |
| 182 // Periodic: crbug.com/482093 | 194 // Periodic: crbug.com/482093 |
| 183 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); | 195 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
| 184 } | 196 } |
| 185 | 197 |
| 198 void BackgroundSyncServiceImpl::TrackRegistration( | |
| 199 SyncRegistrationPtr sync_registration) { | |
| 200 // TODO(jkarlin): We dont need the whole SyncRegistrationPtr here, just the id | |
| 201 // would do. | |
| 202 | |
| 203 // Registrations that the client acquires without the | |
| 204 // BackgroundSyncServiceImpl's knowing are registered here. | |
| 205 BackgroundSyncManager* background_sync_manager = | |
| 206 background_sync_context_->background_sync_manager(); | |
| 207 DCHECK(background_sync_manager); | |
| 208 | |
| 209 if (hosted_registrations_.Lookup(sync_registration->id)) { | |
|
michaeln
2015/08/21 02:39:24
This is actually using a 'handle_id' to lookup a '
jkarlin
2015/08/25 17:32:58
This method is gone now.
| |
| 210 // TODO(jkarlin): Abort client. | |
| 211 LOG(WARNING) << "Client attempted to track an already tracked registration"; | |
| 212 return; | |
| 213 } | |
| 214 | |
| 215 scoped_ptr<BackgroundSyncRegistrationHandle> registration( | |
|
michaeln
2015/08/21 02:39:24
nit: put 'handle' somewhere in the local variable
jkarlin
2015/08/25 17:32:58
This method is gone now.
| |
| 216 new BackgroundSyncRegistrationHandle(background_sync_manager, | |
| 217 sync_registration->id)); | |
| 218 | |
| 219 if (!registration->IsValid()) { | |
| 220 // TODO(jkarlin): Abort client. | |
| 221 LOG(WARNING) << "Client attempted to track a non-existent registration"; | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 hosted_registrations_.AddWithID(registration.release(), | |
| 226 sync_registration->id); | |
| 227 } | |
| 228 | |
| 229 void BackgroundSyncServiceImpl::ReleaseRegistration(int64_t sync_id) { | |
| 230 if (!hosted_registrations_.Lookup(sync_id)) { | |
| 231 // TODO(jkarlin): Abort client. | |
| 232 LOG(WARNING) << "Client attempted to release non-existing registration"; | |
| 233 return; | |
| 234 } | |
| 235 | |
| 236 hosted_registrations_.Remove(sync_id); | |
| 237 } | |
| 238 | |
| 186 void BackgroundSyncServiceImpl::OnRegisterResult( | 239 void BackgroundSyncServiceImpl::OnRegisterResult( |
| 187 const RegisterCallback& callback, | 240 const RegisterCallback& callback, |
| 188 BackgroundSyncStatus status, | 241 BackgroundSyncStatus status, |
| 189 const BackgroundSyncRegistration& result) { | 242 scoped_ptr<BackgroundSyncRegistrationHandle> result) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 SyncRegistrationPtr mojoResult = ToMojoRegistration(result); | 244 BackgroundSyncRegistrationHandle* result_ptr = result.get(); |
| 245 | |
| 246 if (status != BACKGROUND_SYNC_STATUS_OK) { | |
| 247 callback.Run(static_cast<content::BackgroundSyncError>(status), | |
| 248 SyncRegistrationPtr(content::SyncRegistration::New())); | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 hosted_registrations_.AddWithID(result.release(), result_ptr->handle_id()); | |
| 253 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); | |
| 192 callback.Run(static_cast<content::BackgroundSyncError>(status), | 254 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 193 mojoResult.Pass()); | 255 mojoResult.Pass()); |
| 194 } | 256 } |
| 195 | 257 |
| 196 void BackgroundSyncServiceImpl::OnUnregisterResult( | 258 void BackgroundSyncServiceImpl::OnUnregisterResult( |
| 197 const UnregisterCallback& callback, | 259 const UnregisterCallback& callback, |
| 198 BackgroundSyncStatus status) { | 260 BackgroundSyncStatus status) { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 200 callback.Run(static_cast<content::BackgroundSyncError>(status)); | 262 callback.Run(static_cast<content::BackgroundSyncError>(status)); |
| 201 } | 263 } |
| 202 | 264 |
| 203 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( | 265 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
| 204 const GetRegistrationsCallback& callback, | 266 const GetRegistrationsCallback& callback, |
| 205 BackgroundSyncStatus status, | 267 BackgroundSyncStatus status, |
| 206 const std::vector<BackgroundSyncRegistration>& result_registrations) { | 268 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
| 269 result_registrations) { | |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 270 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 208 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); | 271 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); |
| 209 for (const auto& registration : result_registrations) | 272 for (BackgroundSyncRegistrationHandle* registration : *result_registrations) { |
| 210 mojo_registrations.push_back(ToMojoRegistration(registration)); | 273 hosted_registrations_.AddWithID(registration, registration->handle_id()); |
| 274 mojo_registrations.push_back(ToMojoRegistration(*registration)); | |
| 275 } | |
| 276 | |
| 277 result_registrations->weak_clear(); | |
| 278 | |
| 211 callback.Run(static_cast<content::BackgroundSyncError>(status), | 279 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 212 mojo_registrations.Pass()); | 280 mojo_registrations.Pass()); |
| 213 } | 281 } |
| 214 | 282 |
| 215 } // namespace content | 283 } // namespace content |
| OLD | NEW |