| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | #pragma once |
| 4 | |
| 5 | #include "ExecutionContext.h" |
| 6 | #include "WSLCSession.h" |
| 7 | |
| 8 | namespace wsl::windows::service::wslc { |
| 9 | |
| 10 | // Extends COMServiceExecutionContext with a WSLCSession pointer for lazy COM callback |
| 11 | // registration when warnings are emitted. This enables EMIT_USER_WARNING to stream |
| 12 | // warnings back to the CLI via IWarningCallback, with proper cancellation support |
| 13 | // during session termination via RegisterUserCOMCallback/CoCancelCall. |
| 14 | class WSLCExecutionContext : public wsl::windows::common::COMServiceExecutionContext |
| 15 | { |
| 16 | public: |
| 17 | NON_COPYABLE(WSLCExecutionContext); |
| 18 | NON_MOVABLE(WSLCExecutionContext); |
| 19 | |
| 20 | WSLCExecutionContext(WSLCSession* session, IWarningCallback* warningCallback = nullptr) : |
| 21 | m_session(session), m_warningCallback(warningCallback) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ~WSLCExecutionContext() override = default; |
| 26 | |
| 27 | protected: |
| 28 | bool CollectUserWarning(const std::wstring& warning) override |
| 29 | { |
| 30 | if (m_warningCallback != nullptr) |
| 31 | { |
| 32 | std::unique_ptr<UserCOMCallback> comCallback; |
| 33 | if (m_session != nullptr) |
| 34 | { |
| 35 | comCallback = std::make_unique<UserCOMCallback>(m_session->RegisterUserCOMCallback()); |
| 36 | } |
| 37 | |
| 38 | auto hr = m_warningCallback->OnWarning(warning.c_str()); |
| 39 | if (SUCCEEDED(hr) || hr == RPC_E_CALL_CANCELED || hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) |
| 40 | { |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | LOG_HR(hr); |
| 45 | } |
| 46 | |
| 47 | return COMServiceExecutionContext::CollectUserWarning(warning); |
| 48 | } |
| 49 | |
| 50 | private: |
| 51 | WSLCSession* m_session = nullptr; |
| 52 | IWarningCallback* m_warningCallback = nullptr; |
| 53 | }; |
| 54 | |
| 55 | } // namespace wsl::windows::service::wslc |