Bug 205104

Summary: Feature Request: Implement the Wake Lock API
Product: WebKit Reporter: Thomas Steiner <tomac>
Component: New BugsAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: clshortfuse, eoconnor, henry, hi, igorsantos07, jer.noble, kieren, marcosc, mlopez, pierre-etienne.lord, simon.fraser, webkit-bug-importer, yordan.kanchelov, youennf
Priority: P2 Keywords: InRadar
Version: Safari Technology Preview   
Hardware: Unspecified   
OS: Unspecified   

Description Thomas Steiner 2019-12-10 23:33:56 PST
Many websites like recipe sites or tutorial sites have a legit desire to prevent the screen of a device from turning off. Since the user may be busy with performing a task (following the recipe or tutorial steps), in many cases they may not be able or willing to touch the screen to keep it awake, for example, because their hands are dirty with dough or glue. The current workaround for page authors to obtain the desired behavior is to play an invisible video and to loop it infinitely. The Wake Lock API instead provides them with a proper way to programmatically keep the screen awake via a screen wake lock. When the user navigates away from either the tab or multitasks away from their browser completely, the wake lock would be released automatically.

Spec: https://w3c.github.io/wake-lock/

Article: https://web.dev/wakelock/

Demo: https://wake-lock-demo.glitch.me/

Sample code:

```js
// The wake lock sentinel.
let wakeLock = null;

// Function that attempts to request a wake lock.
const requestWakeLock = async () => {
  try {
    wakeLock = await navigator.wakeLock.request('screen');
    wakeLock.addEventListener('release', () => {
      console.log('Wake Lock was released');
    });
    console.log('Wake Lock is active');
  } catch (err) {
    console.error(`${err.name}, ${err.message}`);
  }
};

// Request a wake lock…
await requestWakeLock();
// …and release it again after 5s.
window.setTimeout(() => {
  wakeLock.release();
  wakeLock = null;
}, 5000);
```
Comment 1 Henry 2020-07-19 06:22:26 PDT
Our web app displays long running photo slideshows and we would love this feature. Like many other sites, we are currently forced to use the 'nosleep' library (https://github.com/richtr/NoSleep.js/) to keep the display awake, but a native solution would be preferable, and presumably much better for battery life.

We also use the 'nosleep' library when users are performing long running file uploads (e.g. large selections of photos), because whenever the screen goes to sleep the upload dies. (I assume this problem would be better solved by allowing background uploads of some sort, but at the moment 'nosleep' seems to be the only option).
Comment 2 Thomas Steiner 2020-07-20 00:04:05 PDT
(The data upload feature would ideally be solved with Background Fetch: https://wicg.github.io/background-fetch/.)
Comment 3 Radar WebKit Bug Importer 2020-11-19 23:52:22 PST
<rdar://problem/71618179>
Comment 4 matías lópez 2021-01-12 07:47:19 PST
Our platform uses a queue where customers need to wait for to someone answer the call, like a call center. There we need to prevent the screen of a device from turning off or browser go to disconnect from the websocket.

Use a hidden video with a loop is a hack, but is not respectful with battery.
Comment 5 Kieren 2021-03-01 19:37:38 PST
Our site uses this for big downloads - The display must be kept on or the requests will fail
Comment 6 yordan kanchelov 2022-04-17 13:21:05 PDT
Useful for creating games which don't require often user interaction
Comment 7 Carlos Lopez 2022-09-05 07:50:15 PDT
We need this for fleet management while users use the application while driving. Having to constantly touch the screen is a distraction as well as a safety risk. Right now iOS users extend the system-wide timeout to 30 minutes to bypass this since NoSleep.js workaround have failed in recent updates.

Users can also receive calls with WebRTC while in the application, and it would be useful for the device to stay on, though we've already worked around it for the previous reasons.
Comment 8 PE Lord 2023-03-29 14:50:51 PDT
Wake lock API has been released (https://github.com/WebKit/WebKit/tree/main/Source/WebCore/Modules/screen-wake-lock) on iOS 16.4

I use NoSleep.js to handle Wake Lock API. NoSleep.js is correctly handled with iOS 16.4.

Here the issue, on iOS 16.4.

When using the Wake Lock API, on Safari, in a browser mode, the Wake Lock API work properly.
On the same website, opened after have done "Add to home screen", the Wake Lock API don't work properly. The Wake Lock API seem to not be able to be acquired.
Here the test page: https://pelord.github.io/wakeLockApiAddHomeScreen/

To test the Wake Lock API, you have to toggle the ZZZ button.

Unchecked ZZZ = Wake Lock API released
Checked ZZZ = Wake Lock API acquired

Is it normal?
Comment 9 PE Lord 2023-03-29 15:06:22 PDT
More info. 

The previous test page is a PWA.
Comment 10 Marcos Caceres 2023-03-29 16:21:58 PDT
Thanks PE Lord! We will take a look.
Comment 11 Marcos Caceres 2023-03-29 19:08:16 PDT
Hi again PE Lord!  

tried it on both the Simulator (iPad) and on a couple of iPhones. 

Can confirm that the behavior you described is occurring. 

I've filed an internal bug rdar://107397143

I'll file a separate bug for this as the API itself is implemented:

https://bugs.webkit.org/show_bug.cgi?id=245551
Comment 12 Marcos Caceres 2023-03-29 19:11:22 PDT
Actually, other folks already file it: 
https://bugs.webkit.org/show_bug.cgi?id=254545