0.2.0-dev Download ZIP
80 lines ·2.6 KB Raw
| 1 | // Copyright (c) 2017-2026 The Khronos Group Inc. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 OR MIT |
| 4 | // |
| 5 | |
| 6 | #include "loader_properties.hpp" |
| 7 | #include <platform_utils.hpp> |
| 8 | |
| 9 | #include <string> |
| 10 | #include <unordered_map> |
| 11 | #include <mutex> |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | std::mutex& GetOverridePropertiesMutex() { |
| 16 | static std::mutex override_properties_mutex; |
| 17 | return override_properties_mutex; |
| 18 | } |
| 19 | |
| 20 | std::unordered_map<std::string, std::string>& GetOverrideProperties() { |
| 21 | static std::unordered_map<std::string, std::string> override_properties; |
| 22 | return override_properties; |
| 23 | } |
| 24 | |
| 25 | const std::string* TryGetPropertyOverride(const std::string& name) { |
| 26 | const auto& overrideProperties = GetOverrideProperties(); |
| 27 | const auto& overrideProperty = overrideProperties.find(name); |
| 28 | if (overrideProperty != overrideProperties.end()) { |
| 29 | return &overrideProperty->second; |
| 30 | } |
| 31 | return nullptr; |
| 32 | } |
| 33 | |
| 34 | } // namespace |
| 35 | |
| 36 | // Loader property overrides take precedence over system environment variables because environment variables are not always |
| 37 | // safe to use (and thus would be ignored). For example, override properties may be the only way to redirect XR_RUNTIME_JSON |
| 38 | // from an elevated process on Windows. |
| 39 | |
| 40 | namespace LoaderProperty { |
| 41 | |
| 42 | std::string Get(const std::string& name) { |
| 43 | std::scoped_lock<std::mutex> lock(GetOverridePropertiesMutex()); |
| 44 | const std::string* propertyOverride = TryGetPropertyOverride(name); |
| 45 | if (propertyOverride != nullptr) { |
| 46 | return *propertyOverride; |
| 47 | } else { |
| 48 | return PlatformUtilsGetEnv(name.c_str()); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | std::string GetSecure(const std::string& name) { |
| 53 | std::scoped_lock<std::mutex> lock(GetOverridePropertiesMutex()); |
| 54 | const std::string* propertyOverride = TryGetPropertyOverride(name); |
| 55 | if (propertyOverride != nullptr) { |
| 56 | return *propertyOverride; |
| 57 | } else { |
| 58 | return PlatformUtilsGetSecureEnv(name.c_str()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool IsSet(const std::string& name) { |
| 63 | std::scoped_lock<std::mutex> lock(GetOverridePropertiesMutex()); |
| 64 | const std::string* propertyOverride = TryGetPropertyOverride(name); |
| 65 | return propertyOverride != nullptr || PlatformUtilsGetEnvSet(name.c_str()); |
| 66 | } |
| 67 | |
| 68 | void SetOverride(std::string name, std::string value) { |
| 69 | std::scoped_lock<std::mutex> lock(GetOverridePropertiesMutex()); |
| 70 | auto& overrideProperties = GetOverrideProperties(); |
| 71 | overrideProperties.insert(std::make_pair(std::move(name), std::move(value))); |
| 72 | } |
| 73 | |
| 74 | void ClearOverrides() { |
| 75 | std::scoped_lock<std::mutex> lock(GetOverridePropertiesMutex()); |
| 76 | auto& overrideProperties = GetOverrideProperties(); |
| 77 | overrideProperties.clear(); |
| 78 | } |
| 79 | |
| 80 | } // namespace LoaderProperty |