New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(powerMonitor): expose interface to query system idle state #11807
Conversation
I am cc'ing @felixrieseberg here as well for the interested party. |
Neat! I love re-using Chromium's well-crafted code. I'm not sure about |
Good point, this is probably me not very well knowledgeable structure of electron. Let me try. |
@felixrieseberg PR updated per suggestion, now interface is expoed via |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about adding it to the shell
module when we have a powerMonitor
module.
atom/common/api/atom_api_shell.cc
Outdated
dict.SetMethod("querySystemIdleTime", &QuerySystemIdleTime); | ||
|
||
#if defined(OS_MACOSX) | ||
ui::InitIdleMonitor(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be moved to AtomBrowserMainParts::PreCreateThreads
I'm fine to pick anywhere to expose this. @deepak1556 Is suggestion moving this interface to |
There isn't a strong argument for choosing between |
Nah, you're right, |
@deepak1556 @felixrieseberg now interface moved into |
ui::CalculateIdleState(idle_threshold, callback); | ||
} else { | ||
isolate->ThrowException(v8::Exception::TypeError( | ||
mate::StringToV8(isolate, "Invalid idle threshold"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's be super nice here, "Invalid idle threshold, must be greater than or equal to 0"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good, message need to be bit amended to greater than 0
, having 0
as threshold will not return valid value anyway (while CalculateIdleState
can accepts it)
docs/api/power-monitor.md
Outdated
|
||
The `powerMonitor` module has the following methods: | ||
|
||
#### `shell.querySystemIdleState(idle_threshold, callback)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
powerMonitor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/idle_threshold/idleThreshold
docs/api/power-monitor.md
Outdated
before considered idle. State will be notified via callback represents current system | ||
idle state. `locked` is available on supported system only. | ||
|
||
#### `shell.querySystemIdleTime(callback)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
powerMonitor.
docs/api/power-monitor.md
Outdated
|
||
#### `shell.querySystemIdleState(idle_threshold, callback)` | ||
|
||
* `idleThreshold` Interger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integer
docs/api/power-monitor.md
Outdated
|
||
* `idleThreshold` Interger | ||
* `callback` Function | ||
* `active` | `idle` | `locked` | `unknown` String |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should be
* `idleState` String - Can be `active, `idle`, `locked` or `unknown`
docs/api/power-monitor.md
Outdated
* `callback` Function | ||
* `active` | `idle` | `locked` | `unknown` String | ||
|
||
Calculate system idle state. `idle_threshold` is the amount of time (in seconds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/idle_threshold/idleThreshold
docs/api/power-monitor.md
Outdated
|
||
Calculate system idle state. `idle_threshold` is the amount of time (in seconds) | ||
before considered idle. State will be notified via callback represents current system | ||
idle state. `locked` is available on supported system only. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document which systems currently support locked
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chromium doesn't document it as well (https://cs.chromium.org/chromium/src/ui/base/idle/idle.h?q=idle&sq=package:chromium&l=17) - I'd rather in sync with chromium as it can change anytime due to platform side patch / version up, etcs.
docs/api/power-monitor.md
Outdated
#### `shell.querySystemIdleTime(callback)` | ||
|
||
* `callback` Function | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be another line here
* `idleTime` Integer - Idle time in seconds
PR updated & fixed mistakes per comment / suggestion. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current specs of PowerMonitor
run only on linux, we need to enable the tests for these new apis on all three platforms.
Noticed that - what's best way? Is it allowed to instantiate powermonitor without dbus mock? (New interface won't nees those mock anyway) |
Not having to lazily initialize the powerMonitor module should allow enabling these test cases along side the existing dbus specs for linux. |
Can remove the call to |
@@ -41,7 +43,7 @@ const skip = process.platform !== 'linux' || !process.env.DBUS_SYSTEM_BUS_ADDRES | |||
before((done) => { | |||
logindMock.on('MethodCalled', onceMethodCalled(done)) | |||
// lazy load powerMonitor after we listen to MethodCalled mock signal | |||
powerMonitor = require('electron').remote.powerMonitor | |||
dbusMockPowerMonitor = require('electron').remote.powerMonitor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tarruda Is it required to lazy initialize the module ? I was hoping we can just initialize the module only once for all the specs here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has to be lazy loaded if we want to verify that electron inhibits the system suspend (it does this by calling a method via dbus).
@@ -68,10 +110,6 @@ using atom::api::PowerMonitor; | |||
|
|||
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused, | |||
v8::Local<v8::Context> context, void* priv) { | |||
#if defined(OS_MACOSX) | |||
base::PowerMonitorDeviceSource::AllocateSystemIOPorts(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per comment suggestion in #11807 (comment) .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM, nice idea && thanks for the PR. 👍
A couple of questions and suggestions inline
int idle_threshold, | ||
const ui::IdleCallback& callback); | ||
void QuerySystemIdleTime(const ui::IdleTimeCallback& callback); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make these methods private since we're only using them with the bindings. Permissions can be loosened later as needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense, moved it.
} else { | ||
isolate->ThrowException(v8::Exception::TypeError( | ||
mate::StringToV8(isolate, | ||
"Invalid idle threshold, must be greater than 0"))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know another reviewer suggested this, but are we sure that we want this?
Since we're not publishing the ui::CheckIdleStateIsLocked()
, if all you want to do is check for system lock it's reasonable to pass a -1
in for idle_threshold
.
If we include that method, we can avoid this wart by having a recommended way to test for lock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I didn't thought in that way, but also checking lock
state is somewhat limited per platform support, so passing -1
doesn't guarantee you can reliable acquire intended state. I think it's rather limit api surface, and expose CheckIdleStateIsLocked
separately if there's demand for it.
docs/api/power-monitor.md
Outdated
* `idleState` String - Can be `active`, `idle`, `locked` or `unknown` | ||
|
||
Calculate system idle state. `idleThreshold` is the amount of time (in seconds) | ||
before considered idle. State will be notified via callback represents current system |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's something missing between "notified via callback" and "represents current system", I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's wrapped line, full sentence is State will be notified via callback represents current system idle state.
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That still doesn't make any sense though. Would this be ok?
+ Calculate the system idle state. `idleThreshold` is the amount of time (in seconds)
+ before considered idle. `callback` will be called synchronously on some systems
+ and with an `idleState` argument that describes the system's state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, it's probably same bucket for my English writing skill. I'll update as suggested.
docs/api/power-monitor.md
Outdated
|
||
Calculate system idle state. `idleThreshold` is the amount of time (in seconds) | ||
before considered idle. State will be notified via callback represents current system | ||
idle state. `locked` is available on supported system only. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/system/systems/
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) | ||
.MakeDestroyable() | ||
.SetMethod("querySystemIdleState", &PowerMonitor::QuerySystemIdleState) | ||
.SetMethod("querySystemIdleTime", &PowerMonitor::QuerySystemIdleTime); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we name these with "query" instead of using libcc's "calculate" nomenclature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably cause of my bad non-native-english-naming skill . 😅 open to suggestion if recommended to rename it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😄 no worries!
My suggestion would be to copy the libchromium API names, e.g. PowerMonitor::CalculateIdleState
and powerMonitor.calculateIdleState
.
I don't feel very strongly about this; if other reviewers prefer querySystem
I will go along
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll wait for few other chime in and update PR if we'd like to go for chromium api names.
I have rebased PR to master and seeing test failures on linux, peeking it some other PR sees as well (i.e https://circleci.com/gh/electron/electron/9330) not sure way to resolve it. |
@kwonoj it appears these are building in CI under your own name and not the electron org's, so we don't have the ability to restart them; often they flake so try restarting the failures yourself and see what happens |
Bit unsure why fork CI setup reports status to PR itself. I've retriggered couple of times already, but did one more time. |
@codebytere stopping my circleci config makes status notification correctly goes for electron's, but doesn't seem like build failure resolves by retriggering it. |
@ckerr What do you think? Anything else OJ could do here to make it mergable? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loving this 👍
Just need to rebase changes onto current master so that arm64 builds don't blow up 👍
To be clear for whoever merges this in the future as well, this does not get backported, new versioning scheme means that this will go out in 2.1.x or 3.0.x, we don't backport features 👍 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for not getting back to this sooner. Yes 👍 for landing this in master.
It would also make sense in the 2.x series when we open it back up to new nonbreaking features.
…tron#11807) * feat(BrowserWindow): expose interface to query system idle state * test(BrowserWindow): update test cases for querySystemIdle interface * docs(BrowserWindow): add querySystemIdle interface documentation * refactor(powerMonitor): move querySystemIdle into powerMonitor * test(powerMonitor): split test cases for all platform
…tron#11807) * feat(BrowserWindow): expose interface to query system idle state * test(BrowserWindow): update test cases for querySystemIdle interface * docs(BrowserWindow): add querySystemIdle interface documentation * refactor(powerMonitor): move querySystemIdle into powerMonitor * test(powerMonitor): split test cases for all platform
Is there any planned release for this feature? |
Looks like this feature is missing in TypeScript typings. |
This PR intend to expose 2 static method to
powerMonitor
-querySystemIdleState
andquerySystemIdleTime
. As interface name stands for, both allows to query system's idle status.There are handful of users need this features (including old backlog issue like #1528), and most cases user relies on native modules (https://www.npmjs.com/package/@paulcbetts/system-idle-time / https://www.npmjs.com/package/desktop-idle, etcs). This PR exposes interface to chromium's implementation removes need of custom native module.
accepts non-negative number as a threshold in seconds to be considered as system idle, notify callback with current state. Callback can be resolved synchronously or either asynchronously, depends on platform support. Among state,
locked
state is only available if platform support to return it.notifys callback with system idle time in seconds.