windows_version.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_WINDOWS_VERSION_H_
  5. #define BASE_WIN_WINDOWS_VERSION_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/gtest_prod_util.h"
  10. #include "base/macros.h"
  11. #include "base/version.h"
  12. using HANDLE = void*;
  13. struct _OSVERSIONINFOEXW;
  14. struct _SYSTEM_INFO;
  15. namespace base {
  16. namespace test {
  17. class ScopedOSInfoOverride;
  18. } // namespace test
  19. } // namespace base
  20. namespace base {
  21. namespace win {
  22. // The running version of Windows. This is declared outside OSInfo for
  23. // syntactic sugar reasons; see the declaration of GetVersion() below.
  24. // NOTE: Keep these in order so callers can do things like
  25. // "if (base::win::GetVersion() >= base::win::Version::VISTA) ...".
  26. //
  27. // This enum is used in metrics histograms, so they shouldn't be reordered or
  28. // removed. New values can be added before Version::WIN_LAST.
  29. enum class Version {
  30. PRE_XP = 0, // Not supported.
  31. XP = 1,
  32. SERVER_2003 = 2, // Also includes XP Pro x64 and Server 2003 R2.
  33. VISTA = 3, // Also includes Windows Server 2008.
  34. WIN7 = 4, // Also includes Windows Server 2008 R2.
  35. WIN8 = 5, // Also includes Windows Server 2012.
  36. WIN8_1 = 6, // Also includes Windows Server 2012 R2.
  37. WIN10 = 7, // Threshold 1: Version 1507, Build 10240.
  38. WIN10_TH2 = 8, // Threshold 2: Version 1511, Build 10586.
  39. WIN10_RS1 = 9, // Redstone 1: Version 1607, Build 14393.
  40. WIN10_RS2 = 10, // Redstone 2: Version 1703, Build 15063.
  41. WIN10_RS3 = 11, // Redstone 3: Version 1709, Build 16299.
  42. WIN10_RS4 = 12, // Redstone 4: Version 1803, Build 17134.
  43. WIN10_RS5 = 13, // Redstone 5: Version 1809, Build 17763.
  44. WIN10_19H1 = 14, // 19H1: Version 1903, Build 18362.
  45. WIN10_20H1 = 15, // 20H1: Version 2004, Build 19041.
  46. // On edit, update tools\metrics\histograms\enums.xml "WindowsVersion" and
  47. // "GpuBlacklistFeatureTestResultsWindows2".
  48. WIN_LAST, // Indicates error condition.
  49. };
  50. // A rough bucketing of the available types of versions of Windows. This is used
  51. // to distinguish enterprise enabled versions from home versions and potentially
  52. // server versions. Keep these values in the same order, since they are used as
  53. // is for metrics histogram ids.
  54. enum VersionType {
  55. SUITE_HOME = 0,
  56. SUITE_PROFESSIONAL,
  57. SUITE_SERVER,
  58. SUITE_ENTERPRISE,
  59. SUITE_EDUCATION,
  60. SUITE_LAST,
  61. };
  62. // A singleton that can be used to query various pieces of information about the
  63. // OS and process state. Note that this doesn't use the base Singleton class, so
  64. // it can be used without an AtExitManager.
  65. class BASE_EXPORT OSInfo {
  66. public:
  67. struct VersionNumber {
  68. int major;
  69. int minor;
  70. int build;
  71. int patch;
  72. };
  73. struct ServicePack {
  74. int major;
  75. int minor;
  76. };
  77. // The processor architecture this copy of Windows natively uses. For
  78. // example, given an x64-capable processor, we have three possibilities:
  79. // 32-bit Chrome running on 32-bit Windows: X86_ARCHITECTURE
  80. // 32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
  81. // 64-bit Chrome running on 64-bit Windows: X64_ARCHITECTURE
  82. enum WindowsArchitecture {
  83. X86_ARCHITECTURE,
  84. X64_ARCHITECTURE,
  85. IA64_ARCHITECTURE,
  86. ARM64_ARCHITECTURE,
  87. OTHER_ARCHITECTURE,
  88. };
  89. // Whether a process is running under WOW64 (the wrapper that allows 32-bit
  90. // processes to run on 64-bit versions of Windows). This will return
  91. // WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit
  92. // Chrome on 64-bit Windows". WOW64_UNKNOWN means "an error occurred", e.g.
  93. // the process does not have sufficient access rights to determine this.
  94. enum WOW64Status {
  95. WOW64_DISABLED,
  96. WOW64_ENABLED,
  97. WOW64_UNKNOWN,
  98. };
  99. static OSInfo* GetInstance();
  100. // Separate from the rest of OSInfo so it can be used during early process
  101. // initialization.
  102. static WindowsArchitecture GetArchitecture();
  103. // Like wow64_status(), but for the supplied handle instead of the current
  104. // process. This doesn't touch member state, so you can bypass the singleton.
  105. static WOW64Status GetWOW64StatusForProcess(HANDLE process_handle);
  106. const Version& version() const { return version_; }
  107. Version Kernel32Version() const;
  108. base::Version Kernel32BaseVersion() const;
  109. // The next two functions return arrays of values, [major, minor(, build)].
  110. const VersionNumber& version_number() const { return version_number_; }
  111. const VersionType& version_type() const { return version_type_; }
  112. const ServicePack& service_pack() const { return service_pack_; }
  113. const std::string& service_pack_str() const { return service_pack_str_; }
  114. const int& processors() const { return processors_; }
  115. const size_t& allocation_granularity() const {
  116. return allocation_granularity_;
  117. }
  118. const WOW64Status& wow64_status() const { return wow64_status_; }
  119. std::string processor_model_name();
  120. const std::string& release_id() const { return release_id_; }
  121. private:
  122. friend class base::test::ScopedOSInfoOverride;
  123. FRIEND_TEST_ALL_PREFIXES(OSInfo, MajorMinorBuildToVersion);
  124. static OSInfo** GetInstanceStorage();
  125. OSInfo(const _OSVERSIONINFOEXW& version_info,
  126. const _SYSTEM_INFO& system_info,
  127. int os_type);
  128. ~OSInfo();
  129. // Returns a Version value for a given OS version tuple.
  130. static Version MajorMinorBuildToVersion(int major, int minor, int build);
  131. Version version_;
  132. VersionNumber version_number_;
  133. VersionType version_type_;
  134. ServicePack service_pack_;
  135. // Represents the version of the OS associated to a release of
  136. // Windows 10. Each version may have different releases (such as patch
  137. // updates). This is the identifier of the release.
  138. // Example:
  139. // Windows 10 Version 1809 (OS build 17763) has multiple releases
  140. // (i.e. build 17763.1, build 17763.195, build 17763.379, ...).
  141. // See https://docs.microsoft.com/en-us/windows/windows-10/release-information
  142. // for more information.
  143. std::string release_id_;
  144. // A string, such as "Service Pack 3", that indicates the latest Service Pack
  145. // installed on the system. If no Service Pack has been installed, the string
  146. // is empty.
  147. std::string service_pack_str_;
  148. int processors_;
  149. size_t allocation_granularity_;
  150. WOW64Status wow64_status_;
  151. std::string processor_model_name_;
  152. DISALLOW_COPY_AND_ASSIGN(OSInfo);
  153. };
  154. // Because this is by far the most commonly-requested value from the above
  155. // singleton, we add a global-scope accessor here as syntactic sugar.
  156. BASE_EXPORT Version GetVersion();
  157. } // namespace win
  158. } // namespace base
  159. #endif // BASE_WIN_WINDOWS_VERSION_H_