From 41d170e5ec4e225bca579347dabe5f20360569ee Mon Sep 17 00:00:00 2001 From: Hannes Wellmann Date: Sat, 17 Feb 2024 10:27:42 +0100 Subject: [PATCH] Support running Windows Defender Autofix startup check in custom apps Currently the startup check for the Windows Defender Autofix only runs for the 'org.eclipse.ui.ide.workbench' application. This adds the possibility to permit the startup check for other applications by setting the preference 'windows.defender.startup.check.app' to the permitted appId. Furthermore handle the absence of the PlatformUI workbench and don't show the link to the preferences in that case. --- .../internal/WindowsDefenderConfigurator.java | 40 +++++++++++-------- .../eclipse/ui/internal/messages.properties | 8 ++-- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java index 135aac4a201..66fa39bd09a 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WindowsDefenderConfigurator.java @@ -76,21 +76,18 @@ @EventTopics(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE) public class WindowsDefenderConfigurator implements EventHandler { private static final String PREFERENCE_EXCLUDED_INSTALLATION_PATH = "windows.defender.excluded.path"; //$NON-NLS-1$ + private static final String PREFERENCE_STARTUP_CHECK_APP = "windows.defender.startup.check.app"; //$NON-NLS-1$ public static final String PREFERENCE_STARTUP_CHECK_SKIP = "windows.defender.startup.check.skip"; //$NON-NLS-1$ public static final boolean PREFERENCE_STARTUP_CHECK_SKIP_DEFAULT = false; @Reference - private IPreferencesService preferences; + protected IPreferencesService preferences; @Override public void handleEvent(Event event) { if (runStartupCheck()) { Job job = Job.create(WorkbenchMessages.WindowsDefenderConfigurator_statusCheck, m -> { SubMonitor monitor = SubMonitor.convert(m, 10); - if (preferences.getBoolean(PI_WORKBENCH, PREFERENCE_STARTUP_CHECK_SKIP, - PREFERENCE_STARTUP_CHECK_SKIP_DEFAULT, null)) { - return; - } Optional installLocation = getInstallationLocation(); if (installLocation.isPresent()) { String checkedPath = getPreference(ConfigurationScope.INSTANCE) @@ -107,9 +104,15 @@ public void handleEvent(Event event) { } } - private static boolean runStartupCheck() { - if (Platform.isRunning() && Platform.OS.isWindows() && !Platform.inDevelopmentMode()) { - return "org.eclipse.ui.ide.workbench".equals(getRunningApplicationId()); //$NON-NLS-1$ + protected boolean runStartupCheck() { + if (Platform.OS.isWindows() && !Platform.inDevelopmentMode()) { + if (preferences.getBoolean(PI_WORKBENCH, PREFERENCE_STARTUP_CHECK_SKIP, + PREFERENCE_STARTUP_CHECK_SKIP_DEFAULT, null)) { + return false; + } + String permittedApp = preferences.getString(PI_WORKBENCH, PREFERENCE_STARTUP_CHECK_APP, + "org.eclipse.ui.ide.workbench", null); //$NON-NLS-1$ + return permittedApp.equals(getRunningApplicationId()); } return false; } @@ -150,8 +153,8 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional inst if (!isWindowsDefenderActive(monitor.split(1))) { return Boolean.FALSE; } - - HandlingOption decision = askForDefenderHandlingDecision(); + Display display = Display.getDefault(); + HandlingOption decision = askForDefenderHandlingDecision(display); if (decision != null) { switch (decision) { case EXECUTE_EXCLUSION -> { @@ -160,9 +163,8 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional inst savePreference(ConfigurationScope.INSTANCE, PREFERENCE_EXCLUDED_INSTALLATION_PATH, installLocation.map(Path::toString).orElse("")); //$NON-NLS-1$ } catch (IOException e) { - PlatformUI.getWorkbench().getDisplay() - .syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$ - bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed))); + display.syncExec(() -> MessageDialog.openError(null, "Exclusion failed", //$NON-NLS-1$ + bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionFailed))); } } case IGNORE_THIS_INSTALLATION -> savePreference(ConfigurationScope.INSTANCE, PREFERENCE_STARTUP_CHECK_SKIP, @@ -172,12 +174,12 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional inst return decision == HandlingOption.EXECUTE_EXCLUSION ? Boolean.TRUE : null; } - private static HandlingOption askForDefenderHandlingDecision() { + private static HandlingOption askForDefenderHandlingDecision(Display display) { String message = bindProductName(WorkbenchMessages.WindowsDefenderConfigurator_exclusionCheckMessage); - return PlatformUI.getWorkbench().getDisplay().syncCall(() -> { + return display.syncCall(() -> { HandlingOption[] choice = new HandlingOption[] { null }; - MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), + MessageDialog dialog = new MessageDialog(display.getActiveShell(), WorkbenchMessages.WindowsDefenderConfigurator_statusCheck, null, message, MessageDialog.INFORMATION, 0, IDialogConstants.PROCEED_LABEL, IDialogConstants.CANCEL_LABEL) { @@ -204,6 +206,9 @@ protected Control createCustomArea(Composite parent) { }).create(parent); GridDataFactory.swtDefaults().indent(0, 5).applyTo(keepScanning); + if (!PlatformUI.isWorkbenchRunning()) { + return parent; // Only show the link to the preferences if the workbench is available + } LinkFactory.newLink(SWT.WRAP) .text(WorkbenchMessages.WindowsDefenderConfigurator_detailsAndOptionsLinkText) .onSelect((e -> { @@ -231,7 +236,8 @@ protected void createButtonsForButtonBar(Composite parent) { } public static String bindProductName(String message) { - return NLS.bind(message, Platform.getProduct().getName()); + String name = Optional.ofNullable(Platform.getProduct()).map(IProduct::getName).orElse("this application"); //$NON-NLS-1$ + return NLS.bind(message, name); } public static IEclipsePreferences getPreference(IScopeContext instance) { diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties index e9bb1c2efad..952ea325f46 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties @@ -569,14 +569,14 @@ CheckedTreeSelectionDialog_select_all=Select &All CheckedTreeSelectionDialog_deselect_all=&Deselect All WindowsDefenderConfigurator_statusCheck=Windows Defender Exclusion Check -WindowsDefenderConfigurator_exclusionCheckMessage=Microsoft's Windows Defender is active and could significantly decrease this application's startup and overall performance.\n\ +WindowsDefenderConfigurator_exclusionCheckMessage=Microsoft''s Windows Defender is active and could significantly decrease the startup and overall performance of {0}.\n\ Select how this installation should be handled by Windows Defender: -WindowsDefenderConfigurator_exclusionInformation=If Windows Defender is active and scans this application it can significantly slow down its startup and overall performance.\n\ +WindowsDefenderConfigurator_exclusionInformation=If Windows Defender is active and scans {0} it can significantly slow down its startup and overall performance.\n\ To prevent a decrease in performance {0} can exclude itself and all files opened from real-time scanning by Windows Defender.\n\ -Be aware that in general adding exclusions could affect this computer's security. +Be aware that in general adding exclusions could affect this computer''s security. WindowsDefenderConfigurator_scriptShowLabel=Show Powershell script >> WindowsDefenderConfigurator_scriptHideLabel=<< Hide Powershell script -WindowsDefenderConfigurator_scriptHint=Adding exclusions respectively running the Powershell script to do this requires administrator privileges. +WindowsDefenderConfigurator_scriptHint=Adding exclusions respectively running the Powershell script to do it requires administrator privileges. WindowsDefenderConfigurator_performExclusionChoice=Exclude {0} from being scanned to improve performance.\n\ (In general adding exclusions may affect the security level of this computer) WindowsDefenderConfigurator_ignoreThisInstallationChoice=Keep {0} being scanned by Windows Defender.