From 14c8e000cbdb917ed5c208462ff95d9dcb7f40ec Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 30 Nov 2020 08:49:18 +0100 Subject: [PATCH] feat: add process.uptime() to sandboxed renderers (#26684) --- docs/api/process.md | 1 + shell/renderer/electron_sandboxed_renderer_client.cc | 6 ++++++ spec-main/api-browser-window-spec.ts | 1 + spec-main/fixtures/module/preload-sandbox.js | 1 + 4 files changed, 9 insertions(+) diff --git a/docs/api/process.md b/docs/api/process.md index ec77bda8b86f..ee00672129ff 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -22,6 +22,7 @@ In sandboxed renderers the `process` object contains only a subset of the APIs: * `getSystemVersion()` * `getCPUUsage()` * `getIOCounters()` +* `uptime()` * `argv` * `execPath` * `env` diff --git a/shell/renderer/electron_sandboxed_renderer_client.cc b/shell/renderer/electron_sandboxed_renderer_client.cc index bba81eb28f5f..dca4cb686162 100644 --- a/shell/renderer/electron_sandboxed_renderer_client.cc +++ b/shell/renderer/electron_sandboxed_renderer_client.cc @@ -90,6 +90,11 @@ v8::Local CreatePreloadScript(v8::Isolate* isolate, preloadSrc); } +double Uptime() { + return (base::Time::Now() - base::Process::Current().CreationTime()) + .InSecondsF(); +} + void InvokeHiddenCallback(v8::Handle context, const std::string& hidden_key, const std::string& callback_name) { @@ -137,6 +142,7 @@ void ElectronSandboxedRendererClient::InitializeBindings( ElectronBindings::BindProcess(isolate, &process, metrics_.get()); + process.SetMethod("uptime", Uptime); process.Set("argv", base::CommandLine::ForCurrentProcess()->argv()); process.SetReadOnly("pid", base::GetCurrentProcId()); process.SetReadOnly("sandboxed", true); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 587457c028b9..d72b8daad002 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -2533,6 +2533,7 @@ describe('BrowserWindow module', () => { expect(test.systemVersion).to.be.a('string'); expect(test.cpuUsage).to.be.an('object'); expect(test.ioCounters).to.be.an('object'); + expect(test.uptime).to.be.a('number'); expect(test.arch).to.equal(process.arch); expect(test.platform).to.equal(process.platform); expect(test.env).to.deep.equal(process.env); diff --git a/spec-main/fixtures/module/preload-sandbox.js b/spec-main/fixtures/module/preload-sandbox.js index 524771046a0e..3f216a3e03fb 100644 --- a/spec-main/fixtures/module/preload-sandbox.js +++ b/spec-main/fixtures/module/preload-sandbox.js @@ -33,6 +33,7 @@ systemVersion: invoke(() => process.getSystemVersion()), cpuUsage: invoke(() => process.getCPUUsage()), ioCounters: invoke(() => process.getIOCounters()), + uptime: invoke(() => process.uptime()), env: process.env, execPath: process.execPath, pid: process.pid,