NSProcessInfo.hpp
1 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 2 // 3 // Foundation/NSProcessInfo.hpp 4 // 5 // Copyright 2020-2024 Apple Inc. 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 // 19 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 20 21 #pragma once 22 23 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 24 25 #include "NSDefines.hpp" 26 #include "NSNotification.hpp" 27 #include "NSObject.hpp" 28 #include "NSPrivate.hpp" 29 #include "NSTypes.hpp" 30 31 #include <functional> 32 33 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 34 35 namespace NS 36 { 37 _NS_CONST(NotificationName, ProcessInfoThermalStateDidChangeNotification); 38 _NS_CONST(NotificationName, ProcessInfoPowerStateDidChangeNotification); 39 _NS_CONST(NotificationName, ProcessInfoPerformanceProfileDidChangeNotification); 40 41 _NS_ENUM(NS::Integer, ProcessInfoThermalState) { 42 ProcessInfoThermalStateNominal = 0, 43 ProcessInfoThermalStateFair = 1, 44 ProcessInfoThermalStateSerious = 2, 45 ProcessInfoThermalStateCritical = 3 46 }; 47 48 _NS_OPTIONS(std::uint64_t, ActivityOptions) { 49 ActivityIdleDisplaySleepDisabled = (1ULL << 40), 50 ActivityIdleSystemSleepDisabled = (1ULL << 20), 51 ActivitySuddenTerminationDisabled = (1ULL << 14), 52 ActivityAutomaticTerminationDisabled = (1ULL << 15), 53 ActivityUserInitiated = (0x00FFFFFFULL | ActivityIdleSystemSleepDisabled), 54 ActivityUserInitiatedAllowingIdleSystemSleep = (ActivityUserInitiated & ~ActivityIdleSystemSleepDisabled), 55 ActivityBackground = 0x000000FFULL, 56 ActivityLatencyCritical = 0xFF00000000ULL, 57 }; 58 59 typedef NS::Integer DeviceCertification; 60 _NS_CONST(DeviceCertification, DeviceCertificationiPhonePerformanceGaming); 61 62 typedef NS::Integer ProcessPerformanceProfile; 63 _NS_CONST(ProcessPerformanceProfile, ProcessPerformanceProfileDefault); 64 _NS_CONST(ProcessPerformanceProfile, ProcessPerformanceProfileSustained); 65 66 class ProcessInfo : public Referencing<ProcessInfo> 67 { 68 public: 69 static ProcessInfo* processInfo(); 70 71 class Array* arguments() const; 72 class Dictionary* environment() const; 73 class String* hostName() const; 74 class String* processName() const; 75 void setProcessName(const String* pString); 76 int processIdentifier() const; 77 class String* globallyUniqueString() const; 78 79 class String* userName() const; 80 class String* fullUserName() const; 81 82 UInteger operatingSystem() const; 83 OperatingSystemVersion operatingSystemVersion() const; 84 class String* operatingSystemVersionString() const; 85 bool isOperatingSystemAtLeastVersion(OperatingSystemVersion version) const; 86 87 UInteger processorCount() const; 88 UInteger activeProcessorCount() const; 89 unsigned long long physicalMemory() const; 90 TimeInterval systemUptime() const; 91 92 void disableSuddenTermination(); 93 void enableSuddenTermination(); 94 95 void disableAutomaticTermination(const class String* pReason); 96 void enableAutomaticTermination(const class String* pReason); 97 bool automaticTerminationSupportEnabled() const; 98 void setAutomaticTerminationSupportEnabled(bool enabled); 99 100 class Object* beginActivity(ActivityOptions options, const class String* pReason); 101 void endActivity(class Object* pActivity); 102 void performActivity(ActivityOptions options, const class String* pReason, void (^block)(void)); 103 void performActivity(ActivityOptions options, const class String* pReason, const std::function<void()>& func); 104 void performExpiringActivity(const class String* pReason, void (^block)(bool expired)); 105 void performExpiringActivity(const class String* pReason, const std::function<void(bool expired)>& func); 106 107 ProcessInfoThermalState thermalState() const; 108 bool isLowPowerModeEnabled() const; 109 110 bool isiOSAppOnMac() const; 111 bool isMacCatalystApp() const; 112 113 bool isDeviceCertified(DeviceCertification performanceTier) const; 114 bool hasPerformanceProfile(ProcessPerformanceProfile performanceProfile) const; 115 116 }; 117 } 118 119 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 120 121 _NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoThermalStateDidChangeNotification); 122 _NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoPowerStateDidChangeNotification); 123 124 // The linker searches for these symbols in the Metal framework, be sure to link it in as well: 125 _NS_PRIVATE_DEF_CONST(NS::NotificationName, ProcessInfoPerformanceProfileDidChangeNotification); 126 _NS_PRIVATE_DEF_CONST(NS::DeviceCertification, DeviceCertificationiPhonePerformanceGaming); 127 _NS_PRIVATE_DEF_CONST(NS::ProcessPerformanceProfile, ProcessPerformanceProfileDefault); 128 _NS_PRIVATE_DEF_CONST(NS::ProcessPerformanceProfile, ProcessPerformanceProfileSustained); 129 130 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 131 132 _NS_INLINE NS::ProcessInfo* NS::ProcessInfo::processInfo() 133 { 134 return Object::sendMessage<ProcessInfo*>(_NS_PRIVATE_CLS(NSProcessInfo), _NS_PRIVATE_SEL(processInfo)); 135 } 136 137 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 138 139 _NS_INLINE NS::Array* NS::ProcessInfo::arguments() const 140 { 141 return Object::sendMessage<Array*>(this, _NS_PRIVATE_SEL(arguments)); 142 } 143 144 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 145 146 _NS_INLINE NS::Dictionary* NS::ProcessInfo::environment() const 147 { 148 return Object::sendMessage<Dictionary*>(this, _NS_PRIVATE_SEL(environment)); 149 } 150 151 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 152 153 _NS_INLINE NS::String* NS::ProcessInfo::hostName() const 154 { 155 return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(hostName)); 156 } 157 158 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 159 160 _NS_INLINE NS::String* NS::ProcessInfo::processName() const 161 { 162 return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(processName)); 163 } 164 165 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 166 167 _NS_INLINE void NS::ProcessInfo::setProcessName(const String* pString) 168 { 169 Object::sendMessage<void>(this, _NS_PRIVATE_SEL(setProcessName_), pString); 170 } 171 172 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 173 174 _NS_INLINE int NS::ProcessInfo::processIdentifier() const 175 { 176 return Object::sendMessage<int>(this, _NS_PRIVATE_SEL(processIdentifier)); 177 } 178 179 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 180 181 _NS_INLINE NS::String* NS::ProcessInfo::globallyUniqueString() const 182 { 183 return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(globallyUniqueString)); 184 } 185 186 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 187 188 _NS_INLINE NS::String* NS::ProcessInfo::userName() const 189 { 190 return Object::sendMessageSafe<String*>(this, _NS_PRIVATE_SEL(userName)); 191 } 192 193 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 194 195 _NS_INLINE NS::String* NS::ProcessInfo::fullUserName() const 196 { 197 return Object::sendMessageSafe<String*>(this, _NS_PRIVATE_SEL(fullUserName)); 198 } 199 200 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 201 202 _NS_INLINE NS::UInteger NS::ProcessInfo::operatingSystem() const 203 { 204 return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(operatingSystem)); 205 } 206 207 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 208 209 _NS_INLINE NS::OperatingSystemVersion NS::ProcessInfo::operatingSystemVersion() const 210 { 211 return Object::sendMessage<OperatingSystemVersion>(this, _NS_PRIVATE_SEL(operatingSystemVersion)); 212 } 213 214 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 215 216 _NS_INLINE NS::String* NS::ProcessInfo::operatingSystemVersionString() const 217 { 218 return Object::sendMessage<String*>(this, _NS_PRIVATE_SEL(operatingSystemVersionString)); 219 } 220 221 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 222 223 _NS_INLINE bool NS::ProcessInfo::isOperatingSystemAtLeastVersion(OperatingSystemVersion version) const 224 { 225 return Object::sendMessage<bool>(this, _NS_PRIVATE_SEL(isOperatingSystemAtLeastVersion_), version); 226 } 227 228 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 229 230 _NS_INLINE NS::UInteger NS::ProcessInfo::processorCount() const 231 { 232 return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(processorCount)); 233 } 234 235 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 236 237 _NS_INLINE NS::UInteger NS::ProcessInfo::activeProcessorCount() const 238 { 239 return Object::sendMessage<UInteger>(this, _NS_PRIVATE_SEL(activeProcessorCount)); 240 } 241 242 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 243 244 _NS_INLINE unsigned long long NS::ProcessInfo::physicalMemory() const 245 { 246 return Object::sendMessage<unsigned long long>(this, _NS_PRIVATE_SEL(physicalMemory)); 247 } 248 249 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 250 251 _NS_INLINE NS::TimeInterval NS::ProcessInfo::systemUptime() const 252 { 253 return Object::sendMessage<TimeInterval>(this, _NS_PRIVATE_SEL(systemUptime)); 254 } 255 256 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 257 258 _NS_INLINE void NS::ProcessInfo::disableSuddenTermination() 259 { 260 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(disableSuddenTermination)); 261 } 262 263 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 264 265 _NS_INLINE void NS::ProcessInfo::enableSuddenTermination() 266 { 267 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(enableSuddenTermination)); 268 } 269 270 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 271 272 _NS_INLINE void NS::ProcessInfo::disableAutomaticTermination(const String* pReason) 273 { 274 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(disableAutomaticTermination_), pReason); 275 } 276 277 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 278 279 _NS_INLINE void NS::ProcessInfo::enableAutomaticTermination(const String* pReason) 280 { 281 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(enableAutomaticTermination_), pReason); 282 } 283 284 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 285 286 _NS_INLINE bool NS::ProcessInfo::automaticTerminationSupportEnabled() const 287 { 288 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(automaticTerminationSupportEnabled)); 289 } 290 291 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 292 293 _NS_INLINE void NS::ProcessInfo::setAutomaticTerminationSupportEnabled(bool enabled) 294 { 295 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(setAutomaticTerminationSupportEnabled_), enabled); 296 } 297 298 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 299 300 _NS_INLINE NS::Object* NS::ProcessInfo::beginActivity(ActivityOptions options, const String* pReason) 301 { 302 return Object::sendMessage<Object*>(this, _NS_PRIVATE_SEL(beginActivityWithOptions_reason_), options, pReason); 303 } 304 305 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 306 307 _NS_INLINE void NS::ProcessInfo::endActivity(Object* pActivity) 308 { 309 Object::sendMessage<void>(this, _NS_PRIVATE_SEL(endActivity_), pActivity); 310 } 311 312 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 313 314 _NS_INLINE void NS::ProcessInfo::performActivity(ActivityOptions options, const String* pReason, void (^block)(void)) 315 { 316 Object::sendMessage<void>(this, _NS_PRIVATE_SEL(performActivityWithOptions_reason_usingBlock_), options, pReason, block); 317 } 318 319 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 320 321 _NS_INLINE void NS::ProcessInfo::performActivity(ActivityOptions options, const String* pReason, const std::function<void()>& function) 322 { 323 __block std::function<void()> blockFunction = function; 324 325 performActivity(options, pReason, ^() { blockFunction(); }); 326 } 327 328 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 329 330 _NS_INLINE void NS::ProcessInfo::performExpiringActivity(const String* pReason, void (^block)(bool expired)) 331 { 332 Object::sendMessageSafe<void>(this, _NS_PRIVATE_SEL(performExpiringActivityWithReason_usingBlock_), pReason, block); 333 } 334 335 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 336 337 _NS_INLINE void NS::ProcessInfo::performExpiringActivity(const String* pReason, const std::function<void(bool expired)>& function) 338 { 339 __block std::function<void(bool expired)> blockFunction = function; 340 341 performExpiringActivity(pReason, ^(bool expired) { blockFunction(expired); }); 342 } 343 344 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 345 346 _NS_INLINE NS::ProcessInfoThermalState NS::ProcessInfo::thermalState() const 347 { 348 return Object::sendMessage<ProcessInfoThermalState>(this, _NS_PRIVATE_SEL(thermalState)); 349 } 350 351 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 352 353 _NS_INLINE bool NS::ProcessInfo::isLowPowerModeEnabled() const 354 { 355 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isLowPowerModeEnabled)); 356 } 357 358 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 359 360 _NS_INLINE bool NS::ProcessInfo::isiOSAppOnMac() const 361 { 362 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isiOSAppOnMac)); 363 } 364 365 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 366 367 _NS_INLINE bool NS::ProcessInfo::isMacCatalystApp() const 368 { 369 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isMacCatalystApp)); 370 } 371 372 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 373 374 _NS_INLINE bool NS::ProcessInfo::isDeviceCertified(DeviceCertification performanceTier) const 375 { 376 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(isDeviceCertified_), performanceTier); 377 } 378 379 //------------------------------------------------------------------------------------------------------------------------------------------------------------- 380 381 _NS_INLINE bool NS::ProcessInfo::hasPerformanceProfile(ProcessPerformanceProfile performanceProfile) const 382 { 383 return Object::sendMessageSafe<bool>(this, _NS_PRIVATE_SEL(hasPerformanceProfile_), performanceProfile); 384 } 385 386 //-------------------------------------------------------------------------------------------------------------------------------------------------------------