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).
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.
//lock display when the recipe is showing: $( "#recipe" ).on( "show", function() { navigator.wakeLock.request("screen").then(haveFun, boo); } ); //release lock: $( "#recipe" ).on( "hide", function() { navigator.wakeLock.release("screen"); navigator.wakeLock.onlost = null; } ); //get notified of lost locks: function lostHandler(e){ var msg = "oh noes! We lost the " + e.type + " lock!"; console.log( msg ); } //register listener navigator.wakeLock.onlost = lostHandler;
A wake lock prevents some aspect of the device or operating system from entering a power-saving state.
This specification deals with the following wake lock types and their expected behavior:
Each top-level browsing context has a holding flag for a lock type T. The holding flags are initially unset. The flags are used to track if a particular top-level browsing context is holding a particular lock type.
To hold a lock of some type T means that the document of a top-level browsing context has requested, through the user agent, that the underlying system apply a particular lock type. A top-level browsing context is able to hold a lock even if the underlying operating system has not explicitly signaled that the user agent has been granted the lock.
Conversely, to release a lock of some type means that the document of a top-level browsing context has requested, through the user agent, that the underlying system no longer holds a particular lock type.
A wake lock of type T is lost when the underlying system has explicitly signaled to the user agent that it can no longer hold a particular lock type (e.g., the battery level is too low to allow a wake lock). When a lock of type T is lost, the user agent MUST run the steps to notify scripts that a lock was lost with T.
The steps to notify scripts that a lock was lost with lock type T as input:
WakeLockEvent
that does
not bubble, is not cancelable, has isTrusted
attribute
initialized to true
, and whose name is
lost
.
wakeLock
attribute of the
current top-level browsing context.
The user agent MAY require that a top-level browsing context meet one or more security conditions in order to hold a lock. For example, the user could have indicated through the user interface that they don't wish for a web application to have this capability.
WakeLock
interface
request()
method
The request()
method causes the user agent to requests a wake lock of a particular
type from the underlying system. When called,
the user agent MUST run the steps to request a wake lock
with WakeLockType
T as input. The algorithm
returns a promise.
DOMException
whose
name is SecurityError
and abort these steps.
DOMException
whose name is
NotSupportedError.
DOMException
whose name
is InvalidAccessError.
undefined
.
release()
method
The release()
method releases a previously acquired wake lock of a particular
type. When called, the user agent MUST run the
steps to release a wake lock with
WakeLockType
T as input. The algorithm
returns a promise.
Promise
.
DOMException
whose
name is NotFoundError.
DOMException
whose
name is AbortError.
undefined
.
isHeld()
method
The isHeld()
method provides a means to determine if the top-level browsing
context is holding a particular wake lock type. When called, the user agent MUST run the
steps to check if a lock is held with
WakeLockType
T as input. The algorithm
returns a boolean.
true
.
false
.
onlost
attribute
The onlost
event
handler provides a means for scripts of a top-level browsing
context to be notified if a wake lock is lost.
WakeLockEvent
interface
The WakeLockEvent
provides a way for a script to be
notified when a wake lock is lost.
type
attribute
The type
attribute is the WakeLockType
that generated this event.
When getting, the use agent MUST return the WakeLockType
that generated this event.
WakeLockType
enumeration
In the API, the wake lock types are expressed as
the WakeLockType
enum.
The primary use case for this API is to prevent a device from dimming or locking the device screen, as often happens when a device is left without user interactions.
Keeping the screen from dimming or locking can drain the underlying device power reserve. Preventing it from locking may expose the device and its content to unwanted parties if the user expected it to lock automatically.
To mitigate the risks mentioned above, user agents may want to:
navigator.wakeLock.request
on visible browsing contexts;
navigator.wakeLock.request
when the power reserve reaches
low levels;
The following concepts and interfaces are defined in [[!HTML]]:
The following concepts and interfaces are defined in [[!DOM]]:
The
Promise
interface is defined in [[!ECMASCRIPT]].
The use cases and requirements are documented in [[WAKE-LOCK-USECASES]].
This specification defines conformance criteria for a single product: a user agent that implements the interfaces that it contains.
A user agent MUST implement the APIs defined in this specification in a manner that conforms to the ECMAScript Bindings defined in [[!WEBIDL]].
I would like to offer my sincere thanks to Mounir Lamouri, Sergey Konstantinov and Matvey Larionov for their contributions to this work.