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 62e32653543..56ca6846dd9 100644 --- a/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +++ b/vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj @@ -144,7 +144,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/Views/ReactRootControl.cpp b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp index a6d02ea5bfc..4ffa80aafc9 100644 --- a/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp +++ b/vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -623,6 +624,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/Microsoft.ReactNative/pch.h b/vnext/Microsoft.ReactNative/pch.h index d322bc57ab9..08e15416c59 100644 --- a/vnext/Microsoft.ReactNative/pch.h +++ b/vnext/Microsoft.ReactNative/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 #include diff --git a/vnext/ReactUWP/Pch/pch.h b/vnext/ReactUWP/Pch/pch.h index 18dcbdfbcab..e3e6e6708fc 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 #include diff --git a/vnext/ReactUWP/Utils/Helpers.cpp b/vnext/ReactUWP/Utils/Helpers.cpp index 7877ac62f2a..01d7c20591c 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 Windows::UI::Xaml::Controls::Primitives; using namespace Windows::UI::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 ad12e1a9ee6..3770674621e 100644 --- a/vnext/include/ReactUWP/Utils/Helpers.h +++ b/vnext/include/ReactUWP/Utils/Helpers.h @@ -34,5 +34,7 @@ bool IsRS3OrHigher(); bool IsRS4OrHigher(); bool IsRS5OrHigher(); bool Is19H1OrHigher(); + +bool IsXamlIsland(); } // namespace uwp } // namespace react