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

Code Quality: Introduced IWindowsAppLauncherService #15927

Closed
Closed
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
14 changes: 14 additions & 0 deletions src/Files.App/Data/Contracts/IWindowsAppLauncherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

namespace Files.App.Data.Contracts
{
public interface IWindowsAppLauncherService
{
Task<bool> LaunchStorageSensePolicySettingsAsync();

Task<bool> LaunchProgramCompatibilityTroubleshooterAsync(string path);

Task<bool> LaunchApplicationAsync(string path, string arguments, string workingDirectory);
}
}
1 change: 1 addition & 0 deletions src/Files.App/Helpers/Application/AppLifecycleHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public static IHost ConfigureHost()
.AddSingleton<ITagsContext, TagsContext>()
.AddSingleton<ISidebarContext, SidebarContext>()
// Services
.AddSingleton<IWindowsAppLauncherService, WindowsAppLauncherService>()
.AddSingleton<IWindowsIniService, WindowsIniService>()
.AddSingleton<IWindowsWallpaperService, WindowsWallpaperService>()
.AddSingleton<IWindowsSecurityService, WindowsSecurityService>()
Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/Helpers/Win32/Win32Helper.Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static async Task<bool> InvokeWin32ComponentsAsync(IEnumerable<string> ap
}
else
{
return await LaunchHelper.LaunchAppAsync(application, arguments, workingDirectory);
var windowsLaunchAppService = Ioc.Default.GetRequiredService<IWindowsAppLauncherService>();
return await windowsLaunchAppService.LaunchApplicationAsync(application, arguments, workingDirectory);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Files.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@ public static void OpenFileFromTile(string filePath)

Task.Run(() =>
{
LaunchHelper.LaunchAppAsync(filePath, null, null).Wait();
var windowsLaunchAppService = Ioc.Default.GetRequiredService<IWindowsAppLauncherService>();
windowsLaunchAppService.LaunchApplicationAsync(filePath, null, null).Wait();
SetEvent(eventHandle);
});

Expand Down
245 changes: 245 additions & 0 deletions src/Files.App/Services/Windows/WindowsAppLauncherService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using Files.Shared.Helpers;
using Microsoft.Extensions.Logging;
using Windows.System;

namespace Files.App.Services
{
/// <inheritdoc cref="IWindowsAppLauncherService"/>
public class WindowsAppLauncherService : IWindowsAppLauncherService
{
/// <inheritdoc/>
public async Task<bool> LaunchStorageSensePolicySettingsAsync()
{
bool result = await Launcher.LaunchUriAsync(new Uri("ms-settings:storagepolicies"));
return result;
}

/// <inheritdoc/>
public async Task<bool> LaunchProgramCompatibilityTroubleshooterAsync(string path)
{
var compatibilityTroubleshooterAnswerFile = SystemIO.Path.Combine(SystemIO.Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile.xml");
string troubleshooterAnswerFileText = $@"<?xml version=""1.0"" encoding=""UTF-8""?><Answers Version=""1.0""><Interaction ID=""IT_LaunchMethod""><Value>CompatTab</Value></Interaction><Interaction ID=""IT_BrowseForFile""><Value>{path}</Value></Interaction></Answers>";

try
{
SystemIO.File.WriteAllText(compatibilityTroubleshooterAnswerFile, troubleshooterAnswerFileText);
}
catch (SystemIO.IOException)
{
// Try with a different file name
SafetyExtensions.IgnoreExceptions(() =>
{
compatibilityTroubleshooterAnswerFile = SystemIO.Path.Combine(SystemIO.Path.GetTempPath(), "CompatibilityTroubleshooterAnswerFile1.xml");
SystemIO.File.WriteAllText(compatibilityTroubleshooterAnswerFile, troubleshooterAnswerFileText);
});
}

return await LaunchApplicationAsync("MSDT.exe", @$"/id PCWDiagnostic /af ""{compatibilityTroubleshooterAnswerFile}""", "");
}

/// <inheritdoc/>
public async Task<bool> LaunchApplicationAsync(string path, string arguments, string workingDirectory)
{
var currentWindows = Win32Helper.GetDesktopWindows();

// Use PowerShell to mount Vhd Disk as this requires admin rights
if (FileExtensionHelpers.IsVhdFile(path))
return await Win32Helper.MountVhdDisk(path);

try
{
using Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = path;

// Show window if workingDirectory (opening terminal)
process.StartInfo.CreateNoWindow = string.IsNullOrEmpty(workingDirectory);

if (arguments.Equals("RunAs", StringComparison.OrdinalIgnoreCase))
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "RunAs";

if (FileExtensionHelpers.IsMsiFile(path))
{
process.StartInfo.FileName = "MSIEXEC.exe";
process.StartInfo.Arguments = $"/a \"{path}\"";
}
}
else if (arguments.Equals("RunAsUser", StringComparison.OrdinalIgnoreCase))
{
process.StartInfo.UseShellExecute = true;
process.StartInfo.Verb = "RunAsUser";

if (FileExtensionHelpers.IsMsiFile(path))
{
process.StartInfo.FileName = "MSIEXEC.exe";
process.StartInfo.Arguments = $"/i \"{path}\"";
}
}
else
{
process.StartInfo.Arguments = arguments;

// Refresh env variables for the child process
foreach (DictionaryEntry ent in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User))
process.StartInfo.EnvironmentVariables[(string)ent.Key] = (string)ent.Value;

process.StartInfo.EnvironmentVariables["PATH"] = string.Join(';',
Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine),
Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User));
}

process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? PathNormalization.GetParentDir(path) : workingDirectory;
process.Start();

Win32Helper.BringToForeground(currentWindows);

return true;
}
catch (Win32Exception)
{
using Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = path;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = arguments;
process.StartInfo.WorkingDirectory = string.IsNullOrEmpty(workingDirectory) ? PathNormalization.GetParentDir(path) : workingDirectory;

try
{
process.Start();

Win32Helper.BringToForeground(currentWindows);

return true;
}
catch (Win32Exception ex) when (ex.NativeErrorCode == 50)
{
// ShellExecute return code 50 (ERROR_NOT_SUPPORTED) for some exes (#15179)
return Win32Helper.RunPowershellCommand($"\"{path}\"", PowerShellExecutionOptions.Hidden);
}
catch (Win32Exception)
{
try
{
var opened = await Win32Helper.StartSTATask(async () =>
{
var split = path.Split('|').Where(x => !string.IsNullOrWhiteSpace(x)).Select(x => GetMtpPath(x));
if (split.Count() == 1)
{
Process.Start(split.First());

Win32Helper.BringToForeground(currentWindows);
}
else
{
var groups = split.GroupBy(x => new
{
Dir = SystemIO.Path.GetDirectoryName(x),
Prog = Win32Helper.GetFileAssociationAsync(x).Result ?? SystemIO.Path.GetExtension(x)
});

foreach (var group in groups)
{
if (!group.Any())
continue;

using var cMenu = await ContextMenu.GetContextMenuForFiles(group.ToArray(), Vanara.PInvoke.Shell32.CMF.CMF_DEFAULTONLY);

if (cMenu is not null)
await cMenu.InvokeVerb(Vanara.PInvoke.Shell32.CMDSTR_OPEN);
}
}

return true;
});

if (!opened)
{
if (path.StartsWith(@"\\SHELL\", StringComparison.Ordinal))
{
opened = await Win32Helper.StartSTATask(async () =>
{
using var cMenu = await ContextMenu.GetContextMenuForFiles(new[] { path }, Vanara.PInvoke.Shell32.CMF.CMF_DEFAULTONLY);

if (cMenu is not null)
await cMenu.InvokeItem(cMenu.Items.FirstOrDefault()?.ID ?? -1);

return true;
});
}
}

if (!opened)
{
var isAlternateStream = RegexHelpers.AlternateStream().IsMatch(path);
if (isAlternateStream)
{
var basePath = SystemIO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), Guid.NewGuid().ToString("n"));
Vanara.PInvoke.Kernel32.CreateDirectory(basePath);

var tempPath = SystemIO.Path.Combine(basePath, new string(SystemIO.Path.GetFileName(path).SkipWhile(x => x != ':').Skip(1).ToArray()));
using var hFileSrc = Vanara.PInvoke.Kernel32.CreateFile(path, Vanara.PInvoke.Kernel32.FileAccess.GENERIC_READ, SystemIO.FileShare.ReadWrite, null, SystemIO.FileMode.Open, Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL);
using var hFileDst = Vanara.PInvoke.Kernel32.CreateFile(tempPath, Vanara.PInvoke.Kernel32.FileAccess.GENERIC_WRITE, 0, null, SystemIO.FileMode.Create, Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL | Vanara.PInvoke.FileFlagsAndAttributes.FILE_ATTRIBUTE_READONLY);

if (!hFileSrc.IsInvalid && !hFileDst.IsInvalid)
{
// Copy ADS to temp folder and open
using (var inStream = new SystemIO.FileStream(hFileSrc.DangerousGetHandle(), SystemIO.FileAccess.Read))
using (var outStream = new SystemIO.FileStream(hFileDst.DangerousGetHandle(), SystemIO.FileAccess.Write))
{
await inStream.CopyToAsync(outStream);
await outStream.FlushAsync();
}

opened = await LaunchApplicationAsync(tempPath, arguments, workingDirectory);
}
}
}

return opened;
}
catch (Win32Exception)
{
// Cannot open file (e.g DLL)
return false;
}
catch (ArgumentException)
{
// Cannot open file (e.g DLL)
return false;
}
}
}
catch (InvalidOperationException)
{
// Invalid file path
return false;
}
catch (Exception ex)
{
// Generic error, log
App.Logger.LogWarning(ex, $"Error launching: {path}");
return false;
}

string GetMtpPath(string executable)
{
if (executable.StartsWith("\\\\?\\", StringComparison.Ordinal))
{
using var computer = new Vanara.Windows.Shell.ShellFolder(Vanara.PInvoke.Shell32.KNOWNFOLDERID.FOLDERID_ComputerFolder);
using var device = computer.FirstOrDefault(i => executable.Replace("\\\\?\\", "", StringComparison.Ordinal).StartsWith(i.Name, StringComparison.Ordinal));
var deviceId = device?.ParsingName;
var itemPath = RegexHelpers.WindowsPath().Replace(executable, "");
return deviceId is not null ? SystemIO.Path.Combine(deviceId, itemPath) : executable;
}

return executable;
}
}
}
}
3 changes: 2 additions & 1 deletion src/Files.App/UserControls/Widgets/DrivesWidget.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Files.App.UserControls.Widgets
public sealed partial class DrivesWidget : UserControl
{
public DrivesWidgetViewModel ViewModel { get; set; } = Ioc.Default.GetRequiredService<DrivesWidgetViewModel>();
private IWindowsAppLauncherService WindowsLaunchAppService = Ioc.Default.GetRequiredService<IWindowsAppLauncherService>();

public DrivesWidget()
{
Expand Down Expand Up @@ -52,7 +53,7 @@ private async void GoToStorageSense_Click(object sender, RoutedEventArgs e)
button.Tag.ToString() is not string path)
return;

await StorageSenseHelper.OpenStorageSenseAsync(path);
await WindowsLaunchAppService.LaunchStorageSensePolicySettingsAsync();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you include the path in the arguments?

Copy link
Member Author

@0x5bfa 0x5bfa Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first implementation to include path is made by Gave (3 yrs ago) but I'm not sure why because Storage Sense works for only system drive and for the whole system drive not for specifically a path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually path isn't used to be passed to settings to give extra context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a thread on GitHub about this with more details, but in any event, it makes a difference for which page is opened in Settings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both open wrong page.

For other than C (system) drive:
image

For thers:
image

Both should open:
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My implementapention opens what should be opened.

Copy link
Member

@yaira2 yaira2 Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both open wrong page.

It's supposed to open the Storage Page for the C:\ drive, and the "Storage used on other drives" page for other drives.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even so, how would you free up storage for other drives? The "Storage used on other drives" doesn't contains any free up page.
As to C, why not open Policy page directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this weird page navigation might come from win10 when Gave implemented

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even so, how would you free up storage for other drives? The "Storage used on other drives" doesn't contains any free up page.

You can access the "Free up storage" page by clicking on a drive. Ideally we'd open directly to that page, but that's not supported by the Settings app.

}
}
}
Loading