Skip to content

Commit c465b5d

Browse files
added Get sample and made changes to API surface
1 parent 753bca1 commit c465b5d

File tree

1 file changed

+110
-36
lines changed

1 file changed

+110
-36
lines changed

specs/TrustedOriginSetting.md

Lines changed: 110 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This specification introduces the following APIs.
1919

2020
1. On `CoreWebView2Profile`
2121

22-
- **CreateTrustedOriginFeatureSettings**: Creates a collection of CoreWebView2TrustedOriginFeatureSetting objects, which can be used to call SetTrustedOriginFeatures to configure features for trusted origins
22+
- **CreateTrustedOriginFeatureSetting**: Creates a CoreWebView2TrustedOriginFeatureSetting objects, which can be used to create an array to call SetTrustedOriginFeatures to configure features for trusted origins
2323
- **SetTrustedOriginFeatures**: Sets the feature settings for specified origins.
2424
- **GetTrustedOriginFeatures**: Gets the feature settings (Feature and isEnabled) for a specified origin asynchronously.
2525
2. `ICoreWebView2TrustedOriginFeatureSetting`interface, is simply a tuple which has feature enum and feature state ( enabled or disabled ). For now the feature enum can have three values
@@ -47,39 +47,111 @@ void ScenarioTrustedOrigin::SetFeatureForOrigins(std::vector<std::wstring> origi
4747
auto stagingProfile3 =
4848
m_webviewProfile.try_query<ICoreWebView2StagingProfile3>();
4949

50-
std::vector<COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE> comFeatures;
51-
std::vector<BOOL> comIsEnabled;
50+
// featureSettings holds wil::com_ptr for COM lifetime management (keeps refcount > 0).
51+
// featureSettingsRaw holds raw pointers extracted from featureSettings to pass to the API.
52+
// Both are needed because the API requires a pointer array, but we need smart pointers to prevent premature COM object destruction.
53+
std::vector<wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSetting>> featureSettings;
54+
std::vector<ICoreWebView2StagingTrustedOriginFeatureSetting*> featureSettingsRaw;
55+
5256
if (feature.size() >= 3)
5357
{
54-
comFeatures.push_back(COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ACCENT_COLOR);
55-
comIsEnabled.push_back(feature[0] ? TRUE : FALSE);
56-
57-
comFeatures.push_back(COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_PERSISTENT_STORAGE);
58-
comIsEnabled.push_back(feature[1] ? TRUE : FALSE);
59-
60-
comFeatures.push_back(COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE);
61-
comIsEnabled.push_back(feature[2] ? TRUE : FALSE);
58+
if (feature[0])
59+
{
60+
wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSetting> setting;
61+
CHECK_FAILURE(stagingProfile3->CreateTrustedOriginFeatureSetting(
62+
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ACCENT_COLOR,
63+
TRUE,
64+
&setting));
65+
featureSettings.push_back(setting);
66+
featureSettingsRaw.push_back(setting.get());
67+
}
68+
69+
if (feature[1])
70+
{
71+
wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSetting> setting;
72+
CHECK_FAILURE(stagingProfile3->CreateTrustedOriginFeatureSetting(
73+
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_PERSISTENT_STORAGE,
74+
TRUE,
75+
&setting));
76+
featureSettings.push_back(setting);
77+
featureSettingsRaw.push_back(setting.get());
78+
}
79+
80+
if (feature[2])
81+
{
82+
wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSetting> setting;
83+
CHECK_FAILURE(stagingProfile3->CreateTrustedOriginFeatureSetting(
84+
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE,
85+
TRUE,
86+
&setting));
87+
featureSettings.push_back(setting);
88+
featureSettingsRaw.push_back(setting.get());
89+
}
6290
}
6391

64-
// Create a feature collection from the arrays.
65-
wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSettingCollectionView> featureCollection;
66-
CHECK_FAILURE(
67-
stagingProfile3->CreateTrustedOriginFeatureSettings(
68-
static_cast<UINT32>(comFeatures.size()),
69-
comFeatures.data(),
70-
comIsEnabled.data(),
71-
&featureCollection));
72-
7392
std::vector<LPCWSTR> origins;
7493
for (const auto& pattern : originPatterns)
7594
{
7695
origins.push_back(pattern.c_str());
7796
}
97+
7898
CHECK_FAILURE(
7999
stagingProfile3->SetTrustedOriginFeatures(
80100
static_cast<UINT32>(origins.size()),
81101
origins.data(),
82-
featureCollection.get()));
102+
static_cast<UINT32>(featureSettingsRaw.size()),
103+
featureSettingsRaw.data()));
104+
}
105+
106+
void ScenarioTrustedOrigin::ShowFeatureSettingsForOrigin()
107+
{
108+
auto stagingProfile3 =
109+
m_webviewProfile.try_query<ICoreWebView2StagingProfile3>();
110+
111+
TextInputDialog inputDialog(
112+
m_appWindow->GetMainWindow(),
113+
L"Get Trusted Origin Features",
114+
L"Enter the origin to retrieve feature settings for:",
115+
L"Origin:",
116+
std::wstring(L"https://www.microsoft.com"),
117+
false); // not read-only
118+
119+
if (inputDialog.confirmed)
120+
{
121+
std::wstring origin = inputDialog.input;
122+
123+
CHECK_FAILURE(
124+
stagingProfile3->GetTrustedOriginFeatures(
125+
origin.c_str(),
126+
Callback<ICoreWebView2StagingGetTrustedOriginFeaturesCompletedHandler>(
127+
[this, origin](HRESULT errorCode,
128+
ICoreWebView2StagingTrustedOriginFeatureSettingCollectionView* result) -> HRESULT
129+
{
130+
if (SUCCEEDED(errorCode))
131+
{
132+
UINT32 count = 0;
133+
CHECK_FAILURE(result->get_Count(&count));
134+
135+
std::wstring message = L"Features for origin: " + origin + L"\n";
136+
for (UINT32 i = 0; i < count; i++)
137+
{
138+
wil::com_ptr<ICoreWebView2StagingTrustedOriginFeatureSetting> setting;
139+
CHECK_FAILURE(result->GetValueAtIndex(i, &setting));
140+
141+
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE feature;
142+
BOOL isEnabled;
143+
CHECK_FAILURE(setting->get_Feature(&feature));
144+
CHECK_FAILURE(setting->get_IsEnabled(&isEnabled));
145+
146+
message += L"Feature: " + std::to_wstring(static_cast<int>(feature)) +
147+
L", Enabled: " + (isEnabled ? L"True" : L"False") + L"\n";
148+
}
149+
150+
MessageBoxW(m_appWindow->GetMainWindow(), message.c_str(), L"Trusted Origin Features", MB_OK);
151+
}
152+
return S_OK;
153+
}).Get()));
154+
}
83155
}
84156
```
85157
@@ -113,10 +185,13 @@ var originFeatures = await profile.GetTrustedOriginFeaturesAsync("https://app.co
113185
[v1_enum]
114186
typedef enum COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE {
115187
/// Specifies the accent color feature for the origin.
188+
/// By default, the accent color feature is disabled for all origins.
116189
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ACCENT_COLOR,
117-
/// Specifies persistent storage capabilitity for the origin.
190+
/// Specifies persistent storage capabilities for the origin.
191+
/// By default, persistent storage is disabled for all origins.
118192
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_PERSISTENT_STORAGE,
119-
/// Specifies enhanced security mode setting for the origin.
193+
/// Specifies enhanced security mode settings for the origin.
194+
/// Enhanced security mode can be configured globally via EnhancedSecurityModeLevel API on profile.
120195
COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE,
121196
} COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE;
122197

@@ -129,14 +204,13 @@ interface ICoreWebView2StagingGetTrustedOriginFeaturesCompletedHandler : IUnknow
129204

130205
/// This is the ICoreWebView2Profile interface for trusted origin feature management.
131206
interface ICoreWebView2StagingProfile3 : IUnknown {
132-
/// Creates a collection of CoreWebView2TrustedOriginFeatureSetting objects.
133-
/// This method allows creating a feature settings collection that can be used with
207+
/// Creates a CoreWebView2TrustedOriginFeatureSetting objects.
208+
/// This method allows creating a feature settings object that can be used with
134209
/// SetTrustedOriginFeatures to configure features for trusted origins.
135-
HRESULT CreateTrustedOriginFeatureSettings(
136-
[in] UINT32 featuresCount,
137-
[in] COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE* features,
138-
[in] BOOL* isEnabled
139-
, [out, retval] ICoreWebView2StagingTrustedOriginFeatureSettingCollectionView** value);
210+
HRESULT CreateTrustedOriginFeatureSetting(
211+
[in] COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE features,
212+
[in] BOOL isEnabled
213+
, [out, retval] ICoreWebView2StagingTrustedOriginFeatureSetting** value);
140214

141215
/// Sets the feature configurations for specified origins.
142216
/// This method allows configuring multiple features for trusted origins,
@@ -145,22 +219,22 @@ interface ICoreWebView2StagingProfile3 : IUnknown {
145219
/// For detailed examples, refer to the table at: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter.
146220
HRESULT SetTrustedOriginFeatures(
147221
[in] UINT32 originsCount,
148-
[in] LPCWSTR* origins,
149-
[in] ICoreWebView2StagingTrustedOriginFeatureSettingCollectionView* features
222+
[in] LPCWSTR* originPatterns,
223+
[in] UINT32 featureCount,
224+
[in] ICoreWebView2StagingTrustedOriginFeatureSetting** features
150225
);
151226

152227
/// Gets the feature configurations for a specified origin.
153228
/// Returns a collection of feature settings that have been configured for the origin.
154229
/// If no features have been configured for the origin, an empty collection is returned.
155-
/// The origin can be both exact origin strings and wildcard patterns.
156-
/// For detailed examples, refer to the table at: https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter.
230+
/// The origin should have a valid scheme and host (e.g. "https://www.example.com"),
231+
/// otherwise the method fails with `E_INVALIDARG`.
157232
HRESULT GetTrustedOriginFeatures(
158233
[in] LPCWSTR origin
159234
, [in] ICoreWebView2StagingGetTrustedOriginFeaturesCompletedHandler* handler);
160235
}
161236

162237
/// Represents a feature setting configuration for a trusted origin.
163-
[uuid(edf2c30e-daab-572c-887b-61e5acb8c305), object, pointer_default(unique)]
164238
interface ICoreWebView2StagingTrustedOriginFeatureSetting : IUnknown {
165239
/// The feature type for this setting.
166240
[propget] HRESULT Feature([out, retval] COREWEBVIEW2_TRUSTED_ORIGIN_FEATURE* value);
@@ -197,7 +271,7 @@ namespace Microsoft.Web.WebView2.Core
197271

198272
public partial class CoreWebView2Profile
199273
{
200-
public async Task GetTrustedOriginFeaturesAsync(string origins);
274+
public async Task<IEnumerable<KeyValuePair<CoreWebView2TrustedOriginFeature, bool>>> GetTrustedOriginFeaturesAsync(string origins);
201275

202276
public void SetTrustedOriginFeatures(IEnumerable<string> origins, IEnumerable<KeyValuePair<CoreWebView2TrustedOriginFeature, bool>> features);
203277
}

0 commit comments

Comments
 (0)