Abstract

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).

Status of This Document

This document is merely a public working draft of a potential specification. It has no official standing of any kind and does not represent the support or consensus of any standards organisation.

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.

Table of Contents

1. Examples

This section is non-normative.

Example 1
//request to keep the display awake when the recipe is showing:
document.keepScreenAwake = true;

//stop keeping the display awake:
document.keepScreenAwake = false;

2. Wake Locks

A wake lock prevents some aspect of the device or operating system from entering a power-saving state.

This specification deals specifically with the screen wake lock which prevents the screen(s) of the device from entering a power-saving state.

3. Requesting Wake Locks

Each Document has a request flag for the wake lock. The request flag is unset when the Document is created. Request flags are used by user agent to track if a particular Document is requesting the lock (for the purpose of acquiring or releasing the corresponding operating system lock).

A Document requests wake lock by setting its lock request flag. This means that the Document indicates to the user agent a preference that the underlying operating system apply wake lock, i.e. keep the screen on.

A Document can successfully request wake lock regardless of whether the actual lock can be applied by the user agent and/or the underlying operating system at the time when the request is made.

Conversely, a Document cancels wake lock request by resetting its lock request flag . This means that the Document indicates to the user agent that it no longer prefers that the underlying system apply the wake lock.

Note

Lock request flag merely indicates a preference of the Document to have the wake lock applied. Requesting the lock or cancelling wake lock request does not entail synchronous acquisition or release of the corresponding operating system lock, and can only result in an error when the document is explicitly prohibited form executing those operations by some security mechanism.

No Document "owns" the operating system lock and the state of operating system lock does not affect the state of the lock request flag for any Document.

Instead, user agent is responsible for mapping the state of request flags for all Documents to the state the operating system lock.

4. Managing Wake Locks

User agent acquires the wake lock by requesting the undrelying operating system to apply the lock. The lock is considered acquired only when the request to the operating system succeeds.

Conversely, user agent releases 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.

A Document is relevant to the wake lock if:

  1. The Document is fully active.
  2. The Document's browsing context is in position to interact with the user, i.e. it is in an active tab and the browser window is focused.
  3. The Document's wake lock request flag is set.
Note
If a Document does not have a browsing context, it is not relevant to the wake lock.

The wake lock is applicable if the state of the operating system permits application of the lock (e.g. there is sufficient battery charge and wake locking is not disabled by a user setting).

User agent MUST acquire the wake lock when all of the following conditions become true:

  1. The wake lock is applicable.
  2. There is at least one Document that is relevant to the wake lock.

User agent MUST release the wake lock when any of the conditions above become false.

Note

From the user's perspective, a Document which has requested the wake lock will no longer cause the lock to be applied if one of the following happens:

  • Browser tab or window is closed.
  • Switching from one tab to another.
  • Browser is minimized.
  • Switching from one browser window to another.
  • Switching from a web browser to another (non-browser) application.
  • Navigating to a different page or reloading a page.

Should the tab and/or browser window once again become focused, the lock will be applied again if it is still requested and the state of operating system permits its application.

5. Extension to the Document interface

partial interface Document {
                attribute boolean keepScreenAwake;
};

5.1 Attributes

keepScreenAwake of type boolean,
Reflects the state of the wake lock request flag. Document script can set this attribute to true to request wake lock or to false to cancel wake lock request.

6. Additional lock types

The lock request and management mechanism presented in this document can be extended to support additional types of locks by adding new lock request flags to Document interface. E.g. a "system" wake lock could be introduced which would prevent the device's cpu, radio(s), and other system services from entering a power-saving state while allowing the screen to switch off.

7. Dependencies

The following concepts and interfaces are defined in [HTML]:

8. Use cases

This section is non-normative.

The use cases and requirements are documented in [WAKE-LOCK-USECASES].

9. Conformance

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 word MUST is 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.

A user agent MUST implement the APIs defined in this specification in a manner that conforms to the ECMAScript Bindings defined in [WEBIDL].

A. Acknowledgments

This section is non-normative.

We would like to offer my sincere thanks to Mounir Lamouri, Sergey Konstantinov and Matvey Larionov for their contributions to this work.

B. References

B.1 Normative references

[HTML]
Ian Hickson. HTML. Living Standard. URL: https://html.spec.whatwg.org/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://tools.ietf.org/html/rfc2119
[WEBIDL]
Cameron McCormack. Web IDL. 19 April 2012. W3C Candidate Recommendation. URL: http://www.w3.org/TR/WebIDL/

B.2 Informative references

[WAKE-LOCK-USECASES]
Reference not found.