From f375e8a7db14e199736b3ef390c107e4b329d593 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Tue, 18 Apr 2017 12:31:20 +0200 Subject: [PATCH 1/5] Add OS process id to web-contents --- atom/browser/api/atom_api_web_contents.cc | 8 ++++++++ atom/browser/api/atom_api_web_contents.h | 1 + 2 files changed, 9 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index d3e7bae2e412..9ea413fcccee 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -46,6 +46,7 @@ #include "chrome/browser/printing/print_preview_message_handler.h" #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/ssl/security_state_tab_helper.h" +#include "base/process/process_handle.h" #include "content/browser/frame_host/navigation_entry_impl.h" #include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" @@ -77,6 +78,7 @@ #include "third_party/WebKit/public/web/WebFindOptions.h" #include "ui/display/screen.h" + #if !defined(OS_MACOSX) #include "ui/aura/window.h" #endif @@ -959,6 +961,11 @@ int WebContents::GetProcessID() const { return web_contents()->GetRenderProcessHost()->GetID(); } +int WebContents::GetOSProcessID() const { + auto process_handle = web_contents()->GetRenderProcessHost()->GetHandle(); + return base::GetProcId(process_handle); +} + WebContents::Type WebContents::GetType() const { return type_; } @@ -1708,6 +1715,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .MakeDestroyable() .SetMethod("getId", &WebContents::GetID) .SetMethod("getProcessId", &WebContents::GetProcessID) + .SetMethod("getOSProcessId", &WebContents::GetOSProcessID) .SetMethod("equal", &WebContents::Equal) .SetMethod("_loadURL", &WebContents::LoadURL) .SetMethod("downloadURL", &WebContents::DownloadURL) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 1301ed15f7fa..a608c8a401ea 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -82,6 +82,7 @@ class WebContents : public mate::TrackableObject, int64_t GetID() const; int GetProcessID() const; + int GetOSProcessID() const; Type GetType() const; bool Equal(const WebContents* web_contents) const; void LoadURL(const GURL& url, const mate::Dictionary& options); From 9aff17afeafb85a94860253f59a16a28d6c483fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Tue, 18 Apr 2017 13:44:31 +0200 Subject: [PATCH 2/5] :shirt: alphabetical order --- atom/browser/api/atom_api_web_contents.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 9ea413fcccee..d5395a6872d2 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -39,6 +39,7 @@ #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/options_switches.h" +#include "base/process/process_handle.h" #include "base/strings/utf_string_conversions.h" #include "base/threading/thread_task_runner_handle.h" #include "brightray/browser/inspectable_web_contents.h" @@ -46,7 +47,6 @@ #include "chrome/browser/printing/print_preview_message_handler.h" #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/ssl/security_state_tab_helper.h" -#include "base/process/process_handle.h" #include "content/browser/frame_host/navigation_entry_impl.h" #include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" @@ -78,7 +78,6 @@ #include "third_party/WebKit/public/web/WebFindOptions.h" #include "ui/display/screen.h" - #if !defined(OS_MACOSX) #include "ui/aura/window.h" #endif From 2f297b18936a846d7e43f12435554fd474858b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Mon, 15 May 2017 19:57:19 +0200 Subject: [PATCH 3/5] :white_check_mark: Add simple spec for getOSProcessId() --- spec/api-web-contents-spec.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index f081b3900944..166c752f18ff 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -324,6 +324,22 @@ describe('webContents module', function () { }) }) + describe('getOSProcessId()', function() { + it('returns a valid procress id', function() { + + // load URL otherwise getOSProcessId() returns 0 + w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'focus-web-contents.html')) + + const specWebContents = w.webContents + let pid = null + assert.doesNotThrow(function () { + pid = specWebContents.getOSProcessId() + }) + assert(typeof pid === 'number', 'is a number') + assert(pid > 0 , 'superior to 0') + }) + }) + describe('zoom api', () => { const zoomScheme = remote.getGlobal('zoomScheme') const hostZoomMap = { From 77bf4c8ebb3ca43307b47ead4ce22ceaacf1d946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Mon, 15 May 2017 20:02:20 +0200 Subject: [PATCH 4/5] :memo: document getOSProcessId() --- docs/api/web-contents.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 98b1cbd2f241..5f86a04048a5 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1259,6 +1259,10 @@ Setting the WebRTC IP handling policy allows you to control which IPs are exposed via WebRTC. See [BrowserLeaks](https://browserleaks.com/webrtc) for more details. +#### `contents.getOSProcessId()` + +Returns `Integer` - The `pid` of the associated renderer process. + ### Instance Properties #### `contents.id` From 9b776369e66b103bf81980dfc36c1171460eb4c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Lach=C3=A8ze?= Date: Mon, 15 May 2017 21:00:13 +0200 Subject: [PATCH 5/5] :shirt: --- spec/api-web-contents-spec.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 166c752f18ff..457a0b5ffb20 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -324,9 +324,8 @@ describe('webContents module', function () { }) }) - describe('getOSProcessId()', function() { - it('returns a valid procress id', function() { - + describe('getOSProcessId()', function () { + it('returns a valid procress id', function () { // load URL otherwise getOSProcessId() returns 0 w.loadURL('file://' + path.join(__dirname, 'fixtures', 'pages', 'focus-web-contents.html')) @@ -336,7 +335,7 @@ describe('webContents module', function () { pid = specWebContents.getOSProcessId() }) assert(typeof pid === 'number', 'is a number') - assert(pid > 0 , 'superior to 0') + assert(pid > 0, 'superior to 0') }) })