Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 42d4788a authored by Oliver Scott's avatar Oliver Scott Committed by Sam Mortimer
Browse files

fw/b data restrictions: Don't call getNetworkCapabilities() in the callback

* Docs say that calling getNetworkCapabilities() from within a network
  callback is racy and not to do it.

* Refactor to make use of onCapabilitiesChanged() to glean capabilities
  instead.

Change-Id: If9c4cd7c1bd0594697b0ac98903600ecd583e55b
parent 635d9289
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -298,6 +298,8 @@ public class NetworkManagementService extends INetworkManagementService.Stub {
        throw new IllegalStateException("Unknown interface restriction");
    }

    private final HashMap<Network, NetworkCapabilities> mNetworkCapabilitiesMap = new HashMap<>();

    /**
     * Constructs a new NetworkManagementService instance
     *
@@ -355,9 +357,24 @@ public class NetworkManagementService extends INetworkManagementService.Stub {

        final ConnectivityManager.NetworkCallback mNetworkCallback =
                new ConnectivityManager.NetworkCallback() {
            @Override
            public void onCapabilitiesChanged(Network network,
                    NetworkCapabilities networkCapabilities) {
                mNetworkCapabilitiesMap.put(network, networkCapabilities);
            }

            @Override
            public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
                NetworkCapabilities nc = mConnectivityManager.getNetworkCapabilities(network);
                // Callback ordering in Oreo+ is documented to be:
                // onCapabilitiesChanged, onLinkPropertiesChanged
                // At this point, we should always find the network in our
                // local map but guard anyway.
                NetworkCapabilities nc = mNetworkCapabilitiesMap.get(network);
                if (nc == null) {
                    Slog.e(TAG, "onLinkPropertiesChanged: network was not in map: "
                            + "network=" + network + " linkProperties=" + linkProperties);
                    return;
                }
                RestrictIf matchedRestrictIf = null;
                for (RestrictIf restrictIf : mRestrictIf) {
                    if (nc.hasTransport(restrictIf.transport)) {
@@ -378,6 +395,11 @@ public class NetworkManagementService extends INetworkManagementService.Stub {
                mDaemonHandler.post(() ->
                        updateAppOnInterfaceCallback(finalRestrictIf, iface));
            }

            @Override
            public void onLost(Network network) {
                mNetworkCapabilitiesMap.remove(network);
            }
        };

        mConnectivityManager.registerNetworkCallback(request, mNetworkCallback);