| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | ProgressCallback.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | Implementation of a type that implements IWSLCCompatProgressCallback. |
| 12 | |
| 13 | --*/ |
| 14 | #include "precomp.h" |
| 15 | #include "ProgressCallback.h" |
| 16 | |
| 17 | using namespace std::string_view_literals; |
| 18 | |
| 19 | namespace { |
| 20 | WslcImageProgressStatus ConvertStatus(LPCSTR Status) |
| 21 | { |
| 22 | #define WSLC_STRING_TO_STATUS_MAPPING(_status_, _string_) \ |
| 23 | if (_string_##sv == Status) \ |
| 24 | { \ |
| 25 | return _status_; \ |
| 26 | } |
| 27 | |
| 28 | #define WSLC_PREFIX_TO_STATUS_MAPPING(_status_, _prefix_) \ |
| 29 | if (std::string_view{Status}.starts_with(_prefix_##sv)) \ |
| 30 | { \ |
| 31 | return _status_; \ |
| 32 | } |
| 33 | |
| 34 | if (Status) |
| 35 | { |
| 36 | // TODO: Mapping engine strings to status values seems fragile. |
| 37 | // WSLC is intentionally avoiding this kind of thing for localization of engine strings, which amounts to the same |
| 38 | // thing. If we keep this, a test should be added to explicitly validate that each status is returned properly. |
| 39 | WSLC_PREFIX_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_PULLING, "Pulling from "); |
| 40 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_PULLING, "Pulling fs layer"); |
| 41 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_WAITING, "Waiting"); |
| 42 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_DOWNLOADING, "Downloading"); |
| 43 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_COMPLETE, "Download complete"); |
| 44 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_VERIFYING, "Verifying Checksum"); |
| 45 | WSLC_PREFIX_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_VERIFYING, "Digest: "); |
| 46 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_EXTRACTING, "Extracting"); |
| 47 | WSLC_STRING_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_COMPLETE, "Pull complete"); |
| 48 | WSLC_PREFIX_TO_STATUS_MAPPING(WSLC_IMAGE_PROGRESS_STATUS_COMPLETE, "Status: "); |
| 49 | } |
| 50 | |
| 51 | WSL_LOG_DEBUG("UnknownImageProgressStatus", TraceLoggingString(Status, "status")); |
| 52 | return WSLC_IMAGE_PROGRESS_STATUS_UNKNOWN; |
| 53 | } |
| 54 | } // namespace |
| 55 | |
| 56 | ProgressCallback::ProgressCallback(WslcContainerImageProgressCallback callback, PVOID context) : |
| 57 | m_callback(callback), m_context(context) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | HRESULT STDMETHODCALLTYPE ProgressCallback::OnProgress(LPCSTR Status, LPCSTR Id, ULONGLONG Current, ULONGLONG Total) |
| 62 | { |
| 63 | if (m_callback) |
| 64 | { |
| 65 | WslcImageProgressMessage message{}; |
| 66 | |
| 67 | message.id = Id; |
| 68 | message.status = ConvertStatus(Status); |
| 69 | message.detail.currentBytes = Current; |
| 70 | message.detail.totalBytes = Total; |
| 71 | |
| 72 | return m_callback(&message, m_context); |
| 73 | } |
| 74 | |
| 75 | return S_OK; |
| 76 | } |