ServiceWorkerGlobalScope
[[!service-workers]] with APIs for managing the lifecycle of an
application and associated events.
The extensions to the service worker global execution context defined in this specification allow web developers to author applications that manage the application lifecycle and react to system events. These capabilities allow application developers to create applications that integrate closely with the underlying system.
Using the APIs defined in this specification, an application is able to run application logic independently of any user interface scripts and react to:
There is only one class of product that can claim conformance to this specification: a user agent.
Implementations that use ECMAScript to implement the APIs defined in this specification MUST implement them in a manner consistent with the ECMAScript Bindings defined in the Web IDL specification [[WEBIDL]], as this specification uses that specification and terminology.
This specification relies on the following specifications:
The term JavaScript global environment refers to the global environment concept defined in [[!ECMA-262]].
The EventHandler interface represents a callback used for event handlers as defined in [[!HTML5]].
The concepts queue a task and fire a simple event are defined in [[!HTML5]].
The terms event handlers and event handler event types are defined in [[!HTML5]].
The ServiceWorkerGlobalScope
interface is defined in
[[!service-workers]]
The TaskScheduler
interface is defined in
[[!TASKSCHEDULER]].
Below is a list of use cases derived from the initial input to the System Applications Working Group Charter that were not addressed by the Runtime and Security Model for Web Applications [[SYSAPPS-RUNTIME]] — a proposal the group decided to obsolete due to lack of implementers' interest. Currently, the domain initially covered by [[SYSAPPS-RUNTIME]] is split across multiple specifications and the use cases compatible with the Web security model are being addressed by a set of specifications: Application Lifecycle and Events and its normative dependencies Service Workers [[!service-workers]], Task Scheduler [[!TASKSCHEDULER]], and the Manifest for web applications [[!appmanifest]]. The set of specifications is expected to grow over time to cover more of the domain and use cases.
The main document is the main entry point of the application to the system. When loading the main document, the runtime does not display it to the user. If the application intends to show a user interface it has to create windows or interact with the platform in other ways such as by using a notification system.
The runtime can unload the main document in certain circumstances, which results in termination of the application. When the main document is not executing any script, has no pending callbacks, and no open windows, the runtime can decide to unload the main document. In addition, the runtime unloads the main document in order to reduce resource consumption. For example, after loading the main document and no window is visible, the application can be terminated by the runtime.
This section is non-normative.
The runtime does not create any visible windows by itself when launching an application. This is up to the application and is handled as part of the launch event. When launched, the application will know the reason for the launch. The reason could be a scheduled wake up, a persistent event of interest or a direct launch either by the system, another application, or initiated by the user. This allows the application to act differently depending on the reason.
The runtime starts the application for the purpose of delivering events from the system. For example, a system-level service might ask an email application to send an email on its behalf. To handle these cases, the runtime will listen to system events in order to launch the main document in response to them.
An application subscribes to system events either at install time or at runtime.
This section is non-normative.
An application can schedule itself for wake-up by scheduling a task. A task will ensure that the application is running and that the launch event has been fired at the main document before resolving the the task.
Note that in order to run a task at the scheduled time, the application can be started a bit earlier.
This section is non-normative.
The runtime terminates the application if it becomes idle or in case of resource constraints.
Before actual termination, the terminate event is sent, giving the application the ability to clear up, store state and close windows. In case that the application does not terminate within a given time, the runtime can consider the application as too slow or hanging and has the ability to terminate it immediately (forced termination). The application can save its state periodically to protect against data loss, in such a case.
After the terminate event, or in case of forced termination, the runtime will progress to actual termination, which at least includes closing all remaining windows and unloading of the main document. What other resources the runtime unloads is up to the implementation.
There is an exception as the application can receive a wakeup or a system event while processing the terminate event. If this is the case, the terminate event is followed by a terminate canceled event, as actual termination will not happen.
The runtime will avoid terminating an active, focused application if at all possible. However, the runtime can terminate the application as a last resort (e.g., due to resource exhaustion or bad behavior).
The runtime prevents an application from interfering with the application's termination, e.g. event listeners or long-running scripts using APIs such as Geolocation, setTimeout, XMLHttpRequest will not block the runtime from terminating the application.
This section is non-normative.
The runtime context associated with the main document can be launched and terminated either by the user, another application, or by the system. The system can decide to terminate an application as long as it is considered idle (no visible views or connected message ports). In the case the application received an event (or something similar, like a task was scheduled) while processing the terminate event, the terminate canceled event will be fired successively and the main document will stay active until considered idle again.
When the application is launched, the launch event is fired with a reason which can be of type pending event, scheduled or other. If the application was launched in order to handle an event it subscribed to or in response to a scheduled task, the type will be pending event and scheduled respectively. This allows for the application to avoid creating any user interface not resulting from the respective event handling.
In the case the user or another application started the
application, the reason is set to other
, allowing
the main document to load the default user interface and
potentially a “screenshot” of the application while the
main user interface is being built in the background.
For handling application lifecycle, the application's main document can listen to the events.
This section is non-normative.
A system event is an event sent by the system. This event type does not originate from the DOM itself and thus lives outside of the main document's lifetime. A system event can wake up a terminated application. An implementation can also allow a system event to wake up the system from sleeping.
A typical browser-driven use case for system events is an email application that wants to show a desktop notification when a push notification is received so the user is informed that there are new emails even though the tab in which the email application was running has been closed.
System events are applicable to event types that fire at low frequency and support filtering. For example, they are not appropriate for user interaction events that require a visible user interface or fire frequently.
When an application is launched in response to a system event, its main document is loaded, a launch event is dispatched, and immediately after the launch event, the system event handler is called. If the application did not register the listener as part of the launch event, nothing happens.
This section is non-normative.
Filtered events are a mechanism that allows listeners to specify a subset of events that they are interested in. A listener that makes use of a filter is not invoked for events that do not pass the filter, which makes the listening code more declarative and efficient.
To prevent an application from being woken up for no reason, filtering happens in the runtime and not in an application.
This section is non-normative.
When an application subscribes to an event, it will be subscribed to it until it is unsubscribed, the application is uninstalled, updated or terminated.
The listeners only exist in the context of the main document. Event listeners for system events need to be registered each time the main document is launched after termination.
This section is non-normative.
One or more windows might be created from the main document. These windows are directly scriptable by the main document.
Below is a summary of requirements derived from the above use cases:
An environment through which the interfaces defined in this specification are exposed to JavaScript is referred to as the service worker global execution context (also referred to as the global execution context of a Service Worker in [[!service-workers]]) whose global object is referred to as service worker global scope.
ServiceWorkerGlobalScope
interfaceServiceWorkerGlobalScope
inherits from
WorkerGlobalScope
:
navigator
object
location
object (read-only)
XMLHttpRequest()
method
setTimeout()/clearTimeout()
and
setInterval()/clearInterval()
applicationCache
object
importScripts()
method
Worker()
method (spawning web workers)
indexedDB
object
MessagePort
objects similar to shared workers.
ServiceWorkerGlobalScope [[!service-workers]] has a
clients
attribute, which represents a list of
windows or workers that match the service worker's origin
and scope.
reason
in LaunchEvent
,
terminate
and terminatecanceled
are proposed to be deferred to v2 without strong use cases.
When the application is launched, the user agent MUST queue a task to launch the application.
When the application is terminated, the user agent MUST queue a task to terminate the application.
When the application termination is canceled, the user agent MUST queue a task to cancel the termination.
The following are the event handlers (and their corresponding
event handler event types) that MUST be supported, as event
handler IDL attributes, by all objects implementing the
ServiceWorkerGlobalScope
interface:
event handler | event handler event type |
---|---|
onlaunch
|
launch
|
onterminate
|
terminate
|
onterminatecanceled
|
terminatecanceled
|
When the user agent is REQUIRED to launch the application, the user agent MUST run the following steps:
launch
.
reason
attribute to a value
corresponding to a launch reason as defined in the
table below.
LaunchEvent
object at the ServiceWorkerGlobalScope
object.
reason attribute value
|
Description | Times [fired] | When |
---|---|---|---|
pending-event
|
The application is launched in response to a system event it is listening to. | Zero or more. | |
scheduled
|
The application is launched by the system at the scheduled time. | Zero or more. | |
other
|
Other reason. | Zero or more. |
LaunchEvent
?
When the user agent is REQUIRED to terminate the application, it MUST run the following steps:
ServiceWorkerGlobalScope
object.
onunload
.
When the user agent is REQUIRED to cancel the termination, the user agent MUST run the following steps:
TerminateCanceledEvent
interface, with the name terminatecanceled
.
reason
attribute to a value
corresponding to a terminate cancellation reason as defined in the
table below.
TerminateCanceledEvent
object at the ServiceWorkerGlobalScope
object.
reason attribute value
|
Description | Times [fired] | When |
---|---|---|---|
- | - | - | - |
- | - | - | - |
Thanks to everyone who have contributed to the Service Worker proposal that provides primitives for this specification to build atop.
Some use cases are derived from Adam Barth’s execution model proposal referenced in the System Applications Working Group Charter. Thanks to the Chrome team for their experiments with Packaged Apps. Also special thanks to Thiago Marcos P. Santos and Caio Marcelo de Oliveira Filho for their comments.
Also, big thank you to all SysApps Toronto participants who reviewed the proposal, sent feedback and participated in the task force session.