Revert "WifiNetworkFactory: Stop restricting peer to peer API network callback"

Bug: 187921303
Test: atest FrameworksWifiTests
This reverts commit a51b1623e17c30f6350b5e316a051d110c4a1cdc.

Reason for revert: location leak, new solution proposed

Change-Id: I9825d2c09f495375e30dbda5ed07e0be9d48201e
diff --git a/service/java/com/android/server/wifi/ClientModeImpl.java b/service/java/com/android/server/wifi/ClientModeImpl.java
index 31eac23..f7c53db 100644
--- a/service/java/com/android/server/wifi/ClientModeImpl.java
+++ b/service/java/com/android/server/wifi/ClientModeImpl.java
@@ -3924,10 +3924,16 @@
             }
         }
 
+        Pair<Integer, String> specificRequestUidAndPackageName =
+                mNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                        currentWifiConfiguration, currentBssid);
         // There is an active specific request.
-        if (mNetworkFactory.isSpecificRequestInProgress(currentWifiConfiguration, currentBssid)) {
+        if (specificRequestUidAndPackageName.first != Process.INVALID_UID) {
             // Remove internet capability.
             builder.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+            // Fill up the uid/packageName for this connection.
+            builder.setRequestorUid(specificRequestUidAndPackageName.first);
+            builder.setRequestorPackageName(specificRequestUidAndPackageName.second);
             // Fill up the network agent specifier for this connection, allowing NetworkCallbacks
             // to match local-only specifiers in requests. TODO(b/187921303): a third-party app can
             // observe this location-sensitive information by registering a NetworkCallback.
diff --git a/service/java/com/android/server/wifi/WifiNetworkFactory.java b/service/java/com/android/server/wifi/WifiNetworkFactory.java
index 3f029bf..afc7521 100644
--- a/service/java/com/android/server/wifi/WifiNetworkFactory.java
+++ b/service/java/com/android/server/wifi/WifiNetworkFactory.java
@@ -62,6 +62,7 @@
 import android.os.WorkSource;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.Pair;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.wifi.proto.nano.WifiMetricsProto;
@@ -880,29 +881,31 @@
     }
 
     /**
-     * Return true if the specific network request is being processed if connected to the requested
+     * Return the uid of the specific network request being processed if connected to the requested
      * network.
      *
      * @param connectedNetwork WifiConfiguration corresponding to the connected network.
-     * @return true if the specific request is in progress, else false.
+     * @return Pair of uid & package name of the specific request (if any), else <-1, "">.
      */
-    public boolean isSpecificRequestInProgress(
+    public Pair<Integer, String> getSpecificNetworkRequestUidAndPackageName(
             @NonNull WifiConfiguration connectedNetwork, @NonNull String connectedBssid) {
         if (mUserSelectedNetwork == null || connectedNetwork == null) {
-            return false;
+            return Pair.create(Process.INVALID_UID, "");
         }
         if (!isUserSelectedNetwork(connectedNetwork, connectedBssid)) {
             Log.w(TAG, "Connected to unknown network " + connectedNetwork + ":" + connectedBssid
                     + ". Ignoring...");
-            return false;
+            return Pair.create(Process.INVALID_UID, "");
         }
         if (mConnectedSpecificNetworkRequestSpecifier != null) {
-            return true;
+            return Pair.create(mConnectedSpecificNetworkRequest.getRequestorUid(),
+                    mConnectedSpecificNetworkRequest.getRequestorPackageName());
         }
         if (mActiveSpecificNetworkRequestSpecifier != null) {
-            return true;
+            return Pair.create(mActiveSpecificNetworkRequest.getRequestorUid(),
+                    mActiveSpecificNetworkRequest.getRequestorPackageName());
         }
-        return false;
+        return Pair.create(Process.INVALID_UID, "");
     }
 
     // Helper method to add the provided network configuration to WifiConfigManager, if it does not
diff --git a/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java b/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
index 5fcf778..1db82b7 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/ClientModeImplTest.java
@@ -521,6 +521,9 @@
         /** uncomment this to enable logs from ClientModeImpls */
         // enableDebugLogs();
         mWifiMonitor = spy(new MockWifiMonitor());
+        when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mDataTelephonyManager);
+        when(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(any(), any()))
+                .thenReturn(Pair.create(Process.INVALID_UID, ""));
         setUpWifiNative();
         doAnswer(new AnswerWithArguments() {
             public MacAddress answer(
@@ -2983,6 +2986,8 @@
     public void verifyConnectedModeNetworkCapabilitiesBandwidthUpdate() throws Exception {
         when(mPerNetwork.getTxLinkBandwidthKbps()).thenReturn(40_000);
         when(mPerNetwork.getRxLinkBandwidthKbps()).thenReturn(50_000);
+        when(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(any(), any()))
+                .thenReturn(Pair.create(Process.INVALID_UID, ""));
         // Simulate the first connection.
         connectWithValidInitRssi(-42);
 
@@ -4337,6 +4342,8 @@
         mWifiInfo.setFrequency(5825);
         when(mPerNetwork.getTxLinkBandwidthKbps()).thenReturn(40_000);
         when(mPerNetwork.getRxLinkBandwidthKbps()).thenReturn(50_000);
+        when(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(any(), any()))
+                .thenReturn(Pair.create(Process.INVALID_UID, ""));
         // Simulate the first connection.
         connectWithValidInitRssi(-42);
 
@@ -4382,7 +4389,8 @@
         mWifiInfo.setFrequency(2437);
         when(mPerNetwork.getTxLinkBandwidthKbps()).thenReturn(30_000);
         when(mPerNetwork.getRxLinkBandwidthKbps()).thenReturn(40_000);
-        when(mWifiNetworkFactory.isSpecificRequestInProgress(any(), any())).thenReturn(true);
+        when(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(any(), any()))
+                .thenReturn(Pair.create(TEST_UID, OP_PACKAGE_NAME));
         // Simulate the first connection.
         connectWithValidInitRssi(-42);
         ArgumentCaptor<NetworkCapabilities> networkCapabilitiesCaptor =
@@ -4410,6 +4418,8 @@
                 new WifiNetworkAgentSpecifier(expectedConfig, ScanResult.WIFI_BAND_24_GHZ,
                         true /* matchLocalOnlySpecifiers */);
         assertEquals(expectedWifiNetworkAgentSpecifier, wifiNetworkAgentSpecifier);
+        assertEquals(TEST_UID, networkCapabilities.getRequestorUid());
+        assertEquals(OP_PACKAGE_NAME, networkCapabilities.getRequestorPackageName());
         assertEquals(30_000, networkCapabilities.getLinkUpstreamBandwidthKbps());
         assertEquals(40_000, networkCapabilities.getLinkDownstreamBandwidthKbps());
     }
diff --git a/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java b/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
index 880037b..64a1d3d 100644
--- a/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
+++ b/service/tests/wifitests/src/com/android/server/wifi/WifiNetworkFactoryTest.java
@@ -63,6 +63,7 @@
 import android.os.IBinder;
 import android.os.PatternMatcher;
 import android.os.PowerManager;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.os.WorkSource;
@@ -2263,10 +2264,17 @@
                                 secondaryConnectionDurationMillis)));
     }
 
