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
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 "ImageLoaderModule.h"

namespace facebook::react {

// TODO: T170340321 - actual implementation

jsi::Object ImageLoaderModule::getConstants(jsi::Runtime& rt) {
return jsi::Object(rt);
}

void ImageLoaderModule::abortRequest(jsi::Runtime& rt, int32_t requestId) {}

AsyncPromise<ImageSize> ImageLoaderModule::getSize(
jsi::Runtime& rt,
const std::string& /*uri*/) {
auto promise = AsyncPromise<ImageSize>(rt, jsInvoker_);
promise.resolve({.width = 0.0, .height = 0.0});
return promise;
}

AsyncPromise<ImageSize> ImageLoaderModule::getSizeWithHeaders(
jsi::Runtime& rt,
const std::string& /*uri*/,
jsi::Object /*headers*/) {
auto promise = AsyncPromise<ImageSize>(rt, jsInvoker_);
promise.resolve({.width = 0.0, .height = 0.0});
return promise;
}

AsyncPromise<bool> ImageLoaderModule::prefetchImage(
jsi::Runtime& rt,
const std::string& /*uri*/,
int32_t /*requestId*/) {
auto promise = AsyncPromise<bool>(rt, jsInvoker_);
promise.resolve(false);
return promise;
}

jsi::Object ImageLoaderModule::queryCache(
jsi::Runtime& rt,
const std::vector<std::string>& /*uris*/) {
return jsi::Object(rt);
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 <FBReactNativeSpec/FBReactNativeSpecJSI.h>
#include <react/bridging/Promise.h>
#include <memory>
#include <string>
#include <vector>

namespace facebook::react {

using ImageSize = NativeImageLoaderAndroidImageSize<double, double>;

template <>
struct Bridging<ImageSize>
: NativeImageLoaderAndroidImageSizeBridging<ImageSize> {};

class ImageLoaderModule
: public NativeImageLoaderAndroidCxxSpec<ImageLoaderModule> {
public:
explicit ImageLoaderModule(std::shared_ptr<CallInvoker> jsInvoker)
: NativeImageLoaderAndroidCxxSpec(jsInvoker) {}

jsi::Object getConstants(jsi::Runtime& rt);
void abortRequest(jsi::Runtime& rt, int32_t requestId);

AsyncPromise<ImageSize> getSize(jsi::Runtime& rt, const std::string& uri);

AsyncPromise<ImageSize> getSizeWithHeaders(
jsi::Runtime& rt,
const std::string& uri,
jsi::Object headers);

AsyncPromise<bool>
prefetchImage(jsi::Runtime& rt, const std::string& uri, int32_t requestId);

jsi::Object queryCache(
jsi::Runtime& rt,
const std::vector<std::string>& uris);
};

} // namespace facebook::react
Loading
Loading