Skip to content

feat: add disableWakeLocks to WebPreferences - #52342

Merged
jkleinsc merged 5 commits into
electron:mainfrom
YUCLing:feat/disable-wake-locks
Aug 18, 2026
Merged

jkleinsc merged 5 commits into
electron:mainfrom
YUCLing:feat/disable-wake-locks

Conversation

@YUCLing

@YUCLing YUCLing commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

The PR adds a new option in WebPreferences: disableWakeLocks. It allows the user to disable the wake locks of the WebContents, thus some webpage activities (e.g. Playing audio) no longer block the system sleep.

The C++ side is done — the option flows through WebContentsPreferences, all four CreateParams construction sites, and MaybeOverrideCreateParamsForNewWindow. What I'd appreciate help with:

  • Review — is this the right approach?
  • Testing — I'd like to add a spec test in api-browser-window-spec.ts, but I'm not sure the best way to observe wake lock state from JS. Any suggestions?
Without disableWakeLocks: true
image
With disableWakeLocks: true
image

As you can see, with wake locks enabled, Chromium will prevent the system from sleeping when there is audio playing. This option provides a way to disable it.

Checklist

Release Notes

Notes: Added disableWakeLocks option to WebPreferences

@welcome

welcome Bot commented Jul 14, 2026

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖

Semantic PR titles

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Commit signing

This repo enforces commit signatures for all incoming PRs.
To sign your commits, see GitHub's documentation on Telling Git about your signing key.

PR tips

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Jul 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ This PR contains unsigned commits. This repository enforces commit signatures
for all incoming PRs. To get your PR merged, please sign those commits
(git rebase --exec 'git commit -S --amend --no-edit -n' @{upstream}) and force push them to this branch
(git push --force-with-lease)

For more information on signing commits, see GitHub's documentation on Telling Git about your signing key.

@github-actions github-actions Bot added the needs-signed-commits Currently some or all of the commits in this PR are not signed label Jul 14, 2026
@YUCLing
YUCLing force-pushed the feat/disable-wake-locks branch from 1da4b93 to fbd7912 Compare July 14, 2026 07:43
@github-actions github-actions Bot removed the needs-signed-commits Currently some or all of the commits in this PR are not signed label Jul 14, 2026
@YUCLing
YUCLing marked this pull request as ready for review July 15, 2026 00:58
@jkleinsc jkleinsc added the semver/minor backwards-compatible functionality label Jul 15, 2026
@samuelmaddock

Copy link
Copy Markdown
Member

Would this flag work for your application?
--disable-blink-features=WakeLock

Blink exposes turning this off as a feature flag.
https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/platform/runtime_enabled_features.json5;l=6786-6794;drc=8977a266a109474ccbb5b3182f1aa08dfdf2597f

You can add this switch using Electron's app.commandLine.appendSwitch.

@YUCLing

YUCLing commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

@samuelmaddock

Would this flag work for your application?

Ahh, thank you for bringing it up! I didn't know about this. But it looks like it doesn't do anything?

image

Chromium is still preventing the system from sleeping after adding it.

@samuelmaddock

Copy link
Copy Markdown
Member

@YUCLing the Blink runtime flags are sourced from https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/platform/runtime_enabled_features.json5

There's another named SystemWakeLock. Maybe check if these make any difference. 🙏
--disable-blink-features=WakeLock,SystemWakeLock

@YUCLing

YUCLing commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

There's another named SystemWakeLock. Maybe check if these make any difference. 🙏
--disable-blink-features=WakeLock,SystemWakeLock

Sadly, I tried with WakeLock, SystemWakeLock and WakeLock,SystemWakeLock. None of them actually disabled the wake lock

@samuelmaddock

Copy link
Copy Markdown
Member

@YUCLing ok one more existing API to try. Electron has a permission check system.
https://www.electronjs.org/docs/latest/api/session#sessetpermissionrequesthandlerhandler

It looks like we may have missed documenting 'screen-wake-lock' and 'system-wake-lock'.

case blink::PermissionType::WAKE_LOCK_SCREEN:
return StringToV8(isolate, "screen-wake-lock");
case blink::PermissionType::SENSORS:
return StringToV8(isolate, "sensors");
case blink::PermissionType::STORAGE_ACCESS_GRANT:
return StringToV8(isolate, "storage-access");
case blink::PermissionType::VR:
return StringToV8(isolate, "vr");
case blink::PermissionType::WAKE_LOCK_SYSTEM:
return StringToV8(isolate, "system-wake-lock");

