Skip to content

Commit

Permalink
Merge pull request #5989 from yesseruser/#24696-OpenUrlExternally-exc…
Browse files Browse the repository at this point in the history
…eption-handling

Fix `OpenUrlExternally` potentially crashing applications due to misconfigurations (and log errors instead)
  • Loading branch information
peppy committed Sep 15, 2023
2 parents 6511a59 + dc6ea14 commit 52c58b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
16 changes: 8 additions & 8 deletions osu.Framework.Android/AndroidGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ public override void OpenUrlExternally(string url)
if (!url.CheckIsValidUrl())
throw new ArgumentException("The provided URL must be one of either http://, https:// or mailto: protocols.", nameof(url));

using (var intent = new Intent(Intent.ActionView, Uri.Parse(url)))
try
{
// Recommended way to open URLs on Android 11+
// https://developer.android.com/training/package-visibility/use-cases#open-urls-browser-or-other-app
try
using (var intent = new Intent(Intent.ActionView, Uri.Parse(url)))
{
// Recommended way to open URLs on Android 11+
// https://developer.android.com/training/package-visibility/use-cases#open-urls-browser-or-other-app
gameView.Activity.StartActivity(intent);
}
catch (ActivityNotFoundException e)
{
Logger.Error(e, $"Failed to start intent: {intent}");
}
}
catch (Exception ex)
{
Logger.Error(ex, "Unable to open external link.");
}
}

Expand Down
18 changes: 13 additions & 5 deletions osu.Framework.iOS/IOSGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using osu.Framework.IO.Stores;
using osu.Framework.iOS.Graphics.Textures;
using osu.Framework.iOS.Graphics.Video;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Platform.MacOS;
using UIKit;
Expand Down Expand Up @@ -65,12 +66,19 @@ public override void OpenUrlExternally(string url)
&& !url.StartsWith("itms-beta://", StringComparison.Ordinal))
throw new ArgumentException("The provided URL must be one of either http://, https:// or mailto: protocols.", nameof(url));

UIApplication.SharedApplication.InvokeOnMainThread(() =>
try
{
NSUrl nsurl = NSUrl.FromString(url).AsNonNull();
if (UIApplication.SharedApplication.CanOpenUrl(nsurl))
UIApplication.SharedApplication.OpenUrl(nsurl, new NSDictionary(), null);
});
UIApplication.SharedApplication.InvokeOnMainThread(() =>
{
NSUrl nsurl = NSUrl.FromString(url).AsNonNull();
if (UIApplication.SharedApplication.CanOpenUrl(nsurl))
UIApplication.SharedApplication.OpenUrl(nsurl, new NSDictionary(), null);
});
}
catch (Exception ex)
{
Logger.Error(ex, "Unable to open external link.");
}
}

public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore)
Expand Down
10 changes: 9 additions & 1 deletion osu.Framework/Platform/DesktopGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using osu.Framework.Configuration;
using osu.Framework.Extensions;
using osu.Framework.Logging;

namespace osu.Framework.Platform
{
Expand Down Expand Up @@ -81,7 +82,14 @@ public override void OpenUrlExternally(string url)
if (!url.CheckIsValidUrl())
throw new ArgumentException("The provided URL must be one of either http://, https:// or mailto: protocols.", nameof(url));

openUsingShellExecute(url);
try
{
openUsingShellExecute(url);
}
catch (Exception ex)
{
Logger.Error(ex, "Unable to open external link.");
}
}

public override bool PresentFileExternally(string filename)
Expand Down

0 comments on commit 52c58b5

Please sign in to comment.