Skip to content

Commit

Permalink
Fixes for bundle loading and back handler crash using xaml islands (#…
Browse files Browse the repository at this point in the history
…4919)

* Fixes for bundle loading and back handler crash using xaml islands

* Change files

* formatting

* missed a change

* Build fix for ReactUWP.
  • Loading branch information
acoates-ms authored and NickGerleman committed May 19, 2020
1 parent 82d78b9 commit 38eb1f7
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
@@ -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"
}
2 changes: 1 addition & 1 deletion vnext/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj
Expand Up @@ -144,7 +144,7 @@
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile>
<Link>
<AdditionalDependencies>winsqlite3.lib;ChakraRT.lib;dxguid.lib;dloadhelper.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>winsqlite3.lib;ChakraRT.lib;dxguid.lib;dloadhelper.lib;OneCoreUap.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>winsqlite3.dll;%(DelayLoadDLLs)</DelayLoadDLLs>
<SubSystem>Console</SubSystem>
<GenerateWindowsMetadata>true</GenerateWindowsMetadata>
Expand Down
9 changes: 9 additions & 0 deletions vnext/Microsoft.ReactNative/Views/ReactRootControl.cpp
Expand Up @@ -34,6 +34,7 @@
#include <object/unknownObject.h>

#include <ReactHost/MsoUtils.h>
#include <Utils/Helpers.h>

#include <winrt/Windows.UI.Xaml.Controls.Primitives.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions vnext/Microsoft.ReactNative/pch.h
Expand Up @@ -15,11 +15,16 @@
#define NOGDI
#endif

#undef WINAPI_FAMILY
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP

#include <combaseapi.h>
#include <guiddef.h>
#include <intrin.h>
#include <unknwn.h>
#include <windows.h>
// When WINAPI_FAMILY is DESKTOP_APP, windows.h creates a macro for GetCurrentTime, which conflicts with other headers
#undef GetCurrentTime

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
Expand Down
5 changes: 5 additions & 0 deletions vnext/ReactUWP/Pch/pch.h
Expand Up @@ -17,11 +17,16 @@
#define NOGDI
#endif

#undef WINAPI_FAMILY
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP

#include <combaseapi.h>
#include <guiddef.h>
#include <intrin.h>
#include <unknwn.h>
#include <windows.h>
// When WINAPI_FAMILY is DESKTOP_APP, windows.h creates a macro for GetCurrentTime, which conflicts with other headers
#undef GetCurrentTime

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.h>
Expand Down
12 changes: 12 additions & 0 deletions vnext/ReactUWP/Utils/Helpers.cpp
Expand Up @@ -8,6 +8,9 @@
#include <winrt/Windows.Foundation.Metadata.h>
#include <winrt/Windows.UI.Xaml.Media.h>

#include <appmodel.h>
#include <processthreadsapi.h>

namespace winrt {
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Media;
Expand Down Expand Up @@ -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
11 changes: 9 additions & 2 deletions vnext/ReactUWP/Utils/LocalBundleReader.cpp
Expand Up @@ -18,11 +18,18 @@ namespace uwp {

std::future<std::string> 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.
Expand Down
2 changes: 2 additions & 0 deletions vnext/include/ReactUWP/Utils/Helpers.h
Expand Up @@ -34,5 +34,7 @@ bool IsRS3OrHigher();
bool IsRS4OrHigher();
bool IsRS5OrHigher();
bool Is19H1OrHigher();

bool IsXamlIsland();
} // namespace uwp
} // namespace react

0 comments on commit 38eb1f7

Please sign in to comment.