It'd be worth checking if these permissions come through the check handler and whether denying them solves the problem.

@YUCLing

YUCLing commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor Author

@samuelmaddock

ok one more existing API to try. Electron has a permission check system.

No luck here. I'm using the code below, it's never called with any screen-lock permission (same for setPermissionCheckHandler).

session.defaultSession.setPermissionRequestHandler((c, p, cb) => {
    console.log("Permission", p, "requested");
    if (p.endsWith("screen-lock")) {
        console.log("Reject", p);
        cb(false);
        return;
    }
    cb(true);
});

The wake lock is created by Chromium itself directly and doesn't seem to follow the permission system. I found the enable_wake_locks in CreateParams from here too.

https://source.chromium.org/chromium/chromium/src/+/main:content/browser/media/media_web_contents_observer.cc;l=719;drc=9a158bed51d4160609cbdc6242c338bfcc97e8f3

@samuelmaddock

Copy link
Copy Markdown
Member

@YUCLing I think the best outcome would be if these changes resulted in the wake lock permission being surfaced through setPermissionRequestHandler. This would be the ideal implementation which doesn't add to Electron's API surface and provides greater developer control over permissions.

Would you be willing to look into whether this would be a viable approach? The API WG will discuss again in next week's meeting.

@YUCLing

YUCLing commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Would you be willing to look into whether this would be a viable approach?

I have just checked that, it might be viable for the audio. The WebContents exposes a path to PermissionController through BrowserContext.

However, I prefer a simple switch for all wake locks, because that's what I needed, I've already found these locks while browsing around:

I don't know if there are more of them.

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jul 22, 2026
@jkleinsc

jkleinsc commented Jul 27, 2026 •

Copy link
Copy Markdown
Member

@YUCLing I just tested and using setPermissionRequestHandler does work if you use screen-wake-lock:

session.defaultSession.setPermissionRequestHandler((c, p, cb) => {
    console.log("Permission", p, "requested");
    if (p == "screen-wake-lock") {
        console.log("Reject", p);
        cb(false);
        return;
    }
    cb(true);
});

In light of that, I am going to close this PR.

@jkleinsc jkleinsc closed this Jul 27, 2026
@jkleinsc

Copy link
Copy Markdown
Member
session.defaultSession.setPermissionRequestHandler((c, p, cb) => {
    console.log("Permission", p, "requested");
    if (p.endsWith("wake-lock")) {
        console.log("Reject", p);
        cb(false);
        return;
    }
    cb(true);
});

Also works.

@YUCLing

YUCLing commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@jkleinsc

I just tested and using setPermissionRequestHandler does work if you use screen-wake-lock

I'm sorry but both of your code doesn't work for me.

image image

I'm quite confused here, should I go with an issue then?

EDIT: It's the same on Windows

image image

and mac

image image

@jkleinsc

jkleinsc commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

@YUCLing is there an API other than navigator.wakeLock that you are looking to block? Could you use Electron Fiddle to show your test case? You can publish the test case to a gist from Electron Fiddle. Here is the Electron fiddle gist I used to test the issue https://gist.github.com/df6caf895f2839148ec779704e476dd0.

@YUCLing

YUCLing commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc

is there an API other than navigator.wakeLock that you are looking to block?

I mentioned in the comments (#52342 (comment) and #52342 (comment)) earlier, they are created by Chromium itself, not through the Wake Lock API.

Could you use Electron Fiddle to show your test case?

It's as simple as playing a video on YouTube, but here's the gist if you still need it.

@jkleinsc

Copy link
Copy Markdown
Member

@YUCLing it is unclear from your fiddle what you are trying to prevent from happening? That youtube video is not invoking wake lock. Its possible youtube is using some other trick to prevent system sleep, but I don't think it is wake lock.

@YUCLing

YUCLing commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc

it is unclear from your fiddle what you are trying to prevent from happening

My fiddle doesn't prevent anything (because it's not possible), it simply proves that wake locks exists, even there are no Wake Lock API usages.

That youtube video is not invoking wake lock. Its possible youtube is using some other trick to prevent system sleep, but I don't think it is wake lock.

