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" +} diff --git a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj index 5fa4e8c16cd..234c8d355fd 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 diff --git a/vnext/Microsoft.ReactNative/Pch/pch.h b/vnext/Microsoft.ReactNative/Pch/pch.h index c339ff1e823..62878a3f82f 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 creates 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..b094e1388c9 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/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/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