| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | Session.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains the definition of the WinRT wrapper for the WSLC SDK Session class. |
| 12 | --*/ |
| 13 | |
| 14 | #pragma once |
| 15 | #include "Microsoft.WSL.Containers.Session.g.h" |
| 16 | #include "Helpers.h" |
| 17 | |
| 18 | namespace winrt::Microsoft::WSL::Containers::implementation { |
| 19 | struct Session : SessionT<Session> |
| 20 | { |
| 21 | Session() = default; |
| 22 | Session(winrt::Microsoft::WSL::Containers::SessionSettings const& settings); |
| 23 | |
| 24 | void Start(); |
| 25 | void Terminate(); |
| 26 | winrt::Microsoft::WSL::Containers::Container CreateContainer(winrt::Microsoft::WSL::Containers::ContainerSettings const& containerSettings); |
| 27 | winrt::Microsoft::WSL::Containers::Container OpenContainer( |
| 28 | hstring const& nameOrId, winrt::Microsoft::WSL::Containers::ProcessOutputMode const& initProcessOutputMode); |
| 29 | void PullImage(winrt::Microsoft::WSL::Containers::PullImageOptions const& options); |
| 30 | winrt::Windows::Foundation::IAsyncActionWithProgress<winrt::Microsoft::WSL::Containers::ImageProgress> PullImageAsync( |
| 31 | winrt::Microsoft::WSL::Containers::PullImageOptions options); |
| 32 | void ImportImage(hstring const& path, hstring const& imageName); |
| 33 | winrt::Windows::Foundation::IAsyncActionWithProgress<winrt::Microsoft::WSL::Containers::ImageProgress> ImportImageAsync(hstring path, hstring imageName); |
| 34 | void LoadImage(hstring const& path); |
| 35 | winrt::Windows::Foundation::IAsyncActionWithProgress<winrt::Microsoft::WSL::Containers::ImageProgress> LoadImageAsync(hstring path); |
| 36 | void PushImage(winrt::Microsoft::WSL::Containers::PushImageOptions const& options); |
| 37 | winrt::Windows::Foundation::IAsyncActionWithProgress<winrt::Microsoft::WSL::Containers::ImageProgress> PushImageAsync( |
| 38 | winrt::Microsoft::WSL::Containers::PushImageOptions options); |
| 39 | void DeleteImage(hstring const& nameOrId); |
| 40 | void TagImage(winrt::Microsoft::WSL::Containers::TagImageOptions const& options); |
| 41 | void CreateVhdVolume(winrt::Microsoft::WSL::Containers::VhdOptions const& options); |
| 42 | void DeleteVhdVolume(hstring const& name); |
| 43 | winrt::Microsoft::WSL::Containers::AuthenticateResult Authenticate( |
| 44 | winrt::Windows::Foundation::Uri const& serverAddress, hstring const& username, hstring const& password); |
| 45 | winrt::Windows::Foundation::Collections::IVectorView<winrt::Microsoft::WSL::Containers::ImageInfo> GetImages(); |
| 46 | winrt::event_token Terminated(winrt::Microsoft::WSL::Containers::SessionTerminationHandler const& handler); |
| 47 | void Terminated(winrt::event_token const& token) noexcept; |
| 48 | winrt::event_token ProcessCrashed(winrt::Microsoft::WSL::Containers::ProcessCrashHandler const& handler); |
| 49 | void ProcessCrashed(winrt::event_token const& token) noexcept; |
| 50 | |
| 51 | void Close(); |
| 52 | static void final_release(std::unique_ptr<Session> self); |
| 53 | |
| 54 | WslcSession ToHandle(); |
| 55 | |
| 56 | private: |
| 57 | void EnsureStarted() const; |
| 58 | winrt::Microsoft::WSL::Containers::SessionSettings m_settings; // Only kept until Start() is called |
| 59 | |
| 60 | // Threadpool callback that raises the Terminated event once the session's termination handle is signaled. |
| 61 | static void CALLBACK OnTerminated(PTP_CALLBACK_INSTANCE instance, PVOID context, PTP_WAIT wait, TP_WAIT_RESULT waitResult) noexcept; |
| 62 | static void CALLBACK OnCrashDump(const WslcSessionCrashDumpInfo* info, PVOID context) noexcept; |
| 63 | |
| 64 | winrt::event<winrt::Microsoft::WSL::Containers::SessionTerminationHandler> m_terminatedEvent; |
| 65 | winrt::event<winrt::Microsoft::WSL::Containers::ProcessCrashHandler> m_crashDumpEvent; |
| 66 | wil::unique_any<WslcSession, decltype(&WslcReleaseSession), &WslcReleaseSession> m_session{nullptr}; |
| 67 | |
| 68 | // Bridges the one-off termination event surfaced by the SDK to the WinRT Terminated event. |
| 69 | wil::unique_handle m_terminationEvent; |
| 70 | wil::unique_threadpool_wait m_terminationWait; |
| 71 | |
| 72 | wil::unique_any<WslcCrashDumpSubscription, decltype(&WslcReleaseCrashDumpSubscription), &WslcReleaseCrashDumpSubscription> m_crashDumpSubscription; |
| 73 | }; |
| 74 | } // namespace winrt::Microsoft::WSL::Containers::implementation |
| 75 | namespace winrt::Microsoft::WSL::Containers::factory_implementation { |
| 76 | struct Session : SessionT<Session, implementation::Session> |
| 77 | { |
| 78 | }; |
| 79 | } // namespace winrt::Microsoft::WSL::Containers::factory_implementation |
| 80 | |
| 81 | DEFINE_TYPE_HELPERS(Session); |