Migrate to Kotlin - CxxInspectorPackagerConnection

Summary:
This diff migrates the following file to Kotlin - CxxInspectorPackagerConnection
as part of our ongoing effort of migrating the codebase to Kotlin

Changelog:
[Internal] [Changed] - CxxInspectorPackagerConnection to Kotlin

Reviewed By: rshest

Differential Revision: D72566596

fbshipit-source-id: 046e7549841f64d73fed56d8bc8d70f2d3ea63e8
This commit is contained in:
Nicola Corti authored and Facebook GitHub Bot committed 2025-04-08 02:59:28 -07:00
1 parent d12bcaac3b
commit 5491be6fe6
2 files changed
+159 -179

No files matched your search

@@ -1,179 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
import com.facebook.infer.annotation.Nullsafe;
import com.facebook.jni.HybridData;
import com.facebook.proguard.annotations.DoNotStrip;
import com.facebook.proguard.annotations.DoNotStripAny;
import com.facebook.soloader.SoLoader;
import java.io.Closeable;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
/** Java wrapper around a C++ InspectorPackagerConnection. */
/* package */ @Nullsafe(Nullsafe.Mode.LOCAL)
class CxxInspectorPackagerConnection implements IInspectorPackagerConnection {
static {
SoLoader.loadLibrary("react_devsupportjni");
}
@DoNotStrip private final HybridData mHybridData;
public CxxInspectorPackagerConnection(String url, String deviceName, String packageName) {
mHybridData = initHybrid(url, deviceName, packageName, new DelegateImpl());
}
private static native HybridData initHybrid(
String url, String deviceName, String packageName, DelegateImpl delegate);
public native void connect();
public native void closeQuietly();
public native void sendEventToAllConnections(@Nullable String event);
/** Java wrapper around a C++ IWebSocketDelegate, allowing us to call the interface from Java. */
@DoNotStrip
private static class WebSocketDelegate implements Closeable {
private final HybridData mHybridData;
public native void didFailWithError(@Nullable Integer posixCode, String error);
public native void didReceiveMessage(String message);
public native void didOpen();
public native void didClose();
/**
* Release the C++ part of the hybrid WebSocketDelegate object. This should be called when the
* delegate is not needed anymore ( = the socket will not send more events).
*/
@Override
public void close() {
mHybridData.resetNative();
}
@DoNotStrip
private WebSocketDelegate(HybridData hybridData) {
mHybridData = hybridData;
}
}
/**
* Java counterpart of the C++ IWebSocket interface, allowing us to implement the interface in
* Java.
*/
@DoNotStripAny
private interface IWebSocket extends Closeable {
void send(String message);
/**
* Close the WebSocket connection. NOTE: There is no close() method in the C++ interface.
* Instead, this method is called when the IWebSocket is destroyed on the C++ side.
*/
void close();
}
/** Java implementation of the C++ InspectorPackagerConnectionDelegate interface. */
private static class DelegateImpl {
private final OkHttpClient mHttpClient =
new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build();
private final Handler mHandler = new Handler(Looper.getMainLooper());
@DoNotStrip
public IWebSocket connectWebSocket(String url, WebSocketDelegate delegate) {
Request request = new Request.Builder().url(url).build();
final WebSocket webSocket =
mHttpClient.newWebSocket(
request,
new WebSocketListener() {
@Override
public void onFailure(WebSocket _unused, Throwable t, @Nullable Response response) {
scheduleCallback(
new Runnable() {
public void run() {
@Nullable String message = t.getMessage();
delegate.didFailWithError(
null, message != null ? message : "<Unknown error>");
// "No further calls to this listener will be made." -OkHttp docs for
// WebSocketListener.onFailure
delegate.close();
}
},
0);
}
@Override
public void onMessage(WebSocket _unused, String text) {
scheduleCallback(
new Runnable() {
public void run() {
delegate.didReceiveMessage(text);
}
},
0);
}
@Override
public void onOpen(WebSocket _webSocket, Response response) {
scheduleCallback(
new Runnable() {
public void run() {
delegate.didOpen();
}
},
0);
}
@Override
public void onClosed(WebSocket _unused, int code, String reason) {
scheduleCallback(
new Runnable() {
public void run() {
delegate.didClose();
// "No further calls to this listener will be made." -OkHttp docs for
// WebSocketListener.onClosed
delegate.close();
}
},
0);
}
});
return new IWebSocket() {
@Override
public void send(String message) {
webSocket.send(message);
}
@Override
public void close() {
webSocket.close(1000, "End of session");
}
};
}
@DoNotStrip
public void scheduleCallback(Runnable runnable, long delayMs) {
mHandler.postDelayed(runnable, delayMs);
}
}
}
@@ -0,0 +1,159 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.devsupport
import android.os.Handler
import android.os.Looper
import com.facebook.jni.HybridData
import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.proguard.annotations.DoNotStripAny
import com.facebook.soloader.SoLoader
import java.io.Closeable
import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import okhttp3.WebSocket
import okhttp3.WebSocketListener
/** Java wrapper around a C++ InspectorPackagerConnection. */
internal class CxxInspectorPackagerConnection(
url: String,
deviceName: String,
packageName: String
) : IInspectorPackagerConnection {
@DoNotStrip private val mHybridData: HybridData
init {
mHybridData = initHybrid(url, deviceName, packageName, DelegateImpl())
}
external override fun connect()
external override fun closeQuietly()
external override fun sendEventToAllConnections(event: String?)
/** Java wrapper around a C++ IWebSocketDelegate, allowing us to call the interface from Java. */
@DoNotStrip
private class WebSocketDelegate @DoNotStrip constructor(private val mHybridData: HybridData) :
Closeable {
external fun didFailWithError(posixCode: Int?, error: String?)
external fun didReceiveMessage(message: String?)
external fun didOpen()
external fun didClose()
/**
* Release the C++ part of the hybrid WebSocketDelegate object. This should be called when the
* delegate is not needed anymore ( = the socket will not send more events).
*/
override fun close() {
mHybridData.resetNative()
}
}
/**
* Java counterpart of the C++ IWebSocket interface, allowing us to implement the interface in
* Java.
*/
@DoNotStripAny
private interface IWebSocket : Closeable {
fun send(message: String)
/**
* Close the WebSocket connection. NOTE: There is no close() method in the C++ interface.
* Instead, this method is called when the IWebSocket is destroyed on the C++ side.
*/
override fun close()
}
/** Java implementation of the C++ InspectorPackagerConnectionDelegate interface. */
private class DelegateImpl {
private val httpClient =
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
.build()
private val mHandler = Handler(Looper.getMainLooper())
@Suppress("unused")
@DoNotStrip
fun connectWebSocket(urlParam: String?, delegate: WebSocketDelegate): IWebSocket {
val url = requireNotNull(urlParam)
val request = Request.Builder().url(url).build()
val webSocket =
httpClient.newWebSocket(
request,
object : WebSocketListener() {
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
scheduleCallback(
{
val message = t.message
delegate.didFailWithError(null, message ?: "<Unknown error>")
// "No further calls to this listener will be made." -OkHttp docs for
// WebSocketListener.onFailure
delegate.close()
},
delayMs = 0)
}
override fun onMessage(webSocket: WebSocket, text: String) {
scheduleCallback({ delegate.didReceiveMessage(text) }, delayMs = 0)
}
override fun onOpen(webSocket: WebSocket, response: Response) {
scheduleCallback({ delegate.didOpen() }, delayMs = 0)
}
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
scheduleCallback(
{
delegate.didClose()
// "No further calls to this listener will be made." -OkHttp docs for
// WebSocketListener.onClosed
delegate.close()
},
delayMs = 0)
}
})
return object : IWebSocket {
override fun send(message: String) {
webSocket.send(message)
}
override fun close() {
webSocket.close(1000, "End of session")
}
}
}
@DoNotStrip
fun scheduleCallback(runnable: Runnable, delayMs: Long) {
mHandler.postDelayed(runnable, delayMs)
}
}
companion object {
init {
SoLoader.loadLibrary("react_devsupportjni")
}
@JvmStatic
private external fun initHybrid(
url: String,
deviceName: String,
packageName: String,
delegate: DelegateImpl
): HybridData
}
}