Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions WindowsAppRuntime.sln
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ApplicationDataTests", "tes
{B73AD907-6164-4294-88FB-F3C9C10DA1F1} = {B73AD907-6164-4294-88FB-F3C9C10DA1F1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ApplicationDataContextTestApp", "test\ApplicationData\ContextTestApp\ApplicationDataContextTestApp.vcxproj", "{9FC9537C-93C7-4824-BD51-F6837B46E785}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Windows.Storage.Projection", "dev\Projections\CS\Microsoft.Windows.Storage.Projection\Microsoft.Windows.Storage.Projection.csproj", "{A43865A2-151E-4064-B5DA-445DFF9688D9}"
ProjectSection(ProjectDependencies) = postProject
{B73AD907-6164-4294-88FB-F3C9C10DA1F1} = {B73AD907-6164-4294-88FB-F3C9C10DA1F1}
Expand Down Expand Up @@ -2197,6 +2199,24 @@ Global
{F4CBBB7A-5575-45B7-B586-2C99D2BE81A3}.Release|x64.Build.0 = Release|x64
{F4CBBB7A-5575-45B7-B586-2C99D2BE81A3}.Release|x86.ActiveCfg = Release|Win32
{F4CBBB7A-5575-45B7-B586-2C99D2BE81A3}.Release|x86.Build.0 = Release|Win32
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|Any CPU.ActiveCfg = Debug|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|Any CPU.Build.0 = Debug|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|ARM64.ActiveCfg = Debug|ARM64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|ARM64.Build.0 = Debug|ARM64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|x64.ActiveCfg = Debug|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|x64.Build.0 = Debug|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|x86.ActiveCfg = Debug|Win32
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Debug|x86.Build.0 = Debug|Win32
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|Any CPU.ActiveCfg = Release|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|Any CPU.Build.0 = Release|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|ARM64.ActiveCfg = Release|ARM64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|ARM64.Build.0 = Release|ARM64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|ARM64EC.ActiveCfg = Release|ARM64EC
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|ARM64EC.Build.0 = Release|ARM64EC
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|x64.ActiveCfg = Release|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|x64.Build.0 = Release|x64
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|x86.ActiveCfg = Release|Win32
{9FC9537C-93C7-4824-BD51-F6837B46E785}.Release|x86.Build.0 = Release|Win32
{A43865A2-151E-4064-B5DA-445DFF9688D9}.Debug|Any CPU.ActiveCfg = Debug|x64
{A43865A2-151E-4064-B5DA-445DFF9688D9}.Debug|Any CPU.Build.0 = Debug|x64
{A43865A2-151E-4064-B5DA-445DFF9688D9}.Debug|ARM64.ActiveCfg = Debug|arm64
Expand Down Expand Up @@ -3155,6 +3175,7 @@ Global
{7CF52890-56FA-47E2-84FB-68EE274324B6} = {095B9BD4-CBBD-417E-84E9-63F2C3EA2BFC}
{B6440BA7-351C-4EAB-B38E-88995085643B} = {8630F7AA-2969-4DC9-8700-9B468C1DC21D}
{F4CBBB7A-5575-45B7-B586-2C99D2BE81A3} = {B6440BA7-351C-4EAB-B38E-88995085643B}
{9FC9537C-93C7-4824-BD51-F6837B46E785} = {B6440BA7-351C-4EAB-B38E-88995085643B}
{A43865A2-151E-4064-B5DA-445DFF9688D9} = {716C26A0-E6B0-4981-8412-D14A4D410531}
{1AC589D7-FA54-470E-8640-AB2477CA844C} = {6213B1A3-E854-498F-AAFA-4CFC1E71023E}
{4567CD12-A0A7-4E65-8075-B17C09261767} = {6213B1A3-E854-498F-AAFA-4CFC1E71023E}
Expand Down
25 changes: 24 additions & 1 deletion dev/ApplicationData/ApplicationData.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Windows.Storage
{
[contractversion(2)]
[contractversion(3)]
apicontract ApplicationDataContract{};

/// Specifies the type of data store.
Expand Down Expand Up @@ -91,6 +91,29 @@ namespace Microsoft.Windows.Storage
[contract(ApplicationDataContract, 2)]
static ApplicationData GetForUnpackaged(String publisher, String product);

/// Get an instance for the current user using the process's unpackaged context.
/// @note API-set context takes precedence over resources in the process executable.
[contract(ApplicationDataContract, 3), feature(Feature_ApplicationDataUnpackagedContext)]
[method_name("GetDefaultForUnpackaged")]
static ApplicationData GetForUnpackaged();

/// Set the process's unpackaged publisher and product. Repeating the same values succeeds.
[contract(ApplicationDataContract, 3), feature(Feature_ApplicationDataUnpackagedContext)]
static void SetContextForUnpackaged(String publisher, String product);

/// Set the process's unpackaged context from resources in the process executable.
[contract(ApplicationDataContract, 3), feature(Feature_ApplicationDataUnpackagedContext)]
[method_name("SetDefaultContextForUnpackaged")]
static void SetContextForUnpackaged();

/// Get the explicitly set publisher, or an empty string if no context has been set.
[contract(ApplicationDataContract, 3), feature(Feature_ApplicationDataUnpackagedContext)]
static String PublisherContextForUnpackaged { get; };

/// Get the explicitly set product, or an empty string if no context has been set.
[contract(ApplicationDataContract, 3), feature(Feature_ApplicationDataUnpackagedContext)]
static String ProductContextForUnpackaged { get; };

/// Return true if the package family supports the machine data store.
Boolean IsMachinePathSupported { get; };

Expand Down
105 changes: 105 additions & 0 deletions dev/ApplicationData/M.W.S.ApplicationData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "UnpackagedApplicationData.h"

#include <shlobj_core.h>
#include <string_view>

#include <winrt/Windows.Management.Core.h>
#include <TerminalVelocityFeatures-ApplicationData.h>

#include "ApplicationDataTelemetry.h"

Expand All @@ -22,8 +24,42 @@ static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::Applicati
static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::SharedLocal) == static_cast<int32_t>(winrt::Windows::Storage::ApplicationDataLocality::SharedLocal));
static_assert(static_cast<int32_t>(winrt::Microsoft::Windows::Storage::ApplicationDataLocality::Temporary) == static_cast<int32_t>(winrt::Windows::Storage::ApplicationDataLocality::Temporary));

