Skip to content

[Feature]: Add sessionId support to DevToolsProtocolHelper #5258

Open
@AFatNiBBa

Description

@AFatNiBBa

Describe the feature/enhancement you need

I started using the DevToolsProtocolHelper and it's incredibly useful, its only problem is that it doesn't provide any way of calling CoreWebView2.CallDevToolsProtocolMethodForSessionAsync() so I can never use it when I need to execute the method on a specific session

The scenario/use case where you would use this feature

I want to execute DevToolsProtocolHelper.Page.PrintToPDFAsync() on a new session created with DevToolsProtocolHelper.Target.CreateTargetAsync() and DevToolsProtocolHelper.Target.AttachToTargetAsync()

How important is this request to you?

Nice to have. There are other ways to tackle this, but having official API support would be beneficial.

Suggested implementation

Instead of passing the CoreWebView2 to DevToolsProtocolHelper and its domains, it would be wise to pass an interface with one of two implementations

namespace Microsoft.Web.WebView2.Core.DevToolsProtocolExtension
{
    public static class DevToolsExtension
    {
        public static DevToolsProtocolHelper GetDevToolsProtocolHelper(this CoreWebView2 coreWebView2)
        {
            return new DevToolsProtocolHelper(new BaseSessionDevToolsProtocolProvider(coreWebView2));
        }

        public static DevToolsProtocolHelper GetDevToolsProtocolHelper(this CoreWebView2 coreWebView2, string sessionId)
        {
            return new DevToolsProtocolHelper(new CustomSessionDevToolsProtocolProvider(coreWebView2, sessionId));
        }
    }
}

internal interface IDevToolsProtocolProvider
{
    Task<string> CallDevToolsProtocolMethodAsync(string methodName, string parametersAsJson);
}

internal class BaseSessionDevToolsProtocolProvider(CoreWebView2 core) : IDevToolsProtocolProvider
{
    public Task<string> CallDevToolsProtocolMethodAsync(string methodName, string parametersAsJson) => core.CallDevToolsProtocolMethodAsync(methodName, parametersAsJson);
}

internal class CustomSessionDevToolsProtocolProvider(CoreWebView2 core, string sessionId) : IDevToolsProtocolProvider
{
    public Task<string> CallDevToolsProtocolMethodAsync(string methodName, string parametersAsJson) => core.CallDevToolsProtocolMethodForSessionAsync(sessionId, methodName, parametersAsJson);
}

What does your app do? Is there a pending deadline for this request?

My app shows some documents that I would like to print, but I need to do so in a new document because in the main page there are some menus that shouldn't be printed.
As a requirement I mustn't show the default print UI, so I can't just put all in an iframe and then call the Window.print() on that.
I have a deadline but I can call CoreWebView2.CallDevToolsProtocolMethodForSessionAsync() for the time being

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions