diff --git a/change/react-native-windows-2d37aae0-3bb6-4f89-8428-11cefbabf3b5.json b/change/react-native-windows-2d37aae0-3bb6-4f89-8428-11cefbabf3b5.json new file mode 100644 index 00000000000..84f9578f0f5 --- /dev/null +++ b/change/react-native-windows-2d37aae0-3bb6-4f89-8428-11cefbabf3b5.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Use TurboModules for networking in MSRN", + "packageName": "react-native-windows", + "email": "julio.rocha@microsoft.com", + "dependentChangeType": "patch" +} diff --git a/vnext/Microsoft.ReactNative/Base/CoreNativeModules.cpp b/vnext/Microsoft.ReactNative/Base/CoreNativeModules.cpp deleted file mode 100644 index e7cdc886edb..00000000000 --- a/vnext/Microsoft.ReactNative/Base/CoreNativeModules.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -#include "pch.h" -#include "CoreNativeModules.h" - -// Modules -#include -#include -#include -#include -#include - -// Shared -#include - -namespace Microsoft::ReactNative { - -std::vector GetCoreModules( - const std::shared_ptr &batchingUIMessageQueue, - const std::shared_ptr - &jsMessageQueue, // JS engine thread (what we use for external modules) - Mso::CntPtr &&context) noexcept { - std::vector modules; - - modules.emplace_back( - "Networking", - [props = context->Properties()]() { return Microsoft::React::CreateHttpModule(props); }, - jsMessageQueue); - - modules.emplace_back( - Microsoft::React::GetBlobModuleName(), - [props = context->Properties()]() { return Microsoft::React::CreateBlobModule(props); }, - batchingUIMessageQueue); - - modules.emplace_back( - Microsoft::React::GetFileReaderModuleName(), - [props = context->Properties()]() { return Microsoft::React::CreateFileReaderModule(props); }, - batchingUIMessageQueue); - - return modules; -} - -} // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Base/CoreNativeModules.h b/vnext/Microsoft.ReactNative/Base/CoreNativeModules.h deleted file mode 100644 index 19533574515..00000000000 --- a/vnext/Microsoft.ReactNative/Base/CoreNativeModules.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -#pragma once - -#include -#include -#include -#include -#include - -namespace facebook::react { -class AppState; -struct DevSettings; -class IUIManager; -class MessageQueueThread; -} // namespace facebook::react - -namespace Microsoft::ReactNative { - -struct DeviceInfo; -struct IReactInstance; -struct ViewManagerProvider; - -std::vector GetCoreModules( - const std::shared_ptr &batchingUIMessageQueue, - const std::shared_ptr &jsMessageQueue, - Mso::CntPtr &&context) noexcept; - -} // namespace Microsoft::ReactNative diff --git a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj index f42ba04b5d0..8ed10fc993b 100644 --- a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +++ b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj @@ -185,7 +185,6 @@ - @@ -421,7 +420,6 @@ - CoreAppPage.xaml @@ -679,4 +677,4 @@ - + \ No newline at end of file diff --git a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters index 02e9b6b23f3..f6e44c0e498 100644 --- a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters +++ b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj.filters @@ -15,9 +15,6 @@ - - Base - Base @@ -357,9 +354,6 @@ - - Base - Modules @@ -632,7 +626,6 @@ Views - diff --git a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp index 5a80b6c3bc2..ab038acd916 100644 --- a/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +++ b/vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp @@ -6,7 +6,6 @@ #include "MsoUtils.h" #include -#include #include #include #include @@ -30,7 +29,6 @@ #include #include #include -#include #include #include "DynamicWriter.h" #ifndef CORE_ABI @@ -86,6 +84,7 @@ #include #include "ChakraRuntimeHolder.h" +#include #include #include "CrashManager.h" #include "JsiApi.h" @@ -392,6 +391,16 @@ void ReactInstanceWin::LoadModules( registerTurboModule( L"Timing", winrt::Microsoft::ReactNative::MakeTurboModuleProvider<::Microsoft::ReactNative::Timing>()); #endif + + registerTurboModule(::Microsoft::React::GetBlobTurboModuleName(), ::Microsoft::React::GetBlobModuleProvider()); + + registerTurboModule(::Microsoft::React::GetHttpTurboModuleName(), ::Microsoft::React::GetHttpModuleProvider()); + + registerTurboModule( + ::Microsoft::React::GetFileReaderTurboModuleName(), ::Microsoft::React::GetFileReaderModuleProvider()); + + registerTurboModule( + ::Microsoft::React::GetWebSocketTurboModuleName(), ::Microsoft::React::GetWebSocketModuleProvider()); } //! Initialize() is called from the native queue. @@ -459,15 +468,7 @@ void ReactInstanceWin::Initialize() noexcept { } }; -#ifdef CORE_ABI std::vector cxxModules; -#else - // Acquire default modules and then populate with custom modules. - // Note that some of these have custom thread affinity. - std::vector cxxModules = - Microsoft::ReactNative::GetCoreModules(m_batchingUIThread, m_jsMessageThread.Load(), m_reactContext); -#endif - auto nmp = std::make_shared(); LoadModules(nmp, m_options.TurboModuleProvider); diff --git a/vnext/Shared/CreateModules.h b/vnext/Shared/CreateModules.h index 535f26e0273..2bc4ed8abfe 100644 --- a/vnext/Shared/CreateModules.h +++ b/vnext/Shared/CreateModules.h @@ -5,7 +5,6 @@ // React Native #include -#include // Windows API #include @@ -14,12 +13,16 @@ #include // Forward declarations. Desktop projects can not access +namespace winrt::Microsoft::ReactNative { +struct ReactContext; +struct ReactModuleProvider; +} // namespace winrt::Microsoft::ReactNative + namespace Mso::React { struct IReactContext; } -namespace facebook { -namespace react { +namespace facebook::react { class MessageQueueThread; @@ -29,11 +32,12 @@ class MessageQueueThread; extern std::unique_ptr CreateTimingModule( const std::shared_ptr &nativeThread) noexcept; -} // namespace react -} // namespace facebook +} // namespace facebook::react namespace Microsoft::React { +#pragma region CxxModules + extern const char *GetHttpModuleName() noexcept; extern std::unique_ptr CreateHttpModule( winrt::Windows::Foundation::IInspectable const &inspectableProperties) noexcept; @@ -50,4 +54,22 @@ extern const char *GetFileReaderModuleName() noexcept; extern std::unique_ptr CreateFileReaderModule( winrt::Windows::Foundation::IInspectable const &inspectableProperties) noexcept; +#pragma endregion CxxModules + +#pragma region TurboModules + +extern const wchar_t *GetBlobTurboModuleName() noexcept; +extern const winrt::Microsoft::ReactNative::ReactModuleProvider &GetBlobModuleProvider() noexcept; + +extern const wchar_t *GetHttpTurboModuleName() noexcept; +extern const winrt::Microsoft::ReactNative::ReactModuleProvider &GetHttpModuleProvider() noexcept; + +extern const wchar_t *GetFileReaderTurboModuleName() noexcept; +extern const winrt::Microsoft::ReactNative::ReactModuleProvider &GetFileReaderModuleProvider() noexcept; + +extern const wchar_t *GetWebSocketTurboModuleName() noexcept; +extern const winrt::Microsoft::ReactNative::ReactModuleProvider &GetWebSocketModuleProvider() noexcept; + +#pragma endregion TurboModules + } // namespace Microsoft::React diff --git a/vnext/Shared/Modules/BlobModule.cpp b/vnext/Shared/Modules/BlobModule.cpp index d76a462c670..4750d3e58a1 100644 --- a/vnext/Shared/Modules/BlobModule.cpp +++ b/vnext/Shared/Modules/BlobModule.cpp @@ -3,6 +3,7 @@ #include "BlobModule.h" +#include #include // React Native @@ -20,8 +21,12 @@ namespace msrn = winrt::Microsoft::ReactNative; namespace { constexpr char s_moduleName[] = "BlobModule"; +constexpr wchar_t s_moduleNameW[] = L"BlobModule"; const auto &blobKeys = IBlobResource::FieldNames(); + +msrn::ReactModuleProvider s_moduleProvider = msrn::MakeTurboModuleProvider(); + } // namespace namespace Microsoft::React { @@ -156,4 +161,12 @@ vector BlobModule::getMethods() { return nullptr; } +/*extern*/ const wchar_t *GetBlobTurboModuleName() noexcept { + return s_moduleNameW; +} + +/*extern*/ const msrn::ReactModuleProvider &GetBlobModuleProvider() noexcept { + return s_moduleProvider; +} + } // namespace Microsoft::React diff --git a/vnext/Shared/Modules/FileReaderModule.cpp b/vnext/Shared/Modules/FileReaderModule.cpp index 19a2910be34..09db7bdd4fd 100644 --- a/vnext/Shared/Modules/FileReaderModule.cpp +++ b/vnext/Shared/Modules/FileReaderModule.cpp @@ -3,6 +3,7 @@ #include "FileReaderModule.h" +#include #include #include @@ -28,6 +29,9 @@ using winrt::Windows::Foundation::IInspectable; namespace { constexpr char s_moduleName[] = "FileReaderModule"; +constexpr wchar_t s_moduleNameW[] = L"FileReaderModule"; + +msrn::ReactModuleProvider s_moduleProvider = msrn::MakeTurboModuleProvider(); } // namespace namespace Microsoft::React { @@ -198,4 +202,12 @@ void FileReaderTurboModule::ReadAsText( return nullptr; } +/*extern*/ const wchar_t *GetFileReaderTurboModuleName() noexcept { + return s_moduleNameW; +} + +/*extern*/ const msrn::ReactModuleProvider &GetFileReaderModuleProvider() noexcept { + return s_moduleProvider; +} + } // namespace Microsoft::React diff --git a/vnext/Shared/Modules/HttpModule.cpp b/vnext/Shared/Modules/HttpModule.cpp index cab864e3d33..f3941b31e40 100644 --- a/vnext/Shared/Modules/HttpModule.cpp +++ b/vnext/Shared/Modules/HttpModule.cpp @@ -5,6 +5,7 @@ #include "HttpModule.h" +#include #include #include @@ -32,6 +33,7 @@ using Microsoft::React::Modules::SendEvent; using Microsoft::React::Networking::IHttpResource; constexpr char s_moduleName[] = "Networking"; +constexpr wchar_t s_moduleNameW[] = L"Networking"; // React event names constexpr char completedResponse[] = "didCompleteNetworkResponse"; @@ -48,6 +50,8 @@ constexpr wchar_t receivedIncrementalDataW[] = L"didReceiveNetworkIncrementalDat constexpr wchar_t receivedDataProgressW[] = L"didReceiveNetworkDataProgress"; constexpr wchar_t receivedDataW[] = L"didReceiveNetworkData"; +msrn::ReactModuleProvider s_moduleProvider = msrn::MakeTurboModuleProvider(); + static void SetUpHttpResource( shared_ptr resource, weak_ptr weakReactInstance, @@ -327,4 +331,12 @@ std::vector HttpModule::getMethods() return s_moduleName; } +/*extern*/ const wchar_t *GetHttpTurboModuleName() noexcept { + return s_moduleNameW; +} + +/*extern*/ const msrn::ReactModuleProvider &GetHttpModuleProvider() noexcept { + return s_moduleProvider; +} + } // namespace Microsoft::React diff --git a/vnext/Shared/Modules/WebSocketModule.cpp b/vnext/Shared/Modules/WebSocketModule.cpp index f53b62e5321..5d572d84e46 100644 --- a/vnext/Shared/Modules/WebSocketModule.cpp +++ b/vnext/Shared/Modules/WebSocketModule.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -50,6 +51,9 @@ using Microsoft::React::Modules::SendEvent; using Microsoft::React::Networking::IWebSocketResource; constexpr char s_moduleName[] = "WebSocketModule"; +constexpr wchar_t s_moduleNameW[] = L"WebSocketModule"; + +msrn::ReactModuleProvider s_moduleProvider = msrn::MakeTurboModuleProvider(); static shared_ptr GetOrCreateWebSocket(int64_t id, string &&url, weak_ptr weakState) { @@ -495,4 +499,12 @@ void WebSocketTurboModule::RemoveListeners(double /*count*/) noexcept {} return nullptr; } +/*extern*/ const wchar_t *GetWebSocketTurboModuleName() noexcept { + return s_moduleNameW; +} + +/*extern*/ const msrn::ReactModuleProvider &GetWebSocketModuleProvider() noexcept { + return s_moduleProvider; +} + } // namespace Microsoft::React