summaryrefslogtreecommitdiffstats
path: root/content/browser/background_sync/background_sync_manager.cc
diff options
context:
space:
mode:
authorjkarlin <jkarlin@chromium.org>2015-09-18 06:55:16 -0700
committerCommit bot <commit-bot@chromium.org>2015-09-18 13:55:48 +0000
commit6da0b6f3b2159374954c9e77b5f5f468f901a0cb (patch)
tree3b5abef79918460e668ff4587c7a9f6493a83202 /content/browser/background_sync/background_sync_manager.cc
parent87345d917e3a411f343cd90e6129b8824f5512a1 (diff)
downloadchromium_src-6da0b6f3b2159374954c9e77b5f5f468f901a0cb.zip
chromium_src-6da0b6f3b2159374954c9e77b5f5f468f901a0cb.tar.gz
chromium_src-6da0b6f3b2159374954c9e77b5f5f468f901a0cb.tar.bz2
[BackgroundSync] Add browser side support for SyncRegistration.done
This CL adds support for the "done" promise of background sync registrations. See http://slightlyoff.github.io/BackgroundSync/spec/index.html for more detail. Done resolves with true if the sync event fired successfully and false if it failed or is unregistered. Changes: 1. Add and test BackgroundSyncManager::NotifyWhenDone. 2. Plumb NotifyWhenDone to the child process. 3. Test with BrowserTests that done can be called before or after a registration completes. Depends on: https://codereview.chromium.org/1353613002/ https://codereview.chromium.org/1348603003/ BUG=502214 Review URL: https://codereview.chromium.org/1344843003 Cr-Commit-Position: refs/heads/master@{#349660}
Diffstat (limited to 'content/browser/background_sync/background_sync_manager.cc')
-rw-r--r--content/browser/background_sync/background_sync_manager.cc94
1 files changed, 92 insertions, 2 deletions
diff --git a/content/browser/background_sync/background_sync_manager.cc b/content/browser/background_sync/background_sync_manager.cc
index 938a87b..3e28bf3 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,24 @@ void BackgroundSyncManager::UnregisterImpl(
return;
}
+ DCHECK(!existing_registration->value()->HasCompleted());
+
+ bool firing = existing_registration->value()->sync_state() ==
+ BACKGROUND_SYNC_STATE_FIRING ||
+ existing_registration->value()->sync_state() ==
+ BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING;
+
+ existing_registration->value()->set_sync_state(
+ firing ? BACKGROUND_SYNC_STATE_UNREGISTERED_WHILE_FIRING
+ : 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 +772,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);
+ DCHECK_EQ(SYNC_ONE_SHOT, registration_handle->options()->periodicity);
+
+ if (disabled_) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE, base::Bind(callback, BACKGROUND_SYNC_STATUS_STORAGE_ERROR,
+ BACKGROUND_SYNC_STATE_FAILED));
+ return;
+ }
+
+ if (!registration_handle->registration()->HasCompleted()) {
+ 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,
@@ -1025,10 +1106,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_WHILE_FIRING first. If so then set the state to failed
+ // if it was already out of retry attempts otherwise keep the state as
+ // unregistered. Then call RunDoneCallbacks(); (crbug.com/501838)
+ // 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 =