namespace
{
winrt::hstring GetUnpackagedContextResource(HMODULE module, PCWSTR name)
{
const auto resource{ ::FindResourceW(module, name, L"ApplicationDataContext") };
if (!resource)
{
const auto error{ ::GetLastError() };
if ((error == ERROR_RESOURCE_DATA_NOT_FOUND) || (error == ERROR_RESOURCE_TYPE_NOT_FOUND) ||
(error == ERROR_RESOURCE_NAME_NOT_FOUND) || (error == ERROR_RESOURCE_LANG_NOT_FOUND))
{
return {};
}
THROW_WIN32_MSG(error, "%ls", name);
}

const auto size{ ::SizeofResource(module, resource) };
THROW_HR_IF_MSG(E_UNEXPECTED, size < sizeof(wchar_t), "%ls", name);
THROW_HR_IF_MSG(E_UNEXPECTED, (size % sizeof(wchar_t)) != 0, "%ls", name);
const auto loadedResource{ ::LoadResource(module, resource) };
THROW_LAST_ERROR_IF_MSG(!loadedResource, "%ls", name);
const auto data{ static_cast<PCWSTR>(::LockResource(loadedResource)) };
THROW_WIN32_IF_MSG(ERROR_RESOURCE_DATA_NOT_FOUND, !data, "%ls", name);

const std::wstring_view value{ data, size / sizeof(wchar_t) };
THROW_HR_IF_MSG(E_UNEXPECTED, value.back() != L'\0', "%ls", name);
return winrt::hstring{ data };
}
}

namespace winrt::Microsoft::Windows::Storage::implementation
{
std::mutex ApplicationData::s_unpackagedContextLock;
winrt::hstring ApplicationData::s_unpackagedPublisher;
winrt::hstring ApplicationData::s_unpackagedProduct;

ApplicationData::ApplicationData(hstring const& packageFamilyName) :
m_applicationData(nullptr),
m_packageFamilyName(packageFamilyName)
Expand Down Expand Up @@ -107,6 +143,75 @@ namespace winrt::Microsoft::Windows::Storage::implementation

return winrt::make<winrt::Microsoft::Windows::Storage::implementation::UnpackagedApplicationData>(publisher, product);
}
winrt::Microsoft::Windows::Storage::ApplicationData ApplicationData::GetForUnpackaged()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::ApplicationData::Feature_ApplicationDataUnpackagedContext::IsEnabled());

