Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1189644 - Update waitUntil() and activation to spec. r=catalinb
Browse files Browse the repository at this point in the history
waitUntil() has been updated to accept multiple calls and concatenate them into Promise.all().
activation does not fail any more even if the promise(s) passed to waitUntil() reject.

Update web-platform-tests expected data
  • Loading branch information
nikhilm committed Aug 24, 2015
1 parent fd7bba1 commit ceac74f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
23 changes: 19 additions & 4 deletions dom/workers/ServiceWorkerEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,25 @@ ExtendableEvent::WaitUntil(Promise& aPromise, ErrorResult& aRv)
return;
}

// Only first caller counts.
if (EventPhase() == AT_TARGET && !mPromise) {
mPromise = &aPromise;
mPromises.AppendElement(&aPromise);
}

already_AddRefed<Promise>
ExtendableEvent::GetPromise()
{
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(worker);
worker->AssertIsOnWorkerThread();

GlobalObject global(worker->GetJSContext(), worker->GlobalScope()->GetGlobalJSObject());

ErrorResult result;
nsRefPtr<Promise> p = Promise::All(global, Move(mPromises), result);
if (NS_WARN_IF(result.Failed())) {
return nullptr;
}

return p.forget();
}

NS_IMPL_ADDREF_INHERITED(ExtendableEvent, Event)
Expand All @@ -416,7 +431,7 @@ NS_IMPL_RELEASE_INHERITED(ExtendableEvent, Event)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ExtendableEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)

NS_IMPL_CYCLE_COLLECTION_INHERITED(ExtendableEvent, Event, mPromise)
NS_IMPL_CYCLE_COLLECTION_INHERITED(ExtendableEvent, Event, mPromises)

#ifndef MOZ_SIMPLEPUSH

Expand Down
8 changes: 2 additions & 6 deletions dom/workers/ServiceWorkerEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class FetchEvent final : public Event

class ExtendableEvent : public Event
{
nsRefPtr<Promise> mPromise;
nsTArray<nsRefPtr<Promise>> mPromises;

protected:
explicit ExtendableEvent(mozilla::dom::EventTarget* aOwner);
Expand Down Expand Up @@ -144,11 +144,7 @@ class ExtendableEvent : public Event
WaitUntil(Promise& aPromise, ErrorResult& aRv);

already_AddRefed<Promise>
GetPromise() const
{
nsRefPtr<Promise> p = mPromise;
return p.forget();
}
GetPromise();

virtual ExtendableEvent* AsExtendableEvent() override
{
Expand Down
12 changes: 4 additions & 8 deletions dom/workers/ServiceWorkerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2980,14 +2980,10 @@ ServiceWorkerRegistrationInfo::FinishActivate(bool aSuccess)
return;
}

if (aSuccess) {
mActiveWorker->UpdateState(ServiceWorkerState::Activated);
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
swm->StoreRegistration(mPrincipal, this);
} else {
mActiveWorker->UpdateState(ServiceWorkerState::Redundant);
mActiveWorker = nullptr;
}
// Activation never fails, so aSuccess is ignored.
mActiveWorker->UpdateState(ServiceWorkerState::Activated);
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
swm->StoreRegistration(mPrincipal, this);
}

NS_IMETHODIMP
Expand Down
36 changes: 27 additions & 9 deletions dom/workers/test/serviceworkers/test_install_event.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,40 @@
});
}

function activateError() {
function testActive(worker) {
is(worker.state, "activating", "Should be activating");
return new Promise(function(resolve, reject) {
worker.onstatechange = function(e) {
e.target.onstatechange = null;
is(e.target.state, "activated", "Activation of worker with error in activate event handler should still succeed.");
resolve();
}
});
}

function activateErrorShouldSucceed() {
// Silence worker errors so they don't cause the test to fail.
window.onerror = function() { }
return navigator.serviceWorker.register("activate_event_error_worker.js", { scope: "./activate_error" })
.then(function(swr) {
return new Promise(function(resolve, reject) {
ok(swr.installing.state == "installing", "activateError(): Installing worker's state should be 'installing'");
var p = new Promise(function(resolve, reject) {
ok(swr.installing.state == "installing", "activateErrorShouldSucceed(): Installing worker's state should be 'installing'");
swr.installing.onstatechange = function(e) {
ok(swr.active, "transition to active successfully");
is(swr.active.state, "activating", "should be activating");
swr.active.onstatechange = function(e) {
is(e.target.state, "redundant", "Activation of worker with error in activate event handler should fail.");
resolve(swr);
e.target.onstatechange = null;
if (swr.waiting) {
swr.waiting.onstatechange = function(e) {
e.target.onstatechange = null;
testActive(swr.active).then(resolve, reject);
}
} else {
testActive(swr.active).then(resolve, reject);
}
}
});

return p.then(function() {
return Promise.resolve(swr);
});
}).then(function(swr) {
return swr.unregister();
});
Expand All @@ -90,7 +108,7 @@
.then(simpleRegister)
.then(nextRegister)
.then(installError)
.then(activateError)
.then(activateErrorShouldSucceed)
.then(unregister)
.then(function() {
SimpleTest.finish();
Expand Down

This file was deleted.

0 comments on commit ceac74f

Please sign in to comment.