Yes, the YouTube video is not invoking the wake locks, as I said in the comments earlier, wake locks are created by Chromium itself. Check the source code links in the comments before and you'll see them. This PR provides the simplest way to disable all of them.

I don't know if there are any other ways to disable all of them, I'd be happy if you have any idea on this.

@jkleinsc jkleinsc reopened this Jul 28, 2026
@jkleinsc

Copy link
Copy Markdown
Member

@YUCLing ok I understand now. I built your PR locally and I do see it works as intended. The @electron/wg-api will need to discuss if the API shape is the best approach, but I understand the need here. As far as testing, it looks like the navigator.wakeLock API can be used to test this. In the gist I provided when disableWakeLocks: true is set and wakeLock = await navigator.wakeLock.request('screen'); is called, the release event fires on the wakeLock object almost immediately. So a test that sets disableWakeLocks: true, calls await navigator.wakeLock.request('screen'); and then verifies that the release event fires on the wakeLock object should be sufficient.

@YUCLing
YUCLing force-pushed the feat/disable-wake-locks branch from fbd7912 to 6a954c4 Compare July 29, 2026 10:59
@YUCLing

YUCLing commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

The @electron/wg-api will need to discuss if the API shape is the best approach, but I understand the need here.

Sure, I understand that, take your time 👍

As far as testing, it looks like the navigator.wakeLock API can be used to test this. In the gist I provided when disableWakeLocks: true is set and wakeLock = await navigator.wakeLock.request('screen'); is called, the release event fires on the wakeLock object almost immediately. So a test that sets disableWakeLocks: true, calls await navigator.wakeLock.request('screen'); and then verifies that the release event fires on the wakeLock object should be sufficient.

I've added the tests, I'm not sure what's the proper way to get access to Wake Lock API (it requires HTTPS) in the tests, so I simply copied the HTTPS server code from other tests. Can you check with it?

Also, I've rebased the PR to the latest commits.

Comment thread spec/api-web-contents-spec.ts Resolved

@jkleinsc jkleinsc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look again at the test. I think the test for wakeLocalAvailable can be a bit more precise.

Comment thread spec/api-web-contents-spec.ts Resolved
@YUCLing
YUCLing force-pushed the feat/disable-wake-locks branch from 87c29f9 to 18cae39 Compare August 5, 2026 21:34
@YUCLing

YUCLing commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc I see the tests are failing, so I also rebased the PR to latest main, that should apply the fix from #52641 ?

@jkleinsc

jkleinsc commented Aug 5, 2026 •

Copy link
Copy Markdown
Member

Yes rebase should fix.

@erickzhao erickzhao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM

@samuelmaddock samuelmaddock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM

Thanks for helping us through understanding the context for this change!

@itsananderson itsananderson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM

@YUCLing

YUCLing commented Aug 6, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc I've fixed the test and confirmed it works locally. But I'm not sure what are those src cache errors, should I worry about them?

@jkleinsc

jkleinsc commented Aug 6, 2026

Copy link
Copy Markdown
Member

No they are being caused by a GitHub outage. Once that is resolved everything should run properly.

@jkleinsc

jkleinsc commented Aug 7, 2026

Copy link
Copy Markdown
Member

@YUCLing it looks like the GitHub issues yesterday have left your PR in a strange state. Can you push an empty commit to trigger CI?

@YUCLing
YUCLing force-pushed the feat/disable-wake-locks branch from 907965a to 4dd8c90 Compare August 7, 2026 16:17
@YUCLing

YUCLing commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc Does this work?

@YUCLing

YUCLing commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

@jkleinsc Hi there, sry for disturbing, but I think you should take care of CI?

@MarshallOfSound

Copy link
Copy Markdown
Member

Heads up: Windows CI builds moved to new runners on Monday and the old ones have now been removed. This branch predates that, so its Windows build jobs still point at the old runners and will sit queued forever if CI runs again. Rebasing on main (or "Update branch") picks up the change, nothing else needed.

@jkleinsc
jkleinsc merged commit 0bbbb4f into electron:main Aug 18, 2026
76 checks passed
@release-clerk

release-clerk Bot commented Aug 18, 2026

Copy link
Copy Markdown

Release Notes Persisted

Added disableWakeLocks option to WebPreferences

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

7 participants