Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/CascadiaSettingsSerialization.cpp (C++)

Code before:
        _ApplyJsonStubsHelper(ProgramDataFolder, ignoredNamespaces);
    }

    // Search through app extensions
    // Gets the catalog of extensions with the name "com.microsoft.windows.terminal.settings"
    const auto catalog = Windows::ApplicationModel::AppExtensions::AppExtensionCatalog::Open(winrt::to_hstring(AppExtensionHostName));

    // Extracting the list of extensions from the catalog is an async operation, but since
    // we are on a UI thread - and so cannot call a blocking operation like .get() - we use
    // a mutex and condition variable
    std::condition_variable cv;
    std::mutex mtx;
    Windows::Foundation::Collections::IVectorView<Windows::ApplicationModel::AppExtensions::AppExtension> extensions;

    auto lambda = [&]() -> winrt::fire_and_forget {
        co_await resume_background();
        const auto localExtensions = catalog.FindAllAsync().get();

        std::unique_lock<std::mutex> lock{ mtx };
        extensions = localExtensions;
        cv.notify_all();
    };

    std::unique_lock<std::mutex> lock{ mtx };
    lambda();
    cv.wait(lock);

    for (auto ext : extensions)
    {
        // Only apply the stubs if the package name is not in ignored namespaces
        if (ignoredNamespaces.find(ext.Package().DisplayName().c_str()) == ignoredNamespaces.end())
        {
            // Likewise, getting the public folder from an extension is an async operation
            // So we use another mutex and condition variable
            std::condition_variable cv2;
            std::mutex mtx2;
            Windows::Storage::StorageFolder foundFolder{ nullptr };

            auto lambda2 = [&]() -> winrt::fire_and_forget {
                co_await resume_background();
                const auto localFolder = ext.GetPublicFolderAsync().get();
                std::unique_lock<std::mutex> lock{ mtx2 };
                foundFolder = localFolder;
                cv2.notify_all();
            };

            std::unique_lock<std::mutex> lock2{ mtx2 };
            lambda2();
            cv2.wait(lock2);

            // the StorageFolder class has its own methods for obtaining the files within the folder

Review comment (suggestion):
I am 90% sure that there is a `wil::wait_for_completion` function that does effectively this in a one liner.

Code after:
        else
        {
            try
            {
                auto profiles = generator->GenerateProfiles();
                for (auto& profile : profiles)
                {
                    profile.Source(generatorNamespace);

                    _allProfiles.Append(profile);
                }
            }
            CATCH_LOG_MSG("Dynamic Profile Namespace: \"%ls\"", generatorNamespace.data());
        }
    }
}

// Method Description:
// - Searches the local app data folder, global app data folder and app
//   extensions for json stubs we should use to create new profiles,
//   modify existing profiles or add new color schemes
// - If the user settings has any namespaces in the "disabledProfileSources"
//   property, we'll ensure that the corresponding folders do not get searched
void CascadiaSettings::_LoadFragmentExtensions()
{
    // First, accumulate the namespaces the user wants to ignore
    std::unordered_set<std::wstring> ignoredNamespaces;
    const auto disabledProfileSources = CascadiaSettings::_GetDisabledProfileSourcesJsonObject(_userSettings);
    if (disabledProfileSources.isArray())
    {
        for (const auto& json : disabledProfileSources)
        {
            ignoredNamespaces.emplace(JsonUtils::GetValue<std::wstring>(json));
        }
    }

    // Search through the local app data folder
    wil::unique_cotaskmem_string localAppDataFolder;
    THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &localAppDataFolder));
    auto localAppDataFragments = std::wstring(localAppDataFolder.get()) + FragmentsPath.data();

    if (std::filesystem::exists(localAppDataFragments))
    {
        _ApplyJsonStubsHelper(localAppDataFragments, ignoredNamespaces);
    }

    // Search through the program data folder
    wil::unique_cotaskmem_string programDataFolder;
    THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &programDataFolder));
    auto programDataFragments = std::wstring(programDataFolder.get()) + FragmentsPath.data();
    if (std::filesystem::exists(programDataFragments))