Chromium Code Reviews| Index: content/browser/background_sync/background_sync_service_impl.cc |
| diff --git a/content/browser/background_sync/background_sync_service_impl.cc b/content/browser/background_sync/background_sync_service_impl.cc |
| index 7c9507f685a8ee3188ec7d2262d93949c20e2ed8..dcb9fb46c63da4b4239934d73d8148952bf48a63 100644 |
| --- a/content/browser/background_sync/background_sync_service_impl.cc |
| +++ b/content/browser/background_sync/background_sync_service_impl.cc |
| @@ -4,9 +4,10 @@ |
| #include "content/browser/background_sync/background_sync_service_impl.h" |
| +#include "background_sync_registration_handle.h" |
| #include "base/memory/weak_ptr.h" |
| +#include "base/stl_util.h" |
| #include "content/browser/background_sync/background_sync_context_impl.h" |
| -#include "content/browser/background_sync/background_sync_registration.h" |
| #include "content/public/browser/browser_thread.h" |
| namespace content { |
| @@ -28,9 +29,10 @@ BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
| return out; |
| } |
| -SyncRegistrationPtr ToMojoRegistration(const BackgroundSyncRegistration& in) { |
| +SyncRegistrationPtr ToMojoRegistration( |
| + const BackgroundSyncRegistrationHandle& in) { |
| SyncRegistrationPtr out(content::SyncRegistration::New()); |
| - out->id = in.id(); |
| + out->id = in.handle_id(); |
| out->tag = in.options()->tag; |
| out->min_period_ms = in.options()->min_period; |
| out->periodicity = static_cast<content::BackgroundSyncPeriodicity>( |
| @@ -88,6 +90,9 @@ COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, |
| BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BackgroundSyncManager* background_sync_manager = |
| + background_sync_context_->background_sync_manager(); |
| + 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.
|
| } |
| BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
| @@ -129,15 +134,22 @@ void BackgroundSyncServiceImpl::Register(content::SyncRegistrationPtr options, |
| void BackgroundSyncServiceImpl::Unregister( |
| BackgroundSyncPeriodicity periodicity, |
| int64_t id, |
| - const mojo::String& tag, |
| int64_t sw_registration_id, |
| const UnregisterCallback& callback) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| BackgroundSyncManager* background_sync_manager = |
| background_sync_context_->background_sync_manager(); |
| DCHECK(background_sync_manager); |
| - background_sync_manager->Unregister( |
| - sw_registration_id, tag, content::SYNC_ONE_SHOT, id, |
| + |
| + BackgroundSyncRegistrationHandle* registration = |
| + hosted_registrations_.Lookup(id); |
| + if (!registration) { |
| + callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED); |
| + return; |
| + } |
| + |
| + registration->Unregister( |
| + sw_registration_id, |
| base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
| weak_ptr_factory_.GetWeakPtr(), callback)); |
| } |
| @@ -183,12 +195,62 @@ void BackgroundSyncServiceImpl::GetPermissionStatus( |
| callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
| } |
| +void BackgroundSyncServiceImpl::TrackRegistration( |
| + SyncRegistrationPtr sync_registration) { |
| + // TODO(jkarlin): We dont need the whole SyncRegistrationPtr here, just the id |
| + // would do. |
| + |
| + // Registrations that the client acquires without the |
| + // BackgroundSyncServiceImpl's knowing are registered here. |
| + BackgroundSyncManager* background_sync_manager = |
| + background_sync_context_->background_sync_manager(); |
| + DCHECK(background_sync_manager); |
| + |
| + 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.
|
| + // TODO(jkarlin): Abort client. |
| + LOG(WARNING) << "Client attempted to track an already tracked registration"; |
| + return; |
| + } |
| + |
| + 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.
|
| + new BackgroundSyncRegistrationHandle(background_sync_manager, |
| + sync_registration->id)); |
| + |
| + if (!registration->IsValid()) { |
| + // TODO(jkarlin): Abort client. |
| + LOG(WARNING) << "Client attempted to track a non-existent registration"; |
| + return; |
| + } |
| + |
| + hosted_registrations_.AddWithID(registration.release(), |
| + sync_registration->id); |
| +} |
| + |
| +void BackgroundSyncServiceImpl::ReleaseRegistration(int64_t sync_id) { |
| + if (!hosted_registrations_.Lookup(sync_id)) { |
| + // TODO(jkarlin): Abort client. |
| + LOG(WARNING) << "Client attempted to release non-existing registration"; |
| + return; |
| + } |
| + |
| + hosted_registrations_.Remove(sync_id); |
| +} |
| + |
| void BackgroundSyncServiceImpl::OnRegisterResult( |
| const RegisterCallback& callback, |
| BackgroundSyncStatus status, |
| - const BackgroundSyncRegistration& result) { |
| + scoped_ptr<BackgroundSyncRegistrationHandle> result) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - SyncRegistrationPtr mojoResult = ToMojoRegistration(result); |
| + BackgroundSyncRegistrationHandle* result_ptr = result.get(); |
| + |
| + if (status != BACKGROUND_SYNC_STATUS_OK) { |
| + callback.Run(static_cast<content::BackgroundSyncError>(status), |
| + SyncRegistrationPtr(content::SyncRegistration::New())); |
| + return; |
| + } |
| + |
| + hosted_registrations_.AddWithID(result.release(), result_ptr->handle_id()); |
| + SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); |
| callback.Run(static_cast<content::BackgroundSyncError>(status), |
| mojoResult.Pass()); |
| } |
| @@ -203,11 +265,17 @@ void BackgroundSyncServiceImpl::OnUnregisterResult( |
| void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
| const GetRegistrationsCallback& callback, |
| BackgroundSyncStatus status, |
| - const std::vector<BackgroundSyncRegistration>& result_registrations) { |
| + scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
| + result_registrations) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); |
| - for (const auto& registration : result_registrations) |
| - mojo_registrations.push_back(ToMojoRegistration(registration)); |
| + for (BackgroundSyncRegistrationHandle* registration : *result_registrations) { |
| + hosted_registrations_.AddWithID(registration, registration->handle_id()); |
| + mojo_registrations.push_back(ToMojoRegistration(*registration)); |
| + } |
| + |
| + result_registrations->weak_clear(); |
| + |
| callback.Run(static_cast<content::BackgroundSyncError>(status), |
| mojo_registrations.Pass()); |
| } |