Copyright © 2024 World Wide Web Consortium. W3C® liability, trademark and permissive document license rules apply.
This document specifies an API that allows web applications to request a screen wake lock. Under the right conditions, and if allowed, the screen wake lock prevents the system from turning off a device's screen.
This section describes the status of this document at the time of its publication. 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 Devices and Sensors Working Group and the Web Applications Working Group as a Working Draft using the Recommendation track.
Publication as a Working Draft does not imply endorsement by W3C and its Members.
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 groups operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures (Devices and Sensors Working Group) and a public list of any patent disclosures (Web Applications Working Group) made in connection with the deliverables of each group; these pages also include 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 03 November 2023 W3C Process Document.
This section is non-normative.
Modern operating systems achieve longer battery life by implementing aggressive power management, meaning that shortly after the lack of user activity, a host device may lower the screen brightness, turn the screen off and even let the CPU go into a deep power state, limiting power usage as much as possible.
Though this is great for prolonged battery life, it can sometime hinder some use cases such as scanning a barcode, reading an ebook, following a recipe, presenting to an audience, and so on. See also Wake Lock: Use cases.
A wake lock will generally prevent something from happening, but UAs (and the underlying OS) may time limit a wake lock given the battery status (wall power connected, discharging, low battery level), or even disallow wake locks in the case a power saving mode is activated.
This specification defines the following wake lock type:
In the API, the wake lock types are represented by the
WakeLockType
enum values.
Other specifications might define different wake lock types.
The Screen Wake Lock API defines a policy-controlled feature
identified by the string "screen-wake-lock"
. Its default allowlist is 'self'
.
The [PERMISSIONS] API provides a uniform way for websites to request permissions from users and query which permissions they have.
A user agent can deny a wake lock of a
particular wake lock type for a particular Document
by any
implementation-specific reason, such as platform setting or user
preference.
It is RECOMMENDED that a user agent show some form of unobtrusive notification that informs the user when a wake lock is active, as well as provides the user with the means to block the ongoing operation, or simply dismiss the notification.
The "screen-wake-lock"
powerful feature enables the
capability defined by this specification.
The "screen-wake-lock"
powerful feature defines a permission revocation algorithm. To invoke the Screen
Wake Lock permission revocation algorithm, run these steps:
[[ActiveLocks]]
["screen
"].
screen
".
The task source for the tasks mentioned in this specification is the screen wake lock task source.
The term platform wake lock refers to platform interfaces with which the user agent interacts to query state and acquire and release a wake lock.
A platform wake lock can be defined by the underlying platform (e.g. in a native wake lock framework) or by the user agent, if it has direct hardware control.
Internal slot | Initial value | Description |
---|---|---|
[[ActiveLocks]] | An ordered map mapping wake lock types to empty lists. |
An ordered map of wake lock types to a
list of WakeLockSentinel objects associated with
this Document .
|
The WakeLock
interface allows a document to acquire a screen wake lock.
WebIDL[SecureContext, Exposed=(Window)]
interface WakeLock
{
Promise<WakeLockSentinel
> request
(optional WakeLockType
type = "screen");
};
The request(type)
method steps are:
NotAllowedError
" DOMException
.
screen-wake-lock
", return a promise rejected with a "NotAllowedError
" DOMException
.
NotAllowedError
" DOMException
.
hidden
", return a promise rejected with "NotAllowedError
" DOMException
.
screen-wake-lock
".
denied
", then:
NotAllowedError
"
DOMException
.
NotAllowedError
"
DOMException
.
hidden
", then:
NotAllowedError
"
DOMException
.
[[ActiveLocks]]
["screen
"]
is empty, then invoke the following steps in
parallel:
screen
".
WakeLockSentinel
object with its type
attribute set to
type.
[[ActiveLocks]]
["screen
"].
WebIDL[SecureContext, Exposed=(Window)]
interface WakeLockSentinel
: EventTarget {
readonly attribute boolean released
;
readonly attribute WakeLockType
type
;
Promise<undefined> release
();
attribute EventHandler onrelease
;
};
A WakeLockSentinel
object provides a handle to a platform wake
lock, and it holds on to it until it is either manually released or
until the underlying platform wake lock is released. Its
existence keeps a platform wake lock for a given wake lock
type active, and releasing all WakeLockSentinel
instances of a
given wake lock type will cause the underlying platform wake
lock to be released.
WakeLockSentinel
instances are created with the following
internal
slots:
Internal slot | Initial value | Description (non-normative) |
---|---|---|
[[Released]] |
false
|
Whether the given WakeLockSentinel has been released.
|
The released
getter steps are to return
this.[[Released]]
.
The type
getter steps are to return this's
wake lock type.
The release
()
method steps are:
[[Released]]
is false
,
then run release a wake lock with lock set
to this and type set to the value of
this's type
attribute.
undefined
.
The onrelease
attribute is an event handler
IDL attribute for the "onrelease
" event
handler, whose event handler event type is
"release
".
It is used to notify scripts that a given WakeLockSentinel
object's handle has been released, either due to the
release
()
method being called or because the
wake lock was released by the user agent.
While a WakeLockSentinel
object has one or more event listeners
registered for "
", and the
release
WakeLockSentinel
object hasn't already been released, there MUST
be a strong reference from the Window
object that the
WakeLockSentinel
object's constructor was invoked from to the
WakeLockSentinel
object itself.
While there is a task queued by an WakeLockSentinel
object on the
screen wake lock task source, there MUST be a strong reference
from the Window
object that the WakeLockSentinel
object's
constructor was invoked from to that WakeLockSentinel
object.
For the purpose of wake lock type description, this specification defines the following enumeration to represent wake lock types:
WebIDLenum WakeLockType
{ "screen
" };
screen
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. A possible return value of the request to the underlying operating system is not checked. In other words, user agents MUST treat wake lock acquisition as advisory-only.
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 switched off by the user until it is switched on again.
A user agent may release a wake lock at any time. For example, when:
When a Document
document becomes no longer
fully active, the user agent must run these steps:
[[ActiveLocks]]
["screen
"]:
screen
".
This specification defines the following page visibility change steps with visibility state state and document:
hidden
", abort these steps.
[[ActiveLocks]]
["screen
"]:
screen
".
To acquire a wake lock for a given type, run these steps:
To release a wake lock for a given document, lock, and type, run these steps:
[[ActiveLocks]]
[type] does not
contain lock, abort these steps.
[[ActiveLocks]]
[type].
[[ActiveLocks]]
[type] is empty, then run the following steps in parallel:
true
if
the operation succeeded, or else false
.
true
and type is "screen"
run the
following:
[[Released]]
to true
.
release
" at lock.
Screen wake locks can cause various device components - particularly the display - to operate at higher power levels than they otherwise would. This can lead to undesirable effects, such as preventing the device from automatically locking itself and faster battery depletion. Faster battery depletion is of particular concern for mobile devices, which often don't 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 MAY ignore requests for screen wake lock if, for example, the battery capacity is low, or the user has put their device in a power-saving mode.
It is RECOMMENDED that a user agent provide some UI or indicator that allows the user to know when a screen wake lock is active. Providing such a UI could help end users to identify if a particular web application is having a negative energy impact on the device, and allow them to take action if so desired.
This section is non-normative.
function tryKeepScreenAlive(minutes) {
navigator.wakeLock.request("screen").then(lock => {
setTimeout(() => lock.release(), minutes * 60 * 1000);
});
}
tryKeepScreenAlive(10);
This example allows the user to request a screen wake lock by clicking on a checkbox, but updates the checkbox checked state in case the wake lock state changes:
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
document.body.appendChild(checkbox);
const sentinel = await navigator.wakeLock.request("screen");
checkbox.checked = !sentinel.released;
sentinel.onrelease = () => checkbox.checked = !sentinel.released;
In this example, two different wake lock requests are created and released independently:
let lock1 = await navigator.wakeLock.request("screen");
let lock2 = await navigator.wakeLock.request("screen");
lock1.release();
lock2.release();
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 MAY, MUST, MUST NOT, and RECOMMENDED in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
This specification defines conformance criteria for a single product: a user agent that implements the interfaces that it contains.
This section is non-normative.
We would like to offer our sincere thanks to Mounir Lamouri, Sergey Konstantinov, Matvey Larionov, Dominique Hazael-Massieux, Domenic Denicola, Thomas Steiner, Anne van Kesteren for their contributions to this work.
This section is non-normative.
This section documents the changes since previous publications.
WakeLock.request()
to deal with hidden documents.
ScreenWakeLock
constructable.
WakeLockSentinel.released
.
[[ActiveLocks]]
internal slot for Document
§6.1onrelease
attribute for WakeLockSentinel
§9.5release()
method for WakeLockSentinel
§9.4released
attribute for WakeLockSentinel
§9.2[[Released]]
internal slot for WakeLockSentinel
§9.1request()
method for WakeLock
§8.1"screen"
enum value for WakeLockType
§10.type
attribute for WakeLockSentinel
§9.3wakeLock
attribute for Navigator
§7.WakeLock
interface
§8.WakeLockSentinel
interface
§9.WakeLockType
enum
§10.Document
interface
EventTarget
interface
EventHandler
Document
)
Document
)
Window
interface
list
)
list
)
list
)
denied
(for PermissionState
)
boolean
type
DOMException
interface
[Exposed]
extended attribute
NotAllowedError
exception
Promise
interface
[SameObject]
extended attribute
[SecureContext]
extended attribute
undefined
type
WebIDL[SecureContext]
partial interface Navigator {
[SameObject] readonly attribute WakeLock
wakeLock
;
};
[SecureContext, Exposed=(Window)]
interface WakeLock
{
Promise<WakeLockSentinel
> request
(optional WakeLockType
type = "screen");
};
[SecureContext, Exposed=(Window)]
interface WakeLockSentinel
: EventTarget {
readonly attribute boolean released
;
readonly attribute WakeLockType
type
;
Promise<undefined> release
();
attribute EventHandler onrelease
;
};
enum WakeLockType
{ "screen
" };
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in:
Referenced in: