feat: WebContents.getOrCreateDevToolsTargetId() (#50176)

* Feat: support getDevToolsId() on WebContents

* Rename to `getOrCreateDevToolsTargetId`

* build: use spawn instead of spawnSync for build (#49774)

* Fix build

* formatting

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Kyle Cutler <67761731+kycutler@users.noreply.github.com>
This commit is contained in:
trop[bot]
2026-03-10 12:22:32 -04:00
committed by GitHub
parent cb4d31ae61
commit 5aad1e433d
4 changed files with 31 additions and 0 deletions

View File

@@ -2235,6 +2235,16 @@ Returns `string` - The identifier of a WebContents stream. This identifier can b
with `navigator.mediaDevices.getUserMedia` using a `chromeMediaSource` of `tab`.
The identifier is restricted to the web contents that it is registered to and is only valid for 10 seconds.
#### `contents.getOrCreateDevToolsTargetId()`
Returns `string` - The Chrome DevTools Protocol
[TargetID](https://chromedevtools.github.io/devtools-protocol/tot/Target/#type-TargetID)
associated with this WebContents. This is the reverse of
[`webContents.fromDevToolsTargetId()`](#webcontentsfromdevtoolstargetidtargetid).
> [!NOTE]
> This method creates a new DevTools agent for this WebContents if one does not already exist.
#### `contents.getOSProcessId()`
Returns `Integer` - The operating system `pid` of the associated renderer

View File

@@ -50,6 +50,7 @@
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/browser/desktop_streams_registry.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/file_select_listener.h"
@@ -2801,6 +2802,11 @@ std::string WebContents::GetMediaSourceID(
return id;
}
std::string WebContents::GetOrCreateDevToolsTargetId() {
auto agent_host = content::DevToolsAgentHost::GetOrCreateFor(web_contents());
return agent_host->GetId();
}
bool WebContents::IsCrashed() const {
return web_contents()->IsCrashed();
}
@@ -4667,6 +4673,8 @@ void WebContents::FillObjectTemplate(v8::Isolate* isolate,
&WebContents::SetWebRTCIPHandlingPolicy)
.SetMethod("setWebRTCUDPPortRange", &WebContents::SetWebRTCUDPPortRange)
.SetMethod("getMediaSourceId", &WebContents::GetMediaSourceID)
.SetMethod("getOrCreateDevToolsTargetId",
&WebContents::GetOrCreateDevToolsTargetId)
.SetMethod("getWebRTCIPHandlingPolicy",
&WebContents::GetWebRTCIPHandlingPolicy)
.SetMethod("getWebRTCUDPPortRange", &WebContents::GetWebRTCUDPPortRange)

View File

@@ -230,6 +230,7 @@ class WebContents final : public ExclusiveAccessContext,
v8::Local<v8::Value> GetWebRTCUDPPortRange(v8::Isolate* isolate) const;
void SetWebRTCUDPPortRange(gin::Arguments* args);
std::string GetMediaSourceID(content::WebContents* request_web_contents);
std::string GetOrCreateDevToolsTargetId();
bool IsCrashed() const;
void ForcefullyCrashRenderer();
void SetUserAgent(const std::string& user_agent);

View File

@@ -1666,6 +1666,18 @@ describe('webContents module', () => {
});
});
describe('getOrCreateDevToolsTargetId()', () => {
afterEach(closeAllWindows);
it('returns the devtools target id', async () => {
const w = new BrowserWindow({ show: false });
await w.loadURL('about:blank');
const devToolsId = w.webContents.getOrCreateDevToolsTargetId();
expect(devToolsId).to.be.a('string').that.is.not.empty();
// Verify it's the inverse of fromDevToolsTargetId
expect(webContents.fromDevToolsTargetId(devToolsId)).to.equal(w.webContents);
});
});
describe('userAgent APIs', () => {
afterEach(closeAllWindows);
it('is not empty by default', () => {