Browse Source

Merge pull request #9222 from alexstrat/add-os-process-id

Add OS pid to web-contents
Kevin Sawicki 8 years ago
parent
commit
d79ac8d9c1

+ 7 - 0
atom/browser/api/atom_api_web_contents.cc

@@ -41,6 +41,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"
@@ -1007,6 +1008,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_;
 }
@@ -1754,6 +1760,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)

+ 1 - 0
atom/browser/api/atom_api_web_contents.h

@@ -83,6 +83,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
 
   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);

+ 4 - 0
docs/api/web-contents.md

@@ -1289,6 +1289,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`

+ 15 - 0
spec/api-web-contents-spec.js

@@ -324,6 +324,21 @@ 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 = {