Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions specs/ServiceWorkerPostMessageSetting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Service Worker PostMessage Setting
===
# Background

This API provides a setting to expose the webview2 specific JS APIs on service worker script.

# Description

We propose adding `ServiceWorkerJSAPIsEnabled` setting API as it allows developers to expose the webview2 specific JS APIs on service worker script. When enabled this would enable the developers to use webview2's service worker postmessage APIs to communicate directly between the service worker script and webview2 host.
Comment thread
chetanpandey1266 marked this conversation as resolved.
Outdated

# Examples

## Win32 C++

```cpp
void ToggleServiceWorkerJsApiSetting()
{
wil::com_ptr<ICoreWebView2StagingSettings> webviewStagingSettings;
webviewStagingSettings = m_settings.try_query<ICoreWebView2StagingSettings>();
Comment thread
chetanpandey1266 marked this conversation as resolved.
Outdated

if (webviewStagingSettings)
{
BOOL value;
webviewStagingSettings->get_IsServiceWorkerJSAPIsEnabled(&value);
CHECK_FAILURE(webviewStagingSettings->put_IsServiceWorkerJSAPIsEnabled(!value));
MessageBox(
nullptr,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should probably use reinterpret_cast<HWND>(m_appWindow.Id().Value) as the parent window for the message box.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix.

(std::wstring(L"Service Worker JS API setting has been ") +
(!value ? L"enabled." : L"disabled."))
.c_str(),
L"Service Worker JS API Setting", MB_OK);
}
}
```

## C#/.NET

```c#
private void ToggleServiceWorkerJsApiSetting()
{
WebViewSettings.IsServiceWorkerJSAPIsEnabled = !WebViewSettings.AreDefaultScriptDialogsEnabled;

MessageBox.Show(this,
$"IsServiceWorkerJSAPIsEnabled is now set to: {WebViewSettings.IsServiceWorkerJSAPIsEnabled}",
"Trusted Origins", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Comment thread
david-risney marked this conversation as resolved.
```

# API Details

## Win32 C++
```cpp
/// A continuation of the ICoreWebView2Settings interface to manage Service Worker JS APIs.
Comment thread
chetanpandey1266 marked this conversation as resolved.
Outdated
interface ICoreWebView2StagingSettings : IUnknown {
Comment thread
chetanpandey1266 marked this conversation as resolved.
Outdated
/// Gets the `IsServiceWorkerJSAPIsEnabled` property.
[propget] HRESULT IsServiceWorkerJSAPIsEnabled([out, retval] BOOL* value);

/// Enables or disables webview2 specific Service Worker JS APIs in the WebView2.
/// When set to `TRUE`, chrome and webview objects are available in Service Workers .
/// chrome.webview exposes APIs to interact with the WebView from Service Workers.
/// The default value is `FALSE`.
/// When enabled, this setting takes effect for all the newly installed Service Workers.
Comment thread
chetanpandey1266 marked this conversation as resolved.
/// \snippet SettingsComponent.cpp ToggleServiceWorkerJSAPIsEnabled
[propput] HRESULT IsServiceWorkerJSAPIsEnabled([in] BOOL value);
}

```

## .NET/C#
```c#
namespace Microsoft.Web.WebView2.Core
{
runtimeclass CoreWebView2Settings
{
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2StagingSettings")]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Precedent is to increment a number suffix of the interface, for new properties on CoreWebView2Settings, rather than insert adjectives. (see ICoreWebView2Settings12 adding IsNonClientRegionSupportEnabled. If we haven't otherwise already changed patterns, stay consistent for new properties.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please fix like previous comment.

{
Boolean IsServiceWorkerJSAPIsEnabled { get; set; };
Comment thread
chetanpandey1266 marked this conversation as resolved.
Outdated
}
}
}
```