omni/extras/PrivacySettings.h

File members: omni/extras/PrivacySettings.h

// Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//
#pragma once

#include "../../carb/InterfaceUtils.h"
#include "../../carb/settings/ISettings.h"
#include "../../carb/extras/StringSafe.h"

namespace omni
{
namespace extras
{

enum class ConsentLevel
{
    ePerformance,

    ePersonalization,

    eUsage,

    eCount,
};

class PrivacySettings
{
public:
    static constexpr const char* kVersionKey = "/privacy/version";

    static constexpr const char* kPerformanceKey = "/privacy/performance";

    static constexpr const char* kPersonalizationKey = "/privacy/personalization";

    static constexpr const char* kUsageKey = "/privacy/usage";

    static constexpr const char* kUserIdKey = "/privacy/userId";

    static constexpr const char* kEmailKey = "/privacy/email";

    static constexpr const char* kExternalBuildKey = "/privacy/externalBuild";

    static constexpr const char* kExtraDiagnosticDataOptInKey = "/privacy/extraDiagnosticDataOptIn";

    static constexpr const char* kIdpNameKey = "/privacy/idpName";

    static constexpr const char* kIdpIdKey = "/privacy/idpId";

    static constexpr const char* kSettingTree = "/privacy";
    static const char* getVersion()
    {
        return _getSettingStringValue(kVersionKey, "1.0");
    }

    static const char* getUserId()
    {
        return _getSettingStringValue(kUserIdKey, "");
    }

    static const char* getEmail()
    {
        return _getSettingStringValue(kEmailKey, "");
    }

    static const char* getIdpName()
    {
        return _getSettingStringValue(kIdpNameKey, "");
    }

    static const char* getIdpId()
    {
        return _getSettingStringValue(kIdpIdKey, "");
    }

    static bool getConsentLevel(ConsentLevel level)
    {
        static const char* map[] = { kPerformanceKey, kPersonalizationKey, kUsageKey };

        if (((size_t)level) >= ((size_t)ConsentLevel::eCount))
            return false;

        return _getSettingBoolValue(map[(size_t)level], false);
    }

    static bool canSendExtraDiagnosticData()
    {
        const char* optIn = _getSettingStringValue(kExtraDiagnosticDataOptInKey, "");

        return carb::extras::compareStringsNoCase(optIn, "externalBuilds") == 0;
    }

private:
    static carb::settings::ISettings* _getSettingsInterface()
    {
        return carb::getCachedInterface<carb::settings::ISettings>();
    }

    static const char* _getSettingStringValue(const char* name, const char* defaultValue)
    {
        carb::settings::ISettings* settings = _getSettingsInterface();
        const char* value;

        if (settings == nullptr || name == nullptr)
            return defaultValue;

        value = settings->getStringBuffer(name);

        if (value == nullptr)
            return defaultValue;

        return value;
    }

    static bool _getSettingBoolValue(const char* name, bool defaultValue)
    {
        carb::settings::ISettings* settings = _getSettingsInterface();

        if (settings == nullptr || !settings->isAccessibleAs(carb::dictionary::ItemType::eBool, name))
            return defaultValue;

        return settings->getAsBool(name);
    }
};

} // namespace extras
} // namespace omni