-
Notifications
You must be signed in to change notification settings - Fork 520
Expand file tree
/
Copy pathScenarioOriginConfigurationAPI.cpp
More file actions
206 lines (178 loc) · 8.12 KB
/
Copy pathScenarioOriginConfigurationAPI.cpp
File metadata and controls
206 lines (178 loc) · 8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include <string>
#include <vector>
#include "CheckFailure.h"
#include "TextInputDialog.h"
#include "Util.h"
#include "ScenarioOriginConfigurationAPI.h"
using namespace Microsoft::WRL;
static constexpr int kEnhancedSecurityModeValue = 1;
static constexpr int kSmartScreenValue = 2;
ScenarioOriginConfigurationAPI::ScenarioOriginConfigurationAPI(AppWindow* appWindow)
: m_appWindow(appWindow)
{
CHECK_FAILURE(m_appWindow ? S_OK : E_POINTER);
wil::com_ptr<ICoreWebView2> webView = m_appWindow->GetWebView();
CHECK_FAILURE(webView ? S_OK : E_POINTER);
auto webView2_13 = webView.try_query<ICoreWebView2_13>();
CHECK_FAILURE(webView2_13 ? S_OK : E_POINTER);
CHECK_FAILURE(webView2_13->get_Profile(&m_webviewProfile));
}
ScenarioOriginConfigurationAPI::~ScenarioOriginConfigurationAPI() = default;
std::wstring ScenarioOriginConfigurationAPI::FeatureToString(
COREWEBVIEW2_ORIGIN_FEATURE feature)
{
switch (feature)
{
case COREWEBVIEW2_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE:
return L"EnhancedSecurityMode";
case COREWEBVIEW2_ORIGIN_FEATURE_REPUTATION_CHECKING:
return L"ReputationChecking";
default:
return L"Unknown";
}
}
void ScenarioOriginConfigurationAPI::SetFeatureForOrigins(
const std::vector<std::wstring>& originPatterns,
const std::vector<
std::pair<COREWEBVIEW2_ORIGIN_FEATURE, COREWEBVIEW2_ORIGIN_FEATURE_STATE>>& features)
{
auto experimentalProfile16 =
m_webviewProfile.try_query<ICoreWebView2ExperimentalProfile16>();
CHECK_FEATURE_RETURN_EMPTY(experimentalProfile16);
// featureSettings holds wil::com_ptr for COM lifetime management (keeps refcount > 0).
// featureSettingsRaw holds raw pointers extracted from featureSettings to pass to the API.
// Both are needed because the API requires a pointer array, but we need smart pointers to
// prevent premature COM object destruction.
std::vector<wil::com_ptr<ICoreWebView2ExperimentalOriginFeatureSetting>> featureSettings;
std::vector<ICoreWebView2ExperimentalOriginFeatureSetting*> featureSettingsRaw;
for (const auto& [featureKind, featureState] : features)
{
wil::com_ptr<ICoreWebView2ExperimentalOriginFeatureSetting> setting;
CHECK_FAILURE(experimentalProfile16->CreateOriginFeatureSetting(
featureKind, featureState, &setting));
featureSettings.push_back(setting);
featureSettingsRaw.push_back(setting.get());
}
std::vector<LPCWSTR> origins;
for (const auto& pattern : originPatterns)
{
origins.push_back(pattern.c_str());
}
CHECK_FAILURE(experimentalProfile16->SetOriginFeatures(
static_cast<UINT32>(origins.size()), origins.data(),
static_cast<UINT32>(featureSettingsRaw.size()), featureSettingsRaw.data()));
}
void ScenarioOriginConfigurationAPI::GetOriginFeatures()
{
auto experimentalProfile16 =
m_webviewProfile.try_query<ICoreWebView2ExperimentalProfile16>();
CHECK_FEATURE_RETURN_EMPTY(experimentalProfile16);
TextInputDialog inputDialog(
m_appWindow->GetMainWindow(), L"Get Trusted Origin Features",
L"Enter the origin to retrieve feature settings for:", L"Origin:",
std::wstring(L"https://www.microsoft.com"),
false); // not read-only
if (inputDialog.confirmed)
{
std::wstring origin = inputDialog.input;
CHECK_FAILURE(experimentalProfile16->GetEffectiveFeaturesForOrigin(
origin.c_str(),
Callback<ICoreWebView2ExperimentalGetEffectiveFeaturesForOriginCompletedHandler>(
[appWindow = m_appWindow, origin](
HRESULT errorCode,
ICoreWebView2ExperimentalOriginFeatureSettingCollectionView* result)
-> HRESULT
{
if (SUCCEEDED(errorCode))
{
UINT32 count = 0;
CHECK_FAILURE(result->get_Count(&count));
std::wstring message = L"Features for origin: " + origin + L"\n";
for (UINT32 i = 0; i < count; i++)
{
wil::com_ptr<ICoreWebView2ExperimentalOriginFeatureSetting> setting;
CHECK_FAILURE(result->GetValueAtIndex(i, &setting));
COREWEBVIEW2_ORIGIN_FEATURE feature;
COREWEBVIEW2_ORIGIN_FEATURE_STATE featureState;
CHECK_FAILURE(setting->get_Feature(&feature));
CHECK_FAILURE(setting->get_State(&featureState));
message +=
L"Feature: " + FeatureToString(feature) + L", Enabled: " +
(featureState == COREWEBVIEW2_ORIGIN_FEATURE_STATE_ENABLED
? L"True"
: L"False") +
L"\n";
}
MessageBoxW(
appWindow->GetMainWindow(), message.c_str(),
L"Trusted Origin Features", MB_OK);
}
else
{
ShowFailure(
errorCode,
L"Failed to get effective features for origin: " + origin);
}
return S_OK;
})
.Get()));
}
}
void ScenarioOriginConfigurationAPI::SetOriginFeatures()
{
static constexpr wchar_t kOriginPatternsLabel[] = L"Enter origin patterns separated with ;";
static constexpr wchar_t kFeaturesGroupLabel[] = L"Select features to enable:";
// Builder with text area for origin input and checkbox for feature selection.
auto enhancedDialog =
TextInputDialog::Builder(
m_appWindow->GetMainWindow(), L"Configure Trusted Origin",
L"Trusted Origin Configuration:")
.AddTextArea(kOriginPatternsLabel, L"https://www.microsoft.com")
.AddCheckBoxGroup(
kFeaturesGroupLabel,
{{L"Enable Enhanced Security Mode", kEnhancedSecurityModeValue, true},
{L"Disable Reputation Checking", kSmartScreenValue, false}})
.Build();
if (enhancedDialog.confirmed)
{
// Retrieve the origin text area. Skip if the control is missing.
auto textAreaIt = enhancedDialog.results.find(kOriginPatternsLabel);
if (textAreaIt == enhancedDialog.results.end())
return;
auto& textArea = std::get<TextArea>(textAreaIt->second);
std::wstring input = textArea.input;
// Retrieve the feature checkbox group. Skip if the control is missing.
std::vector<std::pair<COREWEBVIEW2_ORIGIN_FEATURE, COREWEBVIEW2_ORIGIN_FEATURE_STATE>>
features;
auto groupIt = enhancedDialog.results.find(kFeaturesGroupLabel);
if (groupIt == enhancedDialog.results.end())
return;
auto& group = std::get<CheckBoxGroup>(groupIt->second);
for (const auto& option : group.options)
{
if (option.isSelected && option.value == kEnhancedSecurityModeValue)
{
features.push_back(
{COREWEBVIEW2_ORIGIN_FEATURE_ENHANCED_SECURITY_MODE,
COREWEBVIEW2_ORIGIN_FEATURE_STATE_ENABLED});
}
if (option.isSelected && option.value == kSmartScreenValue)
{
// Disabling SmartScreen for the origin = allow-listing it.
features.push_back(
{COREWEBVIEW2_ORIGIN_FEATURE_REPUTATION_CHECKING,
COREWEBVIEW2_ORIGIN_FEATURE_STATE_DISABLED});
}
}
std::vector<std::wstring> origins = Util::SplitString(input, L';');
// Set features for all origins in a single call.
if (!features.empty())
{
SetFeatureForOrigins(origins, features);
}
}
}