Skip to content

Commit 108def9

Browse files
committed
Return empty ManifestLanguages list for unpackaged apps
Check if valid language tag is passed to PrimaryLanguageOverride
1 parent e3b424e commit 108def9

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/ApplicationLanguages.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
#include <AppModel.Identity.h>
1111

12+
#include "Helper.h"
13+
1214
namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
1315
{
1416
hstring ApplicationLanguages::m_language;
@@ -20,7 +22,14 @@ namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
2022

2123
winrt::Windows::Foundation::Collections::IVectorView<hstring> ApplicationLanguages::ManifestLanguages()
2224
{
23-
return winrt::Windows::Globalization::ApplicationLanguages::ManifestLanguages();
25+
if (AppModel::Identity::IsPackagedProcess())
26+
{
27+
return winrt::Windows::Globalization::ApplicationLanguages::ManifestLanguages();
28+
}
29+
else
30+
{
31+
return {};
32+
}
2433
}
2534

2635
hstring ApplicationLanguages::PrimaryLanguageOverride()
@@ -33,16 +42,18 @@ namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation
3342

3443
void ApplicationLanguages::PrimaryLanguageOverride(hstring const& language)
3544
{
45+
bool isValidLanguageTag = IsWellFormedLanguageTag(language.c_str());
46+
47+
THROW_HR_IF_MSG(E_INVALIDARG, isValidLanguageTag, "The parameter is incorrect");
48+
49+
static wil::srwlock lock;
50+
51+
auto criticalSection {lock.lock_exclusive()};
52+
m_language = language;
53+
3654
if (AppModel::Identity::IsPackagedProcess())
3755
{
3856
winrt::Windows::Globalization::ApplicationLanguages::PrimaryLanguageOverride(language);
3957
}
40-
else
41-
{
42-
static wil::srwlock lock;
43-
44-
auto criticalSection{ lock.lock_exclusive() };
45-
m_language = language;
46-
}
4758
}
4859
} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::implementation

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/ApplicationLanguages.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ namespace winrt::Microsoft::Windows::ApplicationModel::Resources::factory_implem
2525
struct ApplicationLanguages : ApplicationLanguagesT<ApplicationLanguages, implementation::ApplicationLanguages>
2626
{
2727
};
28-
} // winrt::Microsoft::Windows::ApplicationModel::Resources::factory_implementation
28+
} // namespace winrt::Microsoft::Windows::ApplicationModel::Resources::factory_implementation

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/Helper.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,31 @@ HRESULT GetDefaultPriFile(winrt::hstring& filePath)
8686
bool isPackaged = (hr != HRESULT_FROM_WIN32(APPMODEL_ERROR_NO_PACKAGE));
8787
return GetDefaultPriFileForCurentModule(isPackaged, filePath);
8888
}
89+
90+
bool IsWellFormedLanguageTag(const wchar_t* languageTag)
91+
{
92+
// Implementation taken from Platform.h, without accepting semi-colon, as it must be a single language tag
93+
if (languageTag == nullptr || *languageTag == L'\0')
94+
{
95+
return FALSE;
96+
}
97+
98+
BOOLEAN ok = TRUE;
99+
PCWSTR test = languageTag;
100+
101+
while (ok && (*test != L'\0'))
102+
{
103+
// we accept letters, numbers, dash and semi-colon (as list separator)
104+
if (((*test >= L'0') && (*test <= L'9')) || ((*test >= L'a') && (*test <= L'z')) || ((*test >= L'A') && (*test <= L'Z')) ||
105+
(*test == L'-'))
106+
{
107+
test++;
108+
}
109+
else
110+
{
111+
ok = FALSE;
112+
}
113+
}
114+
115+
return ok && ((test - languageTag) >= 2);
116+
}

dev/MRTCore/mrt/Microsoft.Windows.ApplicationModel.Resources/src/Helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
#pragma once
55
bool IsResourceNotFound(HRESULT hr);
66
HRESULT GetDefaultPriFile(winrt::hstring& path);
7+
bool IsWellFormedLanguageTag(const wchar_t* languageTag);

0 commit comments

Comments
 (0)