From bd168b7fe607314a154043a44346473b1b12e61a Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 28 Sep 2022 00:11:45 +0200 Subject: [PATCH 1/3] feat(webview2): disable smartscreen & allow disabling internal webview2 args, closes #704 --- .changes/smart-screen.md | 5 ++ .changes/webview2-disable-additional-args.md | 5 ++ src/webview/android/mod.rs | 1 + src/webview/mod.rs | 51 +++++++++++++++++++- src/webview/webkitgtk/mod.rs | 1 + src/webview/webview2/mod.rs | 19 +++++--- src/webview/wkwebview/mod.rs | 1 + 7 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 .changes/smart-screen.md create mode 100644 .changes/webview2-disable-additional-args.md diff --git a/.changes/smart-screen.md b/.changes/smart-screen.md new file mode 100644 index 000000000..58f0962fd --- /dev/null +++ b/.changes/smart-screen.md @@ -0,0 +1,5 @@ +--- +"wry": "patch" +--- + +Disabled Microsoft SmartScreen by default on Windows. \ No newline at end of file diff --git a/.changes/webview2-disable-additional-args.md b/.changes/webview2-disable-additional-args.md new file mode 100644 index 000000000..5b806b81e --- /dev/null +++ b/.changes/webview2-disable-additional-args.md @@ -0,0 +1,5 @@ +--- +"wry": "patch" +--- + +Add `WebviewBuilderExtWindows::disable_additionl_browser_args` method to prevent wry from passing additional browser args to Webview2 On Windows. By default wry passes `--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection` so if you use this method, you also need to add disable these components by yourself if you want. \ No newline at end of file diff --git a/src/webview/android/mod.rs b/src/webview/android/mod.rs index f794476f1..b13e449c2 100644 --- a/src/webview/android/mod.rs +++ b/src/webview/android/mod.rs @@ -122,6 +122,7 @@ impl InnerWebView { pub fn new( window: Rc, attributes: WebViewAttributes, + _pl_attrs: super::PlatformSpecificWebViewAttributes, _web_context: Option<&mut WebContext>, ) -> Result { let WebViewAttributes { diff --git a/src/webview/mod.rs b/src/webview/mod.rs index d5d262d44..122eb1eb2 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -208,6 +208,24 @@ impl Default for WebViewAttributes { } } +#[cfg(windows)] +#[derive(Default)] +pub(crate) struct PlatformSpecificWebViewAttributes { + disable_additionl_browser_args: bool, +} +#[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd", + target_os = "macos", + target_os = "android", + target_os = "ios", +))] +#[derive(Default)] +struct PlatformSpecificWebViewAttributes; + /// Type alias for a color in the RGBA format. /// /// Each value can be 0..255 inclusive. @@ -220,6 +238,7 @@ pub type RGBA = (u8, u8, u8, u8); /// [`WebViewBuilder`] provides ability to setup initialization before web engine starts. pub struct WebViewBuilder<'a> { pub webview: WebViewAttributes, + platform_specific: PlatformSpecificWebViewAttributes, web_context: Option<&'a mut WebContext>, window: Window, } @@ -229,11 +248,13 @@ impl<'a> WebViewBuilder<'a> { pub fn new(window: Window) -> Result { let webview = WebViewAttributes::default(); let web_context = None; + let platform_specific = PlatformSpecificWebViewAttributes::default(); Ok(Self { webview, web_context, window, + platform_specific, }) } @@ -448,11 +469,39 @@ impl<'a> WebViewBuilder<'a> { /// [`EventLoop`]: crate::application::event_loop::EventLoop pub fn build(self) -> Result { let window = Rc::new(self.window); - let webview = InnerWebView::new(window.clone(), self.webview, self.web_context)?; + let webview = InnerWebView::new( + window.clone(), + self.webview, + self.platform_specific, + self.web_context, + )?; Ok(WebView { window, webview }) } } +#[cfg(windows)] +pub trait WebViewBuilderExtWindows { + /// Disables ther internal use of the additional browser arguments + /// passed to Webview2, so the env var like below isn't overwritten + /// ``` + /// std::env::set_var("WEBVIEW2_ADDITIONAL_BROWSER_ARGUMENTS", "--disable-features=msSmartScreenProtection"); + /// ``` + /// + /// ## Warning + /// + /// By default wry passes `--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection` + /// so if you use this method, you also need to add disable these components by yourself if you want. + fn disable_additionl_browser_args(self) -> Self; +} + +#[cfg(windows)] +impl WebViewBuilderExtWindows for WebViewBuilder<'_> { + fn disable_additionl_browser_args(mut self) -> Self { + self.platform_specific.disable_additionl_browser_args = true; + self + } +} + /// The fundamental type to present a [`WebView`]. /// /// [`WebViewBuilder`] / [`WebView`] are the basic building blocks to construct WebView contents and diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index 62a01dd09..77753339f 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -48,6 +48,7 @@ impl InnerWebView { pub fn new( window: Rc, mut attributes: WebViewAttributes, + _pl_attrs: super::PlatformSpecificWebViewAttributes, web_context: Option<&mut WebContext>, ) -> Result { let window_rc = Rc::clone(&window); diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs index 8f735e94f..302d92b32 100644 --- a/src/webview/webview2/mod.rs +++ b/src/webview/webview2/mod.rs @@ -49,8 +49,8 @@ impl From for Error { } } -pub struct InnerWebView { - pub(crate) controller: ICoreWebView2Controller, +pub(crate) struct InnerWebView { + pub controller: ICoreWebView2Controller, webview: ICoreWebView2, // Store FileDropController in here to make sure it gets dropped when // the webview gets dropped, otherwise we'll have a memory leak @@ -62,6 +62,7 @@ impl InnerWebView { pub fn new( window: Rc, mut attributes: WebViewAttributes, + pl_attrs: super::PlatformSpecificWebViewAttributes, web_context: Option<&mut WebContext>, ) -> Result { let hwnd = HWND(window.hwnd() as _); @@ -69,7 +70,7 @@ impl InnerWebView { let file_drop_handler = attributes.file_drop_handler.take(); let file_drop_window = window.clone(); - let env = Self::create_environment(&web_context)?; + let env = Self::create_environment(&web_context, pl_attrs)?; let controller = Self::create_controller(hwnd, &env)?; let webview = Self::init_webview(window, hwnd, attributes, &env, &controller)?; @@ -88,6 +89,7 @@ impl InnerWebView { fn create_environment( web_context: &Option<&mut WebContext>, + pl_attrs: super::PlatformSpecificWebViewAttributes, ) -> webview2_com::Result { let (tx, rx) = mpsc::channel(); @@ -118,10 +120,13 @@ impl InnerWebView { options }; - // remove "mini menu" - See https://github.com/tauri-apps/wry/issues/535 - let _ = options.SetAdditionalBrowserArguments(PCWSTR::from_raw( - encode_wide("--disable-features=msWebOOUI,msPdfOOUI").as_ptr(), - )); + if !pl_attrs.disable_additionl_browser_args { + // remove "mini menu" - See https://github.com/tauri-apps/wry/issues/535 + // and "smart screen" - See https://github.com/tauri-apps/tauri/issues/1345 + let _ = options.SetAdditionalBrowserArguments(PCWSTR::from_raw( + encode_wide("--disable-features=msWebOOUI,msPdfOOUI,msSmartScreenProtection").as_ptr(), + )); + } if let Some(data_directory) = data_directory { CreateCoreWebView2EnvironmentWithOptions( diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 9afd35710..8a498c32f 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -69,6 +69,7 @@ impl InnerWebView { pub fn new( window: Rc, attributes: WebViewAttributes, + _pl_attrs: super::PlatformSpecificWebViewAttributes, mut web_context: Option<&mut WebContext>, ) -> Result { // Function for ipc handler From cecf40e1f68abd1f44d8622b16960d59dd1dfdcf Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 28 Sep 2022 00:15:39 +0200 Subject: [PATCH 2/3] fix build macos and linux --- src/webview/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webview/mod.rs b/src/webview/mod.rs index 122eb1eb2..3dfd59ddd 100644 --- a/src/webview/mod.rs +++ b/src/webview/mod.rs @@ -224,7 +224,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes { target_os = "ios", ))] #[derive(Default)] -struct PlatformSpecificWebViewAttributes; +pub(crate) struct PlatformSpecificWebViewAttributes; /// Type alias for a color in the RGBA format. /// From aee63421189e283ee65e3de3f335794859996bee Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 28 Sep 2022 01:58:37 +0200 Subject: [PATCH 3/3] fix macos and linux and android --- src/webview/android/mod.rs | 2 +- src/webview/webkitgtk/mod.rs | 4 ++-- src/webview/wkwebview/mod.rs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/webview/android/mod.rs b/src/webview/android/mod.rs index b13e449c2..0cdc0dc75 100644 --- a/src/webview/android/mod.rs +++ b/src/webview/android/mod.rs @@ -114,7 +114,7 @@ pub unsafe fn setup(env: JNIEnv, looper: &ForeignLooper, activity: GlobalRef) { .unwrap(); } -pub struct InnerWebView { +pub(crate) struct InnerWebView { pub window: Rc, } diff --git a/src/webview/webkitgtk/mod.rs b/src/webview/webkitgtk/mod.rs index 77753339f..baf60705e 100644 --- a/src/webview/webkitgtk/mod.rs +++ b/src/webview/webkitgtk/mod.rs @@ -38,8 +38,8 @@ use crate::{ mod file_drop; mod web_context; -pub struct InnerWebView { - pub(crate) webview: Rc, +pub(crate) struct InnerWebView { + pub webview: Rc, #[cfg(any(debug_assertions, feature = "devtools"))] is_inspector_open: Arc, } diff --git a/src/webview/wkwebview/mod.rs b/src/webview/wkwebview/mod.rs index 8a498c32f..4ec7b511d 100644 --- a/src/webview/wkwebview/mod.rs +++ b/src/webview/wkwebview/mod.rs @@ -51,11 +51,11 @@ use crate::http::{ Request as HttpRequest, RequestBuilder as HttpRequestBuilder, Response as HttpResponse, }; -pub struct InnerWebView { - pub(crate) webview: id, +pub(crate) struct InnerWebView { + pub webview: id, #[cfg(target_os = "macos")] - pub(crate) ns_window: id, - pub(crate) manager: id, + pub ns_window: id, + pub manager: id, // Note that if following functions signatures are changed in the future, // all functions pointer declarations in objc callbacks below all need to get updated. ipc_handler_ptr: *mut (Box, Rc),