COMContext.h (3446B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include "AppInstallerProgress.h" 5 #include "ExecutionContext.h" 6 #include "Workflows/WorkflowBase.h" 7 8 namespace AppInstaller::CLI::Execution 9 { 10 enum class ReportType : uint32_t 11 { 12 ExecutionPhaseUpdate, 13 BeginProgress, 14 Progressing, 15 EndProgress, 16 }; 17 18 class NullStreamBuf : public std::streambuf {}; 19 20 struct NullStream 21 { 22 NullStream(); 23 24 ~NullStream() = default; 25 26 protected: 27 NullStreamBuf m_nullStreamBuf; 28 std::unique_ptr<std::ostream> m_nullOut; 29 std::unique_ptr<std::istream> m_nullIn; 30 }; 31 32 typedef std::function<void(ReportType reportType, uint64_t current, uint64_t maximum, ProgressType progressType, CLI::Workflow::ExecutionStage executionPhase)> ProgressCallBackFunction; 33 34 // NullStream constructs the Stream parameters for Context constructor 35 // Hence, NullStream should always precede Context in base class order of COMContext's inheritance 36 struct COMContext : IProgressSink, NullStream, CLI::Execution::Context 37 { 38 // When no Console streams need involvement, construct NullStreams instead to pass to Context 39 COMContext() : NullStream(), CLI::Execution::Context(*m_nullOut, *m_nullIn) 40 { 41 Reporter.SetChannel(Reporter::Channel::Disabled); 42 Reporter.SetProgressSink(this); 43 SetFlags(CLI::Execution::ContextFlag::DisableInteractivity); 44 } 45 46 COMContext(std::ostream& out, std::istream& in) : CLI::Execution::Context(out, in) 47 { 48 Reporter.SetProgressSink(this); 49 SetFlags(CLI::Execution::ContextFlag::DisableInteractivity); 50 } 51 52 ~COMContext() = default; 53 54 // IProgressSink 55 void BeginProgress() override; 56 void OnProgress(uint64_t current, uint64_t maximum, ProgressType type) override; 57 void SetProgressMessage(std::string_view message) override; 58 void EndProgress(bool) override; 59 60 //Execution::Context 61 void SetExecutionStage(CLI::Workflow::ExecutionStage executionPhase); 62 63 CLI::Workflow::ExecutionStage GetExecutionStage() const { return m_executionStage; } 64 65 void AddProgressCallbackFunction(ProgressCallBackFunction&& f); 66 67 // Set Diagnostic and Telemetry loggers, Wil failure callback 68 // This should be called only once per COM Server instance 69 static void SetLoggers(std::optional<AppInstaller::Logging::Channel> channel = std::nullopt, std::optional<AppInstaller::Logging::Level> level = std::nullopt); 70 71 // Set COM call context for diagnostic and telemetry loggers 72 // This should be called for every COMContext object instance 73 void SetContextLoggers(const std::wstring_view telemetryCorrelationJson, const std::string& caller); 74 75 std::wstring_view GetCorrelationJson(); 76 77 private: 78 void FireCallbacks(ReportType reportType, uint64_t current, uint64_t maximum, ProgressType progressType, ::AppInstaller::CLI::Workflow::ExecutionStage executionPhase); 79 80 CLI::Workflow::ExecutionStage m_executionStage = CLI::Workflow::ExecutionStage::Initial; 81 std::vector<ProgressCallBackFunction> m_comProgressCallbacks; 82 std::wstring m_correlationData = L""; 83 std::mutex m_callbackLock; 84 }; 85 }