Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add webContents.fromDevToolsTargetId() #29399

Merged

Conversation

samuelmaddock
Copy link
Member

@samuelmaddock samuelmaddock commented May 28, 2021

Description of Change

Playwright has experimental support for Electron which works pretty well for single BrowserWindow use cases. However, there are currently issues when multiple BrowserWindows are involved (microsoft/playwright#6760).

The proposed changes introduce the static method webContents.fromDevToolsTargetId(targetId) which allows for looking up a WebContents based on the stable DevTools TargetID.

Using this method will allow Playwright to properly lookup WebContents instances and get their associated BrowserWindows.

Since this enables use cases in a third-party application, it'd be great to be backported to all actively supported versions of Electron (>=11).

Checklist

Release Notes

Notes: Added webContents.fromDevToolsTargetId(targetId) to lookup a WebContents instance from an associated Chrome DevTools Protocol TargetID.

@pavelfeldman
Copy link
Contributor

@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Jun 15, 2021
Copy link
Member

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Looks good to me.

docs/api/web-contents.md Show resolved Hide resolved
Copy link
Member

@nornagon nornagon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM

Copy link
Member

@codebytere codebytere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API LGTM here as well, and agree with @nornagon above about webcontents creation.

@samuelmaddock
Copy link
Member Author

It might be easier to expose https://source.chromium.org/chromium/chromium/src/+/main:content/browser/renderer_host/frame_tree_node.h;l=118;bpv=0;bpt=1 instead.

@pavelfeldman Just so this doesn't go unaddressed, I'll look into binding this in another PR if I find it's needed for debugging specific frames. FrameTreeNode is not a public API which we want to try to avoid. If later it's found to be needed, we can bind the public API RenderFrameHost::GetDevToolsFrameToken() for Electron's WebFrameMain.

@samuelmaddock samuelmaddock force-pushed the feat/webcontents-devtools-target branch from 2a8324d to 29bebdc Compare August 25, 2021 17:36
@samuelmaddock
Copy link
Member Author

Discussion came up as to whether this is the right way forward for this API.

DevToolsAgentHost is an interface which is implemented for multiple contexts (see overrides for DevToolsAgentHost::GetType()). Some contexts are associated with a specific WebContents while others are not.

For this specific use case, we want to get the associated WebContents for a DevToolsAgentHost currently attached to a frame. We do so by looking up the DevToolsAgentHost by its unique TargetID. If it exists and is of the expected DevToolsAgentHost subclass, it will have a WebContents accessible from DevToolsAgentHost::GetWebContents().

This approach is sufficient for the problem being solved.


One alternative design would be to expand the capabilities of the Debugger API, currently only accessible from a WebContents.

interface Debugger {
  /** Get a debugger from an existing, attached DevTools host ID. */
  static fromTargetId(id: string): Debugger | undefined;

  /** An alternative to webContents.fromDevToolsTargetId() */
  webContents?: WebContents;

  /**
    * The type of DevToolsAgentHost.
    * https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/devtools_agent_host_impl.cc;l=43-50;drc=2205e9942d2fc0ef177f6802683345a1822da88e
    */
  type: 'page' | 'iframe' | 'worker' | 'shared_worker' | /* etc. */;
}

With this approach, we could create a Debugger instance by looking up an existing DevToolsAgentHost's ID (Target ID). Then its associated WebContents would be accessible via a getter.

This is more complex and would involve careful memory considerations with a likely needed introduction of the Pinnable interface to Debugger.

I'm not currently aware of another use case that this design would solve so the complexity might not be worth it. Another exported module would be needed as well for the static method introduced to Debugger.

Copy link
Member

@nornagon nornagon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-✅ , I think this approach is fine.

@nornagon nornagon merged commit 352ac21 into electron:main Aug 27, 2021
@release-clerk
Copy link

release-clerk bot commented Aug 27, 2021

Release Notes Persisted

Added webContents.fromDevToolsTargetId(targetId) to lookup a WebContents instance from an associated Chrome DevTools Protocol TargetID.

@trop
Copy link
Contributor

trop bot commented Aug 27, 2021

I have automatically backported this PR to "12-x-y", please check out #30730

@trop
Copy link
Contributor

trop bot commented Aug 27, 2021

I have automatically backported this PR to "13-x-y", please check out #30731

@trop trop bot removed the target/12-x-y label Aug 27, 2021
@trop
Copy link
Contributor

trop bot commented Aug 27, 2021

I have automatically backported this PR to "14-x-y", please check out #30732

@trop
Copy link
Contributor

trop bot commented Aug 27, 2021

I have automatically backported this PR to "15-x-y", please check out #30733

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-review/requested 🗳 semver/minor backwards-compatible functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants