Repository navigation
feat: add disableWakeLocks to WebPreferences - #52342
Conversation
|
💖 Thanks for opening this pull request! 💖 Semantic PR titlesWe 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:
Commit signingThis repo enforces commit signatures for all incoming PRs. PR tipsThings that will help get your PR across the finish line:
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. |
|
For more information on signing commits, see GitHub's documentation on Telling Git about your signing key. |
1da4b93 to
fbd7912
Compare
|
Would this flag work for your application? Blink exposes turning this off as a feature flag. You can add this switch using Electron's app.commandLine.appendSwitch. |
|
@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 |
Sadly, I tried with |
|
@YUCLing ok one more existing API to try. Electron has a permission check system. It looks like we may have missed documenting 'screen-wake-lock' and 'system-wake-lock'. electron/shell/common/gin_converters/content_converter.cc Lines 207 to 216 in 349e1a6 It'd be worth checking if these permissions come through the check handler and whether denying them solves the problem. |
No luck here. I'm using the code below, it's never called with any 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 |
|
@YUCLing I think the best outcome would be if these changes resulted in the wake lock permission being surfaced through 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. |
I have just checked that, it might be viable for the audio. The 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. |
|
@YUCLing I just tested and using 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. |
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 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. |
I mentioned in the comments (#52342 (comment) and #52342 (comment)) earlier, they are created by Chromium itself, not through the Wake Lock API.
It's as simple as playing a video on YouTube, but here's the gist if you still need it. |
|
@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. |
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.
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. |
|
@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 |
fbd7912 to
6a954c4
Compare
Sure, I understand that, take your time 👍
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. |
jkleinsc
left a comment
There was a problem hiding this comment.
I took a look again at the test. I think the test for wakeLocalAvailable can be a bit more precise.
Co-authored-by: John Kleinschmidt <[email protected]>
87c29f9 to
18cae39
Compare
|
Yes rebase should fix. |
samuelmaddock
left a comment
There was a problem hiding this comment.
API LGTM
Thanks for helping us through understanding the context for this change!
|
@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? |
|
No they are being caused by a GitHub outage. Once that is resolved everything should run properly. |
|
@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? |
907965a to
4dd8c90
Compare
|
@jkleinsc Does this work? |
|
@jkleinsc Hi there, sry for disturbing, but I think you should take care of CI? |
|
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 |
|
Release Notes Persisted
|







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 fourCreateParamsconstruction sites, andMaybeOverrideCreateParamsForNewWindow. What I'd appreciate help with:api-browser-window-spec.ts, but I'm not sure the best way to observe wake lock state from JS. Any suggestions?Without
disableWakeLocks: trueWith
disableWakeLocks: trueAs 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
npm testpassesRelease Notes
Notes: Added
disableWakeLocksoption to WebPreferences