@@ -37,20 +37,20 @@ bool ScenarioFileTypePolicyStaging::AddCustomFileTypePolicies()
3737 ICoreWebView2* sender,
3838 ICoreWebView2StagingFileTypePolicyCheckingEventArgs* args) -> HRESULT
3939 {
40- LPWSTR extension;
4140 // Get the file extension for file to be saved.
41+ // And create your own rules of file type policy.
42+ LPWSTR extension;
4243 CHECK_FAILURE (args->get_FileExtension(&extension));
43- // Create your own rules of file type policy.
4444 if (lstrcmpW(extension, L"eml") == 0)
45- // Set the ` SuppressDefaultPolicy ` property to skip the default
46- // file type policy checking and a possible security alert dialog.
47- //
48- // This will consent to save the file.
45+ // Set the ` SuppressDefaultPolicy ` property to skip the
46+ // default file type policy checking and a possible security
47+ // alert dialog for "eml" file. This will consent to
48+ // save the file.
4949 CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE));
5050 if (lstrcmpW(extension, L"exe") == 0)
51- // Set the ` Cancel ` property to cancel the saving directly.
52- //
53- // This will abort to save the file.
51+ // Set the ` Cancel ` property to cancel the saving for "exe"
52+ // file directly. Save action will be aborted without any
53+ // alert.
5454 CHECK_FAILURE(args->put_Cancel(TRUE));
5555 wil::com_ptr<ICoreWebView2Deferral > deferral;
5656 CHECK_FAILURE(args->GetDeferral(&deferral));
@@ -66,8 +66,43 @@ bool ScenarioFileTypePolicyStaging::AddCustomFileTypePolicies()
6666```
6767
6868## .Net/ WinRT
69- ###
69+ This example shows suppressing file type policy, security dialog, and
70+ allow to save the file directly. It also blocks saving the exe file.
71+ The sample code will register the event with custom rules.
7072``` c#
73+ void AddCustomFileTypePoliciesExecuted (object target , ExecutedRoutedEventArgs e )
74+ {
75+ // Register a handler for the `FileTypePolicyChecking` event.
76+ webView .CoreWebView2 .FileTypePolicyChecking += (sender , args ) =>
77+ {
78+ // Developer can obtain a deferral for the event so that the CoreWebView2
79+ // doesn't examine the properties we set on the event args until
80+ // after the deferral completes asynchronously.
81+ CoreWebView2Deferral deferral = args .GetDeferral ();
82+
83+ // We avoid potential reentrancy from running a message loop in the event
84+ // handler. Complete the deferral asynchronously.
85+ System .Threading .SynchronizationContext .Current .Post ((_ ) =>
86+ {
87+ using (deferral )
88+ {
89+ // Get the file extension for file to be saved.
90+ // And create your own rules of file type policy.
91+ if (args .FileExtension == " eml" )
92+ // Set the `SuppressDefaultPolicy` property to skip the
93+ // default file type policy checking and a possible security
94+ // alert dialog for "eml" file. This will consent to
95+ // save the file.
96+ args .SuppressDefaultPolicy = true ;
97+ if (args .FileExtension == " exe" )
98+ // Set the `Cancel` property to cancel the saving for "exe"
99+ // file directly. Save action will be aborted without any
100+ // alert.
101+ args .Cancel = true ;
102+ }
103+ }, null );
104+ };
105+ }
71106```
72107
73108# API Details
@@ -160,4 +195,31 @@ interface ICoreWebView2StagingFileTypePolicyCheckingEventHandler : IUnknown {
160195
161196## .Net/ WinRT
162197``` c# (but really MIDL3)
198+ namespace Microsoft .Web .WebView2 .Core
199+ {
200+
201+ runtimeclass CoreWebView2FileTypePolicyCheckingEventArgs;
202+ runtimeclass CoreWebView2;
203+
204+ runtimeclass CoreWebView2FileTypePolicyCheckingEventArgs
205+ {
206+ Boolean Cancel { get; set; };
207+ String FileExtension { get ; };
208+ String FilePath { get ; };
209+ Boolean SuppressDefaultPolicy { get ; set ; };
210+ String Uri { get ; };
211+ Windows .Foundation .Deferral GetDeferral ();
212+ };
213+
214+ runtimeclass CoreWebView2
215+ {
216+ // ...
217+
218+ [interface_name (" Microsoft.Web.WebView2.Core.ICoreWebView2_25" )]
219+ {
220+ event Windows .Foundation .TypedEventHandler
221+ < CoreWebView2 , CoreWebView2FileTypePolicyCheckingEventArgs > FileTypePolicyChecking ;
222+ }
223+ };
224+ }
163225```
0 commit comments