[PT Run] Uri/Web search plugin crash fix (#15472)

* [PT Run] Browser path bug fix

* [PT Run][URI/Web Search] Refactoring

* [PT Run][URI] Small change

* [PT Run] Small modifications

* [PT Run] Refactoring: moved common files to Plugins\Common and added references to plugins that use them

* Fixed spelling

* [PT Run][URI] Small adjustment

* [PT Run] Fixed refactoring error

* [PT Run] Reversed refactoring NativeMethods.cs into single file

* Fixed PR changed files list (unchanged files appeared modified because of different encodings)

* [PT Run] Moved BrowserInfo.cs to Wox.Plugin/SharedCommands and removed Plugins/Common

* [PT Run] Renamed BrowserInfo to DefaultBrowserInfo and made it static

* Minor modifications

* [PT Run] Fixed refactoring error

* Minor modifications

* [PT Run] Renamed Wox.Plugin.SharedCommands to Wox.Plugin.Common + small change
This commit is contained in:
cyberrex5
2022-01-25 20:31:57 +02:00
committed by GitHub
parent 022ed601ec
commit f9434ac812
15 changed files with 400 additions and 412 deletions

View File

@@ -1,11 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Microsoft.Plugin.Uri.Interfaces
{
public interface IRegistryWrapper
{
string GetRegistryValue(string registryLocation, string valueName);
}
}

View File

@@ -4,29 +4,22 @@
using System;
using System.Collections.Generic;
using System.IO.Abstractions;
using System.Text;
using ManagedCommon;
using Microsoft.Plugin.Uri.UriHelper;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using Wox.Plugin.Logger;
using BrowserInfo = Wox.Plugin.Common.DefaultBrowserInfo;
namespace Microsoft.Plugin.Uri
{
public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IDisposable
public class Main : IPlugin, IPluginI18n, IContextMenu, ISavable, IReloadable, IDisposable
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IPath Path = FileSystem.Path;
private static readonly IFile File = FileSystem.File;
private readonly ExtendedUriParser _uriParser;
private readonly UriResolver _uriResolver;
private readonly PluginJsonStorage<UriSettings> _storage;
private bool _disposed;
private UriSettings _uriSettings;
private RegistryWrapper _registryWrapper;
public Main()
{
@@ -34,13 +27,8 @@ namespace Microsoft.Plugin.Uri
_uriSettings = _storage.Load();
_uriParser = new ExtendedUriParser();
_uriResolver = new UriResolver();
_registryWrapper = new RegistryWrapper();
}
public string BrowserIconPath { get; set; }
public string BrowserPath { get; set; }
public string DefaultIconPath { get; set; }
public PluginInitContext Context { get; protected set; }
@@ -59,16 +47,16 @@ namespace Microsoft.Plugin.Uri
var results = new List<Result>();
if (IsActivationKeyword(query)
&& IsDefaultBrowserSet())
&& BrowserInfo.IsDefaultBrowserSet)
{
results.Add(new Result
{
Title = Properties.Resources.Microsoft_plugin_uri_open,
SubTitle = BrowserPath,
SubTitle = BrowserInfo.Path,
IcoPath = DefaultIconPath,
Action = action =>
{
if (!Helper.OpenInShell(BrowserPath))
if (!Helper.OpenInShell(BrowserInfo.Path))
{
var title = $"Plugin: {Properties.Resources.Microsoft_plugin_uri_plugin_name}";
var message = $"{Properties.Resources.Microsoft_plugin_uri_open_failed}: ";
@@ -95,8 +83,8 @@ namespace Microsoft.Plugin.Uri
SubTitle = isWebUriBool
? Properties.Resources.Microsoft_plugin_uri_website
: Properties.Resources.Microsoft_plugin_uri_open,
IcoPath = isWebUriBool
? BrowserIconPath
IcoPath = isWebUriBool && BrowserInfo.IconPath != null
? BrowserInfo.IconPath
: DefaultIconPath,
Action = action =>
{
@@ -122,17 +110,12 @@ namespace Microsoft.Plugin.Uri
&& query?.ActionKeyword == query?.RawQuery;
}
private bool IsDefaultBrowserSet()
{
return !string.IsNullOrEmpty(BrowserPath);
}
public void Init(PluginInitContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
Context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(Context.API.GetCurrentTheme());
UpdateBrowserIconPath(Context.API.GetCurrentTheme());
BrowserInfo.UpdateIfTimePassed();
}
public string GetTranslatedPluginTitle()
@@ -153,70 +136,6 @@ namespace Microsoft.Plugin.Uri
private void OnThemeChanged(Theme oldtheme, Theme newTheme)
{
UpdateIconPath(newTheme);
UpdateBrowserIconPath(newTheme);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Design",
"CA1031:Do not catch general exception types",
Justification = "We want to keep the process alive but will log the exception")]
private void UpdateBrowserIconPath(Theme newTheme)
{
try
{
var progId = _registryWrapper.GetRegistryValue(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
"ProgId");
var programLocation =
// Resolve App Icon (UWP)
_registryWrapper.GetRegistryValue(
"HKEY_CLASSES_ROOT\\" + progId + "\\Application",
"ApplicationIcon")
// Resolves default file association icon (UWP + Normal)
?? _registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\DefaultIcon", null);
// "Handles 'Indirect Strings' (UWP programs)"
// Using Ordinal since this is internal and used with a symbol
if (programLocation.StartsWith("@", StringComparison.Ordinal))
{
var directProgramLocationStringBuilder = new StringBuilder(128);
if (NativeMethods.SHLoadIndirectString(
programLocation,
directProgramLocationStringBuilder,
(uint)directProgramLocationStringBuilder.Capacity,
IntPtr.Zero) ==
NativeMethods.Hresult.Ok)
{
// Check if there's a postfix with contract-white/contrast-black icon is available and use that instead
var directProgramLocation = directProgramLocationStringBuilder.ToString();
var themeIcon = newTheme == Theme.Light || newTheme == Theme.HighContrastWhite
? "contrast-white"
: "contrast-black";
var extension = Path.GetExtension(directProgramLocation);
var themedProgLocation =
$"{directProgramLocation.Substring(0, directProgramLocation.Length - extension.Length)}_{themeIcon}{extension}";
BrowserIconPath = File.Exists(themedProgLocation)
? themedProgLocation
: directProgramLocation;
}
}
else
{
// Using Ordinal since this is internal and used with a symbol
var indexOfComma = programLocation.IndexOf(',', StringComparison.Ordinal);
BrowserIconPath = indexOfComma > 0
? programLocation.Substring(0, indexOfComma)
: programLocation;
BrowserPath = BrowserIconPath;
}
}
catch (Exception e)
{
BrowserIconPath = DefaultIconPath;
Log.Exception("Exception when retrieving icon", e, GetType());
}
}
private void UpdateIconPath(Theme theme)
@@ -231,6 +150,16 @@ namespace Microsoft.Plugin.Uri
}
}
public void ReloadData()
{
if (Context is null)
{
return;
}
BrowserInfo.UpdateIfTimePassed();
}
public void Dispose()
{
Dispose(true);

View File

@@ -1,21 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Microsoft.Plugin.Uri
{
internal static class NativeMethods
{
internal enum Hresult : uint
{
Ok = 0x0000,
}
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
internal static extern Hresult SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, uint cchOutBuf, IntPtr ppvReserved);
}
}

View File

@@ -1,17 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Plugin.Uri.Interfaces;
using Microsoft.Win32;
namespace Microsoft.Plugin.Uri
{
public class RegistryWrapper : IRegistryWrapper
{
public string GetRegistryValue(string registryLocation, string valueName)
{
return Registry.GetValue(registryLocation, valueName, null) as string;
}
}
}

View File

@@ -11,6 +11,7 @@ namespace Microsoft.Plugin.Uri.UriHelper
{
public class ExtendedUriParser : IUriParser
{
// When updating this method, also update the local method IsUri() in Community.PowerToys.Run.Plugin.WebSearch.Main.Query
public bool TryParse(string input, out System.Uri result, out bool isWebUri)
{
if (string.IsNullOrEmpty(input))