ConfigurationEnvironment.cpp (5676B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "ConfigurationEnvironment.h" 5 #include "ConfigurationEnvironment.g.cpp" 6 #include "ArgumentValidation.h" 7 8 namespace winrt::Microsoft::Management::Configuration::implementation 9 { 10 namespace 11 { 12 template <typename T> 13 void DeepCopyEnvironmentFrom(implementation::ConfigurationEnvironment& self, const T& toDeepCopy) 14 { 15 self.Context(toDeepCopy.Context()); 16 self.ProcessorIdentifier(toDeepCopy.ProcessorIdentifier()); 17 self.ProcessorProperties(toDeepCopy.ProcessorProperties()); 18 } 19 } 20 21 ConfigurationEnvironment::ConfigurationEnvironment() : 22 m_processorProperties(multi_threaded_map<hstring, hstring>()) 23 {} 24 25 ConfigurationEnvironment::ConfigurationEnvironment(SecurityContext context, const std::wstring& processorIdentifier, std::map<hstring, hstring>&& processorProperties) : 26 m_context(context), m_processorIdentifier(processorIdentifier), m_processorProperties(multi_threaded_map(std::move(processorProperties))) 27 {} 28 29 ConfigurationEnvironment::ConfigurationEnvironment(const implementation::ConfigurationEnvironment& toDeepCopy) 30 { 31 DeepCopy(toDeepCopy); 32 } 33 34 ConfigurationEnvironment::ConfigurationEnvironment(const Configuration::ConfigurationEnvironment& toDeepCopy) 35 { 36 DeepCopyEnvironmentFrom(*this, toDeepCopy); 37 } 38 39 SecurityContext ConfigurationEnvironment::Context() const 40 { 41 return m_context; 42 } 43 44 void ConfigurationEnvironment::Context(SecurityContext value) 45 { 46 m_context = value; 47 } 48 49 hstring ConfigurationEnvironment::ProcessorIdentifier() const 50 { 51 return m_processorIdentifier; 52 } 53 54 void ConfigurationEnvironment::ProcessorIdentifier(const hstring& value) 55 { 56 m_processorIdentifier = value; 57 } 58 59 Windows::Foundation::Collections::IMap<hstring, hstring> ConfigurationEnvironment::ProcessorProperties() const 60 { 61 return m_processorProperties; 62 } 63 64 void ConfigurationEnvironment::DeepCopy(const implementation::ConfigurationEnvironment& toDeepCopy) 65 { 66 DeepCopyEnvironmentFrom(*this, toDeepCopy); 67 } 68 69 void ConfigurationEnvironment::ProcessorProperties(const Windows::Foundation::Collections::IMap<hstring, hstring>& values) 70 { 71 std::map<hstring, hstring> properties; 72 for (const auto& property : values) 73 { 74 properties.emplace(property.Key(), property.Value()); 75 } 76 m_processorProperties = multi_threaded_map(std::move(properties)); 77 } 78 79 void ConfigurationEnvironment::ProcessorProperties(const Windows::Foundation::Collections::ValueSet& values) 80 { 81 std::map<hstring, hstring> properties; 82 83 for (const auto& value : values) 84 { 85 Windows::Foundation::IPropertyValue property = value.Value().try_as<Windows::Foundation::IPropertyValue>(); 86 if (property && IsStringableType(property.Type())) 87 { 88 properties.emplace(value.Key(), ToString(property)); 89 } 90 } 91 92 m_processorProperties = multi_threaded_map(std::move(properties)); 93 } 94 95 bool ConfigurationEnvironment::IsDefault() const 96 { 97 return m_context == SecurityContext::Current && m_processorIdentifier.empty() && m_processorProperties.Size() == 0; 98 } 99 100 com_ptr<ConfigurationEnvironment> ConfigurationEnvironment::CalculateCommonEnvironment(const std::vector<Configuration::ConfigurationEnvironment>& environments) 101 { 102 com_ptr<ConfigurationEnvironment> result; 103 104 if (environments.empty()) 105 { 106 result = make_self<ConfigurationEnvironment>(); 107 } 108 else 109 { 110 result = make_self<ConfigurationEnvironment>(environments.front()); 111 112 for (size_t i = 1; i < environments.size(); ++i) 113 { 114 const Configuration::ConfigurationEnvironment& environment = environments[i]; 115 116 if (result->m_context != environment.Context()) 117 { 118 result->m_context = SecurityContext::Current; 119 } 120 121 if (result->m_processorIdentifier != environment.ProcessorIdentifier()) 122 { 123 result->m_processorIdentifier.clear(); 124 } 125 126 if (!AreEqual(result->m_processorProperties, environment.ProcessorProperties())) 127 { 128 result->m_processorProperties = single_threaded_map<hstring, hstring>(); 129 } 130 131 // Check if we have already found everything to be different 132 if (result->IsDefault()) 133 { 134 break; 135 } 136 } 137 } 138 139 return result; 140 } 141 142 bool ConfigurationEnvironment::AreEqual(const Windows::Foundation::Collections::IMap<hstring, hstring>& a, const Windows::Foundation::Collections::IMap<hstring, hstring>& b) 143 { 144 uint32_t a_size = a ? a.Size() : 0; 145 uint32_t b_size = b ? b.Size() : 0; 146 147 if (a_size == 0 && b_size == 0) 148 { 149 return true; 150 } 151 else if (a_size != b_size) 152 { 153 return false; 154 } 155 156 for (const auto& entry : a) 157 { 158 hstring key = entry.Key(); 159 if (!b.HasKey(key) || entry.Value() != b.Lookup(key)) 160 { 161 return false; 162 } 163 } 164 165 return true; 166 } 167 }