service worker: Improve async waitUntil/respondWith().

This CL:
* Stops a crash that occurred when respondWith() was called in
  a microtask during event dispatch. The crash occurred because
  RespondWithObserver is expected to determine the response before
  WaitUntilObserver decides the event is finished. RespondWithObserver
  was relying on WaitUntilObserver to respect the respondWith()
  promise, but in some cases WaitUntilObserver would drop the promise
* Improves determination of when an extendable event is active.
  We had tricky logic to consider an event inactive during the
  microtask checkpoint at the end of event dispatch, but according to
  the spec that's still part of event dispatch and the event is still
* Removes some early returns when the execution context is null, which
  made it more unclear when the promises would be observed. It should
  only be necessary to check the execution context when the context is
  about to be accessed.
* Removes clearing of |observer_|. This was already unnecessary due to
  Oilpan gc, and RespondWithObserver now needs |observer_| in some cases
  after reaching state_ kDone, to check observer_->IsDispatchingEvent().

Bug: 941875, 942414
Change-Id: I14f008f7e76ec76d90fe25104cc6f3849808fb2f
Commit-Queue: Matt Falkenhagen <>
Reviewed-by: Kentaro Hara <>
Reviewed-by: Kenichi Ishibashi <>
Cr-Commit-Position: refs/heads/master@{#641520}
8 files changed