Skip to content

Commit aa157c3

Browse files
miniaknornagon
andauthored
feat: add osProcessId / name properties to webFrameMain (electron#26093)
* feat: add osProcessId / name properties to webFrameMain * Update docs/api/web-frame-main.md Co-authored-by: Jeremy Rose <jeremya@chromium.org> Co-authored-by: Jeremy Rose <jeremya@chromium.org>
1 parent 30b5e15 commit aa157c3

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

docs/api/web-frame-main.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,18 @@ content. The identifier is fixed at the creation of the frame and stays
122122
constant for the lifetime of the frame. When the frame is removed, the id is
123123
not used again.
124124

125+
#### `frame.name` _Readonly_
126+
127+
A `String` representing the frame name.
128+
129+
#### `frame.osProcessId` _Readonly_
130+
131+
An `Integer` representing the operating system `pid` of the process which owns this frame.
132+
125133
#### `frame.processId` _Readonly_
126134

127-
An `Integer` representing the id of the process which owns this frame.
135+
An `Integer` representing the Chromium internal `pid` of the process which owns this frame.
136+
This is not the same as the OS process ID; to read that use `frame.osProcessId`.
128137

129138
#### `frame.routingId` _Readonly_
130139

shell/browser/api/electron_api_web_contents.cc

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,18 +1590,6 @@ base::ProcessId WebContents::GetOSProcessID() const {
15901590
return base::GetProcId(process_handle);
15911591
}
15921592

1593-
base::ProcessId WebContents::GetOSProcessIdForFrame(
1594-
const std::string& name,
1595-
const std::string& document_url) const {
1596-
for (auto* frame : web_contents()->GetAllFrames()) {
1597-
if (frame->GetFrameName() == name &&
1598-
frame->GetLastCommittedURL().spec() == document_url) {
1599-
return base::GetProcId(frame->GetProcess()->GetProcess().Handle());
1600-
}
1601-
}
1602-
return base::kNullProcessId;
1603-
}
1604-
16051593
WebContents::Type WebContents::GetType() const {
16061594
return type_;
16071595
}
@@ -2941,8 +2929,6 @@ v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
29412929
&WebContents::SetBackgroundThrottling)
29422930
.SetMethod("getProcessId", &WebContents::GetProcessID)
29432931
.SetMethod("getOSProcessId", &WebContents::GetOSProcessID)
2944-
.SetMethod("_getOSProcessIdForFrame",
2945-
&WebContents::GetOSProcessIdForFrame)
29462932
.SetMethod("equal", &WebContents::Equal)
29472933
.SetMethod("_loadURL", &WebContents::LoadURL)
29482934
.SetMethod("downloadURL", &WebContents::DownloadURL)

shell/browser/api/electron_api_web_contents.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ class WebContents : public gin::Wrappable<WebContents>,
209209
void SetBackgroundThrottling(bool allowed);
210210
int GetProcessID() const;
211211
base::ProcessId GetOSProcessID() const;
212-
base::ProcessId GetOSProcessIdForFrame(const std::string& name,
213-
const std::string& document_url) const;
214212
Type GetType() const;
215213
bool Equal(const WebContents* web_contents) const;
216214
void LoadURL(const GURL& url, const gin_helper::Dictionary& options);

shell/browser/api/electron_api_web_frame_main.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ int WebFrameMain::FrameTreeNodeID(v8::Isolate* isolate) const {
120120
return render_frame_->GetFrameTreeNodeId();
121121
}
122122

123+
std::string WebFrameMain::Name(v8::Isolate* isolate) const {
124+
if (!CheckRenderFrame())
125+
return std::string();
126+
return render_frame_->GetFrameName();
127+
}
128+
129+
base::ProcessId WebFrameMain::OSProcessID(v8::Isolate* isolate) const {
130+
if (!CheckRenderFrame())
131+
return -1;
132+
base::ProcessHandle process_handle =
133+
render_frame_->GetProcess()->GetProcess().Handle();
134+
return base::GetProcId(process_handle);
135+
}
136+
123137
int WebFrameMain::ProcessID(v8::Isolate* isolate) const {
124138
if (!CheckRenderFrame())
125139
return -1;
@@ -210,6 +224,8 @@ gin::ObjectTemplateBuilder WebFrameMain::GetObjectTemplateBuilder(
210224
.SetMethod("executeJavaScript", &WebFrameMain::ExecuteJavaScript)
211225
.SetMethod("reload", &WebFrameMain::Reload)
212226
.SetProperty("frameTreeNodeId", &WebFrameMain::FrameTreeNodeID)
227+
.SetProperty("name", &WebFrameMain::Name)
228+
.SetProperty("osProcessId", &WebFrameMain::OSProcessID)
213229
.SetProperty("processId", &WebFrameMain::ProcessID)
214230
.SetProperty("routingId", &WebFrameMain::RoutingID)
215231
.SetProperty("url", &WebFrameMain::URL)

shell/browser/api/electron_api_web_frame_main.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <string>
1010
#include <vector>
1111

12+
#include "base/process/process.h"
1213
#include "gin/handle.h"
1314
#include "gin/wrappable.h"
1415

@@ -68,6 +69,8 @@ class WebFrameMain : public gin::Wrappable<WebFrameMain> {
6869
bool Reload(v8::Isolate* isolate);
6970

7071
int FrameTreeNodeID(v8::Isolate* isolate) const;
72+
std::string Name(v8::Isolate* isolate) const;
73+
base::ProcessId OSProcessID(v8::Isolate* isolate) const;
7174
int ProcessID(v8::Isolate* isolate) const;
7275
int RoutingID(v8::Isolate* isolate) const;
7376
GURL URL(v8::Isolate* isolate) const;

spec-main/api-subframe-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ ifdescribe(process.platform !== 'linux')('cross-site frame sandboxing', () => {
216216
await w.loadURL(serverUrl);
217217

218218
const pidMain = w.webContents.getOSProcessId();
219-
const pidFrame = (w.webContents as any)._getOSProcessIdForFrame('frame', crossSiteUrl);
219+
const pidFrame = w.webContents.mainFrame.frames.find(f => f.name === 'frame')!.osProcessId;
220220

221221
const metrics = app.getAppMetrics();
222222
const isProcessSandboxed = function (pid: number) {

spec-main/api-web-frame-main-spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ describe('webFrameMain module', () => {
127127
await w.loadFile(path.join(subframesPath, 'frame.html'));
128128
const webFrame = w.webContents.mainFrame;
129129
expect(webFrame).to.haveOwnProperty('frameTreeNodeId');
130+
expect(webFrame).to.haveOwnProperty('name');
131+
expect(webFrame).to.haveOwnProperty('osProcessId');
130132
expect(webFrame).to.haveOwnProperty('processId');
131133
expect(webFrame).to.haveOwnProperty('routingId');
132134
});

0 commit comments

Comments
 (0)