|
| 1 | +Service Worker PostMessage Setting |
| 2 | +=== |
| 3 | +# Background |
| 4 | + |
| 5 | +This API provides a setting to expose the webview2 specific JS APIs on service worker script. |
| 6 | + |
| 7 | +# Description |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +# Examples |
| 12 | + |
| 13 | +## Win32 C++ |
| 14 | + |
| 15 | +```cpp |
| 16 | +void ToggleServiceWorkerJsApiSetting() |
| 17 | +{ |
| 18 | + wil::com_ptr<ICoreWebView2StagingSettings> webviewStagingSettings; |
| 19 | + webviewStagingSettings = m_settings.try_query<ICoreWebView2StagingSettings>(); |
| 20 | + |
| 21 | + if (webviewStagingSettings) |
| 22 | + { |
| 23 | + BOOL value; |
| 24 | + webviewStagingSettings->get_IsServiceWorkerJSAPIsEnabled(&value); |
| 25 | + CHECK_FAILURE(webviewStagingSettings->put_IsServiceWorkerJSAPIsEnabled(!value)); |
| 26 | + MessageBox( |
| 27 | + nullptr, |
| 28 | + (std::wstring(L"Service Worker JS API setting has been ") + |
| 29 | + (!value ? L"enabled." : L"disabled.")) |
| 30 | + .c_str(), |
| 31 | + L"Service Worker JS API Setting", MB_OK); |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +## C#/.NET |
| 37 | + |
| 38 | +```c# |
| 39 | +WebViewSettings.IsServiceWorkerJSAPIsEnabled = !WebViewSettings.AreDefaultScriptDialogsEnabled; |
| 40 | + |
| 41 | +MessageBox.Show(this, |
| 42 | + $"IsServiceWorkerJSAPIsEnabled is now set to: {WebViewSettings.IsServiceWorkerJSAPIsEnabled}", |
| 43 | + "Trusted Origins", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| 44 | +``` |
| 45 | + |
| 46 | +# API Details |
| 47 | + |
| 48 | +## Win32 C++ |
| 49 | +```cpp |
| 50 | +/// A continuation of the ICoreWebView2Settings interface to manage Service Worker JS APIs. |
| 51 | +interface ICoreWebView2StagingSettings : IUnknown { |
| 52 | + /// Gets the `IsServiceWorkerJSAPIsEnabled` property. |
| 53 | + [propget] HRESULT IsServiceWorkerJSAPIsEnabled([out, retval] BOOL* value); |
| 54 | + |
| 55 | + /// Enables or disables webview2 specific Service Worker JS APIs in the WebView2. |
| 56 | + /// When set to `TRUE`, chrome and webview objects are available in Service Workers . |
| 57 | + /// chrome.webview exposes APIs to interact with the WebView from Service Workers. |
| 58 | + /// The default value is `FALSE`. |
| 59 | + /// When enabled, this setting takes effect for all the newly registered Service Workers. |
| 60 | + /// \snippet SettingsComponent.cpp ToggleServiceWorkerJSAPIsEnabled |
| 61 | + [propput] HRESULT IsServiceWorkerJSAPIsEnabled([in] BOOL value); |
| 62 | +} |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +## .NET/C# |
| 67 | +```c# |
| 68 | +namespace Microsoft.Web.WebView2.Core |
| 69 | +{ |
| 70 | + runtimeclass CoreWebView2Settings |
| 71 | + { |
| 72 | + [interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2StagingSettings")] |
| 73 | + { |
| 74 | + Boolean IsServiceWorkerJSAPIsEnabled { get; set; }; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
0 commit comments