Move to GNetworkMonitor (#236)

* Move to GNetworkMonitor

Dino currently talks to NetworkManager over DBus to know the state of
the network.

That doesn't work in a Flatpak sandbox by default though, because
Flatpak filters DBus communications and only allows a very small set of
things to pass (which are known to be safe).

Gio provides an API to know the state of the network (and be notified of
changes via a signal): GNetworkMonitor.

And GNetworkMonitor works both inside a Flatpak sandbox, and in
traditional builds. (in Flatpak it uses what we call a "portal", which
are the clean, safe way to let apps exit their sandbox)

Fixes #235

* Don't check for network connectivity for now

The connectivity check really is the correct thing to do:

* network_available means that the computer has network routes to
  "somewhere". That is, it is connected to a router.
* connectivity.FULL means that the computer can access "the
  Internet". That is, if it is behind a router, that router is
  connected.

As a result, only checking for network_available is not correct.

Unfortunately, NetworkManager tends to wait a long time before checking
for connectivity. As a result, it is possible that a transient network
error leaves NetworkManager thinking that network_available is true but
connectivity!=FULL, and it will wait several minutes before realizing
that the Internet connexion did come back.

During that time, apps checking for connectivity (e.g the whole GNOME
desktop) will think they don't have access to the Internet, while apps
that don't (e.g Firefox) will access the Internet just fine. Users are
understandably confused when that happens.

Removing the check for connectivity is an acceptable trade-off in the
short-term, until this situation is improved on the NetworkManager side.

https://bugzilla.gnome.org/show_bug.cgi?id=792240
This commit is contained in:
Mathieu Bridon 2018-01-09 20:39:45 +01:00 committed by fiaxh
parent 5d6cf9d8d5
commit 5557c03be8
4 changed files with 20 additions and 58 deletions

View file

@ -11,8 +11,6 @@ SOURCES
src/application.vala src/application.vala
src/dbus/login1.vala src/dbus/login1.vala
src/dbus/network_manager.vala
src/dbus/network_manager_dbus_properties.vala
src/dbus/upower.vala src/dbus/upower.vala
src/entity/account.vala src/entity/account.vala

View file

@ -1,22 +0,0 @@
namespace Dino {
[DBus (name = "org.freedesktop.NetworkManager")]
public interface NetworkManager : Object {
public const int CONNECTED_GLOBAL = 70;
public abstract uint32 State {owned get;}
public signal void StateChanged(uint32 state);
}
public static NetworkManager? get_network_manager() {
NetworkManager? nm = null;
try {
nm = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager");
} catch (IOError e) {
stderr.printf ("%s\n", e.message);
}
return nm;
}
}

View file

@ -1,14 +0,0 @@
[DBus (name = "org.freedesktop.DBus.Properties")]
public interface NetworkManagerDBusProperties : GLib.Object {
public signal void properties_changed(string iface, HashTable<string, Variant> changed, string[] invalidated);
}
public static NetworkManagerDBusProperties? get_dbus_properties() {
NetworkManagerDBusProperties? dbus_properties = null;
try {
dbus_properties = Bus.get_proxy_sync(BusType.SYSTEM, "org.freedesktop.DBus.Properties", "/org/freedesktop/NetworkManager");
} catch (IOError e) {
stderr.printf("%s\n", e.message);
}
return dbus_properties;
}

View file

@ -21,9 +21,8 @@ public class ConnectionManager {
private HashMap<Account, Connection> connections = new HashMap<Account, Connection>(Account.hash_func, Account.equals_func); private HashMap<Account, Connection> connections = new HashMap<Account, Connection>(Account.hash_func, Account.equals_func);
private HashMap<Account, ConnectionError> connection_errors = new HashMap<Account, ConnectionError>(Account.hash_func, Account.equals_func); private HashMap<Account, ConnectionError> connection_errors = new HashMap<Account, ConnectionError>(Account.hash_func, Account.equals_func);
private NetworkManager? network_manager; private NetworkMonitor? network_monitor;
private Login1Manager? login1; private Login1Manager? login1;
private NetworkManagerDBusProperties? dbus_properties;
private ModuleManager module_manager; private ModuleManager module_manager;
public string? log_options; public string? log_options;
@ -66,25 +65,15 @@ public class ConnectionManager {
public ConnectionManager(ModuleManager module_manager) { public ConnectionManager(ModuleManager module_manager) {
this.module_manager = module_manager; this.module_manager = module_manager;
network_manager = get_network_manager(); network_monitor = GLib.NetworkMonitor.get_default();
if (network_manager != null) { if (network_monitor != null) {
network_manager.StateChanged.connect(on_nm_state_changed); network_monitor.network_changed.connect(on_network_changed);
network_monitor.notify["connectivity"].connect(on_network_changed);
} }
login1 = get_login1(); login1 = get_login1();
if (login1 != null) { if (login1 != null) {
login1.PrepareForSleep.connect(on_prepare_for_sleep); login1.PrepareForSleep.connect(on_prepare_for_sleep);
} }
dbus_properties = get_dbus_properties();
if (dbus_properties != null) {
dbus_properties.properties_changed.connect((s, sv, sa) => {
foreach (string key in sv.get_keys()) {
if (key == "PrimaryConnection") {
print("primary connection changed\n");
check_reconnects();
}
}
});
}
Timeout.add_seconds(60, () => { Timeout.add_seconds(60, () => {
foreach (Account account in connection_todo) { foreach (Account account in connection_todo) {
if (connections[account].last_activity != null && if (connections[account].last_activity != null &&
@ -228,7 +217,7 @@ public class ConnectionManager {
} else if (error.source == ConnectionError.Source.SASL) { } else if (error.source == ConnectionError.Source.SASL) {
return; return;
} }
if (network_manager != null && network_manager.State != NetworkManager.CONNECTED_GLOBAL) { if (network_is_online()) {
wait_sec = 30; wait_sec = 30;
} }
print(@"recovering in $wait_sec\n"); print(@"recovering in $wait_sec\n");
@ -266,11 +255,22 @@ public class ConnectionManager {
}); });
} }
private void on_nm_state_changed(uint32 state) { private bool network_is_online() {
print("nm " + state.to_string() + "\n"); /* FIXME: We should also check for connectivity eventually. For more
if (state == NetworkManager.CONNECTED_GLOBAL) { * details on why we don't do it for now, see:
*
* - https://github.com/dino/dino/pull/236#pullrequestreview-86851793
* - https://bugzilla.gnome.org/show_bug.cgi?id=792240
*/
return network_monitor != null && network_monitor.network_available;
}
private void on_network_changed() {
if (network_is_online()) {
print("network online\n");
check_reconnects(); check_reconnects();
} else { } else {
print("network offline\n");
foreach (Account account in connection_todo) { foreach (Account account in connection_todo) {
change_connection_state(account, ConnectionState.DISCONNECTED); change_connection_state(account, ConnectionState.DISCONNECTED);
} }