Tag sockets in FlipperSocketImpl (#3928)

Summary:
Resolves https://github.com/facebook/flipper/issues/3926. Open to suggestions on the tag value, just sort of picked one that could be pointed to for anyone that wants to track these in their apps.

## Changelog

Tag sockets used by Flipper to fix strict mode warnings about untagged sockets.

Pull Request resolved: https://github.com/facebook/flipper/pull/3928

Test Plan:
Edit FlipperSampleApplication.java with the following:

```
public class FlipperSampleApplication extends Application {
  Override
  public void onCreate() {

    StrictMode.setThreadPolicy(
            new StrictMode.ThreadPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build()
    );
    StrictMode.setVmPolicy(
            new StrictMode.VmPolicy.Builder()
                    .detectAll()
                    .penaltyLog()
                    .build()
    );
...
```

That will enable strict mode. Now build/launch the sample application.

Use adb logcat to inspect warnings:

```
 adb logcat -s StrictMode
```

Without tagging the socket, the following warning should be observed:

```
D/StrictMode: StrictMode policy violation: android.os.strictmode.UntaggedSocketViolation: Untagged socket detected; use TrafficStats.setTrafficStatsTag() to track all network usage
```

After applying the fix, that warning should go away.

Reviewed By: lblasa

Differential Revision: D38280819

Pulled By: passy

fbshipit-source-id: 0c841b13237cbcb0ff8b8226fb44655a74b775c6
This commit is contained in:
Zac Sweers
2022-08-01 08:34:00 -07:00
committed by Facebook GitHub Bot
parent 4730c417bc
commit 64c32f62b1
3 changed files with 83 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2014 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.flipper.android;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.net.SocketFactory;
/**
* A {@link SocketFactory} that delegates calls. Sockets can be configured after creation by
* overriding {@link #configureSocket}.
*/
class DelegatingSocketFactory extends SocketFactory {
private final SocketFactory delegate;
DelegatingSocketFactory(SocketFactory delegate) {
this.delegate = delegate;
}
@Override
public Socket createSocket() throws IOException {
Socket socket = delegate.createSocket();
return configureSocket(socket);
}
@Override
public Socket createSocket(String host, int port) throws IOException {
Socket socket = delegate.createSocket(host, port);
return configureSocket(socket);
}
@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
throws IOException {
Socket socket = delegate.createSocket(host, port, localAddress, localPort);
return configureSocket(socket);
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
Socket socket = delegate.createSocket(host, port);
return configureSocket(socket);
}
@Override
public Socket createSocket(InetAddress host, int port, InetAddress localAddress, int localPort)
throws IOException {
Socket socket = delegate.createSocket(host, port, localAddress, localPort);
return configureSocket(socket);
}
protected Socket configureSocket(Socket socket) {
// No-op by default.
return socket;
}
}

View File

@@ -7,6 +7,7 @@
package com.facebook.flipper.android;
import android.net.TrafficStats;
import android.util.Log;
import com.facebook.flipper.BuildConfig;
import com.facebook.flipper.core.FlipperObject;
@@ -17,6 +18,7 @@ import com.facebook.soloader.SoLoader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.InvalidAlgorithmParameterException;
@@ -100,7 +102,14 @@ class FlipperSocketImpl extends WebSocketClient implements FlipperSocket {
SSLSocketFactory factory = sslContext.getSocketFactory();
this.setSocketFactory(factory);
this.setSocketFactory(
new DelegatingSocketFactory(factory) {
@Override
protected Socket configureSocket(Socket socket) {
TrafficStats.setThreadStatsTag(SOCKET_TAG);
return socket;
}
});
}
this.connect();

View File

@@ -9,6 +9,9 @@ package com.facebook.flipper.core;
public interface FlipperSocket {
/** The value used by Flipper to tag sockets, visible to {@link android.net.TrafficStats}. */
int SOCKET_TAG = 0x000090000;
/** Connect to the endpoint. */
void flipperConnect();