| 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->handle_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 DCHECK(background_sync_context_->background_sync_manager()); |
| 91 } | 94 } |
| 92 | 95 |
| 93 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( | 96 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
| 94 BackgroundSyncContextImpl* background_sync_context, | 97 BackgroundSyncContextImpl* background_sync_context, |
| 95 mojo::InterfaceRequest<BackgroundSyncService> request) | 98 mojo::InterfaceRequest<BackgroundSyncService> request) |
| 96 : background_sync_context_(background_sync_context), | 99 : background_sync_context_(background_sync_context), |
| 97 binding_(this, request.Pass()), | 100 binding_(this, request.Pass()), |
| 98 weak_ptr_factory_(this) { | 101 weak_ptr_factory_(this) { |
| 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 100 DCHECK(background_sync_context); | 103 DCHECK(background_sync_context); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 122 background_sync_context_->background_sync_manager(); | 125 background_sync_context_->background_sync_manager(); |
| 123 DCHECK(background_sync_manager); | 126 DCHECK(background_sync_manager); |
| 124 background_sync_manager->Register( | 127 background_sync_manager->Register( |
| 125 sw_registration_id, mgr_options, requested_from_service_worker, | 128 sw_registration_id, mgr_options, requested_from_service_worker, |
| 126 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 129 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
| 127 weak_ptr_factory_.GetWeakPtr(), callback)); | 130 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 128 } | 131 } |
| 129 | 132 |
| 130 void BackgroundSyncServiceImpl::Unregister( | 133 void BackgroundSyncServiceImpl::Unregister( |
| 131 BackgroundSyncPeriodicity periodicity, | 134 BackgroundSyncPeriodicity periodicity, |
| 132 int64_t id, | 135 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 133 const mojo::String& tag, | |
| 134 int64_t sw_registration_id, | 136 int64_t sw_registration_id, |
| 135 const UnregisterCallback& callback) { | 137 const UnregisterCallback& callback) { |
| 136 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 137 BackgroundSyncManager* background_sync_manager = | 139 BackgroundSyncManager* background_sync_manager = |
| 138 background_sync_context_->background_sync_manager(); | 140 background_sync_context_->background_sync_manager(); |
| 139 DCHECK(background_sync_manager); | 141 DCHECK(background_sync_manager); |
| 140 background_sync_manager->Unregister( | 142 |
| 141 sw_registration_id, tag, content::SYNC_ONE_SHOT, id, | 143 BackgroundSyncRegistrationHandle* registration = |
| 144 active_handles_.Lookup(handle_id); |
| 145 if (!registration) { |
| 146 callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED); |
| 147 return; |
| 148 } |
| 149 |
| 150 registration->Unregister( |
| 151 sw_registration_id, |
| 142 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, | 152 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
| 143 weak_ptr_factory_.GetWeakPtr(), callback)); | 153 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 144 } | 154 } |
| 145 | 155 |
| 146 void BackgroundSyncServiceImpl::GetRegistration( | 156 void BackgroundSyncServiceImpl::GetRegistration( |
| 147 BackgroundSyncPeriodicity periodicity, | 157 BackgroundSyncPeriodicity periodicity, |
| 148 const mojo::String& tag, | 158 const mojo::String& tag, |
| 149 int64_t sw_registration_id, | 159 int64_t sw_registration_id, |
| 150 const GetRegistrationCallback& callback) { | 160 const GetRegistrationCallback& callback) { |
| 151 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 177 int64_t sw_registration_id, | 187 int64_t sw_registration_id, |
| 178 const GetPermissionStatusCallback& callback) { | 188 const GetPermissionStatusCallback& callback) { |
| 179 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 180 | 190 |
| 181 // TODO(iclelland): Implement a real policy. This is a stub implementation. | 191 // TODO(iclelland): Implement a real policy. This is a stub implementation. |
| 182 // OneShot: crbug.com/482091 | 192 // OneShot: crbug.com/482091 |
| 183 // Periodic: crbug.com/482093 | 193 // Periodic: crbug.com/482093 |
| 184 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); | 194 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
| 185 } | 195 } |
| 186 | 196 |
| 197 void BackgroundSyncServiceImpl::DuplicateRegistrationHandle( |
| 198 BackgroundSyncRegistrationHandle::HandleId handle_id, |
| 199 const DuplicateRegistrationHandleCallback& callback) { |
| 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 201 BackgroundSyncManager* background_sync_manager = |
| 202 background_sync_context_->background_sync_manager(); |
| 203 DCHECK(background_sync_manager); |
| 204 |
| 205 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
| 206 background_sync_manager->DuplicateRegistrationHandle(handle_id); |
| 207 |
| 208 BackgroundSyncRegistrationHandle* handle_ptr = registration_handle.get(); |
| 209 |
| 210 if (!registration_handle) { |
| 211 callback.Run(BACKGROUND_SYNC_ERROR_NOT_FOUND, |
| 212 SyncRegistrationPtr(content::SyncRegistration::New())); |
| 213 return; |
| 214 } |
| 215 |
| 216 active_handles_.AddWithID(registration_handle.release(), |
| 217 handle_ptr->handle_id()); |
| 218 SyncRegistrationPtr mojoResult = ToMojoRegistration(*handle_ptr); |
| 219 callback.Run(BACKGROUND_SYNC_ERROR_NONE, mojoResult.Pass()); |
| 220 } |
| 221 |
| 222 void BackgroundSyncServiceImpl::ReleaseRegistration( |
| 223 BackgroundSyncRegistrationHandle::HandleId handle_id) { |
| 224 if (!active_handles_.Lookup(handle_id)) { |
| 225 // TODO(jkarlin): Abort client. |
| 226 LOG(WARNING) << "Client attempted to release non-existing registration"; |
| 227 return; |
| 228 } |
| 229 |
| 230 active_handles_.Remove(handle_id); |
| 231 } |
| 232 |
| 187 void BackgroundSyncServiceImpl::OnRegisterResult( | 233 void BackgroundSyncServiceImpl::OnRegisterResult( |
| 188 const RegisterCallback& callback, | 234 const RegisterCallback& callback, |
| 189 BackgroundSyncStatus status, | 235 BackgroundSyncStatus status, |
| 190 const BackgroundSyncRegistration& result) { | 236 scoped_ptr<BackgroundSyncRegistrationHandle> result) { |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 237 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 192 SyncRegistrationPtr mojoResult = ToMojoRegistration(result); | 238 BackgroundSyncRegistrationHandle* result_ptr = result.get(); |
| 239 |
| 240 if (status != BACKGROUND_SYNC_STATUS_OK) { |
| 241 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 242 SyncRegistrationPtr(content::SyncRegistration::New())); |
| 243 return; |
| 244 } |
| 245 |
| 246 active_handles_.AddWithID(result.release(), result_ptr->handle_id()); |
| 247 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); |
| 193 callback.Run(static_cast<content::BackgroundSyncError>(status), | 248 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 194 mojoResult.Pass()); | 249 mojoResult.Pass()); |
| 195 } | 250 } |
| 196 | 251 |
| 197 void BackgroundSyncServiceImpl::OnUnregisterResult( | 252 void BackgroundSyncServiceImpl::OnUnregisterResult( |
| 198 const UnregisterCallback& callback, | 253 const UnregisterCallback& callback, |
| 199 BackgroundSyncStatus status) { | 254 BackgroundSyncStatus status) { |
| 200 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 255 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 201 callback.Run(static_cast<content::BackgroundSyncError>(status)); | 256 callback.Run(static_cast<content::BackgroundSyncError>(status)); |
| 202 } | 257 } |
| 203 | 258 |
| 204 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( | 259 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
| 205 const GetRegistrationsCallback& callback, | 260 const GetRegistrationsCallback& callback, |
| 206 BackgroundSyncStatus status, | 261 BackgroundSyncStatus status, |
| 207 const std::vector<BackgroundSyncRegistration>& result_registrations) { | 262 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
| 263 result_registrations) { |
| 208 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 264 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 209 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); | 265 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); |
| 210 for (const auto& registration : result_registrations) | 266 for (BackgroundSyncRegistrationHandle* registration : *result_registrations) { |
| 211 mojo_registrations.push_back(ToMojoRegistration(registration)); | 267 active_handles_.AddWithID(registration, registration->handle_id()); |
| 268 mojo_registrations.push_back(ToMojoRegistration(*registration)); |
| 269 } |
| 270 |
| 271 result_registrations->weak_clear(); |
| 272 |
| 212 callback.Run(static_cast<content::BackgroundSyncError>(status), | 273 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 213 mojo_registrations.Pass()); | 274 mojo_registrations.Pass()); |
| 214 } | 275 } |
| 215 | 276 |
| 216 } // namespace content | 277 } // namespace content |
| OLD | NEW |