diff --git a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp index 2832399..c41cb9b 100644 --- a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp +++ b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.cpp @@ -22,7 +22,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow) m_sampleUri = m_appWindow->GetLocalUri(c_samplePath); CHECK_FAILURE(m_webView2->Navigate(m_sampleUri.c_str())); SuppressPolicyForExtension(); - + ListenToWebMessages(); // Turn off this scenario if we navigate away from the demo page. CHECK_FAILURE(m_webView2_2->add_DOMContentLoaded( Callback( @@ -43,7 +43,7 @@ ScenarioFileTypePolicy::ScenarioFileTypePolicy(AppWindow* appWindow) //! [SuppressPolicyForExtension] // This example will register the event with two custom rules. // 1. Suppressing file type policy, security dialog, and allows saving ".eml" files -// directly; when the URI is trusted. +// directly; when the URI is trusted. // 2. Showing customized warning UI when saving ".iso" files. It allows to block // the saving directly. bool ScenarioFileTypePolicy::SuppressPolicyForExtension() @@ -55,8 +55,7 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() Callback( [this]( ICoreWebView2* sender, - ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) - -> HRESULT + ICoreWebView2SaveFileSecurityCheckStartingEventArgs* args) -> HRESULT { // Get the file extension for file to be saved. // And convert the extension to lower case for a @@ -92,6 +91,20 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() CHECK_FAILURE(deferral->Complete()); }); } + if (wcscmp(extension_lower.c_str(), L"exe") == 0) + { + if (is_exe_blocked.has_value()) + { + if (is_exe_blocked) + { + args->put_CancelSave(true); + } + else + { + args->put_SuppressDefaultPolicy(true); + } + } + } return S_OK; }) .Get(), @@ -105,6 +118,51 @@ bool ScenarioFileTypePolicy::SuppressPolicyForExtension() } //! [SuppressPolicyForExtension] +void ScenarioFileTypePolicy::ListenToWebMessages() +{ + CHECK_FAILURE(m_webView2->add_WebMessageReceived( + Callback( + [this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args) + -> HRESULT + { + LPWSTR message; + args->TryGetWebMessageAsString(&message); + ICoreWebView2Settings* settings; + sender->get_Settings(&settings); + ICoreWebView2Settings8* settings8; + settings->QueryInterface(IID_PPV_ARGS(&settings8)); + if (wcscmp(message, L"enable_smartscreen") == 0) + { + + settings8->put_IsReputationCheckingRequired(true); + MessageBox( + m_appWindow->GetMainWindow(), (L"Enabled Smartscreen"), L"Info", MB_OK); + } + else if (wcscmp(L"disable_smartscreen", message) == 0) + { + settings8->put_IsReputationCheckingRequired(false); + MessageBox( + m_appWindow->GetMainWindow(), (L"Disabled Smartscreen"), L"Info", + MB_OK); + } + else if (wcscmp(L"block_exe", message) == 0) + { + is_exe_blocked = true; + } + else if (wcscmp(L"allow_exe", message) == 0) + { + is_exe_blocked = false; + } + else if (wcscmp(L"clear_exe_policy", message) == 0) + { + is_exe_blocked = std::nullopt; + } + return S_OK; + }) + .Get(), + &m_webMessageReceivedToken)); +} + ScenarioFileTypePolicy::~ScenarioFileTypePolicy() { if (m_webView2_26) @@ -112,5 +170,6 @@ ScenarioFileTypePolicy::~ScenarioFileTypePolicy() CHECK_FAILURE(m_webView2_26->remove_SaveFileSecurityCheckStarting( m_saveFileSecurityCheckStartingToken)); } + CHECK_FAILURE(m_webView2_2->remove_WebResourceResponseReceived(m_webMessageReceivedToken)); CHECK_FAILURE(m_webView2_2->remove_DOMContentLoaded(m_DOMcontentLoadedToken)); } \ No newline at end of file diff --git a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h index b8a4ad3..b3e4e3c 100644 --- a/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h +++ b/SampleApps/WebView2APISample/ScenarioFileTypePolicy.h @@ -17,6 +17,7 @@ class ScenarioFileTypePolicy : public ComponentBase private: bool SuppressPolicyForExtension(); + void ListenToWebMessages(); AppWindow* m_appWindow; wil::com_ptr m_webView2; @@ -24,5 +25,7 @@ class ScenarioFileTypePolicy : public ComponentBase wil::com_ptr m_webView2_26; EventRegistrationToken m_saveFileSecurityCheckStartingToken = {}; EventRegistrationToken m_DOMcontentLoadedToken = {}; + EventRegistrationToken m_webMessageReceivedToken = {}; std::wstring m_sampleUri; + std::optional is_exe_blocked = std::nullopt; }; diff --git a/SampleApps/WebView2APISample/ViewComponent.cpp b/SampleApps/WebView2APISample/ViewComponent.cpp index c13c0ea..9e126ca 100644 --- a/SampleApps/WebView2APISample/ViewComponent.cpp +++ b/SampleApps/WebView2APISample/ViewComponent.cpp @@ -157,6 +157,7 @@ ViewComponent::ViewComponent( -> HRESULT { HRESULT hr = S_OK; HCURSOR cursor; + if (!m_useCursorId) { CHECK_FAILURE(sender->get_Cursor(&cursor)); @@ -167,18 +168,24 @@ ViewComponent::ViewComponent( UINT32 cursorId; CHECK_FAILURE(m_compositionController->get_SystemCursorId(&cursorId)); cursor = ::LoadCursor(nullptr, MAKEINTRESOURCE(cursorId)); - if (cursor == nullptr) + + if (cursorId != NULL && cursor == nullptr) { hr = HRESULT_FROM_WIN32(GetLastError()); } //! [SystemCursorId] } - if (SUCCEEDED(hr)) + if (cursor != nullptr) { SetClassLongPtr( m_appWindow->GetMainWindow(), GCLP_HCURSOR, (LONG_PTR)cursor); } + else if (SUCCEEDED(hr)) + { + SetCursor(NULL); + } + return hr; }) .Get(), diff --git a/SampleApps/WebView2APISample/WebView2APISample.vcxproj b/SampleApps/WebView2APISample/WebView2APISample.vcxproj index b9c9795..54a5e5f 100644 --- a/SampleApps/WebView2APISample/WebView2APISample.vcxproj +++ b/SampleApps/WebView2APISample/WebView2APISample.vcxproj @@ -496,13 +496,13 @@ - + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + diff --git a/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html b/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html index 91af992..30b159c 100644 --- a/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html +++ b/SampleApps/WebView2APISample/assets/SecnarioFileTypePolicy.html @@ -2,7 +2,7 @@ ScenarioFileTypePolicy - @@ -23,8 +39,20 @@

