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

Code before:
        _ApplyJsonStubsHelper(localAppDataFragments, ignoredNamespaces);
    }

    // Search through the program data folder
    PWSTR programDataFolder;
    SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &programDataFolder);
    auto programDataFragments = std::wstring(programDataFolder) + FragmentsPath.data();
    if (std::filesystem::exists(programDataFragments))
    {
        _ApplyJsonStubsHelper(programDataFragments, 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 findAllExtensions = [&]() -> 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 };
    findAllExtensions();
    cv.wait(lock);

    for (const auto& ext : extensions)
    {
        // Only apply the stubs if the package name is not in ignored namespaces
        if (ignoredNamespaces.find(ext.Package().Id().FamilyName().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 findPublicFolder = [&]() -> winrt::fire_and_forget {
                co_await resume_background();
                const auto localFolder = ext.GetPublicFolderAsync().get();
                std::unique_lock<std::mutex> lock{ mtx2 };

Review comment (suggestion):
~~In the future, will we need to filter these extensions by the ones that are specifically `com.microsoft.windows.terminal.settings` extensions? Like if there are multiple `appExtensionHost`s for a single package, does~~

nevermind me, `AppExtensionCatalog::Open(AppExtensionHostName)` does what I'm expecting.

Code after:
                    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))
    {
        _ApplyJsonStubsHelper(programDataFragments, 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));