| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WindowsUpdateIntegration.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains objects related to invoking the Windows Update Agent API. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | |
| 17 | namespace wsl::windows::common { |
| 18 | // Class factory for Windows Update Agent objects. |
| 19 | struct WindowsUpdateClassFactory |
| 20 | { |
| 21 | virtual ~WindowsUpdateClassFactory() = default; |
| 22 | |
| 23 | virtual wil::com_ptr<IUpdateSession> CreateUpdateSession() const = 0; |
| 24 | |
| 25 | virtual wil::com_ptr<IUpdateCollection> CreateUpdateCollection() const = 0; |
| 26 | }; |
| 27 | |
| 28 | // Holds the context for performing a Windows Update Agent action. |
| 29 | struct WindowsUpdateContext |
| 30 | { |
| 31 | // Create a context using the default class factory and WSL product. |
| 32 | WindowsUpdateContext(); |
| 33 | |
| 34 | // Create a context using the provided class factory. |
| 35 | WindowsUpdateContext(std::unique_ptr<WindowsUpdateClassFactory> factory); |
| 36 | |
| 37 | NON_COPYABLE(WindowsUpdateContext); |
| 38 | DEFAULT_MOVABLE(WindowsUpdateContext); |
| 39 | |
| 40 | // Gets the appropriate product for the currently running WSL instance. |
| 41 | static std::wstring WslProductIdentifier(); |
| 42 | |
| 43 | // Ensures that the product is registered in with the Windows Update system. |
| 44 | // This is required to use the system for initial installs. |
| 45 | // When `reset` is true, always sets the entry to a value that will result in an install. |
| 46 | void EnsureProductRegistryEntry(bool reset = false) const; |
| 47 | |
| 48 | // Searches for updates for the product. |
| 49 | // Returns the number of updates found. |
| 50 | size_t SearchForUpdates(); |
| 51 | |
| 52 | // Gets the number of updates found by `SearchForUpdates`. |
| 53 | size_t GetUpdateCount() const; |
| 54 | |
| 55 | // Downloads any updates that are not yet downloaded. |
| 56 | // Calls the progress callback, if provided, with the overall download progress estimate. |
| 57 | void DownloadUpdates(const std::function<void(uint32_t)>& progress = {}) const; |
| 58 | |
| 59 | // Installs any updates that were found. |
| 60 | // Calls the progress callback, if provided, with the overall install progress estimate. |
| 61 | void InstallUpdates(const std::function<void(uint32_t)>& progress = {}) const; |
| 62 | |
| 63 | static constexpr uint32_t DownloadProgressPercent = 70; |
| 64 | static constexpr uint32_t InstallProgressPercent = 30; |
| 65 | |
| 66 | enum class UpdateOptions |
| 67 | { |
| 68 | None, |
| 69 | EnsureProductRegistration, |
| 70 | ResetProductRegistration, |
| 71 | }; |
| 72 | |
| 73 | // Performs a complete update flow. This is a convenience method to remove the need to call and coordinate the individual |
| 74 | // actions. Calls the progress callback, if provided, with the overall update progress estimate. |
| 75 | // Download and install phases are split according to the values defined above. |
| 76 | void RunUpdateFlow(UpdateOptions options = UpdateOptions::EnsureProductRegistration, const std::function<void(uint32_t)>& progress = {}); |
| 77 | |
| 78 | private: |
| 79 | using ActivityType = TraceLoggingActivity<g_hTraceLoggingProvider, MICROSOFT_KEYWORD_MEASURES>; |
| 80 | |
| 81 | std::unique_ptr<WindowsUpdateClassFactory> m_factory; |
| 82 | std::wstring m_product; |
| 83 | wil::com_ptr<IUpdateSession> m_session; |
| 84 | wil::com_ptr<IUpdateSearcher> m_searcher; |
| 85 | wil::com_ptr<IUpdateCollection> m_updates; |
| 86 | std::unique_ptr<ActivityType> m_activity; |
| 87 | }; |
| 88 | } // namespace wsl::windows::common |