@@ -58,7 +58,7 @@ void ScenarioFileTypePolicy::AddCustomFileTypePolicies()
5858 // This will consent to save the file.
5959 //
6060 // 'IsTrustedUri' should be your own helper method
61- // to determine whether a uri is trusted.
61+ // to determine whether the uri is trusted.
6262 if (wcscmp(extension_lower.c_str(), L".eml") == 0 && IsTrustedUri(uri))
6363 {
6464 CHECK_FAILURE(args->put_SuppressDefaultPolicy(TRUE));
@@ -80,7 +80,7 @@ void ScenarioFileTypePolicy::AddCustomFileTypePolicies()
8080 // You can let end users make decision on their save
8181 // action with your customized warning UI.
8282 // 'IsCancelledFromCustomizedWarningUI' should be
83- // your async helper method that retrieves result from the UI.
83+ // your helper method that retrieves result from the UI.
8484 if (IsCancelledFromCustomizedWarningUI())
8585 {
8686 CHECK_FAILURE (args->put_Cancel(TRUE));
@@ -96,31 +96,49 @@ void ScenarioFileTypePolicy::AddCustomFileTypePolicies()
9696```
9797
9898## .Net/ WinRT
99- This example shows suppressing file type policy, security dialog, and
100- allows saving eml files directly. It also blocks saving exe files.
101- The sample code will register the event with custom rules.
99+ This example will register the event with two custom rules.
100+ - Suppressing file type policy, security dialog, and
101+ allows saving ".eml" files directly; when the uri is trusted.
102+ - Showing customized warning UI when saving ".iso" files.
103+ It allows to block the saving directly.
102104``` c#
103- void AddCustomFileTypePoliciesExecuted ( object target , ExecutedRoutedEventArgs e )
105+ void AddCustomFileTypePolicies ( )
104106{
105107 // Register a handler for the `SaveFileSecurityCheckStarting` event.
106108 webView .CoreWebView2 .SaveFileSecurityCheckStarting += (sender , args ) =>
107109 {
108- // Get the file extension for file to be saved.
109- // And create your own rules of file type policy.
110- if (args .FileExtension == " eml" )
110+ if (string .Equals (args .FileExtension , " .eml" , StringComparison .OrdinalIgnoreCase )
111+ && IsTrustedUri (args .SourceUri ))
111112 {
112113 // Set the `SuppressDefaultPolicy` property to skip the
113114 // default file type policy checking and a possible security
114- // alert dialog for "eml" file. This will consent to
115- // save the file.
115+ // alert dialog for ".eml" file, when it's from a trusted uri.
116+ // This will consent to save the file.
117+ //
118+ // 'IsTrustedUri' should be your own helper method
119+ // to determine whether the uri is trusted.
116120 args .SuppressDefaultPolicy = true ;
117121 }
118- if (args .FileExtension == " exe " )
122+ if (string . Equals ( args .FileExtension , " .iso " , StringComparison . OrdinalIgnoreCase ) )
119123 {
120- // Set the `Cancel` property to cancel the saving for "exe"
121- // file directly. Save action will be aborted without any
122- // alert.
123- args .Cancel = true ;
124+ CoreWebView2Deferral deferral = args .GetDeferral ();
125+ System .Threading .SynchronizationContext .Current .Post ((_ ) =>
126+ {
127+ using (deferral )
128+ {
129+ if (IsCancelledFromCustomizedWarningUI ())
130+ {
131+ // Set the `Cancel` property to cancel the saving
132+ // for ".iso" file directly. Save action will be aborted.
133+ //
134+ // You can let end users make decision on their save
135+ // action with your customized warning UI.
136+ // 'IsCancelledFromCustomizedWarningUI' should be
137+ // your helper method that retrieves result from the UI.
138+ args .Cancel = true ;
139+ }
140+ }
141+ }, null );
124142 }
125143 };
126144}
0 commit comments