Skip to content

Commit 2fbb221

Browse files
Rewrite win32 example
The rewrite will address the following suggestions: - use m_webview2_25 to match the patterns and simplify to remove the null check. - case insensitive comparison - avoid memory leak with wil::unique_cotaskmem_string - include period for file extension - replace .exe to .iso - show the case of validating a trusted uri for .eml - show the case of using deferral and customized UI
1 parent c05679c commit 2fbb221

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

specs/FileTypePolicy.md

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,76 @@ file type policies.
2222

2323
# Examples
2424
## Win32 C++
25-
This example shows suppressing file type policy, security dialog, and
26-
allows saving eml files directly. It also blocks saving exe files.
27-
The sample code will register the event with custom rules.
25+
This example will register the event with two custom rules.
26+
- Suppressing file type policy, security dialog, and
27+
allows saving ".eml" files directly; when the uri is trusted.
28+
- Showing customized warning UI when saving ".iso" files.
29+
It allows to block the saving directly.
2830
```c++
29-
bool ScenarioFileTypePolicyStaging::AddCustomFileTypePolicies()
31+
void ScenarioFileTypePolicy::AddCustomFileTypePolicies()
3032
{
31-
if (!m_webView2Staging25)
32-
return false;
33+
wil::com_ptr<ICoreWebView2> m_webview;
34+
EventRegistrationToken m_saveFileSecurityCheckStartingToken = {};
35+
auto m_webview2_25 = m_webView.try_query<ICoreWebView2_25>();
3336
// Register a handler for the `SaveFileSecurityCheckStarting` event.
34-
m_webView2Staging25->add_SaveFileSecurityCheckStarting(
35-
Callback<ICoreWebView2StagingSaveFileSecurityCheckStartingEventHandler>(
37+
m_webView2_25->add_saveFileSecurityCheckStarting(
38+
Callback<ICoreWebView2SaveFileSecurityCheckStartingEventHandler>(
3639
[this](
3740
ICoreWebView2* sender,
38-
ICoreWebView2StagingSaveFileSecurityCheckStartingEventArgs* args) -> HRESULT
41+
ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT
3942
{
4043
// Get the file extension for file to be saved.
41-
// And create your own rules of file type policy.
42-
LPWSTR extension;
44+
wil::unique_cotaskmem_string extension;
4345
CHECK_FAILURE(args->get_FileExtension(&extension));
44-
if (lstrcmpW(extension, L"eml") == 0)
46+
// Get the uri of file to be saved.
47+
wil::unique_cotaskmem_string uri;
48+
CHECK_FAILURE(args->get_SourceUri(&uri));
49+
// Convert the file extension value to lower case for
50+
// the case-insensitive comparison.
51+
std::wstring extension_lower = extension.get();
52+
std::transform(
53+
extension_lower.begin(), extension_lower.end(),
54+
extension_lower.begin(), ::towlower);
55+
// Set the `SuppressDefaultPolicy` property to skip the
56+
// default file type policy checking and a possible security
57+
// alert dialog for ".eml" file, when it's from a trusted uri.
58+
// This will consent to save the file.
59+
//
60+
// 'IsTrustedUri' should be your own helper method
61+
// to determine whether a uri is trusted.
62+
if (wcscmp(extension_lower.c_str(), L".eml") == 0 && IsTrustedUri(uri))
4563
{
46-
// Set the `SuppressDefaultPolicy` property to skip the
47-
// default file type policy checking and a possible security
48-
// alert dialog for "eml" file. This will consent to
49-
// save the file.
5064
CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE));
5165
}
52-
if (lstrcmpW(extension, L"exe") == 0)
66+
// Show customized warning UI for ".iso" file with
67+
// the deferral.
68+
if (wcscmp(extension_lower.c_str(), L".iso") == 0)
5369
{
54-
// Set the `Cancel` property to cancel the saving for "exe"
55-
// file directly. Save action will be aborted without any
56-
// alert.
57-
CHECK_FAILURE(args->put_Cancel(TRUE));
70+
wil::com_ptr<ICoreWebView2Deferral> deferral;
71+
CHECK_FAILURE(args->GetDeferral(&deferral));
72+
73+
// 'm_appWindow' should be your main app window.
74+
m_appWindow->RunAsync(
75+
[args = wil::make_com_ptr(args), deferral]()
76+
{
77+
// Set the `Cancel` property to cancel the saving
78+
// for ".iso" file directly. Save action will be aborted.
79+
//
80+
// You can let end users make decision on their save
81+
// action with your customized warning UI.
82+
// 'IsCancelledFromCustomizedWarningUI' should be
83+
// your async helper method that retrieves result from the UI.
84+
if (IsCancelledFromCustomizedWarningUI())
85+
{
86+
CHECK_FAILURE(args->put_Cancel(TRUE));
87+
}
88+
CHECK_FAILURE(deferral->Complete());
89+
});
5890
}
5991
return S_OK;
6092
})
6193
.Get(),
6294
&m_saveFileSecurityCheckStartingToken);
63-
return true;
6495
}
6596
```
6697

0 commit comments

Comments
 (0)