mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[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:
2
.github/actions/spell-check/expect.txt
vendored
2
.github/actions/spell-check/expect.txt
vendored
@@ -9,6 +9,7 @@ Abug
|
||||
accctrl
|
||||
Acceleratorkeys
|
||||
ACCEPTFILES
|
||||
ACCESSDENIED
|
||||
accessibile
|
||||
accessibilityinsights
|
||||
Acl
|
||||
@@ -894,6 +895,7 @@ Intelli
|
||||
interactable
|
||||
Interlop
|
||||
interop
|
||||
Interoperability
|
||||
intptr
|
||||
INTRESOURCE
|
||||
INVALIDARG
|
||||
|
||||
@@ -5,23 +5,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO.Abstractions;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Controls;
|
||||
using ManagedCommon;
|
||||
using Microsoft.PowerToys.Settings.UI.Library;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.Logger;
|
||||
using BrowserInfo = Wox.Plugin.Common.DefaultBrowserInfo;
|
||||
|
||||
namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
{
|
||||
public class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider, IDisposable
|
||||
public class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider, IReloadable, IDisposable
|
||||
{
|
||||
private static readonly IFileSystem FileSystem = new FileSystem();
|
||||
private static readonly IPath Path = FileSystem.Path;
|
||||
private static readonly IFile File = FileSystem.File;
|
||||
// Should only be set in Init()
|
||||
private Action onPluginError;
|
||||
|
||||
private const string NotGlobalIfUri = nameof(NotGlobalIfUri);
|
||||
|
||||
@@ -30,12 +28,7 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
|
||||
private PluginInitContext _context;
|
||||
|
||||
private string _searchEngineUrl;
|
||||
|
||||
private string _browserName = Properties.Resources.plugin_browser;
|
||||
private string _browserIconPath;
|
||||
private string _browserPath;
|
||||
private string _defaultIconPath;
|
||||
private string _iconPath;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
@@ -67,28 +60,22 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
|
||||
var results = new List<Result>();
|
||||
|
||||
if (!AreResultsGlobal()
|
||||
&& query.ActionKeyword == query.RawQuery
|
||||
&& IsDefaultBrowserSet())
|
||||
// empty non-global query:
|
||||
if (!AreResultsGlobal() && query.ActionKeyword == query.RawQuery)
|
||||
{
|
||||
string arguments = "\"? \"";
|
||||
results.Add(new Result
|
||||
{
|
||||
Title = Properties.Resources.plugin_description.Remove(Description.Length - 1, 1),
|
||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_in_browser_name, _browserName),
|
||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_in_browser_name, BrowserInfo.Name ?? BrowserInfo.MSEdgeName),
|
||||
QueryTextDisplay = string.Empty,
|
||||
IcoPath = _defaultIconPath,
|
||||
IcoPath = _iconPath,
|
||||
ProgramArguments = arguments,
|
||||
Action = action =>
|
||||
{
|
||||
if (!Helper.OpenInShell(_browserPath, arguments))
|
||||
if (!Helper.OpenInShell(BrowserInfo.Path ?? BrowserInfo.MSEdgePath, arguments))
|
||||
{
|
||||
string errorMsgString = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_search_failed, _browserName);
|
||||
|
||||
Log.Error(errorMsgString, GetType());
|
||||
_context.API.ShowMsg(
|
||||
$"Plugin: {Properties.Resources.plugin_name}",
|
||||
errorMsgString);
|
||||
onPluginError();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -113,26 +100,21 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
var result = new Result
|
||||
{
|
||||
Title = searchTerm,
|
||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_open, _browserName),
|
||||
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_open, BrowserInfo.Name ?? BrowserInfo.MSEdgeName),
|
||||
QueryTextDisplay = searchTerm,
|
||||
IcoPath = _defaultIconPath,
|
||||
IcoPath = _iconPath,
|
||||
};
|
||||
|
||||
if (_searchEngineUrl is null)
|
||||
if (BrowserInfo.SupportsWebSearchByCmdLineArgument)
|
||||
{
|
||||
string arguments = $"\"? {searchTerm}\"";
|
||||
|
||||
result.ProgramArguments = arguments;
|
||||
result.Action = action =>
|
||||
{
|
||||
if (!Helper.OpenInShell(_browserPath, arguments))
|
||||
if (!Helper.OpenInShell(BrowserInfo.Path ?? BrowserInfo.MSEdgePath, arguments))
|
||||
{
|
||||
string errorMsgString = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_search_failed, _browserName);
|
||||
|
||||
Log.Error(errorMsgString, GetType());
|
||||
_context.API.ShowMsg(
|
||||
$"Plugin: {Properties.Resources.plugin_name}",
|
||||
errorMsgString);
|
||||
onPluginError();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -141,18 +123,13 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
}
|
||||
else
|
||||
{
|
||||
string url = string.Format(CultureInfo.InvariantCulture, _searchEngineUrl, searchTerm);
|
||||
string url = string.Format(CultureInfo.InvariantCulture, BrowserInfo.SearchEngineUrl, searchTerm);
|
||||
|
||||
result.Action = action =>
|
||||
{
|
||||
if (!Helper.OpenInShell(url))
|
||||
{
|
||||
string errorMsgString = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_search_failed, _browserName);
|
||||
|
||||
Log.Error(errorMsgString, GetType());
|
||||
_context.API.ShowMsg(
|
||||
$"Plugin: {Properties.Resources.plugin_name}",
|
||||
errorMsgString);
|
||||
onPluginError();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -176,7 +153,8 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
if (input.EndsWith(":", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.Contains("/", StringComparison.OrdinalIgnoreCase)
|
||||
&& !input.All(char.IsDigit))
|
||||
&& !input.All(char.IsDigit)
|
||||
&& System.Text.RegularExpressions.Regex.IsMatch(input, @"^([a-z][a-z0-9+\-.]*):"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -203,17 +181,22 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
onPluginError = () =>
|
||||
{
|
||||
string errorMsgString = string.Format(CultureInfo.CurrentCulture, Properties.Resources.plugin_search_failed, BrowserInfo.Name ?? BrowserInfo.MSEdgeName);
|
||||
|
||||
Log.Error(errorMsgString, this.GetType());
|
||||
_context.API.ShowMsg(
|
||||
$"Plugin: {Properties.Resources.plugin_name}",
|
||||
errorMsgString);
|
||||
};
|
||||
}
|
||||
|
||||
public string GetTranslatedPluginTitle()
|
||||
@@ -229,185 +212,41 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
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
|
||||
{
|
||||
string progId = GetRegistryValue(
|
||||
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
|
||||
"ProgId");
|
||||
|
||||
// The `?` argument doesn't work on opera, so we get the user's default search engine:
|
||||
if (progId.StartsWith("Opera", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Opera user preferences file:
|
||||
string prefFile;
|
||||
|
||||
if (progId.Contains("GX", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
_browserName = "Opera GX";
|
||||
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera GX Stable\\Preferences");
|
||||
}
|
||||
else
|
||||
{
|
||||
_browserName = "Opera";
|
||||
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera Stable\\Preferences");
|
||||
}
|
||||
|
||||
// "default_search_provider_data" doesn't exist if the user hasn't searched for the first time,
|
||||
// therefore we set `url` to opera's default search engine:
|
||||
string url = "https://www.google.com/search?client=opera&q={0}&sourceid=opera";
|
||||
|
||||
using (System.Text.Json.JsonDocument doc = System.Text.Json.JsonDocument.Parse(prefFile))
|
||||
{
|
||||
if (doc.RootElement.TryGetProperty("default_search_provider_data", out var element))
|
||||
{
|
||||
if (element.TryGetProperty("template_url_data", out element))
|
||||
{
|
||||
if (element.TryGetProperty("url", out element))
|
||||
{
|
||||
url = element.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
url = url
|
||||
.Replace("{searchTerms}", "{0}", StringComparison.Ordinal)
|
||||
.Replace("{inputEncoding}", "UTF-8", StringComparison.Ordinal)
|
||||
.Replace("{outputEncoding}", "UTF-8", StringComparison.Ordinal);
|
||||
|
||||
int startIndex = url.IndexOf('}', StringComparison.Ordinal) + 1;
|
||||
|
||||
// In case there are other url parameters (e.g. `&foo={bar}`), remove them:
|
||||
for (int i = url.IndexOf("}", startIndex, StringComparison.Ordinal);
|
||||
i != -1;
|
||||
i = url.IndexOf("}", startIndex, StringComparison.Ordinal))
|
||||
{
|
||||
for (int j = i - 1; j > 0; --j)
|
||||
{
|
||||
if (url[j] == '&')
|
||||
{
|
||||
url = url.Remove(j, i - j + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_searchEngineUrl = url;
|
||||
}
|
||||
else
|
||||
{
|
||||
string appName = GetRegistryValue($"HKEY_CLASSES_ROOT\\{progId}\\Application", "ApplicationName")
|
||||
?? GetRegistryValue($"HKEY_CLASSES_ROOT\\{progId}", "FriendlyTypeName");
|
||||
|
||||
if (appName is null)
|
||||
{
|
||||
appName = Properties.Resources.plugin_browser;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle indirect strings:
|
||||
if (appName.StartsWith("@", StringComparison.Ordinal))
|
||||
{
|
||||
appName = GetIndirectString(appName);
|
||||
}
|
||||
|
||||
appName = appName
|
||||
.Replace("URL", null, StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("HTML", null, StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("Document", null, StringComparison.OrdinalIgnoreCase)
|
||||
.TrimEnd();
|
||||
}
|
||||
|
||||
_browserName = appName;
|
||||
|
||||
_searchEngineUrl = null;
|
||||
}
|
||||
|
||||
var programLocation =
|
||||
|
||||
// Resolve App Icon (UWP)
|
||||
GetRegistryValue(
|
||||
"HKEY_CLASSES_ROOT\\" + progId + "\\Application",
|
||||
"ApplicationIcon")
|
||||
|
||||
// Resolves default file association icon (UWP + Normal)
|
||||
?? 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))
|
||||
{
|
||||
// Check if there's a postfix with contract-white/contrast-black icon is available and use that instead
|
||||
string directProgramLocation = GetIndirectString(programLocation);
|
||||
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());
|
||||
}
|
||||
|
||||
string GetRegistryValue(string registryLocation, string valueName)
|
||||
{
|
||||
return Microsoft.Win32.Registry.GetValue(registryLocation, valueName, null) as string;
|
||||
}
|
||||
|
||||
string GetIndirectString(string str)
|
||||
{
|
||||
var stringBuilder = new StringBuilder(128);
|
||||
if (NativeMethods.SHLoadIndirectString(
|
||||
str,
|
||||
stringBuilder,
|
||||
(uint)stringBuilder.Capacity,
|
||||
IntPtr.Zero)
|
||||
== NativeMethods.Hresult.Ok)
|
||||
{
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
throw new Exception("Could not load indirect string.");
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateIconPath(Theme theme)
|
||||
{
|
||||
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
|
||||
{
|
||||
_defaultIconPath = "Images/WebSearch.light.png";
|
||||
_iconPath = "Images/WebSearch.light.png";
|
||||
}
|
||||
else
|
||||
{
|
||||
_defaultIconPath = "Images/WebSearch.dark.png";
|
||||
_iconPath = "Images/WebSearch.dark.png";
|
||||
}
|
||||
}
|
||||
|
||||
public Control CreateSettingPanel()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void UpdateSettings(PowerLauncherPluginSettings settings)
|
||||
{
|
||||
_notGlobalIfUri = settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == NotGlobalIfUri)?.Value ?? false;
|
||||
}
|
||||
|
||||
public void ReloadData()
|
||||
{
|
||||
if (_context is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateIconPath(_context.API.GetCurrentTheme());
|
||||
BrowserInfo.UpdateIfTimePassed();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
@@ -426,15 +265,5 @@ namespace Community.PowerToys.Run.Plugin.WebSearch
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
public Control CreateSettingPanel()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void UpdateSettings(PowerLauncherPluginSettings settings)
|
||||
{
|
||||
_notGlobalIfUri = settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == NotGlobalIfUri)?.Value ?? false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 Community.PowerToys.Run.Plugin.WebSearch
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@ using Microsoft.Plugin.Program.Win32;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Image;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.Common;
|
||||
using Wox.Plugin.Logger;
|
||||
using Wox.Plugin.SharedCommands;
|
||||
using static Microsoft.Plugin.Program.Programs.UWP;
|
||||
|
||||
namespace Microsoft.Plugin.Program.Programs
|
||||
|
||||
@@ -15,8 +15,8 @@ using System.Windows.Input;
|
||||
using ManagedCommon;
|
||||
using Wox.Infrastructure.Storage;
|
||||
using Wox.Plugin;
|
||||
using Wox.Plugin.Common;
|
||||
using Wox.Plugin.Logger;
|
||||
using Wox.Plugin.SharedCommands;
|
||||
using Control = System.Windows.Controls.Control;
|
||||
|
||||
namespace Microsoft.Plugin.Shell
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -81,9 +81,11 @@ namespace Wox.Plugin
|
||||
Metadata.ActionKeyword = setting.ActionKeyword;
|
||||
Metadata.IsGlobal = setting.IsGlobal;
|
||||
|
||||
if (Plugin is ISettingProvider)
|
||||
(Plugin as ISettingProvider)?.UpdateSettings(setting);
|
||||
|
||||
if (IsPluginInitialized && !Metadata.Disabled)
|
||||
{
|
||||
(Plugin as ISettingProvider).UpdateSettings(setting);
|
||||
(Plugin as IReloadable)?.ReloadData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
// 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.IO.Abstractions;
|
||||
using System.Text;
|
||||
using Wox.Plugin.Common.Win32;
|
||||
using Wox.Plugin.Logger;
|
||||
|
||||
namespace Wox.Plugin.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains information (e.g. path to executable, name...) about the default browser.
|
||||
/// </summary>
|
||||
public static class DefaultBrowserInfo
|
||||
{
|
||||
private static readonly object _updateLock = new object();
|
||||
private static int _lastUpdateTickCount = -1;
|
||||
|
||||
public static readonly string MSEdgePath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\Microsoft\Edge\Application\msedge.exe";
|
||||
public const string MSEdgeName = "Microsoft Edge";
|
||||
|
||||
private static readonly IPath FilePath = new FileSystem().Path;
|
||||
|
||||
/// <summary>Gets the path to default browser's executable.</summary>
|
||||
public static string Path { get; private set; }
|
||||
|
||||
/// <summary>Gets <see cref="Path" /> since the icon is embedded in the executable.</summary>
|
||||
public static string IconPath { get => Path; }
|
||||
|
||||
public static string Name { get; private set; }
|
||||
|
||||
/// <summary>Gets a value indicating whether the browser supports querying a web search via command line argument (`? <term>`).</summary>
|
||||
public static bool SupportsWebSearchByCmdLineArgument { get => SearchEngineUrl is null; }
|
||||
|
||||
public static bool IsDefaultBrowserSet { get => !string.IsNullOrEmpty(Path); }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the browser's default search engine's url ready to be formatted with a single value (like `https://www.bing.com/search?q={0}`)
|
||||
/// </summary>
|
||||
#pragma warning disable CA1056 // URI-like properties should not be strings
|
||||
public static string SearchEngineUrl { get; private set; }
|
||||
#pragma warning restore CA1056 // URI-like properties should not be strings
|
||||
|
||||
/// <summary>
|
||||
/// Updates only if at least more than 300ms has passed since the last update, to avoid multiple calls to <see cref="Update"/>.
|
||||
/// (because of multiple plugins calling update at the same time.)
|
||||
/// </summary>
|
||||
/// <param name="defaultToEdgeOnFail">If true, If <see cref="Update"/> fails, for any reason, the browser will be set to Microsoft Edge.</param>
|
||||
public static void UpdateIfTimePassed()
|
||||
{
|
||||
int curTickCount = Environment.TickCount;
|
||||
if (curTickCount - _lastUpdateTickCount > 300)
|
||||
{
|
||||
_lastUpdateTickCount = curTickCount;
|
||||
Update();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Consider using <see cref="UpdateIfTimePassed"/> to avoid updating multiple times.
|
||||
/// (because of multiple plugins calling update at the same time.)
|
||||
/// </summary>
|
||||
[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")]
|
||||
public static void Update()
|
||||
{
|
||||
lock (_updateLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
string progId = GetRegistryValue(
|
||||
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice",
|
||||
"ProgId");
|
||||
|
||||
// The `?` argument doesn't work on opera, so we get the user's default search engine:
|
||||
if (progId.StartsWith("Opera", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Opera user preferences file:
|
||||
string prefFile;
|
||||
|
||||
if (progId.Contains("GX", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
Name = "Opera GX";
|
||||
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera GX Stable\\Preferences");
|
||||
}
|
||||
else
|
||||
{
|
||||
Name = "Opera";
|
||||
prefFile = System.IO.File.ReadAllText($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\Opera Software\\Opera Stable\\Preferences");
|
||||
}
|
||||
|
||||
// "default_search_provider_data" doesn't exist if the user hasn't searched for the first time,
|
||||
// therefore we set `url` to opera's default search engine:
|
||||
string url = "https://www.google.com/search?client=opera&q={0}&sourceid=opera";
|
||||
|
||||
using (System.Text.Json.JsonDocument doc = System.Text.Json.JsonDocument.Parse(prefFile))
|
||||
{
|
||||
if (doc.RootElement.TryGetProperty("default_search_provider_data", out var element))
|
||||
{
|
||||
if (element.TryGetProperty("template_url_data", out element))
|
||||
{
|
||||
if (element.TryGetProperty("url", out element))
|
||||
{
|
||||
url = element.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
url = url
|
||||
.Replace("{searchTerms}", "{0}", StringComparison.Ordinal)
|
||||
.Replace("{inputEncoding}", "UTF-8", StringComparison.Ordinal)
|
||||
.Replace("{outputEncoding}", "UTF-8", StringComparison.Ordinal);
|
||||
|
||||
int startIndex = url.IndexOf('}', StringComparison.Ordinal) + 1;
|
||||
|
||||
// In case there are other url parameters (e.g. `&foo={bar}`), remove them:
|
||||
for (int i = url.IndexOf("}", startIndex, StringComparison.Ordinal);
|
||||
i != -1;
|
||||
i = url.IndexOf("}", startIndex, StringComparison.Ordinal))
|
||||
{
|
||||
for (int j = i - 1; j > 0; --j)
|
||||
{
|
||||
if (url[j] == '&')
|
||||
{
|
||||
url = url.Remove(j, i - j + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SearchEngineUrl = url;
|
||||
}
|
||||
else
|
||||
{
|
||||
string appName = GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}\Application", "ApplicationName")
|
||||
?? GetRegistryValue($@"HKEY_CLASSES_ROOT\{progId}", "FriendlyTypeName");
|
||||
|
||||
if (appName != null)
|
||||
{
|
||||
// Handle indirect strings:
|
||||
if (appName.StartsWith("@", StringComparison.Ordinal))
|
||||
{
|
||||
appName = GetIndirectString(appName);
|
||||
}
|
||||
|
||||
appName = appName
|
||||
.Replace("URL", null, StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("HTML", null, StringComparison.OrdinalIgnoreCase)
|
||||
.Replace("Document", null, StringComparison.OrdinalIgnoreCase)
|
||||
.TrimEnd();
|
||||
}
|
||||
|
||||
Name = appName;
|
||||
|
||||
SearchEngineUrl = null;
|
||||
}
|
||||
|
||||
string programLocation =
|
||||
|
||||
// Resolve App Icon (UWP)
|
||||
GetRegistryValue(
|
||||
$@"HKEY_CLASSES_ROOT\{progId}\Application",
|
||||
"ApplicationIcon")
|
||||
|
||||
// Resolves default file association icon (UWP + Normal)
|
||||
?? 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))
|
||||
{
|
||||
string directProgramLocation = GetIndirectString(programLocation);
|
||||
Path = string.Equals(FilePath.GetExtension(directProgramLocation), ".exe", StringComparison.Ordinal)
|
||||
? directProgramLocation
|
||||
: null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Using Ordinal since this is internal and used with a symbol
|
||||
var indexOfComma = programLocation.IndexOf(',', StringComparison.Ordinal);
|
||||
Path = indexOfComma > 0
|
||||
? programLocation.Substring(0, indexOfComma)
|
||||
: programLocation;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(Path))
|
||||
{
|
||||
throw new Exception("Browser path is null or empty.");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Path = null;
|
||||
Name = null;
|
||||
SearchEngineUrl = null;
|
||||
|
||||
Log.Exception("Exception when retrieving browser path/name. Path and Name are null.", e, typeof(DefaultBrowserInfo));
|
||||
}
|
||||
|
||||
string GetRegistryValue(string registryLocation, string valueName)
|
||||
{
|
||||
return Microsoft.Win32.Registry.GetValue(registryLocation, valueName, null) as string;
|
||||
}
|
||||
|
||||
string GetIndirectString(string str)
|
||||
{
|
||||
var stringBuilder = new StringBuilder(128);
|
||||
if (NativeMethods.SHLoadIndirectString(
|
||||
str,
|
||||
stringBuilder,
|
||||
(uint)stringBuilder.Capacity,
|
||||
IntPtr.Zero)
|
||||
== HRESULT.S_OK)
|
||||
{
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
throw new Exception("Could not load indirect string.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +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;
|
||||
using static Wox.Plugin.SharedCommands.ShellCommand;
|
||||
|
||||
namespace Wox.Plugin.SharedCommands
|
||||
{
|
||||
internal static class NativeMethods
|
||||
{
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool EnumThreadWindows(uint threadId, EnumThreadDelegate lpfn, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern int GetWindowTextLength(IntPtr hwnd);
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,9 @@ using System;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Wox.Plugin.Common.Win32;
|
||||
|
||||
namespace Wox.Plugin.SharedCommands
|
||||
namespace Wox.Plugin.Common
|
||||
{
|
||||
public static class ShellCommand
|
||||
{
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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;
|
||||
using SuppressMessageAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute;
|
||||
|
||||
namespace Wox.Plugin.Common.Win32
|
||||
{
|
||||
[SuppressMessage("Interoperability", "CA1401:P/Invokes should not be visible", Justification = "We want plugins to share this NativeMethods class, instead of each one creating its own.")]
|
||||
public static class NativeMethods
|
||||
{
|
||||
[DllImport("user32.dll")]
|
||||
public static extern bool EnumThreadWindows(uint threadId, ShellCommand.EnumThreadDelegate lpfn, IntPtr lParam);
|
||||
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
||||
public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern int GetWindowTextLength(IntPtr hwnd);
|
||||
|
||||
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
|
||||
public static extern HRESULT SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, uint cchOutBuf, IntPtr ppvReserved);
|
||||
}
|
||||
|
||||
[SuppressMessage("Design", "CA1028:Enum Storage should be Int32", Justification = "These values are used by win32 api")]
|
||||
[SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "These are the names win32 api uses.")]
|
||||
public enum HRESULT : uint
|
||||
{
|
||||
/// <summary>
|
||||
/// Operation successful.
|
||||
/// </summary>
|
||||
S_OK = 0x00000000,
|
||||
|
||||
/// <summary>
|
||||
/// Operation successful. (negative condition/no operation)
|
||||
/// </summary>
|
||||
S_FALSE = 0x00000001,
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented.
|
||||
/// </summary>
|
||||
E_NOTIMPL = 0x80004001,
|
||||
|
||||
/// <summary>
|
||||
/// No such interface supported.
|
||||
/// </summary>
|
||||
E_NOINTERFACE = 0x80004002,
|
||||
|
||||
/// <summary>
|
||||
/// Pointer that is not valid.
|
||||
/// </summary>
|
||||
E_POINTER = 0x80004003,
|
||||
|
||||
/// <summary>
|
||||
/// Operation aborted.
|
||||
/// </summary>
|
||||
E_ABORT = 0x80004004,
|
||||
|
||||
/// <summary>
|
||||
/// Unspecified failure.
|
||||
/// </summary>
|
||||
E_FAIL = 0x80004005,
|
||||
|
||||
/// <summary>
|
||||
/// Unexpected failure.
|
||||
/// </summary>
|
||||
E_UNEXPECTED = 0x8000FFFF,
|
||||
|
||||
/// <summary>
|
||||
/// General access denied error.
|
||||
/// </summary>
|
||||
E_ACCESSDENIED = 0x80070005,
|
||||
|
||||
/// <summary>
|
||||
/// Handle that is not valid.
|
||||
/// </summary>
|
||||
E_HANDLE = 0x80070006,
|
||||
|
||||
/// <summary>
|
||||
/// Failed to allocate necessary memory.
|
||||
/// </summary>
|
||||
E_OUTOFMEMORY = 0x8007000E,
|
||||
|
||||
/// <summary>
|
||||
/// One or more arguments are not valid.
|
||||
/// </summary>
|
||||
E_INVALIDARG = 0x80070057,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user