Skip to content

Commit 3678edf

Browse files
trop[bot]kycutler
andauthored
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>
1 parent cb4d31a commit 3678edf

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

docs/api/web-contents.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,16 @@ Returns `string` - The identifier of a WebContents stream. This identifier can b
22352235
with `navigator.mediaDevices.getUserMedia` using a `chromeMediaSource` of `tab`.
22362236
The identifier is restricted to the web contents that it is registered to and is only valid for 10 seconds.
22372237

2238+
#### `contents.getOrCreateDevToolsTargetId()`
2239+
2240+
Returns `string` - The Chrome DevTools Protocol
2241+
[TargetID](https://chromedevtools.github.io/devtools-protocol/tot/Target/#type-TargetID)
2242+
associated with this WebContents. This is the reverse of
2243+
[`webContents.fromDevToolsTargetId()`](#webcontentsfromdevtoolstargetidtargetid).
2244+
2245+
> [!NOTE]
2246+
> This method creates a new DevTools agent for this WebContents if one does not already exist.
2247+
22382248
#### `contents.getOSProcessId()`
22392249

22402250
Returns `Integer` - The operating system `pid` of the associated renderer

shell/browser/api/electron_api_web_contents.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "content/public/browser/context_menu_params.h"
5151
#include "content/public/browser/desktop_media_id.h"
5252
#include "content/public/browser/desktop_streams_registry.h"
53+
#include "content/public/browser/devtools_agent_host.h"
5354
#include "content/public/browser/download_request_utils.h"
5455
#include "content/public/browser/favicon_status.h"
5556
#include "content/public/browser/file_select_listener.h"
@@ -2801,6 +2802,11 @@ std::string WebContents::GetMediaSourceID(
28012802
return id;
28022803
}
28032804

2805+
std::string WebContents::GetOrCreateDevToolsTargetId() {
2806+
auto agent_host = content::DevToolsAgentHost::GetOrCreateFor(web_contents());
2807+
return agent_host->GetId();
2808+
}
2809+
28042810
bool WebContents::IsCrashed() const {
28052811
return web_contents()->IsCrashed();
28062812
}
@@ -4667,6 +4673,8 @@ void WebContents::FillObjectTemplate(v8::Isolate* isolate,
46674673
&WebContents::SetWebRTCIPHandlingPolicy)
46684674
.SetMethod("setWebRTCUDPPortRange", &WebContents::SetWebRTCUDPPortRange)
46694675
.SetMethod("getMediaSourceId", &WebContents::GetMediaSourceID)
4676+
.SetMethod("getOrCreateDevToolsTargetId",
4677+
&WebContents::GetOrCreateDevToolsTargetId)
46704678
.SetMethod("getWebRTCIPHandlingPolicy",
46714679
&WebContents::GetWebRTCIPHandlingPolicy)
46724680
.SetMethod("getWebRTCUDPPortRange", &WebContents::GetWebRTCUDPPortRange)

shell/browser/api/electron_api_web_contents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ class WebContents final : public ExclusiveAccessContext,
230230
v8::Local<v8::Value> GetWebRTCUDPPortRange(v8::Isolate* isolate) const;
231231
void SetWebRTCUDPPortRange(gin::Arguments* args);
232232
std::string GetMediaSourceID(content::WebContents* request_web_contents);
233+
std::string GetOrCreateDevToolsTargetId();
233234
bool IsCrashed() const;
234235
void ForcefullyCrashRenderer();
235236
void SetUserAgent(const std::string& user_agent);

spec/api-web-contents-spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,18 @@ describe('webContents module', () => {
16661666
});
16671667
});
16681668

1669+
describe('getOrCreateDevToolsTargetId()', () => {
1670+
afterEach(closeAllWindows);
1671+
it('returns the devtools target id', async () => {
1672+
const w = new BrowserWindow({ show: false });
1673+
await w.loadURL('about:blank');
1674+
const devToolsId = w.webContents.getOrCreateDevToolsTargetId();
1675+
expect(devToolsId).to.be.a('string').that.is.not.empty();
1676+
// Verify it's the inverse of fromDevToolsTargetId
1677+
expect(webContents.fromDevToolsTargetId(devToolsId)).to.equal(w.webContents);
1678+
});
1679+
});
1680+
16691681
describe('userAgent APIs', () => {
16701682
afterEach(closeAllWindows);
16711683
it('is not empty by default', () => {

0 commit comments

Comments
 (0)