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

Commit 404e5164 authored by Lucas Lin's avatar Lucas Lin Committed by Android (Google) Code Review
Browse files

Merge "Ensure IkeV2VpnRunnerCallback is always called by the mExecutor thread" into tm-dev

parents e2f44dc7 0be926ff
Loading
Loading
Loading
Loading
+58 −61
Original line number Diff line number Diff line
@@ -2540,7 +2540,9 @@ public class Vpn {
            super(TAG);
            mProfile = profile;
            mIpSecManager = (IpSecManager) mContext.getSystemService(Context.IPSEC_SERVICE);
            mNetworkCallback = new VpnIkev2Utils.Ikev2VpnNetworkCallback(TAG, this);
            // Pass mExecutor into Ikev2VpnNetworkCallback and make sure that IkeV2VpnRunnerCallback
            // will be called by the mExecutor thread.
            mNetworkCallback = new VpnIkev2Utils.Ikev2VpnNetworkCallback(TAG, this, mExecutor);
            mSessionKey = UUID.randomUUID().toString();
        }

@@ -2695,16 +2697,12 @@ public class Vpn {
         * <p>The Ikev2VpnRunner will unconditionally switch to the new network, killing the old IKE
         * state in the process, and starting a new IkeSession instance.
         *
         * <p>This method is called multiple times over the lifetime of the Ikev2VpnRunner, and is
         * called on the ConnectivityService thread. Thus, the actual work MUST be proxied to the
         * mExecutor thread in order to ensure consistency of the Ikev2VpnRunner fields.
         * <p>This method MUST always be called on the mExecutor thread in order to ensure
         * consistency of the Ikev2VpnRunner fields.
         */
        public void onDefaultNetworkChanged(@NonNull Network network) {
            Log.d(TAG, "Starting IKEv2/IPsec session on new network: " + network);

            // Proxy to the Ikev2VpnRunner (single-thread) executor to ensure consistency in lieu
            // of locking.
            mExecutor.execute(() -> {
            try {
                if (!mIsRunning) {
                    Log.d(TAG, "onDefaultNetworkChanged after exit");
@@ -2761,7 +2759,6 @@ public class Vpn {
                Log.i(TAG, "Setup failed for network " + network + ". Aborting", e);
                onSessionLost(network, e);
            }
            });
        }

        /** Marks the state as FAILED, and disconnects. */
+7 −3
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * Utility class to build and convert IKEv2/IPsec parameters.
@@ -376,22 +377,25 @@ public class VpnIkev2Utils {
    static class Ikev2VpnNetworkCallback extends NetworkCallback {
        private final String mTag;
        private final Vpn.IkeV2VpnRunnerCallback mCallback;
        private final ExecutorService mExecutor;

        Ikev2VpnNetworkCallback(String tag, Vpn.IkeV2VpnRunnerCallback callback) {
        Ikev2VpnNetworkCallback(String tag, Vpn.IkeV2VpnRunnerCallback callback,
                ExecutorService executor) {
            mTag = tag;
            mCallback = callback;
            mExecutor = executor;
        }

        @Override
        public void onAvailable(@NonNull Network network) {
            Log.d(mTag, "Starting IKEv2/IPsec session on new network: " + network);
            mCallback.onDefaultNetworkChanged(network);
            mExecutor.execute(() -> mCallback.onDefaultNetworkChanged(network));
        }

        @Override
        public void onLost(@NonNull Network network) {
            Log.d(mTag, "Tearing down; lost network: " + network);
            mCallback.onSessionLost(network, null);
            mExecutor.execute(() -> mCallback.onSessionLost(network, null));
        }
    }