|
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using JetBrains.Annotations; |
| 5 | +using Tmds.DBus.SourceGenerator; |
| 6 | + |
| 7 | +namespace LinuxDesktopUtils.XDGDesktopPortal; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Network monitoring portal. |
| 11 | +/// |
| 12 | +/// The NetworkMonitor portal provides network status information to sandboxed applications. |
| 13 | +/// </summary> |
| 14 | +/// <remarks> |
| 15 | +/// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.NetworkMonitor.html. |
| 16 | +/// </remarks> |
| 17 | +[PublicAPI] |
| 18 | +public partial class NetworkMonitorPortal : IPortal |
| 19 | +{ |
| 20 | + private readonly DesktopPortalConnectionManager _connectionManager; |
| 21 | + private readonly OrgFreedesktopPortalNetworkMonitorProxy _instance; |
| 22 | + private readonly uint _version; |
| 23 | + |
| 24 | + /// <inheritdoc/> |
| 25 | + uint IPortal.Version => _version; |
| 26 | + |
| 27 | + private NetworkMonitorPortal( |
| 28 | + DesktopPortalConnectionManager connectionManager, |
| 29 | + OrgFreedesktopPortalNetworkMonitorProxy instance, |
| 30 | + uint version) |
| 31 | + { |
| 32 | + _connectionManager = connectionManager; |
| 33 | + _instance = instance; |
| 34 | + _version = version; |
| 35 | + } |
| 36 | + |
| 37 | + internal static async ValueTask<NetworkMonitorPortal> CreateAsync(DesktopPortalConnectionManager connectionManager) |
| 38 | + { |
| 39 | + var instance = new OrgFreedesktopPortalNetworkMonitorProxy(connectionManager.GetConnection(), destination: DBusHelper.BusName, path: DBusHelper.ObjectPath); |
| 40 | + var version = await instance.GetVersionPropertyAsync().ConfigureAwait(false); |
| 41 | + |
| 42 | + return new NetworkMonitorPortal(connectionManager, instance, version); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Returns whether the network is considered available. That is, whether the system as a default route for at least one of IPv4 or IPv6. |
| 47 | + /// </summary> |
| 48 | + /// <exception cref="PortalVersionException">Thrown if the installed portal backend doesn't support this method.</exception> |
| 49 | + public async Task<bool> GetAvailableAsync() |
| 50 | + { |
| 51 | + const uint addedInVersion = 2; |
| 52 | + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); |
| 53 | + |
| 54 | + var result = await _instance.GetAvailableAsync().ConfigureAwait(false); |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Returns whether the network is considered metered. That is, whether the system as traffic flowing through the default connection that is subject ot limitations by service providers. |
| 60 | + /// </summary> |
| 61 | + /// <exception cref="PortalVersionException">Thrown if the installed portal backend doesn't support this method.</exception> |
| 62 | + public async Task<bool> GetMeteredAsync() |
| 63 | + { |
| 64 | + const uint addedInVersion = 2; |
| 65 | + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); |
| 66 | + |
| 67 | + var result = await _instance.GetMeteredAsync().ConfigureAwait(false); |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Returns more detailed information about the host’s network connectivity. |
| 73 | + /// </summary> |
| 74 | + /// <exception cref="PortalVersionException">Thrown if the installed portal backend doesn't support this method.</exception> |
| 75 | + /// <exception cref="NotSupportedException">Thrown if the portal returned an unknown connectivity status.</exception> |
| 76 | + public async Task<ConnectivityStatus> GetConnectivityAsync() |
| 77 | + { |
| 78 | + const uint addedInVersion = 2; |
| 79 | + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); |
| 80 | + |
| 81 | + var result = await _instance.GetConnectivityAsync().ConfigureAwait(false); |
| 82 | + if (result is < (uint)ConnectivityStatus.LocalOnly or > (uint)ConnectivityStatus.Full) |
| 83 | + throw new NotSupportedException($"Portal returned invalid connectivity status: `{result}`"); |
| 84 | + |
| 85 | + return (ConnectivityStatus)result; |
| 86 | + } |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Returns values from <see cref="GetAvailableAsync"/>, <see cref="GetMeteredAsync"/>, and <see cref="GetConnectivityAsync"/> |
| 90 | + /// in one call. |
| 91 | + /// </summary> |
| 92 | + /// <exception cref="PortalVersionException">Thrown if the installed portal backend doesn't support this method.</exception> |
| 93 | + /// <exception cref="NotSupportedException">Thrown if the portal returned an unknown connectivity status.</exception> |
| 94 | + public async Task<GetStatusResults> GetStatusAsync() |
| 95 | + { |
| 96 | + const uint addedInVersion = 3; |
| 97 | + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); |
| 98 | + |
| 99 | + var values = await _instance.GetStatusAsync().ConfigureAwait(false); |
| 100 | + var res = GetStatusResults.From(values); |
| 101 | + return res; |
| 102 | + } |
| 103 | + |
| 104 | + /// <inheritdoc cref="CanReachAsync(System.String, System.UInt32)"/> |
| 105 | + public Task<bool> CanReachAsync(Uri uri) => CanReachAsync(uri.Host, (uint)uri.Port); |
| 106 | + |
| 107 | + /// <inheritdoc cref="CanReachAsync(System.String, System.UInt32)"/> |
| 108 | + public Task<bool> CanReachAsync(IPAddress address, uint port) => CanReachAsync(address.ToString(), port); |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Returns whether the given hostname is believed to be reachable. |
| 112 | + /// </summary> |
| 113 | + /// <exception cref="PortalVersionException">Thrown if the installed portal backend doesn't support this method.</exception> |
| 114 | + public async Task<bool> CanReachAsync(string hostname, uint port) |
| 115 | + { |
| 116 | + const uint addedInVersion = 3; |
| 117 | + PortalVersionException.ThrowIf(requiredVersion: addedInVersion, availableVersion: _version); |
| 118 | + |
| 119 | + var res = await _instance.CanReachAsync(hostname, port).ConfigureAwait(false); |
| 120 | + return res; |
| 121 | + } |
| 122 | +} |
0 commit comments