From aa157c3f0568d1ff166bbe547d02f3faa478c8e8 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 26 Oct 2020 04:03:34 +0100 Subject: [PATCH] feat: add osProcessId / name properties to webFrameMain (#26093) * feat: add osProcessId / name properties to webFrameMain * Update docs/api/web-frame-main.md Co-authored-by: Jeremy Rose Co-authored-by: Jeremy Rose --- docs/api/web-frame-main.md | 11 ++++++++++- shell/browser/api/electron_api_web_contents.cc | 14 -------------- shell/browser/api/electron_api_web_contents.h | 2 -- shell/browser/api/electron_api_web_frame_main.cc | 16 ++++++++++++++++ shell/browser/api/electron_api_web_frame_main.h | 3 +++ spec-main/api-subframe-spec.ts | 2 +- spec-main/api-web-frame-main-spec.ts | 2 ++ 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/docs/api/web-frame-main.md b/docs/api/web-frame-main.md index 4a3598e32b..fa3c00a28a 100644 --- a/docs/api/web-frame-main.md +++ b/docs/api/web-frame-main.md @@ -122,9 +122,18 @@ content. The identifier is fixed at the creation of the frame and stays constant for the lifetime of the frame. When the frame is removed, the id is not used again. +#### `frame.name` _Readonly_ + +A `String` representing the frame name. + +#### `frame.osProcessId` _Readonly_ + +An `Integer` representing the operating system `pid` of the process which owns this frame. + #### `frame.processId` _Readonly_ -An `Integer` representing the id of the process which owns this frame. +An `Integer` representing the Chromium internal `pid` of the process which owns this frame. +This is not the same as the OS process ID; to read that use `frame.osProcessId`. #### `frame.routingId` _Readonly_ diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index afb915e0f9..8554e1ed6a 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1590,18 +1590,6 @@ base::ProcessId WebContents::GetOSProcessID() const { return base::GetProcId(process_handle); } -base::ProcessId WebContents::GetOSProcessIdForFrame( - const std::string& name, - const std::string& document_url) const { - for (auto* frame : web_contents()->GetAllFrames()) { - if (frame->GetFrameName() == name && - frame->GetLastCommittedURL().spec() == document_url) { - return base::GetProcId(frame->GetProcess()->GetProcess().Handle()); - } - } - return base::kNullProcessId; -} - WebContents::Type WebContents::GetType() const { return type_; } @@ -2941,8 +2929,6 @@ v8::Local WebContents::FillObjectTemplate( &WebContents::SetBackgroundThrottling) .SetMethod("getProcessId", &WebContents::GetProcessID) .SetMethod("getOSProcessId", &WebContents::GetOSProcessID) - .SetMethod("_getOSProcessIdForFrame", - &WebContents::GetOSProcessIdForFrame) .SetMethod("equal", &WebContents::Equal) .SetMethod("_loadURL", &WebContents::LoadURL) .SetMethod("downloadURL", &WebContents::DownloadURL) diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 11e8a85921..6153b6f44f 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -209,8 +209,6 @@ class WebContents : public gin::Wrappable, void SetBackgroundThrottling(bool allowed); int GetProcessID() const; base::ProcessId GetOSProcessID() const; - base::ProcessId GetOSProcessIdForFrame(const std::string& name, - const std::string& document_url) const; Type GetType() const; bool Equal(const WebContents* web_contents) const; void LoadURL(const GURL& url, const gin_helper::Dictionary& options); diff --git a/shell/browser/api/electron_api_web_frame_main.cc b/shell/browser/api/electron_api_web_frame_main.cc index d7c8f45fa9..6a25aab8c7 100644 --- a/shell/browser/api/electron_api_web_frame_main.cc +++ b/shell/browser/api/electron_api_web_frame_main.cc @@ -120,6 +120,20 @@ int WebFrameMain::FrameTreeNodeID(v8::Isolate* isolate) const { return render_frame_->GetFrameTreeNodeId(); } +std::string WebFrameMain::Name(v8::Isolate* isolate) const { + if (!CheckRenderFrame()) + return std::string(); + return render_frame_->GetFrameName(); +} + +base::ProcessId WebFrameMain::OSProcessID(v8::Isolate* isolate) const { + if (!CheckRenderFrame()) + return -1; + base::ProcessHandle process_handle = + render_frame_->GetProcess()->GetProcess().Handle(); + return base::GetProcId(process_handle); +} + int WebFrameMain::ProcessID(v8::Isolate* isolate) const { if (!CheckRenderFrame()) return -1; @@ -210,6 +224,8 @@ gin::ObjectTemplateBuilder WebFrameMain::GetObjectTemplateBuilder( .SetMethod("executeJavaScript", &WebFrameMain::ExecuteJavaScript) .SetMethod("reload", &WebFrameMain::Reload) .SetProperty("frameTreeNodeId", &WebFrameMain::FrameTreeNodeID) + .SetProperty("name", &WebFrameMain::Name) + .SetProperty("osProcessId", &WebFrameMain::OSProcessID) .SetProperty("processId", &WebFrameMain::ProcessID) .SetProperty("routingId", &WebFrameMain::RoutingID) .SetProperty("url", &WebFrameMain::URL) diff --git a/shell/browser/api/electron_api_web_frame_main.h b/shell/browser/api/electron_api_web_frame_main.h index 4eb376a4db..5c5801277c 100644 --- a/shell/browser/api/electron_api_web_frame_main.h +++ b/shell/browser/api/electron_api_web_frame_main.h @@ -9,6 +9,7 @@ #include #include +#include "base/process/process.h" #include "gin/handle.h" #include "gin/wrappable.h" @@ -68,6 +69,8 @@ class WebFrameMain : public gin::Wrappable { bool Reload(v8::Isolate* isolate); int FrameTreeNodeID(v8::Isolate* isolate) const; + std::string Name(v8::Isolate* isolate) const; + base::ProcessId OSProcessID(v8::Isolate* isolate) const; int ProcessID(v8::Isolate* isolate) const; int RoutingID(v8::Isolate* isolate) const; GURL URL(v8::Isolate* isolate) const; diff --git a/spec-main/api-subframe-spec.ts b/spec-main/api-subframe-spec.ts index eb3c8b5b0a..b4a5683596 100644 --- a/spec-main/api-subframe-spec.ts +++ b/spec-main/api-subframe-spec.ts @@ -216,7 +216,7 @@ ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => { await w.loadURL(serverUrl); const pidMain = w.webContents.getOSProcessId(); - const pidFrame = (w.webContents as any)._getOSProcessIdForFrame('frame', crossSiteUrl); + const pidFrame = w.webContents.mainFrame.frames.find(f => f.name === 'frame')!.osProcessId; const metrics = app.getAppMetrics(); const isProcessSandboxed = function (pid: number) { diff --git a/spec-main/api-web-frame-main-spec.ts b/spec-main/api-web-frame-main-spec.ts index 313b9a4c16..0507bf0f3c 100644 --- a/spec-main/api-web-frame-main-spec.ts +++ b/spec-main/api-web-frame-main-spec.ts @@ -127,6 +127,8 @@ describe('webFrameMain module', () => { await w.loadFile(path.join(subframesPath, 'frame.html')); const webFrame = w.webContents.mainFrame; expect(webFrame).to.haveOwnProperty('frameTreeNodeId'); + expect(webFrame).to.haveOwnProperty('name'); + expect(webFrame).to.haveOwnProperty('osProcessId'); expect(webFrame).to.haveOwnProperty('processId'); expect(webFrame).to.haveOwnProperty('routingId'); });