Skip to content

Commit

Permalink
Support running Windows Defender Autofix startup check in custom apps
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
HannesWell committed Feb 17, 2024
1 parent 49bb3f5 commit 41d170e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path> installLocation = getInstallationLocation();
if (installLocation.isPresent()) {
String checkedPath = getPreference(ConfigurationScope.INSTANCE)
Expand All @@ -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;
}
Expand Down Expand Up @@ -150,8 +153,8 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> 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 -> {
Expand All @@ -160,9 +163,8 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> 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,
Expand All @@ -172,12 +174,12 @@ private static Boolean runExclusionCheck(IProgressMonitor m, Optional<Path> 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) {

Expand All @@ -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 -> {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 41d170e

Please sign in to comment.