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

API Review: PostWebMessageAsJson and PostWebMessageAsString APIs for workers #5170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dhveerap
Copy link
Contributor

This is a review for the new PostWebMessageAsJson and PostWebMessageAsString APIs for dedicated and service workers.

…workers (#5151)

This is a review for the new PostWebMessageAsJson and PostWebMessageAsString APIs for dedicated and service workers.
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

@AshleyScirra
Copy link

It might be useful to expose the name of dedicated workers (i.e. the name specified by new Worker(url, { name }). This would provide a way to identify a specific worker without needing to depend on the script URL.

# Description
We propose the following APIs:

**PostWebMessageAsJson**: This API gives ability to post a message to
Copy link
Contributor

Choose a reason for hiding this comment

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

These signatures intentionally align with PostWebMessageAs* from CoreWebView2 that offer the equivalent functionality on the main thread, correct?

Should documentation cross-link?
https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2?view=webview2-dotnet-1.0.3124.44

Copy link
Contributor

Choose a reason for hiding this comment

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

Please update docs of all PostWebMessage APIs in CoreWebView2, CoreWebView2Frame, and these new ones to link to one another saying something like 'See the equivalent for CorewebView2...' something along those lines.

/// `PostWebMessageAsJson` if you want to communicate using simple strings
/// rather than JSON objects.
HRESULT PostWebMessageAsString(
[in] LPCWSTR messageAsString
Copy link
Contributor

Choose a reason for hiding this comment

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

nit, param name webMessageAsString to align with CoreWebView2.PostWebMessageAsString?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please match existing methods and use: webMessageAsString.
Also for above PostWebMessageAsJson.

**PostWebMessageAsJson**: This API gives ability to post a message to
dedicated/service worker. The worker receives the message by subscribing to
message event of `self.chrome.webview`.

Copy link
Contributor

Choose a reason for hiding this comment

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

The CoreWebView2 overload of PostWebMessageAsJson that has params= (String, List) doesn't have a matching proposal here because the DOM objects in the List are only applicable on the main thread, is that right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we do want to do that but it will be in a future spec.

//! [chromeWebView]
};

self.addEventListener('install', (event) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the sample difference between self.chrome.webview.addEventListener and self.addEventListener ? Is this an existing asymmetry of dedicated v service workers?

Copy link
Contributor

Choose a reason for hiding this comment

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

self.addEventListener is for subscribing to existing web event listeners vs self.chrome.webview.addEventListener for new WebView2 related events.

}

[uuid(66833876-edba-5a60-8508-7da64504a9d2), object, pointer_default(unique)]
interface ICoreWebView2DedicatedWorker : IUnknown {
Copy link
Contributor

Choose a reason for hiding this comment

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

I see there is a CoreWebView2DedicatedWorker in .net prerelease. This interface doesn't need a "2", just checking is that because the class hasn't gone to stable release yet?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please investigate why we have this empty class in the SDK. The .NET prerelease contains this class with no members or methods. This looks like a mistake: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2dedicatedworker?view=webview2-dotnet-1.0.2950-prerelease

In the future we'll add a comment to interfaces that are in staging that we're updating

Copy link
Contributor

@david-risney david-risney left a comment

Choose a reason for hiding this comment

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

Please see feedback. Thanks!

===

# Background
Currently, if developers want to post a message to or from a worker, they must
Copy link
Contributor

Choose a reason for hiding this comment

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

Folks were confused by these comments. Provider better explanation of how this is working currently eg: CoreWebView2.PostWebMessage -> DOM window -> worker. And be sure to say main thread of web content since that can be confused for the host app's main thread.

# Description
We propose the following APIs:

**PostWebMessageAsJson**: This API gives ability to post a message to
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update docs of all PostWebMessage APIs in CoreWebView2, CoreWebView2Frame, and these new ones to link to one another saying something like 'See the equivalent for CorewebView2...' something along those lines.

**PostWebMessageAsJson**: This API gives ability to post a message to
dedicated/service worker. The worker receives the message by subscribing to
message event of `self.chrome.webview`.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we do want to do that but it will be in a future spec.

void ScenarioDedicatedWorkerPostMessage::ComputeWithDedicatedWorker(
wil::com_ptr<ICoreWebView2DedicatedWorker> dedicatedWorker)
{
TextInputDialog dialog(
Copy link
Contributor

Choose a reason for hiding this comment

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

Throughout sample please ensure we don't show synchronous UI from webview2 event handlers / async completion handlers. Using the AsyncMessageBox addresses this otherwise you'll need to explicitly get an async code to run to show the message box.

//! [chromeWebView]
};

self.addEventListener('install', (event) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

self.addEventListener is for subscribing to existing web event listeners vs self.chrome.webview.addEventListener for new WebView2 related events.

}

[uuid(66833876-edba-5a60-8508-7da64504a9d2), object, pointer_default(unique)]
interface ICoreWebView2DedicatedWorker : IUnknown {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please investigate why we have this empty class in the SDK. The .NET prerelease contains this class with no members or methods. This looks like a mistake: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2dedicatedworker?view=webview2-dotnet-1.0.2950-prerelease

In the future we'll add a comment to interfaces that are in staging that we're updating

/// `PostWebMessageAsJson` if you want to communicate using simple strings
/// rather than JSON objects.
HRESULT PostWebMessageAsString(
[in] LPCWSTR messageAsString
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please match existing methods and use: webMessageAsString.
Also for above PostWebMessageAsJson.

/// arg of the worker's `self.chrome.webview` message is a string with the same
/// value as `messageAsString`. Use this instead of
/// `PostWebMessageAsJson` if you want to communicate using simple strings
/// rather than JSON objects.
Copy link
Contributor

Choose a reason for hiding this comment

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

Please update doc comment here to say "please see PostWebMessageAsJson for additional information."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Proposal Review WebView2 API Proposal for review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants