Make sure FlipperSocketImpl always sets thread TrafficStats tag

Summary:
When looking at some of our logcats in logview, I noticed lots of StrictMode warnings related to a WebSocket not being tagged, and I figured out it was due to FlipperSocketImpl.

By making sure TrafficStats stats tag is set no matter if the SSL factory is used or not, it reduces the amount of StrictMode warnings we see.

Related diff: D38280819

Differential Revision: D46024367

fbshipit-source-id: 92a0bfcef8698a61ef9b27949a5bc0859e03448f
This commit is contained in:
Richard Zadorozny
2023-05-19 17:41:14 -07:00
committed by Facebook GitHub Bot
parent 4dba523757
commit 03a5aaa3f7

View File

@@ -37,10 +37,10 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import javax.net.SocketFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.java_websocket.client.WebSocketClient;
@@ -79,6 +79,7 @@ class FlipperSocketImpl extends WebSocketClient implements FlipperSocket {
* certificate exchange.
*/
FlipperObject authenticationObject = this.mEventHandler.onAuthenticationChallengeReceived();
SocketFactory socketFactory;
if (authenticationObject.contains("certificates_client_path")
&& authenticationObject.contains("certificates_client_pass")) {
@@ -100,18 +101,20 @@ class FlipperSocketImpl extends WebSocketClient implements FlipperSocket {
sslContext.init(
kmf.getKeyManagers(), new TrustManager[] {new FlipperTrustManager(cert_ca_path)}, null);
SSLSocketFactory factory = sslContext.getSocketFactory();
this.setSocketFactory(
new DelegatingSocketFactory(factory) {
@Override
protected Socket configureSocket(Socket socket) {
TrafficStats.setThreadStatsTag(SOCKET_TAG);
return socket;
}
});
socketFactory = sslContext.getSocketFactory();
} else {
socketFactory = SocketFactory.getDefault();
}
this.setSocketFactory(
new DelegatingSocketFactory(socketFactory) {
@Override
protected Socket configureSocket(Socket socket) {
TrafficStats.setThreadStatsTag(SOCKET_TAG);
return socket;
}
});
this.connect();
} catch (Exception e) {
Log.e("Flipper", "Failed to initialize the socket before connect. " + e.getMessage());