winrt::hstring publisher;
winrt::hstring product;
{
const auto lock{ std::unique_lock<std::mutex>(s_unpackagedContextLock) };
publisher = s_unpackagedPublisher;
product = s_unpackagedProduct;
}
if (publisher.empty() || product.empty())
{
GetUnpackagedContextFromResource(publisher, product);
}
return GetForUnpackaged(publisher, product);
}
void ApplicationData::GetUnpackagedContextFromResource(winrt::hstring& publisher, winrt::hstring& product)
{
const auto module{ ::GetModuleHandleW(nullptr) };
THROW_LAST_ERROR_IF_MSG(!module, "GetModuleHandleW(nullptr)");
publisher = GetUnpackagedContextResource(module, L"ApplicationData.Publisher");
THROW_WIN32_IF_MSG(ERROR_RESOURCE_NOT_PRESENT, publisher.empty(), "ApplicationData.Publisher");
product = GetUnpackagedContextResource(module, L"ApplicationData.Product");
THROW_WIN32_IF_MSG(ERROR_RESOURCE_NOT_PRESENT, product.empty(), "ApplicationData.Product");
}
void ApplicationData::SetContextForUnpackaged()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::ApplicationData::Feature_ApplicationDataUnpackagedContext::IsEnabled());
winrt::hstring publisher;
winrt::hstring product;
GetUnpackagedContextFromResource(publisher, product);
SetContextForUnpackaged(publisher, product);
}
void ApplicationData::SetContextForUnpackaged(winrt::hstring const& publisher, winrt::hstring const& product)
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::ApplicationData::Feature_ApplicationDataUnpackagedContext::IsEnabled());
_VerifyPublisher(publisher);
_VerifyProduct(product);

const auto lock{ std::unique_lock<std::mutex>(s_unpackagedContextLock) };
THROW_WIN32_IF_MSG(ERROR_ALREADY_ASSIGNED, !s_unpackagedPublisher.empty() && (s_unpackagedPublisher != publisher),
"Publisher=%ls Requested=%ls", s_unpackagedPublisher.c_str(), publisher.c_str());
THROW_WIN32_IF_MSG(ERROR_ALREADY_ASSIGNED, !s_unpackagedProduct.empty() && (s_unpackagedProduct != product),
"Product=%ls Requested=%ls", s_unpackagedProduct.c_str(), product.c_str());
if (!s_unpackagedPublisher.empty() && !s_unpackagedProduct.empty())
{
return;
}

// Retain the module so releasing factories cannot reset the process's set-once context.
static wil::winrt_module_reference moduleReference;
auto resetPublisher{ wil::scope_exit([] { s_unpackagedPublisher.clear(); }) };
s_unpackagedPublisher = publisher;
s_unpackagedProduct = product;
resetPublisher.release();
}
winrt::hstring ApplicationData::PublisherContextForUnpackaged()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::ApplicationData::Feature_ApplicationDataUnpackagedContext::IsEnabled());
const auto lock{ std::unique_lock<std::mutex>(s_unpackagedContextLock) };
return s_unpackagedPublisher;
}
winrt::hstring ApplicationData::ProductContextForUnpackaged()
{
THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Storage::ApplicationData::Feature_ApplicationDataUnpackagedContext::IsEnabled());
const auto lock{ std::unique_lock<std::mutex>(s_unpackagedContextLock) };
return s_unpackagedProduct;
}
bool ApplicationData::IsMachinePathSupported()
{
const auto path{ _MachinePath(m_packageFamilyName) };
Expand Down
12 changes: 12 additions & 0 deletions dev/ApplicationData/M.W.S.ApplicationData.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#pragma once

#include <mutex>

#include "Microsoft.Windows.Storage.ApplicationData.g.h"

namespace winrt::Microsoft::Windows::Storage::implementation
Expand All @@ -17,6 +19,11 @@ namespace winrt::Microsoft::Windows::Storage::implementation
static winrt::Microsoft::Windows::Storage::ApplicationData GetForUser(winrt::Windows::System::User user);
static winrt::Microsoft::Windows::Storage::ApplicationData GetForPackageFamily(hstring const& packageFamilyName);
static winrt::Microsoft::Windows::Storage::ApplicationData GetForUnpackaged(hstring const& publisher, hstring const& product);
static winrt::Microsoft::Windows::Storage::ApplicationData GetForUnpackaged();
static void SetContextForUnpackaged(winrt::hstring const& publisher, winrt::hstring const& product);
static void SetContextForUnpackaged();
static winrt::hstring PublisherContextForUnpackaged();
static winrt::hstring ProductContextForUnpackaged();
bool IsMachinePathSupported();
hstring LocalCachePath();
hstring LocalPath();
Expand All @@ -42,8 +49,13 @@ namespace winrt::Microsoft::Windows::Storage::implementation
static hstring StorageFolderToPath(winrt::Windows::Storage::StorageFolder storageFolder);
static void _VerifyPublisher(winrt::hstring const& string);
static void _VerifyProduct(winrt::hstring const& string);
static void GetUnpackagedContextFromResource(winrt::hstring& publisher, winrt::hstring& product);

private:
static std::mutex s_unpackagedContextLock;
static winrt::hstring s_unpackagedPublisher;
static winrt::hstring s_unpackagedProduct;

winrt::Windows::Storage::ApplicationData m_applicationData;
winrt::hstring m_packageFamilyName;
};
Expand Down
10 changes: 9 additions & 1 deletion dev/Common/TerminalVelocityFeatures-ApplicationData.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