File Type Policy API Demo Page

Two customized example rules in this demo:

1. Smoothly save *.eml file without file extension warning

2. Intentionally block save *.iso file

-

Please enter a file extension: -

+

+ Please enter a file extension: + +


+
+

File Type Policy API for download

+ + + +
+ + +
+ Download Flagged file diff --git a/SampleApps/WebView2APISample/packages.config b/SampleApps/WebView2APISample/packages.config index 576adba..25800c6 100644 --- a/SampleApps/WebView2APISample/packages.config +++ b/SampleApps/WebView2APISample/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj index d1392b2..72660ac 100644 --- a/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj +++ b/SampleApps/WebView2WindowsFormsBrowser/WebView2WindowsFormsBrowser.csproj @@ -25,7 +25,7 @@ AnyCPU - + diff --git a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs index be84e5e..ef605ed 100644 --- a/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs +++ b/SampleApps/WebView2WpfBrowser/MainWindow.xaml.cs @@ -1991,7 +1991,10 @@ async void GoToPageCmdExecuted(object target, ExecutedRoutedEventArgs e) // // Setting _iWebView2.Source will not trigger a navigation if the Source is the same - // as the previous Source. CoreWebView.Navigate() will always trigger a navigation. + // as the previous Source.CoreWebView.Navigate() will always trigger a navigation apart + // from few cases: + // 1. When called again after adding fragment to the url, or + // 2. When called again on the same fragmented url. _iWebView2.CoreWebView2.Navigate(uri.ToString()); // } diff --git a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj index 05d9247..dc7af1a 100644 --- a/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj +++ b/SampleApps/WebView2WpfBrowser/WebView2WpfBrowser.csproj @@ -61,7 +61,7 @@ - +