+
+    /**
+     * Verify we return the correct UID when processing network request with network specifier.
+     */
     @Test
-    public void testHandleNetworkRequestWithSpecifierIsInProgress() throws Exception {
-        assertFalse(mWifiNetworkFactory.isSpecificRequestInProgress(
-                new WifiConfiguration(), ""));
+    public void testHandleNetworkRequestWithSpecifierGetUid() throws Exception {
+        assertEquals(Integer.valueOf(Process.INVALID_UID),
+                mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                        new WifiConfiguration(), new String()).first);
+        assertTrue(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                new WifiConfiguration(), new String()).second.isEmpty());
 
         sendNetworkRequestAndSetupForConnectionStatus();
         assertNotNull(mSelectedNetwork);
@@ -2274,14 +2282,21 @@
         // connected to a different network.
         WifiConfiguration connectedNetwork = new WifiConfiguration(mSelectedNetwork);
         connectedNetwork.SSID += "test";
-        assertFalse(mWifiNetworkFactory.isSpecificRequestInProgress(
-                new WifiConfiguration(), ""));
+        assertEquals(Integer.valueOf(Process.INVALID_UID),
+                mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                        new WifiConfiguration(), new String()).first);
+        assertTrue(mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                new WifiConfiguration(), new String()).second.isEmpty());
 
         // connected to the correct network.
         connectedNetwork = new WifiConfiguration(mSelectedNetwork);
         String connectedBssid = TEST_BSSID_1;
-        assertTrue(mWifiNetworkFactory.isSpecificRequestInProgress(
-                connectedNetwork, connectedBssid));
+        assertEquals(Integer.valueOf(TEST_UID_1),
+                mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                        connectedNetwork, connectedBssid).first);
+        assertEquals(TEST_PACKAGE_NAME_1,
+                mWifiNetworkFactory.getSpecificNetworkRequestUidAndPackageName(
+                        connectedNetwork, connectedBssid).second);
     }
 
     /**