// THIS FILE IS AUTOMATICALLY GENERATED; DO NOT EDIT IT
Expand All @@ -10,11 +10,13 @@
namespace features
{
feature_name Feature_ApplicationData = { DisabledByDefault, FALSE };
feature_name Feature_ApplicationDataUnpackagedContext = { DisabledByDefault, FALSE };
}
#endif // defined(__midlrt)

// Feature constants
#define WINDOWSAPPRUNTIME_MICROSOFT_WINDOWS_STORAGE_APPLICATIONDATA_FEATURE_APPLICATIONDATA_ENABLED 1
#define WINDOWSAPPRUNTIME_MICROSOFT_WINDOWS_STORAGE_APPLICATIONDATA_FEATURE_APPLICATIONDATAUNPACKAGEDCONTEXT_ENABLED 1

#if defined(__cplusplus)

Expand All @@ -27,6 +29,12 @@ struct Feature_ApplicationData
static constexpr bool IsEnabled() { return WINDOWSAPPRUNTIME_MICROSOFT_WINDOWS_STORAGE_APPLICATIONDATA_FEATURE_APPLICATIONDATA_ENABLED == 1; }
};

__pragma(detect_mismatch("ODR_violation_WINDOWSAPPRUNTIME_MICROSOFT_WINDOWS_STORAGE_APPLICATIONDATA_FEATURE_APPLICATIONDATAUNPACKAGEDCONTEXT_ENABLED_mismatch", "AlwaysEnabled"))
struct Feature_ApplicationDataUnpackagedContext
{
static constexpr bool IsEnabled() { return WINDOWSAPPRUNTIME_MICROSOFT_WINDOWS_STORAGE_APPLICATIONDATA_FEATURE_APPLICATIONDATAUNPACKAGEDCONTEXT_ENABLED == 1; }
};

} // namespace Microsoft.Windows.Storage.ApplicationData

#endif // defined(__cplusplus)
9 changes: 9 additions & 0 deletions dev/Common/TerminalVelocityFeatures-ApplicationData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
<channelToken>Stable</channelToken>
</alwaysDisabledChannelTokens>
</feature>
<feature>
<name>Feature_ApplicationDataUnpackagedContext</name>
<description>Process-default context for unpackaged ApplicationData</description>
<state>AlwaysEnabled</state>
<alwaysDisabledChannelTokens>
<channelToken>Preview</channelToken>
<channelToken>Stable</channelToken>
</alwaysDisabledChannelTokens>
</feature>
</features>
6 changes: 6 additions & 0 deletions test/ApplicationData/ApplicationDataTests.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
<ClCompile Include="ApplicationDataTests.cpp" />
<ClCompile Include="ApplicationDataTests_Elevated.cpp" />
<ClCompile Include="UnpackagedApplicationDataTests.cpp" />
<ClCompile Include="UnpackagedApplicationDataContextTests.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
Expand All @@ -139,6 +140,11 @@
<Copy SkipUnchangedFiles="true" SourceFiles="Test.testdef" DestinationFolder="$(OutDir)" />
</Target>
<ItemGroup>
<ProjectReference Include="ContextTestApp\ApplicationDataContextTestApp.vcxproj">
<Project>{9FC9537C-93C7-4824-BD51-F6837B46E785}</Project>
<LinkLibraryDependencies>false</LinkLibraryDependencies>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="$(RepoRoot)\dev\WindowsAppRuntime_BootstrapDLL\WindowsAppRuntime_BootstrapDLL.vcxproj">
<Project>{f76b776e-86f5-48c5-8fc7-d2795ecc9746}</Project>
</ProjectReference>
Expand Down
3 changes: 3 additions & 0 deletions test/ApplicationData/ApplicationDataTests.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<ClCompile Include="UnpackagedApplicationDataTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="UnpackagedApplicationDataContextTests.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

#include <winres.h>

LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL

ApplicationData.Publisher ApplicationDataContext
BEGIN
L"TestApplicationDataContext_ResourcePublisher\0"
END

ApplicationData.Product ApplicationDataContext
BEGIN
L"ResourceProduct\0"
END
Loading