Copyright © 2019 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and permissive document license rules apply.
This document specifies an API that allows web applications to request a wake lock. A wake lock prevents some aspect of the device from entering a power-saving state (e.g., preventing the system from turning off the screen).
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
Implementors need to be aware that this specification is extremely unstable. Implementors who are not taking part in the discussions will find the specification changing out from under them in incompatible ways. Vendors interested in implementing this specification before it eventually reaches the Candidate Recommendation phase should subscribe to the repository on GitHub and take part in the discussions.
This document was published by the Device APIs Working Group as an Editor's Draft.
GitHub Issues are preferred for discussion of this specification. Alternatively, you can send comments to our mailing list. Please send them to public-device-apis@w3.org (archives).
Please see the Working Group's implementation report.
Publication as an Editor's Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
This document is governed by the 1 February 2018 W3C Process Document.
A wake lock prevents some aspect of the device or operating system from entering a power-saving state.
This specification defines two wake lock types:
A user agent can deny wake lock of a particular type for a particular Document by any implementation-specific reason, for example, a user or platform setting or preference.
The wake lock feature is a policy-controlled feature
with feature name wake-lock and default
allowlist ["self"].
The default allowlist of ["self"] allows wake
lock usage in same-origin nested frames but prevents third-party
content from using wake locks.
Third-party usage can be selectively enabled by adding
allow="wake-lock" attribute to the frame container
element:
<iframe src="https://third-party.com" allow="wake-lock"/></iframe>
Alternatively, the wake lock feature can be disabled completely by specifying the feature policy in a HTTP response header:
Feature-Policy: {"wake-lock": []}
See [FEATURE-POLICY] for more details.
WakeLockType enum
For the purpose of wake lock type description, this specification defines the following enumeration:
enum WakeLockType { "screen", "system" };screen
system
The relevant settings object of the active document of a browsing context has an associated wake lock state record with the following internal slots:
| Internal Slot | Initial value | Description (non-normative) |
|---|---|---|
| [[RequestCounter]] | A set with entries for each wake lock type with the values equal to 0. | The values indicates the amount of current requests per wake lock type. A number greater than zero indicates an outstanding wake lock request |
WakeLock interface
The WakeLock interface allows the page to request wake locks of
a particular type, to determine the current wake lock state and to
receive notifications when the wake lock state is changed.
[SecureContext, Exposed=Window] interface WakeLock : EventTarget {
readonly attribute WakeLockType type;
readonly attribute boolean active;
attribute EventHandler onactivechange;
void request(optional WakeLockRequestOptions options);
};type
WakeLock object.
active
onactivechange
activechange. Fired when current wake lock
status indicated by the active
attribute changes.
To create and
initialize a WakeLock object of type type, the
following steps MUST be performed:
WakeLock object.
true, otherwise to false.
WakeLockRequestOptions dictionarydictionary WakeLockRequestOptions {
AbortSignal? signal;
};
The WakeLockRequestOptions.signal property allows to abort
any wake lock request.
The request() method, when invoked, MUST run the
following algorithm:
null otherwise.
null, then
add the following abort steps to signal:
this.
this's type is
"screen", run the following steps:
this.
This section applies to each wake lock type equally and independently, unless a particular wake lock type is explicitly mentioned.
The user agent acquires the wake lock by requesting the underlying operating system to apply the lock. The lock is considered acquired only when the request to the operating system succeeds.
Conversely, the user agent releases the wake lock by requesting the underlying operating system to no longer apply the wake lock. The lock is considered released only when the request to the operating system succeeds.
The wake lock is applicable if the state of the operating system permits application of the lock (e.g. there is sufficient battery charge).
The screen wake lock MUST NOT be applicable after the screen is manually swiched off by the user until it is switched on again. Manually switching off the screen MUST NOT affect the applicability of the system wake lock.
WakeLock object
lock, run these steps in parallel:
false.
true
if the operation succeeded, or else false.
"activechange" at lock.
WakeLock object
lock, run these steps in parallel:
true
if the operation succeeded, or else false.
"activechange" at lock.
"screen" run the following:
Application of a wake lock causes various device components such as display or CPU to operate at higher power levels than they otherwise would. This can lead to undesirable and potentially dangerous effects such as excessive heating and faster than normal battery charge depletion. The latter is particularly relevant to mobile devices which may not have a stationary power source readily available. Complete battery depletion at an unexpected time can lead to inability of the user to make or receive calls and use network services, including the emergency call service. Implementations should consider preventing wake lock application if they determine that the remaining battery capacity is low.
The ability to observe the global state of a wake lock can create a
communication channel between two otherwise isolated documents. One document can request wake lock which
changes the global wake lock state, and another document can observe
this change by subscribing to events in WakeLock.
When the user agent does not acquire wake lock even though a browsing context has requested it, this can be observed by the browsing context and can possibly disclose sensitive information about the state of the device such as that battery level is low.
This section is non-normative.
This example acquires a screen wake lock and releases it after a while:
async function tryKeepScreenAlive(minutes) {
const lock = await navigator.getWakeLock("screen");
const controller = new AbortController();
const signal = controller.signal;
lock.request({ signal });
setTimeout(() => controller.abort(), minutes * 60 * 1000);
}
tryKeepScreenAlive(10);
This example requests a screen wake lock and listens to wake lock state changes:
const controller = new AbortController();
const signal = controller.signal;
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
document.body.appendChild(checkbox);
navigator.getWakeLock("screen").then(lock => {
lock.onactivechange = () => {
checkbox.checked = lock.active;
};
lock.request({ signal });
});
setTimeout(() => controller.abort(), 5 * 1000);
In this example, two screen wake lock requests are created and cancelled independently:
const controller = new AbortController();
const signal = controller.signal;
const p1 = navigator.getWakeLock("screen");
const p2 = navigator.getWakeLock("screen");
p1.then(lock => lock.request({ signal }));
// ...somewhere else
p2.then(lock => lock.request({ signal }));
// ...somewhere else
controller.abort();
browsing context,
allowed to use
Document
Navigator
queue a task
event handler
event handler event type
in parallel
relevant settings object and
active document
are defined in [HTML].
fire an event, AbortSignal, aborted flag, and add the following abort steps are defined in [DOM].
AbortError,
NotSupportedError,
DOMException,
Secure context,
SecurityError and
available only in
secure context
are defined in [WEBIDL].
responsible document and current settings object are defined in [HTML].
set, exists and empty are defined in [INFRA].
Promise and
internal slots
are defined in [ECMASCRIPT].
Document's hidden attribute is defined in [PAGE-VISIBILITY]:
policy-controlled feature, feature name, feature policy and default allowlist are defined in [FEATURE-POLICY]:
This section is non-normative.
The use cases and requirements are documented in [WAKE-LOCK-USE-CASES].
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words MUST and MUST NOT are to be interpreted as described in [RFC2119].
This specification defines conformance criteria for a single product: a user agent that implements the interfaces that it contains.
The user agent MUST implement the APIs defined in this specification in a manner that conforms to the ECMAScript Bindings defined in [WEBIDL].
enumWakeLockType{ "screen", "system" }; partial interfaceNavigator{ [SecureContext] Promise<WakeLock>getWakeLock(WakeLockTypetype); }; [SecureContext, Exposed=Window] interfaceWakeLock: EventTarget { readonly attributeWakeLockTypetype; readonly attribute booleanactive; attribute EventHandleronactivechange; voidrequest(optionalWakeLockRequestOptionsoptions); }; dictionaryWakeLockRequestOptions{ AbortSignal? signal; };
This section is non-normative.
We would like to offer our sincere thanks to Mounir Lamouri, Sergey Konstantinov, Matvey Larionov, Dominique Hazael-Massieux (via the HTML5Apps project) for their contributions to this work.