Index: content/browser/background_sync/background_sync_manager.cc |
diff --git a/content/browser/background_sync/background_sync_manager.cc b/content/browser/background_sync/background_sync_manager.cc |
index cba1379c82b7809e024d7bd88c5ede09c701fd0a..7c5818472f774ac85d6457e04d3c6c005e1160cd 100644 |
--- a/content/browser/background_sync/background_sync_manager.cc |
+++ b/content/browser/background_sync/background_sync_manager.cc |
@@ -703,7 +703,7 @@ void BackgroundSyncManager::UnregisterImpl( |
return; |
} |
- const RefCountedRegistration* existing_registration = |
+ RefCountedRegistration* existing_registration = |
LookupActiveRegistration(sw_registration_id, registration_key); |
if (!existing_registration || |
@@ -715,6 +715,19 @@ void BackgroundSyncManager::UnregisterImpl( |
return; |
} |
+ bool firing = existing_registration->value()->sync_state() == |
+ BACKGROUND_SYNC_STATE_FIRING; |
+ |
+ existing_registration->value()->set_sync_state( |
+ BACKGROUND_SYNC_STATE_UNREGISTERED); |
+ |
+ if (!firing) { |
+ // If the registration is currently firing then wait to run |
+ // RunDoneCallbacks until after it has finished as it might |
+ // change state to SUCCESS first. |
+ existing_registration->value()->RunDoneCallbacks(); |
+ } |
+ |
RemoveActiveRegistration(sw_registration_id, registration_key); |
StoreRegistrations(sw_registration_id, |
@@ -754,6 +767,69 @@ void BackgroundSyncManager::UnregisterDidStore(int64 sw_registration_id, |
FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK)); |
} |
+void BackgroundSyncManager::NotifyWhenDone( |
+ BackgroundSyncRegistrationHandle::HandleId handle_id, |
+ const StatusAndStateCallback& callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ if (disabled_) { |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
+ BACKGROUND_SYNC_STATE_FAILED)); |
+ return; |
+ } |
+ |
+ scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
+ DuplicateRegistrationHandle(handle_id); |
+ |
+ op_scheduler_.ScheduleOperation( |
+ base::Bind(&BackgroundSyncManager::NotifyWhenDoneImpl, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ base::Passed(registration_handle.Pass()), callback)); |
+} |
+ |
+void BackgroundSyncManager::NotifyWhenDoneImpl( |
+ scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle, |
+ const StatusAndStateCallback& callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ if (disabled_) { |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
+ BACKGROUND_SYNC_STATE_FAILED)); |
+ return; |
+ } |
+ |
+ if (registration_handle->sync_state() == BACKGROUND_SYNC_STATE_PENDING || |
+ registration_handle->sync_state() == BACKGROUND_SYNC_STATE_FIRING) { |
+ registration_handle->registration()->AddDoneCallback( |
+ base::Bind(&BackgroundSyncManager::NotifyWhenDoneDidFinish, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+ op_scheduler_.CompleteOperationAndRunNext(); |
+ return; |
+ } |
+ |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_OK, |
+ registration_handle->sync_state())); |
+ op_scheduler_.CompleteOperationAndRunNext(); |
+} |
+ |
+void BackgroundSyncManager::NotifyWhenDoneDidFinish( |
+ const StatusAndStateCallback& callback, |
+ BackgroundSyncState sync_state) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ if (disabled_) { |
+ base::ThreadTaskRunnerHandle::Get()->PostTask( |
+ FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR, |
+ BACKGROUND_SYNC_STATE_FAILED)); |
+ return; |
+ } |
+ |
+ callback.Run(BACKGROUND_SYNC_STATUS_OK, sync_state); |
+} |
+ |
void BackgroundSyncManager::GetRegistrationImpl( |
int64 sw_registration_id, |
const RegistrationKey& registration_key, |
@@ -845,7 +921,7 @@ void BackgroundSyncManager::SchedulePendingRegistrations() { |
sw_id_and_registrations.second.registration_map) { |
const BackgroundSyncRegistration& registration = |
*key_and_registration.second->value(); |
- if (registration.sync_state() == SYNC_STATE_PENDING) { |
+ if (registration.sync_state() == BACKGROUND_SYNC_STATE_PENDING) { |
if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
keep_browser_alive_for_one_shot = true; |
} else { |
@@ -1025,10 +1101,19 @@ void BackgroundSyncManager::EventCompleteImpl( |
if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
if (status_code != SERVICE_WORKER_OK) { |
- // TODO(jkarlin) Fire the sync event on the next page load controlled by |
+ // TODO(jkarlin): Insert retry logic here. Be sure to check if the state |
+ // is unregistered first. If unregistered then set the state to failed |
+ // if it was already out of retry attempts otherwise keep the state as |
iclelland
2015/09/17 13:48:09
Add link to bug? crbug.com/501838
jkarlin
2015/09/17 17:40:24
Done.
|
+ // unregistered. Then call RunDoneCallbacks(); |
+ // TODO(jkarlin): Fire the sync event on the next page load controlled |
+ // by |
// this registration. (crbug.com/479665) |
registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); |
+ registration->RunDoneCallbacks(); |
} else { |
+ registration->set_sync_state(BACKGROUND_SYNC_STATE_SUCCESS); |
+ registration->RunDoneCallbacks(); |
+ |
RegistrationKey key(*registration); |
// Remove the registration if it's still active. |
RefCountedRegistration* active_registration = |