[PT Run] Support for application URI (#14391)

* [RUN] Add support for uri with scheme only

* Fix typo

* Add full support for application URI

* Apply suggestions from code review and add tests

* [PT Run] Add support for application uri

* Update error message

* Adapt the icon if the result is web URI

* Update icons for application URI

* Update icons for application URI (dark mode)

* Update icon
This commit is contained in:
Franky Chen
2021-11-17 06:06:45 +08:00
committed by GitHub
parent 2128b88571
commit c934127d84
8 changed files with 140 additions and 71 deletions

View File

@@ -11,62 +11,85 @@ namespace Microsoft.Plugin.Uri.UnitTests.UriHelper
public class ExtendedUriParserTests public class ExtendedUriParserTests
{ {
[DataTestMethod] [DataTestMethod]
[DataRow("google.com", true, "https://google.com/")] [DataRow("google.com", true, "https://google.com/", true)]
[DataRow("http://google.com", true, "http://google.com/")] [DataRow("http://google.com", true, "http://google.com/", true)]
[DataRow("localhost", true, "https://localhost/")] [DataRow("localhost", true, "https://localhost/", true)]
[DataRow("http://localhost", true, "http://localhost/")] [DataRow("http://localhost", true, "http://localhost/", true)]
[DataRow("127.0.0.1", true, "https://127.0.0.1/")] [DataRow("127.0.0.1", true, "https://127.0.0.1/", true)]
[DataRow("http://127.0.0.1", true, "http://127.0.0.1/")] [DataRow("http://127.0.0.1", true, "http://127.0.0.1/", true)]
[DataRow("http://127.0.0.1:80", true, "http://127.0.0.1/")] [DataRow("http://127.0.0.1:80", true, "http://127.0.0.1/", true)]
[DataRow("127", false, null)] [DataRow("127", false, null, false)]
[DataRow("", false, null)] [DataRow("", false, null, false)]
[DataRow("https://google.com", true, "https://google.com/")] [DataRow("https://google.com", true, "https://google.com/", true)]
[DataRow("ftps://google.com", true, "ftps://google.com/")] [DataRow("ftps://google.com", true, "ftps://google.com/", false)]
[DataRow(null, false, null)] [DataRow(null, false, null, false)]
[DataRow("bing.com/search?q=gmx", true, "https://bing.com/search?q=gmx")] [DataRow("bing.com/search?q=gmx", true, "https://bing.com/search?q=gmx", true)]
[DataRow("http://bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx")] [DataRow("http://bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx", true)]
[DataRow("h", true, "https://h/")] [DataRow("h", true, "https://h/", true)]
[DataRow("http://h", true, "http://h/")] [DataRow("http://h", true, "http://h/", true)]
[DataRow("ht", true, "https://ht/")] [DataRow("ht", true, "https://ht/", true)]
[DataRow("http://ht", true, "http://ht/")] [DataRow("http://ht", true, "http://ht/", true)]
[DataRow("htt", true, "https://htt/")] [DataRow("htt", true, "https://htt/", true)]
[DataRow("http://htt", true, "http://htt/")] [DataRow("http://htt", true, "http://htt/", true)]
[DataRow("http", true, "https://http/")] [DataRow("http", true, "https://http/", true)]
[DataRow("http://http", true, "http://http/")] [DataRow("http://http", true, "http://http/", true)]
[DataRow("http:", false, null)] [DataRow("http:", false, null, false)]
[DataRow("http:/", false, null)] [DataRow("http:/", false, null, false)]
[DataRow("http://", false, null)] [DataRow("http://", false, null, false)]
[DataRow("http://t", true, "http://t/")] [DataRow("http://t", true, "http://t/", true)]
[DataRow("http://te", true, "http://te/")] [DataRow("http://te", true, "http://te/", true)]
[DataRow("http://tes", true, "http://tes/")] [DataRow("http://tes", true, "http://tes/", true)]
[DataRow("http://test", true, "http://test/")] [DataRow("http://test", true, "http://test/", true)]
[DataRow("http://test.", false, null)] [DataRow("http://test.", false, null, false)]
[DataRow("http://test.c", true, "http://test.c/")] [DataRow("http://test.c", true, "http://test.c/", true)]
[DataRow("http://test.co", true, "http://test.co/")] [DataRow("http://test.co", true, "http://test.co/", true)]
[DataRow("http://test.com", true, "http://test.com/")] [DataRow("http://test.com", true, "http://test.com/", true)]
[DataRow("http:3", true, "https://http:3/")] [DataRow("http:3", true, "https://http:3/", true)]
[DataRow("http://http:3", true, "http://http:3/")] [DataRow("http://http:3", true, "http://http:3/", true)]
[DataRow("[::]", true, "https://[::]/")] [DataRow("[::]", true, "https://[::]/", true)]
[DataRow("http://[::]", true, "http://[::]/")] [DataRow("http://[::]", true, "http://[::]/", true)]
[DataRow("[2001:0DB8::1]", true, "https://[2001:db8::1]/")] [DataRow("[2001:0DB8::1]", true, "https://[2001:db8::1]/", true)]
[DataRow("http://[2001:0DB8::1]", true, "http://[2001:db8::1]/")] [DataRow("http://[2001:0DB8::1]", true, "http://[2001:db8::1]/", true)]
[DataRow("[2001:0DB8::1]:80", true, "https://[2001:db8::1]/")] [DataRow("[2001:0DB8::1]:80", true, "https://[2001:db8::1]/", true)]
[DataRow("http://[2001:0DB8::1]:80", true, "http://[2001:db8::1]/")] [DataRow("http://[2001:0DB8::1]:80", true, "http://[2001:db8::1]/", true)]
[DataRow("mailto:example@mail.com", true, "mailto:example@mail.com")] [DataRow("mailto:example@mail.com", true, "mailto:example@mail.com", false)]
[DataRow("tel:411", true, "tel:411")] [DataRow("tel:411", true, "tel:411", false)]
[DataRow("ftp://example.com", true, "ftp://example.com/")] [DataRow("ftp://example.com", true, "ftp://example.com/", false)]
[DataRow("example.com:443", true, "example.com:443")]
public void TryParseCanParseHostName(string query, bool expectedSuccess, string expectedResult) // This has been parsed as an application URI. Linked issue: #14260
[DataRow("example.com:443", true, "example.com:443", false)]
[DataRow("mailto:", true, "mailto:", false)]
[DataRow("mailto:/", false, null, false)]
[DataRow("ms-settings:", true, "ms-settings:", false)]
[DataRow("ms-settings:/", false, null, false)]
[DataRow("ms-settings://", false, null, false)]
[DataRow("ms-settings://privacy", true, "ms-settings://privacy/", false)]
[DataRow("ms-settings://privacy/", true, "ms-settings://privacy/", false)]
[DataRow("ms-settings:privacy", true, "ms-settings:privacy", false)]
[DataRow("ms-settings:powersleep", true, "ms-settings:powersleep", false)]
[DataRow("microsoft-edge:http://google.com", true, "microsoft-edge:http://google.com", false)]
[DataRow("microsoft-edge:https://google.com", true, "microsoft-edge:https://google.com", false)]
[DataRow("microsoft-edge:google.com", true, "microsoft-edge:google.com", false)]
[DataRow("microsoft-edge:google.com/", true, "microsoft-edge:google.com/", false)]
[DataRow("microsoft-edge:https://google.com/", true, "microsoft-edge:https://google.com/", false)]
[DataRow("ftp://user:password@localhost:8080", true, "ftp://user:password@localhost:8080/", false)]
[DataRow("ftp://user:password@localhost:8080/", true, "ftp://user:password@localhost:8080/", false)]
[DataRow("ftp://user:password@google.com", true, "ftp://user:password@google.com/", false)]
[DataRow("ftp://user:password@google.com:2121", true, "ftp://user:password@google.com:2121/", false)]
[DataRow("ftp://user:password@1.1.1.1", true, "ftp://user:password@1.1.1.1/", false)]
[DataRow("ftp://user:password@1.1.1.1:2121", true, "ftp://user:password@1.1.1.1:2121/", false)]
public void TryParseCanParseHostName(string query, bool expectedSuccess, string expectedResult, bool expectedIsWebUri)
{ {
// Arrange // Arrange
var parser = new ExtendedUriParser(); var parser = new ExtendedUriParser();
// Act // Act
var success = parser.TryParse(query, out var result); var success = parser.TryParse(query, out var result, out var isWebUriResult);
// Assert // Assert
Assert.AreEqual(expectedResult, result?.ToString()); Assert.AreEqual(expectedResult, result?.ToString());
Assert.AreEqual(expectedIsWebUri, isWebUriResult);
Assert.AreEqual(expectedSuccess, success); Assert.AreEqual(expectedSuccess, success);
} }
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

View File

@@ -6,6 +6,6 @@ namespace Microsoft.Plugin.Uri.Interfaces
{ {
public interface IUriParser public interface IUriParser
{ {
bool TryParse(string input, out System.Uri result); bool TryParse(string input, out System.Uri result, out bool isWebUri);
} }
} }

View File

@@ -63,17 +63,15 @@ namespace Microsoft.Plugin.Uri
{ {
results.Add(new Result results.Add(new Result
{ {
Title = Properties.Resources.Microsoft_plugin_uri_default_browser, Title = Properties.Resources.Microsoft_plugin_uri_open,
SubTitle = BrowserPath, SubTitle = BrowserPath,
IcoPath = _uriSettings.ShowBrowserIcon IcoPath = DefaultIconPath,
? BrowserIconPath
: DefaultIconPath,
Action = action => Action = action =>
{ {
if (!Helper.OpenInShell(BrowserPath)) if (!Helper.OpenInShell(BrowserPath))
{ {
var title = $"Plugin: {Properties.Resources.Microsoft_plugin_uri_plugin_name}"; var title = $"Plugin: {Properties.Resources.Microsoft_plugin_uri_plugin_name}";
var message = $"{Properties.Resources.Microsoft_plugin_default_browser_open_failed}: "; var message = $"{Properties.Resources.Microsoft_plugin_uri_open_failed}: ";
Context.API.ShowMsg(title, message); Context.API.ShowMsg(title, message);
return false; return false;
} }
@@ -85,16 +83,19 @@ namespace Microsoft.Plugin.Uri
} }
if (!string.IsNullOrEmpty(query?.Search) if (!string.IsNullOrEmpty(query?.Search)
&& _uriParser.TryParse(query.Search, out var uriResult) && _uriParser.TryParse(query.Search, out var uriResult, out var isWebUri)
&& _uriResolver.IsValidHost(uriResult)) && _uriResolver.IsValidHost(uriResult))
{ {
var uriResultString = uriResult.ToString(); var uriResultString = uriResult.ToString();
var isWebUriBool = isWebUri;
results.Add(new Result results.Add(new Result
{ {
Title = uriResultString, Title = uriResultString,
SubTitle = Properties.Resources.Microsoft_plugin_uri_website, SubTitle = isWebUriBool
IcoPath = _uriSettings.ShowBrowserIcon ? Properties.Resources.Microsoft_plugin_uri_website
: Properties.Resources.Microsoft_plugin_uri_open,
IcoPath = isWebUriBool
? BrowserIconPath ? BrowserIconPath
: DefaultIconPath, : DefaultIconPath,
Action = action => Action = action =>
@@ -118,7 +119,7 @@ namespace Microsoft.Plugin.Uri
private static bool IsActivationKeyword(Query query) private static bool IsActivationKeyword(Query query)
{ {
return !string.IsNullOrEmpty(query?.ActionKeyword) return !string.IsNullOrEmpty(query?.ActionKeyword)
&& query?.ActionKeyword == query?.RawQuery; && query?.ActionKeyword == query?.RawQuery;
} }
private bool IsDefaultBrowserSet() private bool IsDefaultBrowserSet()
@@ -155,16 +156,23 @@ namespace Microsoft.Plugin.Uri
UpdateBrowserIconPath(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")] [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) private void UpdateBrowserIconPath(Theme newTheme)
{ {
try try
{ {
var progId = _registryWrapper.GetRegistryValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice", "ProgId"); var progId = _registryWrapper.GetRegistryValue(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice",
"ProgId");
var programLocation = var programLocation =
// Resolve App Icon (UWP) // Resolve App Icon (UWP)
_registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\Application", "ApplicationIcon") _registryWrapper.GetRegistryValue(
"HKEY_CLASSES_ROOT\\" + progId + "\\Application",
"ApplicationIcon")
// Resolves default file association icon (UWP + Normal) // Resolves default file association icon (UWP + Normal)
?? _registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\DefaultIcon", null); ?? _registryWrapper.GetRegistryValue("HKEY_CLASSES_ROOT\\" + progId + "\\DefaultIcon", null);
@@ -174,14 +182,21 @@ namespace Microsoft.Plugin.Uri
if (programLocation.StartsWith("@", StringComparison.Ordinal)) if (programLocation.StartsWith("@", StringComparison.Ordinal))
{ {
var directProgramLocationStringBuilder = new StringBuilder(128); var directProgramLocationStringBuilder = new StringBuilder(128);
if (NativeMethods.SHLoadIndirectString(programLocation, directProgramLocationStringBuilder, (uint)directProgramLocationStringBuilder.Capacity, IntPtr.Zero) == if (NativeMethods.SHLoadIndirectString(
programLocation,
directProgramLocationStringBuilder,
(uint)directProgramLocationStringBuilder.Capacity,
IntPtr.Zero) ==
NativeMethods.Hresult.Ok) NativeMethods.Hresult.Ok)
{ {
// Check if there's a postfix with contract-white/contrast-black icon is available and use that instead // Check if there's a postfix with contract-white/contrast-black icon is available and use that instead
var directProgramLocation = directProgramLocationStringBuilder.ToString(); var directProgramLocation = directProgramLocationStringBuilder.ToString();
var themeIcon = newTheme == Theme.Light || newTheme == Theme.HighContrastWhite ? "contrast-white" : "contrast-black"; var themeIcon = newTheme == Theme.Light || newTheme == Theme.HighContrastWhite
? "contrast-white"
: "contrast-black";
var extension = Path.GetExtension(directProgramLocation); var extension = Path.GetExtension(directProgramLocation);
var themedProgLocation = $"{directProgramLocation.Substring(0, directProgramLocation.Length - extension.Length)}_{themeIcon}{extension}"; var themedProgLocation =
$"{directProgramLocation.Substring(0, directProgramLocation.Length - extension.Length)}_{themeIcon}{extension}";
BrowserIconPath = File.Exists(themedProgLocation) BrowserIconPath = File.Exists(themedProgLocation)
? themedProgLocation ? themedProgLocation
: directProgramLocation; : directProgramLocation;

View File

@@ -70,7 +70,7 @@ namespace Microsoft.Plugin.Uri.Properties {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to Open Default Browser. /// Looks up a localized string similar to Open default browser.
/// </summary> /// </summary>
public static string Microsoft_plugin_uri_default_browser { public static string Microsoft_plugin_uri_default_browser {
get { get {
@@ -79,7 +79,16 @@ namespace Microsoft.Plugin.Uri.Properties {
} }
/// <summary> /// <summary>
/// Looks up a localized string similar to Failed to open URL. /// Looks up a localized string similar to Open URI.
/// </summary>
public static string Microsoft_plugin_uri_open {
get {
return ResourceManager.GetString("Microsoft_plugin_uri_open", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Failed to open URI.
/// </summary> /// </summary>
public static string Microsoft_plugin_uri_open_failed { public static string Microsoft_plugin_uri_open_failed {
get { get {

View File

@@ -123,8 +123,11 @@
<data name="Microsoft_plugin_uri_default_browser" xml:space="preserve"> <data name="Microsoft_plugin_uri_default_browser" xml:space="preserve">
<value>Open default browser</value> <value>Open default browser</value>
</data> </data>
<data name="Microsoft_plugin_uri_open" xml:space="preserve">
<value>Open URI</value>
</data>
<data name="Microsoft_plugin_uri_open_failed" xml:space="preserve"> <data name="Microsoft_plugin_uri_open_failed" xml:space="preserve">
<value>Failed to open URL</value> <value>Failed to open URI</value>
</data> </data>
<data name="Microsoft_plugin_uri_plugin_description" xml:space="preserve"> <data name="Microsoft_plugin_uri_plugin_description" xml:space="preserve">
<value>Opens URLs and UNC network shares.</value> <value>Opens URLs and UNC network shares.</value>
@@ -135,4 +138,4 @@
<data name="Microsoft_plugin_uri_website" xml:space="preserve"> <data name="Microsoft_plugin_uri_website" xml:space="preserve">
<value>Open in default browser</value> <value>Open in default browser</value>
</data> </data>
</root> </root>

View File

@@ -10,22 +10,37 @@ namespace Microsoft.Plugin.Uri.UriHelper
{ {
public class ExtendedUriParser : IUriParser public class ExtendedUriParser : IUriParser
{ {
public bool TryParse(string input, out System.Uri result) public bool TryParse(string input, out System.Uri result, out bool isWebUri)
{ {
if (string.IsNullOrEmpty(input)) if (string.IsNullOrEmpty(input))
{ {
result = default; result = default;
isWebUri = false;
return false; return false;
} }
// Handling URL with only scheme, typically mailto or application uri.
// Do nothing, return the result without urlBuilder
if (input.EndsWith(":", StringComparison.OrdinalIgnoreCase)
&& !input.StartsWith("http", StringComparison.OrdinalIgnoreCase)
&& !input.Contains("/", StringComparison.OrdinalIgnoreCase)
&& !input.All(char.IsDigit))
{
result = new System.Uri(input);
isWebUri = false;
return true;
}
// Handle common cases UriBuilder does not handle // Handle common cases UriBuilder does not handle
// Using CurrentCulture since this is a user typed string // Using CurrentCulture since this is a user typed string
if (input.EndsWith(":", StringComparison.CurrentCulture) if (input.EndsWith(":", StringComparison.CurrentCulture)
|| input.EndsWith(".", StringComparison.CurrentCulture) || input.EndsWith(".", StringComparison.CurrentCulture)
|| input.EndsWith(":/", StringComparison.CurrentCulture) || input.EndsWith(":/", StringComparison.CurrentCulture)
|| input.EndsWith("://", StringComparison.CurrentCulture)
|| input.All(char.IsDigit)) || input.All(char.IsDigit))
{ {
result = default; result = default;
isWebUri = false;
return false; return false;
} }
@@ -35,27 +50,31 @@ namespace Microsoft.Plugin.Uri.UriHelper
var hadDefaultPort = urlBuilder.Uri.IsDefaultPort; var hadDefaultPort = urlBuilder.Uri.IsDefaultPort;
urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port; urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port;
if (input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase)) if (input.StartsWith("HTTP://", StringComparison.OrdinalIgnoreCase))
{ {
urlBuilder.Scheme = System.Uri.UriSchemeHttp; urlBuilder.Scheme = System.Uri.UriSchemeHttp;
isWebUri = true;
} }
else if (input.Contains(":", StringComparison.OrdinalIgnoreCase) && else if (input.Contains(":", StringComparison.OrdinalIgnoreCase) &&
!input.Contains("http", StringComparison.OrdinalIgnoreCase) && !input.StartsWith("http", StringComparison.OrdinalIgnoreCase) &&
!input.Contains("[", StringComparison.OrdinalIgnoreCase)) !input.Contains("[", StringComparison.OrdinalIgnoreCase))
{ {
// Do nothing, leave unchanged // Do nothing, leave unchanged
isWebUri = false;
} }
else else
{ {
urlBuilder.Scheme = System.Uri.UriSchemeHttps; urlBuilder.Scheme = System.Uri.UriSchemeHttps;
isWebUri = true;
} }
result = urlBuilder.Uri; result = urlBuilder.Uri;
return true; return true;
} }
catch (System.UriFormatException) catch (UriFormatException)
{ {
result = default; result = default;
isWebUri = false;
return false; return false;
} }
} }