@@ -22,7 +22,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
2222 m_sampleUri = m_appWindow->GetLocalUri (c_samplePath);
2323 CHECK_FAILURE (m_webView2->Navigate (m_sampleUri.c_str ()));
2424 SuppressPolicyForExtension ();
25-
25+ ListenToWebMessages ();
2626 // Turn off this scenario if we navigate away from the demo page.
2727 CHECK_FAILURE (m_webView2_2->add_DOMContentLoaded (
2828 Callback<ICoreWebView2DOMContentLoadedEventHandler>(
@@ -43,7 +43,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow)
4343// ! [SuppressPolicyForExtension]
4444// This example will register the event with two custom rules.
4545// 1. Suppressing file type policy, security dialog, and allows saving ".eml" files
46- // directly; when the URI is trusted.
46+ // directly; when the URI is trusted.
4747// 2. Showing customized warning UI when saving ".iso" files. It allows to block
4848// the saving directly.
4949bool ScenarioFileTypePolicy::SuppressPolicyForExtension ()
@@ -55,8 +55,7 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
5555 Callback<ICoreWebView2SaveFileSecurityCheckStartingEventHandler>(
5656 [this ](
5757 ICoreWebView2* sender,
58- ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args)
59- -> HRESULT
58+ ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT
6059 {
6160 // Get the file extension for file to be saved.
6261 // And convert the extension to lower case for a
@@ -92,6 +91,20 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
9291 CHECK_FAILURE (deferral->Complete ());
9392 });
9493 }
94+ if (wcscmp (extension_lower.c_str (), L" exe" ) == 0 )
95+ {
96+ if (is_exe_blocked.has_value ())
97+ {
98+ if (is_exe_blocked)
99+ {
100+ args->put_CancelSave (true );
101+ }
102+ else
103+ {
104+ args->put_SuppressDefaultPolicy (true );
105+ }
106+ }
107+ }
95108 return S_OK;
96109 })
97110 .Get (),
@@ -105,12 +118,58 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension()
105118}
106119// ! [SuppressPolicyForExtension]
107120
121+ void ScenarioFileTypePolicy::ListenToWebMessages ()
122+ {
123+ CHECK_FAILURE (m_webView2->add_WebMessageReceived (
124+ Callback<ICoreWebView2WebMessageReceivedEventHandler>(
125+ [this ](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
126+ -> HRESULT
127+ {
128+ LPWSTR message;
129+ args->TryGetWebMessageAsString (&message);
130+ ICoreWebView2Settings* settings;
131+ sender->get_Settings (&settings);
132+ ICoreWebView2Settings8* settings8;
133+ settings->QueryInterface (IID_PPV_ARGS (&settings8));
134+ if (wcscmp (message, L" enable_smartscreen" ) == 0 )
135+ {
136+
137+ settings8->put_IsReputationCheckingRequired (true );
138+ MessageBox (
139+ m_appWindow->GetMainWindow (), (L" Enabled Smartscreen" ), L" Info" , MB_OK);
140+ }
141+ else if (wcscmp (L" disable_smartscreen" , message) == 0 )
142+ {
143+ settings8->put_IsReputationCheckingRequired (false );
144+ MessageBox (
145+ m_appWindow->GetMainWindow (), (L" Disabled Smartscreen" ), L" Info" ,
146+ MB_OK);
147+ }
148+ else if (wcscmp (L" block_exe" , message) == 0 )
149+ {
150+ is_exe_blocked = true ;
151+ }
152+ else if (wcscmp (L" allow_exe" , message) == 0 )
153+ {
154+ is_exe_blocked = false ;
155+ }
156+ else if (wcscmp (L" clear_exe_policy" , message) == 0 )
157+ {
158+ is_exe_blocked = std::nullopt ;
159+ }
160+ return S_OK;
161+ })
162+ .Get (),
163+ &m_webMessageReceivedToken));
164+ }
165+
108166ScenarioFileTypePolicy::~ScenarioFileTypePolicy ()
109167{
110168 if (m_webView2_26)
111169 {
112170 CHECK_FAILURE (m_webView2_26->remove_SaveFileSecurityCheckStarting (
113171 m_saveFileSecurityCheckStartingToken));
114172 }
173+ CHECK_FAILURE (m_webView2_2->remove_WebResourceResponseReceived (m_webMessageReceivedToken));
115174 CHECK_FAILURE (m_webView2_2->remove_DOMContentLoaded (m_DOMcontentLoadedToken));
116175}
0 commit comments