From 70ed4556d3283c296df67a8a9cc865ed73dd801f Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Fri, 15 May 2020 12:39:59 -0700 Subject: [PATCH 1/5] Fixes for bundle loading and back handler crash using xaml islands --- vnext/Microsoft.ReactNative/Pch/pch.h | 5 +++++ .../Microsoft.ReactNative/Views/ReactRootControl.cpp | 9 +++++++++ vnext/ReactUWP/Utils/Helpers.cpp | 12 ++++++++++++ vnext/ReactUWP/Utils/LocalBundleReader.cpp | 11 +++++++++-- vnext/include/ReactUWP/Utils/Helpers.h | 2 ++ 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Pch/pch.h b/vnext/Microsoft.ReactNative/Pch/pch.h index c339ff1e823..f2af80ec3d9 100644 --- a/vnext/Microsoft.ReactNative/Pch/pch.h +++ b/vnext/Microsoft.ReactNative/Pch/pch.h @@ -15,11 +15,16 @@ #define NOGDI #endif +#undef WINAPI_FAMILY +#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP + #include #include #include #include #include +// When WINAPI_FAMILY is DESKTOP_APP, windows.h create a macro for GetCurrentTime, which conflicts with other headers +#undef GetCurrentTime #include "CppWinRTIncludes.h" #include "HResult.h" diff --git a/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp index ef076ed36f5..77c01500795 100644 --- a/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp +++ b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -617,6 +618,14 @@ void ReactRootControl::ReloadViewHost() noexcept { } void ReactRootControl::AttachBackHandlers(XamlView const &rootView) noexcept { + /* + * If we are running in a Xaml Island or some other environment where the SystemNavigationManager is unavailable, + * we should just skip hooking up the BackButton handler. SystemNavigationManager->GetForCurrentView seems to + * crash with XamlIslands so we can't just bail if that call fails. + */ + if (react::uwp::IsXamlIsland()) + return; + auto weakThis = weak_from_this(); m_backRequestedRevoker = winrt::Windows::UI::Core::SystemNavigationManager::GetForCurrentView().BackRequested( winrt::auto_revoke, diff --git a/vnext/ReactUWP/Utils/Helpers.cpp b/vnext/ReactUWP/Utils/Helpers.cpp index c169f301d8a..e5955a16493 100644 --- a/vnext/ReactUWP/Utils/Helpers.cpp +++ b/vnext/ReactUWP/Utils/Helpers.cpp @@ -8,6 +8,9 @@ #include #include +#include +#include + namespace winrt { using namespace xaml::Controls::Primitives; using namespace xaml::Media; @@ -91,5 +94,14 @@ bool Is19H1OrHigher() { return IsAPIContractV8Available(); } +bool IsXamlIsland() { + AppPolicyWindowingModel e; + if (FAILED(AppPolicyGetWindowingModel(GetCurrentThreadEffectiveToken(), &e)) || + e == AppPolicyWindowingModel_ClassicDesktop) { + return true; + } + return false; +} + } // namespace uwp }; // namespace react diff --git a/vnext/ReactUWP/Utils/LocalBundleReader.cpp b/vnext/ReactUWP/Utils/LocalBundleReader.cpp index 305d80bad9c..90421dd4632 100644 --- a/vnext/ReactUWP/Utils/LocalBundleReader.cpp +++ b/vnext/ReactUWP/Utils/LocalBundleReader.cpp @@ -18,11 +18,18 @@ namespace uwp { std::future LocalBundleReader::LoadBundleAsync(const std::string &bundleUri) { winrt::hstring str(Microsoft::Common::Unicode::Utf8ToUtf16(bundleUri)); - winrt::Windows::Foundation::Uri uri(str); co_await winrt::resume_background(); - auto file = co_await winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri); + winrt::Windows::Storage::StorageFile file{nullptr}; + + // Supports "ms-appx://" or "ms-appdata://" + if (bundleUri._Starts_with("ms-app")) { + winrt::Windows::Foundation::Uri uri(str); + file = co_await winrt::Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(uri); + } else { + file = co_await winrt::Windows::Storage::StorageFile::GetFileFromPathAsync(str); + } // Read the buffer manually to avoid a Utf8 -> Utf16 -> Utf8 encoding // roundtrip. diff --git a/vnext/include/ReactUWP/Utils/Helpers.h b/vnext/include/ReactUWP/Utils/Helpers.h index c7750034230..3c984a6410c 100644 --- a/vnext/include/ReactUWP/Utils/Helpers.h +++ b/vnext/include/ReactUWP/Utils/Helpers.h @@ -29,5 +29,7 @@ bool IsRS3OrHigher(); bool IsRS4OrHigher(); bool IsRS5OrHigher(); bool Is19H1OrHigher(); + +bool IsXamlIsland(); } // namespace uwp } // namespace react From 738db4ce9c0464ed5a12e802df67072bb69572bb Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Fri, 15 May 2020 12:40:10 -0700 Subject: [PATCH 2/5] Change files --- ...native-windows-2020-05-15-12-40-09-fixbackhandler.json | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 change/react-native-windows-2020-05-15-12-40-09-fixbackhandler.json diff --git a/change/react-native-windows-2020-05-15-12-40-09-fixbackhandler.json b/change/react-native-windows-2020-05-15-12-40-09-fixbackhandler.json new file mode 100644 index 00000000000..e218566888a --- /dev/null +++ b/change/react-native-windows-2020-05-15-12-40-09-fixbackhandler.json @@ -0,0 +1,8 @@ +{ + "type": "prerelease", + "comment": "Fixes for bundle loading and back handler crash using xaml islands", + "packageName": "react-native-windows", + "email": "acoates@microsoft.com", + "dependentChangeType": "patch", + "date": "2020-05-15T19:40:09.796Z" +} From f2c4e5fb36c34b9d914836531c05683ca8087866 Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Fri, 15 May 2020 12:48:31 -0700 Subject: [PATCH 3/5] formatting --- vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp index 77c01500795..b094e1388c9 100644 --- a/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp +++ b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp @@ -625,7 +625,7 @@ void ReactRootControl::AttachBackHandlers(XamlView const &rootView) noexcept { */ if (react::uwp::IsXamlIsland()) return; - + auto weakThis = weak_from_this(); m_backRequestedRevoker = winrt::Windows::UI::Core::SystemNavigationManager::GetForCurrentView().BackRequested( winrt::auto_revoke, From c6b905ab1f116f74f7d8d4755b986d61beef093b Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Fri, 15 May 2020 12:49:41 -0700 Subject: [PATCH 4/5] missed a change --- vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj index 8586d47ef81..1981e4c56ce 100644 --- a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +++ b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj @@ -165,7 +165,7 @@ true - winsqlite3.lib;ChakraRT.lib;dxguid.lib;dloadhelper.lib;%(AdditionalDependencies) + winsqlite3.lib;ChakraRT.lib;dxguid.lib;dloadhelper.lib;OneCoreUap.lib;%(AdditionalDependencies) winsqlite3.dll;%(DelayLoadDLLs) Console true From 285542f81d5fee7d28cdd709e951a7e776191fb8 Mon Sep 17 00:00:00 2001 From: "Andrew Coates (REDMOND)" Date: Fri, 15 May 2020 20:00:40 -0700 Subject: [PATCH 5/5] Build fix for ReactUWP. --- vnext/Microsoft.ReactNative/Pch/pch.h | 2 +- vnext/ReactUWP/Pch/pch.h | 5 +++++ vnext/ReactUWP/ReactUWP.vcxproj | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/vnext/Microsoft.ReactNative/Pch/pch.h b/vnext/Microsoft.ReactNative/Pch/pch.h index f2af80ec3d9..62878a3f82f 100644 --- a/vnext/Microsoft.ReactNative/Pch/pch.h +++ b/vnext/Microsoft.ReactNative/Pch/pch.h @@ -23,7 +23,7 @@ #include #include #include -// When WINAPI_FAMILY is DESKTOP_APP, windows.h create a macro for GetCurrentTime, which conflicts with other headers +// When WINAPI_FAMILY is DESKTOP_APP, windows.h creates a macro for GetCurrentTime, which conflicts with other headers #undef GetCurrentTime #include "CppWinRTIncludes.h" diff --git a/vnext/ReactUWP/Pch/pch.h b/vnext/ReactUWP/Pch/pch.h index ff156c66e5a..900241d0580 100644 --- a/vnext/ReactUWP/Pch/pch.h +++ b/vnext/ReactUWP/Pch/pch.h @@ -17,11 +17,16 @@ #define NOGDI #endif +#undef WINAPI_FAMILY +#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP + #include #include #include #include #include +// When WINAPI_FAMILY is DESKTOP_APP, windows.h creates a macro for GetCurrentTime, which conflicts with other headers +#undef GetCurrentTime #include "CppWinRTIncludes.h" diff --git a/vnext/ReactUWP/ReactUWP.vcxproj b/vnext/ReactUWP/ReactUWP.vcxproj index 7045c842381..568363db725 100644 --- a/vnext/ReactUWP/ReactUWP.vcxproj +++ b/vnext/ReactUWP/ReactUWP.vcxproj @@ -131,7 +131,7 @@ false true -minpdbpathlen:256 - ChakraCore.Debugger.Protocol.lib;ChakraCore.Debugger.ProtocolHandler.lib;ChakraCore.Debugger.Service.lib;ChakraCore.lib;%(AdditionalDependencies) + ChakraCore.Debugger.Protocol.lib;ChakraCore.Debugger.ProtocolHandler.lib;ChakraCore.Debugger.Service.lib;ChakraCore.lib;OneCoreUap.lib;%(AdditionalDependencies) $(ChakraCoreLibDir);$(ChakraCoreDebugLibDir) winsqlite3.lib;dxguid.lib;dloadhelper.lib;%(AdditionalDependencies) winsqlite3.dll;%(DelayLoadDLLs)