Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webview2): disable smartscreen & allow disabling internal webview2 args, closes #704 #705

Merged
merged 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/smart-screen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

Disabled Microsoft SmartScreen by default on Windows.
5 changes: 5 additions & 0 deletions .changes/webview2-disable-additional-args.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@ pub unsafe fn setup(env: JNIEnv, looper: &ForeignLooper, activity: GlobalRef) {
.unwrap();
}

pub struct InnerWebView {
pub(crate) struct InnerWebView {
pub window: Rc<Window>,
}

impl InnerWebView {
pub fn new(
window: Rc<Window>,
attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
_web_context: Option<&mut WebContext>,
) -> Result<Self> {
let WebViewAttributes {
Expand Down
51 changes: 50 additions & 1 deletion src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
pub(crate) struct PlatformSpecificWebViewAttributes;

/// Type alias for a color in the RGBA format.
///
/// Each value can be 0..255 inclusive.
Expand All @@ -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,
}
Expand All @@ -229,11 +248,13 @@ impl<'a> WebViewBuilder<'a> {
pub fn new(window: Window) -> Result<Self> {
let webview = WebViewAttributes::default();
let web_context = None;
let platform_specific = PlatformSpecificWebViewAttributes::default();

Ok(Self {
webview,
web_context,
window,
platform_specific,
})
}

Expand Down Expand Up @@ -448,11 +469,39 @@ impl<'a> WebViewBuilder<'a> {
/// [`EventLoop`]: crate::application::event_loop::EventLoop
pub fn build(self) -> Result<WebView> {
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
Expand Down
5 changes: 3 additions & 2 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use crate::{
mod file_drop;
mod web_context;

pub struct InnerWebView {
pub(crate) webview: Rc<WebView>,
pub(crate) struct InnerWebView {
pub webview: Rc<WebView>,
#[cfg(any(debug_assertions, feature = "devtools"))]
is_inspector_open: Arc<AtomicBool>,
}
Expand All @@ -48,6 +48,7 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
mut attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
web_context: Option<&mut WebContext>,
) -> Result<Self> {
let window_rc = Rc::clone(&window);
Expand Down
19 changes: 12 additions & 7 deletions src/webview/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ impl From<webview2_com::Error> 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
Expand All @@ -62,14 +62,15 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
mut attributes: WebViewAttributes,
pl_attrs: super::PlatformSpecificWebViewAttributes,
web_context: Option<&mut WebContext>,
) -> Result<Self> {
let hwnd = HWND(window.hwnd() as _);
let file_drop_controller: Rc<OnceCell<FileDropController>> = Rc::new(OnceCell::new());
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)?;

Expand All @@ -88,6 +89,7 @@ impl InnerWebView {

fn create_environment(
web_context: &Option<&mut WebContext>,
pl_attrs: super::PlatformSpecificWebViewAttributes,
) -> webview2_com::Result<ICoreWebView2Environment> {
let (tx, rx) = mpsc::channel();

Expand Down Expand Up @@ -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(
Expand Down
9 changes: 5 additions & 4 deletions src/webview/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Fn(&Window, String)>, Rc<Window>),
Expand All @@ -69,6 +69,7 @@ impl InnerWebView {
pub fn new(
window: Rc<Window>,
attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
mut web_context: Option<&mut WebContext>,
) -> Result<Self> {
// Function for ipc handler
Expand Down