Skip to content

Commit

Permalink
dev: full functional isolation host manager
Browse files Browse the repository at this point in the history
  • Loading branch information
seven-mile committed Sep 16, 2023
1 parent 0611897 commit de12390
Show file tree
Hide file tree
Showing 38 changed files with 393 additions and 222 deletions.
6 changes: 4 additions & 2 deletions UFCase.Host.Test/HostManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ namespace winrt::UFCase::Isolation::implementation
.c_str());
wprintf(winrt::format(L"\t{} {}\n", img.Architecture(), img.IsWinPE()).c_str());

for (auto obj : img.SxsStore().GetComponentCollection(L"HyperV-Worker-Control")) {
for (auto obj : img.SxsStore().GetComponentCollection(L"HyperV-Worker-Control"))
{
auto comp = obj.as<Isolation::ComponentModel>();
wprintf(winrt::format(L"Component: {}\n\t{}\n", comp.TextForm(), comp.PayloadPath()).c_str());
wprintf(winrt::format(L"Component: {}\n\t{}\n", comp.TextForm(), comp.PayloadPath())
.c_str());
}

host.Ping();
Expand Down
2 changes: 1 addition & 1 deletion UFCase.Host.Test/HostManagerSingleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
namespace winrt::UFCase::Isolation::implementation
{
CoCreatableCppWinRtClass(HostManagerSingleton);
} // namespace winrt::UFCase::implementation
} // namespace winrt::UFCase::Isolation::implementation
18 changes: 15 additions & 3 deletions UFCase.Host/FeatureModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace winrt::UFCase::Isolation::implementation
{
com_ptr<ICbsPackage> pPkg;
check_hresult(GetInterface()->GetPackage(pPkg.put()));
if (auto self_pkg = m_package.get())
if (auto self_pkg = m_package)
{
auto pkg = make<implementation::PackageModel>(self_pkg.Session(), pPkg);
return pkg;
Expand All @@ -113,7 +113,8 @@ namespace winrt::UFCase::Isolation::implementation
Windows::Foundation::Collections::IVector<Isolation::FeatureModel> FeatureModel::
GetParentFeatureCollection()
{
constexpr HRESULT CBS_E_ARRAY_MISSING_INDEX = 0x800F0809, CBS_E_UNKNOWN_UPDATE = 0x800F080C;
constexpr HRESULT CBS_E_ARRAY_MISSING_INDEX = 0x800F0809, CBS_E_UNKNOWN_UPDATE = 0x800F080C,
CBS_E_DUPLICATE_UPDATENAME = 0x800F0819;
auto res = single_threaded_vector<Isolation::FeatureModel>();
for (uint32_t idx = 0;; idx++)
{
Expand All @@ -130,7 +131,7 @@ namespace winrt::UFCase::Isolation::implementation

try
{
res.Append(m_package.get().OpenFeature(ws_parent_name.get()));
res.Append(m_package.OpenFeature(ws_parent_name.get()));
}
catch (hresult_error const &hr)
{
Expand All @@ -139,6 +140,17 @@ namespace winrt::UFCase::Isolation::implementation
// unknown update, ignore it
continue;
}
else if (hr.code() == CBS_E_DUPLICATE_UPDATENAME)
{
// duplicate update name, fallback to enumeration
for (auto upd : m_package.GetFeatureCollection(CbsApplicabilityApplicable,
CbsSelectabilityAllClass)) {
if (upd.Name() == ws_parent_name.get()) {
res.Append(upd);
break;
}
}
}
else
{
// rethrow
Expand Down
5 changes: 3 additions & 2 deletions UFCase.Host/FeatureModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ namespace winrt::UFCase::Isolation::implementation
{
struct FeatureModel : FeatureModelT<FeatureModel>
{
FeatureModel(Isolation::PackageModel pkg, com_ptr<ICbsUpdate> feature) : m_feature(feature)
FeatureModel(Isolation::PackageModel pkg, com_ptr<ICbsUpdate> feature)
: m_feature(feature), m_package(pkg)
{
}

Expand All @@ -30,7 +31,7 @@ namespace winrt::UFCase::Isolation::implementation

private:
com_ptr<ICbsUpdate> m_feature;
weak_ref<Isolation::PackageModel> m_package;
Isolation::PackageModel m_package;
com_ptr<ICbsUpdate> GetInterface()
{
return m_feature;
Expand Down
9 changes: 9 additions & 0 deletions UFCase.Host/SessionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace winrt::UFCase::Isolation::implementation
Initialize(option);
}

~SessionModel()
{
if (m_session)
{
_CbsRequiredAction ra;
check_hresult(m_session->Finalize(&ra));
}
}

void AddSource(hstring const &source_dir);
Windows::Foundation::IAsyncActionWithProgress<uint32_t> SaveChanges();

Expand Down
7 changes: 3 additions & 4 deletions UFCase.Host/StoreModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ namespace winrt::UFCase::Isolation::implementation
public:
Isolation::ComponentModel OpenComponent(hstring id);

Windows::Foundation::Collections::IIterable<IInspectable>
GetComponentCollection();
Windows::Foundation::Collections::IIterable<IInspectable>
GetComponentCollection(hstring ref_id);
Windows::Foundation::Collections::IIterable<IInspectable> GetComponentCollection();
Windows::Foundation::Collections::IIterable<IInspectable> GetComponentCollection(
hstring ref_id);

com_ptr<IStore2> m_sxs_store;
com_ptr<ICSIStore> m_csi_store;
Expand Down
8 changes: 7 additions & 1 deletion UFCase.Host/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ int WINAPI wWinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ LPWSTR, _In_ int)

host_mgr.RegisterHost(host);

host_mgr.UnregisterHost(host);
// process messages, for long run
MSG msg{};
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return 0;
}
2 changes: 1 addition & 1 deletion UFCase.WinMD.HostManager/HostManager.idl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace UFCase.Isolation
Windows.Foundation.IAsyncActionWithProgress<UInt32> SaveChanges();

PackageModel OpenPackage(String identity);
Windows.Foundation.Collections.IIterable<PackageModel> GetPackageCollection(
Windows.Foundation.Collections.IVector<PackageModel> GetPackageCollection(
UInt32 option
);

Expand Down
4 changes: 4 additions & 0 deletions UFCase/App.xaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "App.xaml.g.h"

#include "HostGuard.h"

namespace winrt::UFCase::implementation
{
struct App : AppT<App>
Expand All @@ -13,6 +15,8 @@ namespace winrt::UFCase::implementation
private:
UFCase::MainWindow window{nullptr};
UFCase::SplashWindow splash{nullptr};

HostGuard host_guard;
};

} // namespace winrt::UFCase::implementation
36 changes: 14 additions & 22 deletions UFCase/ComponentViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

namespace winrt::UFCase::implementation
{
ComponentViewModel::ComponentViewModel(uint64_t handle)
: m_model(ComponentModel::GetInstance(handle))
ComponentViewModel::ComponentViewModel(Isolation::ComponentModel model) : m_model(model)
{
Prefetch();
}
Expand All @@ -26,9 +25,12 @@ namespace winrt::UFCase::implementation

hstring ComponentViewModel::NameRaw()
{
if (auto name = m_model.GetAttribute(L"Name"); name.empty()) {
if (auto name = m_model.GetAttribute(L"Name"); name.empty())
{
return L"(unnamed)";
} else {
}
else
{
return name;
}
}
Expand Down Expand Up @@ -67,15 +69,15 @@ namespace winrt::UFCase::implementation
{
switch (m_model.Status())
{
case CSI_COMPONENT_STATUS_PRESTAGED:
case Isolation::CsiComponentStatus::Prestaged:
return L"Prestaged";
case CSI_COMPONENT_STATUS_STAGED:
case Isolation::CsiComponentStatus::Staged:
return L"Staged";
case CSI_COMPONENT_STATUS_PINNED:
case Isolation::CsiComponentStatus::Pinned:
return L"Pinned";
case CSI_COMPONENT_STATUS_INSTALLED_MATCH:
case Isolation::CsiComponentStatus::Installed:
return L"Installed";
case CSI_COMPONENT_STATUS_INSTALLED_MISMATCH:
case Isolation::CsiComponentStatus::InstalledMismatch:
return L"InstalledMismatch";
default:
return L"Unknown";
Expand All @@ -91,20 +93,10 @@ namespace winrt::UFCase::implementation
{
auto result = multi_threaded_observable_vector<ComponentFileViewModel>();

auto flag_to_hstring = [](DWORD file_flag) {
switch (file_flag)
{
case STORE_ASSEMBLY_FILE_STATUS_FLAG_PRESENT:
return L"Present";
default:
return L"Unknown";
}
};

for (auto &f : m_model.Files())
for (auto f : m_model.GetFileCollection(ISTORE_ENUM_FILES_FLAG_INCLUDE_INSTALLED_FILES))
{
result.Append(ComponentFileViewModel{.Name = f.pszFileName,
.Status = flag_to_hstring(f.dwFileStatusFlags)});
result.Append(ComponentFileViewModel{.Name = f.Name,
.Status = f.Status});
}

return result;
Expand Down
6 changes: 3 additions & 3 deletions UFCase/ComponentViewModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "ComponentViewModel.g.h"

#include "ComponentModel.h"
#include <winrt/UFCase.Isolation.h>

#include "CacheUtil.h"
#include "PropChgUtil.h"
Expand All @@ -12,7 +12,7 @@ namespace winrt::UFCase::implementation
struct ComponentViewModel : ComponentViewModelT<ComponentViewModel>,
ImplPropertyChangedT<ComponentViewModel>
{
ComponentViewModel(uint64_t handle);
ComponentViewModel(Isolation::ComponentModel model);

hstring TextFormRaw();
hstring KeyFormRaw();
Expand Down Expand Up @@ -47,7 +47,7 @@ namespace winrt::UFCase::implementation
// clang-format on

private:
ComponentModel m_model;
Isolation::ComponentModel m_model;

void Prefetch()
{
Expand Down
2 changes: 1 addition & 1 deletion UFCase/ComponentViewModel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace UFCase
[default_interface]
runtimeclass ComponentViewModel : Microsoft.UI.Xaml.Data.INotifyPropertyChanged
{
ComponentViewModel(UInt64 hModel);
ComponentViewModel(UFCase.Isolation.ComponentModel model);

String TextForm{ get; };
String KeyForm{ get; };
Expand Down
12 changes: 6 additions & 6 deletions UFCase/ComponentsPageViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "ComponentsPageViewModel.g.cpp"
#endif

#include "StoreModel.h"
#include "ComponentModel.h"
#include <winrt/UFCase.Isolation.h>

#include "AsyncUtil.h"
#include "CbsUtil.h"
Expand All @@ -29,18 +28,19 @@ namespace winrt::UFCase::implementation

m_components.Clear();

auto &store = StoreModel::GetInstance(m_image.Store());
auto store = m_image.Store();

auto comps = store.Components();
auto comps = store.GetComponentCollection();

// if no reference restriction, estimate 10000 components
// if there is reference restriction, this should be fast enough
// and do not need a progress bar
SIZE_T comps_count = 10000;

for (SIZE_T idx = 0; auto *comp : comps)
for (SIZE_T idx = 0; auto comp_ : comps)
{
UFCase::ComponentViewModel comp_vm{comp->GetHandle()};
auto comp = comp_.as<Isolation::ComponentModel>();
UFCase::ComponentViewModel comp_vm{comp};
RunUITask([=] { m_components.Append(comp_vm); });

++idx;
Expand Down
27 changes: 10 additions & 17 deletions UFCase/FeatureViewModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "FeatureViewModel.g.cpp"
#endif

#include "PackageModel.h"

#include <winrt/Windows.Storage.Streams.h>

#include "AsyncUtil.h"
Expand All @@ -20,7 +18,7 @@

namespace winrt::UFCase::implementation
{
FeatureViewModel::FeatureViewModel(uint64_t hModel) : m_model(FeatureModel::GetInstance(hModel))
FeatureViewModel::FeatureViewModel(Isolation::FeatureModel model) : m_model(model)
{
m_children.VectorChanged([self = get_strong()](auto const &, IVectorChangedEventArgs args) {
switch (args.CollectionChange())
Expand Down Expand Up @@ -62,21 +60,21 @@ namespace winrt::UFCase::implementation
auto state = m_model.RequestedState();
switch (state)
{
case CbsInstallStateInstallRequested:
case CbsInstallStateInstalled:
// case CbsInstallStatePermanent:
case Isolation::CbsInstallState::CbsInstallStateInstallRequested:
case Isolation::CbsInstallState::CbsInstallStateInstalled:
// case Isolation::CbsInstallState::CbsInstallStatePermanent:
if (std::optional<bool> opt = this->IsChecked(); opt.has_value())
{
assert(*opt);
return FeatureState::Enabled;
}
else
return FeatureState::PartiallyEnabled;
case CbsInstallStateUninstallRequested:
case CbsInstallStateStaged:
// case CbsInstallStateResolved:
case Isolation::CbsInstallState::CbsInstallStateUninstallRequested:
case Isolation::CbsInstallState::CbsInstallStateStaged:
// case Isolation::CbsInstallState::CbsInstallStateResolved:
return FeatureState::Disabled;
case CbsInstallStateAbsent:
case Isolation::CbsInstallState::CbsInstallStateAbsent:
return FeatureState::Unavailable;
default:
return FeatureState::Invalid;
Expand Down Expand Up @@ -124,14 +122,9 @@ namespace winrt::UFCase::implementation
return m_model.SetMembership();
}

UFCase::PackageViewModel FeatureViewModel::Package()
{
return UFCase::PackageViewModel(m_model.RawParentPackage()->GetHandle());
}

UFCase::PackageViewModel FeatureViewModel::ContentPackage()
{
return UFCase::PackageViewModel(m_model.RawFeaturePackage()->GetHandle());
return UFCase::PackageViewModel(m_model.ContentPackage());
}

FeatureViewModel::child_t FeatureViewModel::Children()
Expand All @@ -154,7 +147,7 @@ namespace winrt::UFCase::implementation
}
if (cnt < m_children.Size())
return std::optional<bool>(std::nullopt);
if (m_model.RequestedState() >= CbsInstallStateInstallRequested)
if (static_cast<DWORD>(m_model.RequestedState()) >= CbsInstallStateInstallRequested)
return true;
return false;
}
Expand Down
Loading

0 comments on commit de12390

Please sign in to comment.