1+ // Copyright (C) Microsoft Corporation. All rights reserved.
2+ // Use of this source code is governed by a BSD-style license that can be
3+ // found in the LICENSE file.
4+
5+ #include " stdafx.h"
6+
7+ #include " AppWindow.h"
8+ #include " CheckFailure.h"
9+ #include " ScenarioFileTypePolicy.h"
10+
11+ using namespace Microsoft ::WRL;
12+
13+ static constexpr WCHAR c_samplePath[] = L" SecnarioFileTypePolicy.html" ;
14+
15+ ScenarioFileTypePolicy::ScenarioFileTypePolicy (AppWindow* appWindow)
16+ : m_appWindow(appWindow), m_webView2(appWindow->GetWebView ())
17+ {
18+ if (m_webView2)
19+ {
20+ m_webView2Experimental27 = m_webView2.try_query <ICoreWebView2Experimental27>();
21+ m_webView2_2 = m_webView2.try_query <ICoreWebView2_2>();
22+
23+ m_sampleUri = m_appWindow->GetLocalUri (c_samplePath);
24+ CHECK_FAILURE (m_webView2->Navigate (m_sampleUri.c_str ()));
25+ SuppressPolicyForExtension ();
26+
27+ // Turn off this scenario if we navigate away from the demo page.
28+ CHECK_FAILURE (m_webView2_2->add_DOMContentLoaded (
29+ Callback<ICoreWebView2DOMContentLoadedEventHandler>(
30+ [this ](ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args)
31+ -> HRESULT
32+ {
33+ wil::unique_cotaskmem_string uri;
34+ sender->get_Source (&uri);
35+ if (uri.get () != m_sampleUri)
36+ m_appWindow->DeleteComponent (this );
37+ return S_OK;
38+ })
39+ .Get (),
40+ &m_DOMcontentLoadedToken));
41+ }
42+ }
43+
44+ // ! [SuppressPolicyForExtension]
45+ // This example will register the event with two custom rules.
46+ // 1. Suppressing file type policy, security dialog, and allows saving ".eml" files
47+ // directly.
48+ // 2. When the URI is trusted.- Showing customized warning UI when saving ".iso"
49+ // files. It allows to block the saving directly.
50+ bool ScenarioFileTypePolicy::SuppressPolicyForExtension ()
51+ {
52+ if (!m_webView2Experimental27)
53+ return false ;
54+ m_webView2Experimental27->add_SaveFileSecurityCheckStarting (
55+ Callback<ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventHandler>(
56+ [this ](
57+ ICoreWebView2* sender,
58+ ICoreWebView2ExperimentalSaveFileSecurityCheckStartingEventArgs* args)
59+ -> HRESULT
60+ {
61+ // Get the file extension for file to be saved.
62+ // And convert the extension to lower case for a
63+ // case-insensitive comparasion.
64+ wil::unique_cotaskmem_string extension;
65+ CHECK_FAILURE (args->get_FileExtension (&extension));
66+ std::wstring extension_lower = extension.get ();
67+ std::transform (
68+ extension_lower.begin (), extension_lower.end (), extension_lower.begin (),
69+ ::towlower);
70+
71+ // Suppress default policy for ".eml" file.
72+ if (wcscmp (extension_lower.c_str (), L" .eml" ) == 0 )
73+ {
74+ CHECK_FAILURE (args->put_SuppressDefaultPolicy (TRUE ));
75+ }
76+
77+ // Cancel save/download for ".iso" file.
78+ if (wcscmp (extension_lower.c_str (), L" .iso" ) == 0 )
79+ {
80+ wil::com_ptr<ICoreWebView2Deferral> deferral;
81+ CHECK_FAILURE (args->GetDeferral (&deferral));
82+
83+ m_appWindow->RunAsync (
84+ [this , args = wil::make_com_ptr (args), deferral]()
85+ {
86+ // With the deferral, the cancel decision and
87+ // message box can be replaced with a customized UI.
88+ CHECK_FAILURE (args->put_CancelSave (TRUE ));
89+ MessageBox (
90+ m_appWindow->GetMainWindow (), L" The saving has been blocked" ,
91+ L" Info" , MB_OK);
92+ CHECK_FAILURE (deferral->Complete ());
93+ });
94+ }
95+ return S_OK;
96+ })
97+ .Get (),
98+ &m_saveFileSecurityCheckStartingToken);
99+
100+ MessageBox (
101+ m_appWindow->GetMainWindow (),
102+ (L" Example rules of Dangerous File Security Policy has been applied in this demo page" ),
103+ L" Info" , MB_OK);
104+ return true ;
105+ }
106+ // ! [SuppressPolicyForExtension]
107+
108+ ScenarioFileTypePolicy::~ScenarioFileTypePolicy ()
109+ {
110+ if (m_webView2Experimental27)
111+ {
112+ CHECK_FAILURE (m_webView2Experimental27->remove_SaveFileSecurityCheckStarting (
113+ m_saveFileSecurityCheckStartingToken));
114+ }
115+ CHECK_FAILURE (m_webView2_2->remove_DOMContentLoaded (m_DOMcontentLoadedToken));
116+ }
0 commit comments