OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/win/windows_version.h" | 5 #include "base/win/windows_version.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/file_version_info_win.h" | |
robliao
2016/03/11 17:04:46
FileVersionInfoWin seems asks for more information
scottmg
2016/03/11 22:44:26
Yeah, I'm not sure if it's better or not, but it's
| |
10 #include "base/files/file_path.h" | |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 #include "base/win/registry.h" | 14 #include "base/win/registry.h" |
12 | 15 |
13 namespace { | 16 namespace { |
14 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); | 17 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); |
15 } | 18 } // namespace |
16 | 19 |
17 namespace base { | 20 namespace base { |
18 namespace win { | 21 namespace win { |
19 | 22 |
23 namespace { | |
24 | |
25 // Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release. | |
26 Version MajorMinorBuildToVersion(int major, int minor, int build) { | |
27 if ((major == 5) && (minor > 0)) { | |
28 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. | |
29 return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003; | |
30 } else if (major == 6) { | |
31 switch (minor) { | |
32 case 0: | |
33 // Treat Windows Server 2008 the same as Windows Vista. | |
34 return VERSION_VISTA; | |
35 case 1: | |
36 // Treat Windows Server 2008 R2 the same as Windows 7. | |
37 return VERSION_WIN7; | |
38 case 2: | |
39 // Treat Windows Server 2012 the same as Windows 8. | |
40 return VERSION_WIN8; | |
41 default: | |
42 DCHECK_EQ(minor, 3); | |
43 return VERSION_WIN8_1; | |
44 } | |
45 } else if (major == 10) { | |
46 if (build < 10586) { | |
47 return VERSION_WIN10; | |
48 } else { | |
49 return VERSION_WIN10_TH2; | |
50 } | |
51 } else if (major > 6) { | |
52 NOTREACHED(); | |
53 return VERSION_WIN_LAST; | |
54 } | |
55 | |
56 NOTREACHED(); | |
57 return VERSION_WIN_LAST; | |
58 } | |
59 | |
60 // Retrieve a version from kernel32. This is useful because when running in | |
61 // compatibility mode for a down-level version of the OS, the file version of | |
62 // kernel32 will still be the "real" version. | |
63 base::win::Version GetVersionFromKernel32() { | |
grt (UTC plus 2)
2016/03/11 16:13:42
base::win:: here, too
scottmg
2016/03/11 22:44:26
Done.
| |
64 scoped_ptr<FileVersionInfoWin> file_version_info( | |
65 reinterpret_cast<FileVersionInfoWin*>( | |
grt (UTC plus 2)
2016/03/11 16:13:42
barf
robliao
2016/03/11 17:04:46
Shouldn't a static_cast work here? That's done her
scottmg
2016/03/11 22:44:26
Done.
| |
66 FileVersionInfoWin::CreateFileVersionInfo( | |
67 base::FilePath(FILE_PATH_LITERAL("kernel32.dll"))))); | |
68 if (file_version_info.get()) { | |
grt (UTC plus 2)
2016/03/11 16:13:42
nit: omit ".get()"
scottmg
2016/03/11 22:44:26
Done.
| |
69 const int major = | |
70 HIWORD(file_version_info->fixed_file_info()->dwFileVersionMS); | |
71 const int minor = | |
72 LOWORD(file_version_info->fixed_file_info()->dwFileVersionMS); | |
73 const int build = | |
74 HIWORD(file_version_info->fixed_file_info()->dwFileVersionLS); | |
75 return MajorMinorBuildToVersion(major, minor, build); | |
76 } | |
77 | |
78 NOTREACHED(); | |
79 return VERSION_WIN_LAST; | |
80 } | |
81 | |
82 } // namespace | |
83 | |
20 // static | 84 // static |
21 OSInfo* OSInfo::GetInstance() { | 85 OSInfo* OSInfo::GetInstance() { |
22 // Note: we don't use the Singleton class because it depends on AtExitManager, | 86 // Note: we don't use the Singleton class because it depends on AtExitManager, |
23 // and it's convenient for other modules to use this classs without it. This | 87 // and it's convenient for other modules to use this classs without it. This |
24 // pattern is copied from gurl.cc. | 88 // pattern is copied from gurl.cc. |
25 static OSInfo* info; | 89 static OSInfo* info; |
26 if (!info) { | 90 if (!info) { |
27 OSInfo* new_info = new OSInfo(); | 91 OSInfo* new_info = new OSInfo(); |
28 if (InterlockedCompareExchangePointer( | 92 if (InterlockedCompareExchangePointer( |
29 reinterpret_cast<PVOID*>(&info), new_info, NULL)) { | 93 reinterpret_cast<PVOID*>(&info), new_info, NULL)) { |
30 delete new_info; | 94 delete new_info; |
31 } | 95 } |
32 } | 96 } |
33 return info; | 97 return info; |
34 } | 98 } |
35 | 99 |
36 OSInfo::OSInfo() | 100 OSInfo::OSInfo() |
37 : version_(VERSION_PRE_XP), | 101 : version_(VERSION_PRE_XP), |
102 kernel32_version_(VERSION_PRE_XP), | |
103 got_kernel32_version_(false), | |
38 architecture_(OTHER_ARCHITECTURE), | 104 architecture_(OTHER_ARCHITECTURE), |
39 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { | 105 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { |
40 OSVERSIONINFOEX version_info = { sizeof version_info }; | 106 OSVERSIONINFOEX version_info = { sizeof version_info }; |
41 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); | 107 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); |
42 version_number_.major = version_info.dwMajorVersion; | 108 version_number_.major = version_info.dwMajorVersion; |
43 version_number_.minor = version_info.dwMinorVersion; | 109 version_number_.minor = version_info.dwMinorVersion; |
44 version_number_.build = version_info.dwBuildNumber; | 110 version_number_.build = version_info.dwBuildNumber; |
45 if ((version_number_.major == 5) && (version_number_.minor > 0)) { | 111 version_ = MajorMinorBuildToVersion( |
46 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. | 112 version_number_.major, version_number_.minor, version_number_.build); |
47 version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; | |
48 } else if (version_number_.major == 6) { | |
49 switch (version_number_.minor) { | |
50 case 0: | |
51 // Treat Windows Server 2008 the same as Windows Vista. | |
52 version_ = VERSION_VISTA; | |
53 break; | |
54 case 1: | |
55 // Treat Windows Server 2008 R2 the same as Windows 7. | |
56 version_ = VERSION_WIN7; | |
57 break; | |
58 case 2: | |
59 // Treat Windows Server 2012 the same as Windows 8. | |
60 version_ = VERSION_WIN8; | |
61 break; | |
62 default: | |
63 DCHECK_EQ(version_number_.minor, 3); | |
64 version_ = VERSION_WIN8_1; | |
65 break; | |
66 } | |
67 } else if (version_number_.major == 10) { | |
68 if (version_number_.build < 10586) { | |
69 version_ = VERSION_WIN10; | |
70 } else { | |
71 version_ = VERSION_WIN10_TH2; | |
72 } | |
73 } else if (version_number_.major > 6) { | |
74 NOTREACHED(); | |
75 version_ = VERSION_WIN_LAST; | |
76 } | |
77 service_pack_.major = version_info.wServicePackMajor; | 113 service_pack_.major = version_info.wServicePackMajor; |
78 service_pack_.minor = version_info.wServicePackMinor; | 114 service_pack_.minor = version_info.wServicePackMinor; |
79 | 115 |
80 SYSTEM_INFO system_info = {}; | 116 SYSTEM_INFO system_info = {}; |
81 ::GetNativeSystemInfo(&system_info); | 117 ::GetNativeSystemInfo(&system_info); |
82 switch (system_info.wProcessorArchitecture) { | 118 switch (system_info.wProcessorArchitecture) { |
83 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; | 119 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; |
84 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; | 120 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; |
85 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; | 121 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; |
86 } | 122 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 version_type_ = SUITE_PROFESSIONAL; | 178 version_type_ = SUITE_PROFESSIONAL; |
143 } else { | 179 } else { |
144 // Windows is pre XP so we don't care but pick a safe default. | 180 // Windows is pre XP so we don't care but pick a safe default. |
145 version_type_ = SUITE_HOME; | 181 version_type_ = SUITE_HOME; |
146 } | 182 } |
147 } | 183 } |
148 | 184 |
149 OSInfo::~OSInfo() { | 185 OSInfo::~OSInfo() { |
150 } | 186 } |
151 | 187 |
188 Version OSInfo::Kernel32Version() const { | |
189 if (!got_kernel32_version_) { | |
190 kernel32_version_ = GetVersionFromKernel32(); | |
191 got_kernel32_version_ = true; | |
192 } | |
193 return kernel32_version_; | |
194 } | |
195 | |
152 std::string OSInfo::processor_model_name() { | 196 std::string OSInfo::processor_model_name() { |
153 if (processor_model_name_.empty()) { | 197 if (processor_model_name_.empty()) { |
154 const wchar_t kProcessorNameString[] = | 198 const wchar_t kProcessorNameString[] = |
155 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; | 199 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; |
156 base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); | 200 base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); |
157 string16 value; | 201 string16 value; |
158 key.ReadValue(L"ProcessorNameString", &value); | 202 key.ReadValue(L"ProcessorNameString", &value); |
159 processor_model_name_ = UTF16ToUTF8(value); | 203 processor_model_name_ = UTF16ToUTF8(value); |
160 } | 204 } |
161 return processor_model_name_; | 205 return processor_model_name_; |
(...skipping 11 matching lines...) Expand all Loading... | |
173 return WOW64_UNKNOWN; | 217 return WOW64_UNKNOWN; |
174 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; | 218 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; |
175 } | 219 } |
176 | 220 |
177 Version GetVersion() { | 221 Version GetVersion() { |
178 return OSInfo::GetInstance()->version(); | 222 return OSInfo::GetInstance()->version(); |
179 } | 223 } |
180 | 224 |
181 } // namespace win | 225 } // namespace win |
182 } // namespace base | 226 } // namespace base |
OLD | NEW |