Skip to content

Commit 27f58c9

Browse files
Service worker JS API should be exposed in profile
1 parent df752eb commit 27f58c9

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

specs/ServiceWorkerPostMessageSetting.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Service Worker PostMessage Setting
1+
WebView2 Script APIs for Service Workers
22
===
33
# Background
44

@@ -7,7 +7,7 @@ worker script.
77

88
# Description
99

10-
We propose adding the `AreWebViewScriptApisEnabledForServiceWorkers` setting
10+
We propose adding the `AreWebViewScriptApisEnabledForServiceWorkers` setting
1111
API to control the exposure of WebView2-specific JavaScript APIs in service
1212
worker scripts. When enabled, developers can use WebView2's service worker
1313
postmessage APIs to communicate directly between service worker scripts and
@@ -37,24 +37,24 @@ void ToggleServiceWorkerJsApiSetting()
3737
L"});",
3838
nullptr);
3939

40-
wil::com_ptr<ICoreWebView2Settings> webViewSettings;
41-
CHECK_FAILURE(m_webView->get_Settings(&webViewSettings));
42-
auto webViewSettingsStaging =
43-
webViewSettings.try_query<ICoreWebView2StagingSettings>();
40+
wil::com_ptr<ICoreWebView2Profile> webView2Profile;
41+
CHECK_FAILURE(webView2_13->get_Profile(&));
42+
auto webViewProfileStaging =
43+
webView2Profile.try_query<ICoreWebView2StagingProfile2>();
4444

45-
if (webViewSettingsStaging)
45+
if (webViewProfileStaging)
4646
{
4747
// Toggle the service worker post message setting.
4848
BOOL isEnabled;
49-
CHECK_FAILURE(webViewSettingsStaging->get_IsWebViewScriptApisForServiceWorkerEnabled(&isEnabled));
50-
CHECK_FAILURE(webViewSettingsStaging->put_IsWebViewScriptApisForServiceWorkerEnabled(!isEnabled));
49+
CHECK_FAILURE(webViewProfileStaging->get_IsWebViewScriptApisForServiceWorkerEnabled(&isEnabled));
50+
CHECK_FAILURE(webViewProfileStaging->put_IsWebViewScriptApisForServiceWorkerEnabled(!isEnabled));
5151

5252
MessageBox(
5353
reinterpret_cast<HWND>(m_appWindow.Id().Value),
54-
(std::wstring(L"Service Worker JS API setting has been ") +
54+
(std::wstring(L"Service Worker JS API has been ") +
5555
(!isEnabled ? L"enabled." : L"disabled."))
5656
.c_str(),
57-
L"Service Worker JS API Setting", MB_OK);
57+
L"Service Worker JS API", MB_OK);
5858
}
5959
}
6060

@@ -178,7 +178,7 @@ void SetUpEventsAndNavigate()
178178
<head>
179179
<meta charset="UTF-8">
180180
<meta name="viewport" content="width=device-width, initial-scale=1.0">
181-
<title>Service Worker Post Message Setting</title>
181+
<title>Service Worker JS API Availability</title>
182182
<script>
183183
"use strict";
184184
@@ -211,7 +211,7 @@ void SetUpEventsAndNavigate()
211211
</script>
212212
</head>
213213
<body>
214-
<h1>Service Worker Post Message Setting</h1>
214+
<h1>Service Worker JS API Availability</h1>
215215
<p>This page registers a service worker, posts a message to it, and listens for responses.</p>
216216
</body>
217217
</html>
@@ -248,12 +248,12 @@ private void ToggleServiceWorkerJsApiSetting()
248248
});");
249249

250250
// Toggle the service worker post message setting.
251-
var settings = webView.CoreWebView2.Settings;
252-
settings.AreWebViewScriptApisEnabledForServiceWorkers = !settings.AreWebViewScriptApisEnabledForServiceWorkers;
251+
var profile = webView.CoreWebView2.Profile;
252+
profile.AreWebViewScriptApisEnabledForServiceWorkers = !profile.AreWebViewScriptApisEnabledForServiceWorkers;
253253

254254
MessageBox.Show(this,
255-
$"AreWebViewScriptApisEnabledForServiceWorkers is now set to: {settings.AreWebViewScriptApisEnabledForServiceWorkers}",
256-
"Service Worker JS API Setting", MessageBoxButtons.OK, MessageBoxIcon.Information);
255+
$"AreWebViewScriptApisEnabledForServiceWorkers is now set to: {profile.AreWebViewScriptApisEnabledForServiceWorkers}",
256+
"Service Worker JS API", MessageBoxButtons.OK, MessageBoxIcon.Information);
257257
}
258258

259259
private void SetupEventsOnServiceWorker(CoreWebView2ServiceWorker serviceWorker)
@@ -318,15 +318,15 @@ private void SetUpEventsAndNavigate()
318318

319319
## Win32 C++
320320
```cpp
321-
interface ICoreWebView2Settings10 : ICoreWebView2Settings9 {
321+
interface ICoreWebView2Profile9 : ICoreWebView2Profile8 {
322322
/// Gets the `AreWebViewScriptApisEnabledForServiceWorkers` property.
323323
[propget] HRESULT AreWebViewScriptApisEnabledForServiceWorkers([out, retval] BOOL* value);
324324

325325
/// Enables or disables webview2 specific Service Worker JS APIs in the WebView2.
326326
/// When set to `TRUE`, chrome and webview objects are available in Service Workers .
327327
/// chrome.webview exposes APIs to interact with the WebView from Service Workers.
328328
/// The default value is `FALSE`.
329-
/// When enabled, this setting takes effect for all the newly installed Service Workers.
329+
/// This setting applies to all newly installed Service Workers within the profile.
330330
[propput] HRESULT AreWebViewScriptApisEnabledForServiceWorkers([in] BOOL value)
331331
}
332332

@@ -336,9 +336,9 @@ interface ICoreWebView2Settings10 : ICoreWebView2Settings9 {
336336
```c#
337337
namespace Microsoft.Web.WebView2.Core
338338
{
339-
runtimeclass CoreWebView2Settings
339+
runtimeclass CoreWebView2Profile
340340
{
341-
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Settings10")]
341+
[interface_name("Microsoft.Web.WebView2.Core.ICoreWebView2Profile9")]
342342
{
343343
Boolean AreWebViewScriptApisEnabledForServiceWorkers { get; set; };
344344
}

0 commit comments

Comments
 (0)