From 03a5aaa3f7a387b05ad4e7e2939b534828adcea2 Mon Sep 17 00:00:00 2001 From: Richard Zadorozny Date: Fri, 19 May 2023 17:41:14 -0700 Subject: [PATCH] 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 --- .../flipper/android/FlipperSocketImpl.java | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java b/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java index 2ff095d73..ce4c201cb 100644 --- a/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java +++ b/android/src/main/java/com/facebook/flipper/android/FlipperSocketImpl.java @@ -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());