From 6c2386b655e21089999acb8134db32de9ff5f84a Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 9 Jun 2025 13:33:32 -0700 Subject: [PATCH 1/2] Add jni target to ReactCxxPlatform Summary: changelog: [internal] Differential Revision: D76240376 --- .../ReactCxxPlatform/react/jni/JniHelper.cpp | 28 +++++++++++++++++++ .../ReactCxxPlatform/react/jni/JniHelper.h | 19 +++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h diff --git a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp new file mode 100644 index 000000000000..a3689d5d0949 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#include +#include + +namespace facebook::react { + +jobject getApplication(JNIEnv* env) { + auto activityThreadClass = env->FindClass("android/app/ActivityThread"); + auto currentApplicationMethodID = env->GetStaticMethodID( + activityThreadClass, "currentApplication", "()Landroid/app/Application;"); + return env->CallStaticObjectMethod( + activityThreadClass, currentApplicationMethodID); +} + +jni::alias_ref getContext() { + auto env = facebook::jni::Environment::ensureCurrentThreadIsAttached(); + auto application = getApplication(env); + return facebook::jni::wrap_alias( + static_cast(application)); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h new file mode 100644 index 000000000000..8db809186f31 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h @@ -0,0 +1,19 @@ +/* + * 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. + */ + +#pragma once + +#include +#include + +namespace facebook::react { + +jobject getApplication(JNIEnv* env); + +jni::alias_ref getContext(); + +} // namespace facebook::react From 63201799ea06b8ac469fcf7fa52d22f6b004d753 Mon Sep 17 00:00:00 2001 From: Christoph Purrer Date: Mon, 9 Jun 2025 13:55:43 -0700 Subject: [PATCH 2/2] Add http target to ReactCxxPlatform (#51899) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51899 changelog: [internal] Reviewed By: andrewdacenko Differential Revision: D76240550 --- .../react/http/IHttpClient.cpp | 13 +++ .../ReactCxxPlatform/react/http/IHttpClient.h | 89 +++++++++++++++++++ .../react/http/IWebSocketClient.cpp | 13 +++ .../react/http/IWebSocketClient.h | 47 ++++++++++ 4 files changed, 162 insertions(+) create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp create mode 100644 packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h diff --git a/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp new file mode 100644 index 000000000000..c19f481ee22d --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp @@ -0,0 +1,13 @@ +/* + * 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. + */ + +namespace facebook::react { + +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +extern const char HttpClientFactoryKey[] = "HttpClientFactory"; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h new file mode 100644 index 000000000000..9b3d78b5c9d6 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h @@ -0,0 +1,89 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +namespace facebook::react { + +namespace http { + +using Headers = std::vector>; + +struct FormDataField { + std::string fieldName; + Headers headers; + std::optional string; + std::optional uri; +}; + +using FormData = std::vector; + +struct Body { + std::optional string; + std::optional blob; + std::optional formData; + std::optional base64; +}; + +using OnUploadProgress = std::function; +using OnResponse = std::function; +using OnBody = std::function body)>; +using OnBodyIncremental = std::function body)>; +using OnBodyProgress = std::function; +using OnResponseComplete = + std::function; + +struct NetworkCallbacks { + OnUploadProgress onUploadProgress{nullptr}; + OnResponse onResponse{nullptr}; + OnBody onBody{nullptr}; + OnBodyIncremental onBodyIncremental{nullptr}; + OnBodyProgress onBodyProgress{nullptr}; + OnResponseComplete onResponseComplete{nullptr}; + bool sendIncrementalUpdates{false}; + bool sendProgressUpdates{false}; +}; + +struct IRequestToken { + virtual ~IRequestToken() = default; + + virtual void cancel() noexcept = 0; +}; + +} // namespace http + +struct IHttpClient { + virtual ~IHttpClient() = default; + + virtual std::unique_ptr sendRequest( + http::NetworkCallbacks&& callback, + const std::string& method, + const std::string& url, + const http::Headers& headers = {}, + const http::Body& body = {}, + uint32_t timeout = 0, + std::optional loggingId = std::nullopt) = 0; +}; + +extern const char HttpClientFactoryKey[]; + +using HttpClientFactory = std::function()>; + +HttpClientFactory getHttpClientFactory(); + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp new file mode 100644 index 000000000000..93800a35a7b7 --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.cpp @@ -0,0 +1,13 @@ +/* + * 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. + */ + +namespace facebook::react { + +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +extern const char WebSocketClientFactoryKey[] = "WebSocketClientFactory"; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h new file mode 100644 index 000000000000..a2ec1b0859bb --- /dev/null +++ b/packages/react-native/ReactCxxPlatform/react/http/IWebSocketClient.h @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include +#include + +namespace facebook::react { + +class IWebSocketClient { + public: + using OnConnectCallback = std::function; + using OnClosedCallback = std::function; + using OnMessageCallback = std::function; + + virtual ~IWebSocketClient() = default; + + virtual void setOnClosedCallback(OnClosedCallback&& callback) noexcept = 0; + + virtual void setOnMessageCallback(OnMessageCallback&& callback) noexcept = 0; + + virtual void connect( + const std::string& url, + OnConnectCallback&& = nullptr) = 0; + + virtual void close(const std::string& reason) = 0; + + virtual void send(const std::string& message) = 0; + + virtual void ping() = 0; +}; + +extern const char WebSocketClientFactoryKey[]; + +using WebSocketClientFactory = + std::function()>; + +WebSocketClientFactory getWebSocketClientFactory(); + +} // namespace facebook::react