From 19cb5bad9413ec3e7ffc724f2c574a0576266f15 Mon Sep 17 00:00:00 2001 From: Husayn Date: Fri, 10 Aug 2018 07:03:30 -0700 Subject: [PATCH] feat: Add creationTime function to process (#13542) * Add process creation time * Making docs clear for process creation time * Address comments for process creation time * Add process info cc file * fixing comments around documentation * Update doc for return val * Capitalize number in docs * chore: bump electron-typescript-definitions --- atom/common/api/atom_bindings.cc | 12 ++++++++++++ atom/common/api/atom_bindings.h | 1 + docs/api/process.md | 7 +++++++ package-lock.json | 12 ++++++------ package.json | 2 +- spec/api-process-spec.js | 7 +++++++ 6 files changed, 34 insertions(+), 7 deletions(-) diff --git a/atom/common/api/atom_bindings.cc b/atom/common/api/atom_bindings.cc index f1d5c49cd4dd..3681cfd7abb4 100644 --- a/atom/common/api/atom_bindings.cc +++ b/atom/common/api/atom_bindings.cc @@ -14,6 +14,7 @@ #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/node_includes.h" #include "base/logging.h" +#include "base/process/process_info.h" #include "base/process/process_metrics_iocounters.h" #include "base/sys_info.h" #include "native_mate/dictionary.h" @@ -55,6 +56,7 @@ void AtomBindings::BindTo(v8::Isolate* isolate, v8::Local process) { dict.SetMethod("log", &Log); dict.SetMethod("getHeapStatistics", &GetHeapStatistics); dict.SetMethod("getProcessMemoryInfo", &GetProcessMemoryInfo); + dict.SetMethod("getCreationTime", &GetCreationTime); dict.SetMethod("getSystemMemoryInfo", &GetSystemMemoryInfo); dict.SetMethod("getCPUUsage", base::Bind(&AtomBindings::GetCPUUsage, base::Unretained(this))); @@ -177,6 +179,16 @@ v8::Local AtomBindings::GetProcessMemoryInfo(v8::Isolate* isolate) { return dict.GetHandle(); } +// static +v8::Local AtomBindings::GetCreationTime(v8::Isolate* isolate) { + auto timeValue = base::CurrentProcessInfo::CreationTime(); + if (timeValue.is_null()) { + return v8::Null(isolate); + } + double jsTime = timeValue.ToJsTime(); + return v8::Number::New(isolate, jsTime); +} + // static v8::Local AtomBindings::GetSystemMemoryInfo(v8::Isolate* isolate, mate::Arguments* args) { diff --git a/atom/common/api/atom_bindings.h b/atom/common/api/atom_bindings.h index 46a5f36bdcea..5cf339939a91 100644 --- a/atom/common/api/atom_bindings.h +++ b/atom/common/api/atom_bindings.h @@ -37,6 +37,7 @@ class AtomBindings { static void Hang(); static v8::Local GetHeapStatistics(v8::Isolate* isolate); static v8::Local GetProcessMemoryInfo(v8::Isolate* isolate); + static v8::Local GetCreationTime(v8::Isolate* isolate); static v8::Local GetSystemMemoryInfo(v8::Isolate* isolate, mate::Arguments* args); v8::Local GetCPUUsage(v8::Isolate* isolate); diff --git a/docs/api/process.md b/docs/api/process.md index e6b34290b92c..6598135d177d 100644 --- a/docs/api/process.md +++ b/docs/api/process.md @@ -111,6 +111,13 @@ The `process` object has the following methods: Causes the main thread of the current process crash. +### `process.getCreationTime()` + +Returns `Number | null` - The number of milliseconds since epoch, or `null` if the information is unavailable + +Indicates the creation time of the application. +The time is represented as number of milliseconds since epoch. It returns null if it is unable to get the process creation time. + ### `process.getCPUUsage()` Returns [`CPUUsage`](structures/cpu-usage.md) diff --git a/package-lock.json b/package-lock.json index 5a707a79e364..ac958fdabe3f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@types/node": { - "version": "7.0.66", - "resolved": "https://registry.npmjs.org/@types/node/-/node-7.0.66.tgz", - "integrity": "sha512-W11u5kUNSX2+N6bJ7rPyLW4N98/xzrZg8apRoTwC0zbFjIie//oxgKAvqkQNQ97KVchB49ost74kgzoeDiE+Uw==", + "version": "7.0.69", + "resolved": "https://registry.npmjs.org/@types/node/-/node-7.0.69.tgz", + "integrity": "sha512-S5NC8HV6HnRipg8nC0j30TPl7ktXjRTKqgyINLNe8K/64UJUI8Lq0sRopXC0hProsV2F5ibj8IqPkl1xpGggrw==", "dev": true }, "JSONStream": { @@ -2517,9 +2517,9 @@ } }, "electron-typescript-definitions": { - "version": "1.3.5", - "resolved": "https://registry.npmjs.org/electron-typescript-definitions/-/electron-typescript-definitions-1.3.5.tgz", - "integrity": "sha512-7Ryb7AN0Re63oeX3wHytjX0ScVlDcgj+GSBi6Q8aSNpo4B/sffAgZK6P/b4idqF26hp2jPJWcjBMizUb0VNfAQ==", + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/electron-typescript-definitions/-/electron-typescript-definitions-1.3.6.tgz", + "integrity": "sha512-Wz6tPaaZ22lbfWwF9bzVSki7LMW96xilCT1M8eeNYZG6LcpA9x34jlrAZg5HZWTHXR2tfbTQhSmrY+FEVulD/A==", "dev": true, "requires": { "@types/node": "^7.0.18", diff --git a/package.json b/package.json index fa9badc8da05..02d3af350659 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "dugite": "^1.45.0", "electabul": "~0.0.4", "electron-docs-linter": "^2.3.4", - "electron-typescript-definitions": "^1.3.5", + "electron-typescript-definitions": "^1.3.6", "github": "^9.2.0", "html-entities": "^1.2.1", "husky": "^0.14.3", diff --git a/spec/api-process-spec.js b/spec/api-process-spec.js index d7465ca5d9d1..ad8f1faea142 100644 --- a/spec/api-process-spec.js +++ b/spec/api-process-spec.js @@ -1,6 +1,13 @@ const {expect} = require('chai') describe('process module', () => { + describe('process.getCreationTime()', () => { + it('returns a creation time', () => { + const creationTime = process.getCreationTime() + expect(creationTime).to.be.a('number').and.be.at.least(0) + }) + }) + describe('process.getCPUUsage()', () => { it('returns a cpu usage object', () => { const cpuUsage = process.getCPUUsage()