Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/http/IHttpClient.cpp
Original file line number Diff line number Diff line change
@@ -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
89 changes: 89 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/http/IHttpClient.h
Original file line number Diff line number Diff line change
@@ -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 <folly/io/IOBuf.h>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace facebook::react {

namespace http {

using Headers = std::vector<std::pair<std::string, std::string>>;

struct FormDataField {
std::string fieldName;
Headers headers;
std::optional<std::string> string;
std::optional<std::string> uri;
};

using FormData = std::vector<FormDataField>;

struct Body {
std::optional<std::string> string;
std::optional<std::string> blob;
std::optional<FormData> formData;
std::optional<std::string> base64;
};

using OnUploadProgress = std::function<void(int64_t progress, int64_t size)>;
using OnResponse = std::function<void(uint16_t responseCode, Headers headers)>;
using OnBody = std::function<void(std::unique_ptr<folly::IOBuf> body)>;
using OnBodyIncremental = std::function<int64_t(
int64_t progress,
int64_t total,
std::unique_ptr<folly::IOBuf> body)>;
using OnBodyProgress = std::function<void(int64_t loaded, int64_t total)>;
using OnResponseComplete =
std::function<void(std::string error, bool timeoutError)>;

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<http::IRequestToken> 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<std::string> loggingId = std::nullopt) = 0;
};

extern const char HttpClientFactoryKey[];

using HttpClientFactory = std::function<std::unique_ptr<IHttpClient>()>;

HttpClientFactory getHttpClientFactory();

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 <functional>
#include <memory>
#include <string>
#include <utility>

namespace facebook::react {

class IWebSocketClient {
public:
using OnConnectCallback = std::function<void(bool, const std::string&)>;
using OnClosedCallback = std::function<void(const std::string&)>;
using OnMessageCallback = std::function<void(const std::string&)>;

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<std::unique_ptr<IWebSocketClient>()>;

WebSocketClientFactory getWebSocketClientFactory();

} // namespace facebook::react
28 changes: 28 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/jni/JniHelper.cpp
Original file line number Diff line number Diff line change
@@ -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 <fbjni/Context.h>
#include <fbjni/fbjni.h>

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<jni::AContext> getContext() {
auto env = facebook::jni::Environment::ensureCurrentThreadIsAttached();
auto application = getApplication(env);
return facebook::jni::wrap_alias(
static_cast<facebook::jni::AContext::javaobject>(application));
}

} // namespace facebook::react
19 changes: 19 additions & 0 deletions packages/react-native/ReactCxxPlatform/react/jni/JniHelper.h
Original file line number Diff line number Diff line change
@@ -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 <fbjni/Context.h>
#include <fbjni/fbjni.h>

namespace facebook::react {

jobject getApplication(JNIEnv* env);

jni::alias_ref<jni::AContext> getContext();

} // namespace facebook::react
Loading