1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef RTC_BASE_WIN_WINDOWS_VERSION_H_
12 #define RTC_BASE_WIN_WINDOWS_VERSION_H_
13 
14 #include <stddef.h>
15 
16 #include <string>
17 
18 #include "rtc_base/constructor_magic.h"
19 
20 typedef void* HANDLE;
21 
22 namespace rtc {
23 namespace rtc_win {
24 
25 // The running version of Windows.  This is declared outside OSInfo for
26 // syntactic sugar reasons; see the declaration of GetVersion() below.
27 // NOTE: Keep these in order so callers can do things like
28 // "if (rtc_win::GetVersion() >= rtc_win::VERSION_VISTA) ...".
29 //
30 // This enum is used in metrics histograms, so they shouldn't be reordered or
31 // removed. New values can be added before VERSION_WIN_LAST.
32 enum Version {
33   VERSION_PRE_XP = 0,  // Not supported.
34   VERSION_XP = 1,
35   VERSION_SERVER_2003 = 2,  // Also includes XP Pro x64 and Server 2003 R2.
36   VERSION_VISTA = 3,        // Also includes Windows Server 2008.
37   VERSION_WIN7 = 4,         // Also includes Windows Server 2008 R2.
38   VERSION_WIN8 = 5,         // Also includes Windows Server 2012.
39   VERSION_WIN8_1 = 6,       // Also includes Windows Server 2012 R2.
40   VERSION_WIN10 = 7,        // Threshold 1: Version 1507, Build 10240.
41   VERSION_WIN10_TH2 = 8,    // Threshold 2: Version 1511, Build 10586.
42   VERSION_WIN10_RS1 = 9,    // Redstone 1: Version 1607, Build 14393.
43   VERSION_WIN10_RS2 = 10,   // Redstone 2: Version 1703, Build 15063.
44   VERSION_WIN10_RS3 = 11,   // Redstone 3: Version 1709, Build 16299.
45   VERSION_WIN10_RS4 = 12,   // Redstone 4: Version 1803, Build 17134.
46   VERSION_WIN10_RS5 = 13,   // Redstone 5: Version 1809, Build 17763.
47   VERSION_WIN10_19H1 = 14,  // 19H1: Version 1903, Build 18362.
48   // On edit, update tools\metrics\histograms\enums.xml "WindowsVersion" and
49   // "GpuBlacklistFeatureTestResultsWindows2".
50   VERSION_WIN_LAST,  // Indicates error condition.
51 };
52 
53 // A rough bucketing of the available types of versions of Windows. This is used
54 // to distinguish enterprise enabled versions from home versions and potentially
55 // server versions. Keep these values in the same order, since they are used as
56 // is for metrics histogram ids.
57 enum VersionType {
58   SUITE_HOME = 0,
59   SUITE_PROFESSIONAL,
60   SUITE_SERVER,
61   SUITE_ENTERPRISE,
62   SUITE_EDUCATION,
63   SUITE_LAST,
64 };
65 
66 // A singleton that can be used to query various pieces of information about the
67 // OS and process state. Note that this doesn't use the base Singleton class, so
68 // it can be used without an AtExitManager.
69 class OSInfo {
70  public:
71   struct VersionNumber {
72     int major;
73     int minor;
74     int build;
75     int patch;
76   };
77 
78   struct ServicePack {
79     int major;
80     int minor;
81   };
82 
83   // The processor architecture this copy of Windows natively uses.  For
84   // example, given an x64-capable processor, we have three possibilities:
85   //   32-bit Chrome running on 32-bit Windows:           X86_ARCHITECTURE
86   //   32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
87   //   64-bit Chrome running on 64-bit Windows:           X64_ARCHITECTURE
88   enum WindowsArchitecture {
89     X86_ARCHITECTURE,
90     X64_ARCHITECTURE,
91     IA64_ARCHITECTURE,
92     OTHER_ARCHITECTURE,
93   };
94 
95   // Whether a process is running under WOW64 (the wrapper that allows 32-bit
96   // processes to run on 64-bit versions of Windows).  This will return
97   // WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit
98   // Chrome on 64-bit Windows".  WOW64_UNKNOWN means "an error occurred", e.g.
99   // the process does not have sufficient access rights to determine this.
100   enum WOW64Status {
101     WOW64_DISABLED,
102     WOW64_ENABLED,
103     WOW64_UNKNOWN,
104   };
105 
106   static OSInfo* GetInstance();
107 
version()108   Version version() const { return version_; }
version_number()109   VersionNumber version_number() const { return version_number_; }
version_type()110   VersionType version_type() const { return version_type_; }
service_pack()111   ServicePack service_pack() const { return service_pack_; }
service_pack_str()112   std::string service_pack_str() const { return service_pack_str_; }
architecture()113   WindowsArchitecture architecture() const { return architecture_; }
processors()114   int processors() const { return processors_; }
allocation_granularity()115   size_t allocation_granularity() const { return allocation_granularity_; }
wow64_status()116   WOW64Status wow64_status() const { return wow64_status_; }
117   std::string processor_model_name();
118 
119   // Like wow64_status(), but for the supplied handle instead of the current
120   // process.  This doesn't touch member state, so you can bypass the singleton.
121   static WOW64Status GetWOW64StatusForProcess(HANDLE process_handle);
122 
123  private:
124   OSInfo();
125   ~OSInfo();
126 
127   Version version_;
128   VersionNumber version_number_;
129   VersionType version_type_;
130   ServicePack service_pack_;
131 
132   // A string, such as "Service Pack 3", that indicates the latest Service Pack
133   // installed on the system. If no Service Pack has been installed, the string
134   // is empty.
135   std::string service_pack_str_;
136   WindowsArchitecture architecture_;
137   int processors_;
138   size_t allocation_granularity_;
139   WOW64Status wow64_status_;
140   std::string processor_model_name_;
141 
142   RTC_DISALLOW_COPY_AND_ASSIGN(OSInfo);
143 };
144 
145 // Because this is by far the most commonly-requested value from the above
146 // singleton, we add a global-scope accessor here as syntactic sugar.
147 Version GetVersion();
148 
149 }  // namespace rtc_win
150 }  // namespace rtc
151 
152 #endif  // RTC_BASE_WIN_WINDOWS_VERSION_H_
153