Bug 1644795 - Expose OS Pid on nsIDOMProcessParent. r=nika
authorAlexandre Poirot <poirot.alex@gmail.com>
Wed, 01 Jul 2020 09:09:52 +0000
changeset 538166 9cf50d4ea19fb05de3b0822f7e3aab36c96c4e63
parent 538165 7cc7ff20205ef335a54cea79690cb76181102cea
child 538167 c1a9017787af4ac640ee6f010add872066275579
push id37559
push userrmaries@mozilla.com
push dateWed, 01 Jul 2020 21:32:12 +0000
treeherdermozilla-central@597ca7dea687 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnika
bugs1644795
milestone80.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1644795 - Expose OS Pid on nsIDOMProcessParent. r=nika Differential Revision: https://phabricator.services.mozilla.com/D81122
dom/base/test/browser.ini
dom/base/test/browser_chromeutils_getalldomprocesses.js
dom/ipc/ContentParent.cpp
dom/ipc/InProcessImpl.cpp
dom/ipc/nsIDOMProcessParent.idl
--- a/dom/base/test/browser.ini
+++ b/dom/base/test/browser.ini
@@ -67,8 +67,9 @@ skip-if = (os == "win" && processor == "
 [browser_inputStream_structuredClone.js]
 [browser_multiple_popups.js]
 skip-if = os == 'win' && !debug # Bug 1505235
 support-files = browser_multiple_popups.html
 [browser_bug1554070.js]
 support-files =
   file_bug1554070_1.html
   file_bug1554070_2.html
+[browser_chromeutils_getalldomprocesses.js]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/browser_chromeutils_getalldomprocesses.js
@@ -0,0 +1,56 @@
+add_task(async function testParentProcess() {
+  const [parentProcess] = ChromeUtils.getAllDOMProcesses();
+  // browser.xhtml is in the parent process, so its domProcess is the parent process one
+  is(
+    parentProcess,
+    window.browsingContext.currentWindowGlobal.domProcess,
+    "The first element is the parent process"
+  );
+  is(
+    parentProcess.osPid,
+    Services.appinfo.processID,
+    "We got the right OS Pid"
+  );
+  is(parentProcess.childID, 0, "Parent process has childID set to 0");
+});
+
+add_task(async function testContentProcesses() {
+  info("Open a tab in a content process");
+  BrowserTestUtils.loadURI(gBrowser.selectedBrowser, "about:blank");
+  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
+
+  info("Sanity checks against all returned elements of getAllDOMProcesses");
+  const processes = ChromeUtils.getAllDOMProcesses();
+  const allPids = [],
+    allChildIDs = [];
+  for (const process of processes) {
+    ok(
+      process instanceof Ci.nsIDOMProcessParent,
+      `Each element of the array is a nsIDOMProcessParent (${process})`
+    );
+    ok(process.osPid > 0, `OS Pid looks correct ${process.osPid}`);
+    if (process == processes[0]) {
+      is(
+        process.childID,
+        0,
+        `Child ID is 0 for the parent process, which is the first element of the returned array`
+      );
+    } else {
+      ok(process.childID > 0, `Child ID looks also correct ${process.childID}`);
+    }
+
+    ok(
+      !allPids.includes(process.osPid),
+      "We only get one nsIDOMProcessParent per OS process == each osPid is different"
+    );
+    allPids.push(process.osPid);
+    ok(!allChildIDs.includes(process.childID), "Each childID is different");
+    allChildIDs.push(process.childID);
+  }
+
+  info("Search for the nsIDOMProcessParent for the opened tab");
+  const { currentWindowGlobal } = gBrowser.selectedBrowser.browsingContext;
+  const tabProcess = currentWindowGlobal.domProcess;
+  ok(processes.includes(tabProcess), "Found the tab process in the list");
+  is(tabProcess.osPid, currentWindowGlobal.osPid, "We got the right OS Pid");
+});
--- a/dom/ipc/ContentParent.cpp
+++ b/dom/ipc/ContentParent.cpp
@@ -6806,16 +6806,21 @@ mozilla::ipc::IPCResult ContentParent::R
   return aTransaction.CommitFromIPC(aContext, this);
 }
 
 NS_IMETHODIMP ContentParent::GetChildID(uint64_t* aOut) {
   *aOut = this->ChildID();
   return NS_OK;
 }
 
+NS_IMETHODIMP ContentParent::GetOsPid(int32_t* aOut) {
+  *aOut = Pid();
+  return NS_OK;
+}
+
 IPCResult ContentParent::RecvRawMessage(const JSActorMessageMeta& aMeta,
                                         const ClonedMessageData& aData,
                                         const ClonedMessageData& aStack) {
   RefPtr<JSProcessActorParent> actor;
   GetActor(aMeta.actorName(), getter_AddRefs(actor));
   if (actor) {
     StructuredCloneData data;
     data.BorrowFromClonedMessageDataForParent(aData);
--- a/dom/ipc/InProcessImpl.cpp
+++ b/dom/ipc/InProcessImpl.cpp
@@ -124,16 +124,24 @@ void InProcessChild::ActorDestroy(ActorD
 
 NS_IMETHODIMP
 InProcessParent::GetChildID(uint64_t* aChildID) {
   *aChildID = 0;
   return NS_OK;
 }
 
 NS_IMETHODIMP
+InProcessParent::GetOsPid(int32_t* aOsPid) {
+  // InProcessParent always run in the parent process,
+  // so we can return the current process id.
+  *aOsPid = base::GetCurrentProcId();
+  return NS_OK;
+}
+
+NS_IMETHODIMP
 InProcessParent::GetActor(const nsACString& aName,
                           JSProcessActorParent** aActor) {
   if (!CanSend()) {
     return NS_ERROR_DOM_INVALID_STATE_ERR;
   }
 
   // Check if this actor has already been created, and return it if it has.
   if (mProcessActors.Contains(aName)) {
--- a/dom/ipc/nsIDOMProcessParent.idl
+++ b/dom/ipc/nsIDOMProcessParent.idl
@@ -26,16 +26,21 @@ webidl JSProcessActorParent;
 [scriptable, builtinclass, uuid(81fc08b9-c901-471f-ab0d-876362eba770)]
 interface nsIDOMProcessParent: nsISupports {
   /**
    * Internal child process ID. `0` is reserved for the parent process.
    */
   [infallible] readonly attribute unsigned long long childID;
 
   /**
+   * OS ID of the process.
+   */
+  [infallible] readonly attribute long osPid;
+
+  /**
    * Lookup a JSProcessActorParent managed by this interface by name.
    */
   JSProcessActorParent getActor(in ACString name);
 
   /** Can the actor still send messages? */
   [infallible] readonly attribute boolean canSend;
 
   [notxpcom, nostdcall] ContentParentPtr asContentParent();