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/files/file_path.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/threading/thread_restrictions.h" | |
11 #include "base/win/registry.h" | 13 #include "base/win/registry.h" |
12 | 14 |
13 namespace { | 15 namespace { |
16 | |
14 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); | 17 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); |
18 | |
19 // Retrieve the type and version information from a given module. This function | |
20 // calls GetFileVersionInfo() which can implicitly call LoadLibrary(). | |
21 bool GetModuleVersionAndType(const base::FilePath& path, | |
grt (UTC plus 2)
2016/03/10 19:43:44
i propose avoiding the FilePath for this private f
scottmg
2016/03/10 23:51:39
Done.
| |
22 VS_FIXEDFILEINFO* vs_fixedfileinfo) { | |
23 base::ThreadRestrictions::AssertIOAllowed(); | |
24 | |
25 DWORD size = ::GetFileVersionInfoSize(path.value().c_str(), nullptr); | |
26 if (!size) { | |
27 PLOG_IF(WARNING, GetLastError() != ERROR_RESOURCE_TYPE_NOT_FOUND) | |
28 << "GetFileVersionInfoSize: " << base::UTF16ToUTF8(path.value()); | |
grt (UTC plus 2)
2016/03/10 19:43:44
in the future, base::UTF16ToUTF8 isn't needed in c
scottmg
2016/03/10 23:51:39
It works in an ugly way (injecting into operator<<
| |
29 return false; | |
30 } | |
31 | |
32 scoped_ptr<uint8_t[]> data(new uint8_t[size]); | |
33 if (!::GetFileVersionInfo(path.value().c_str(), 0, size, data.get())) { | |
34 PLOG(WARNING) << "GetFileVersionInfo: " << base::UTF16ToUTF8(path.value()); | |
35 return false; | |
36 } | |
37 | |
38 VS_FIXEDFILEINFO* fixed_file_info; | |
39 UINT ffi_size; | |
robliao
2016/03/10 19:28:51
Nit: Given that we don't actually care about this,
scottmg
2016/03/10 23:51:40
Done.
| |
40 if (!::VerQueryValue(data.get(), L"\\", | |
41 reinterpret_cast<void**>(&fixed_file_info), &ffi_size)) { | |
42 PLOG(WARNING) << "VerQueryValue"; | |
43 return false; | |
44 } | |
45 | |
46 *vs_fixedfileinfo = *fixed_file_info; | |
47 vs_fixedfileinfo->dwFileFlags &= vs_fixedfileinfo->dwFileFlagsMask; | |
48 return true; | |
15 } | 49 } |
16 | 50 |
51 } // namespace | |
52 | |
17 namespace base { | 53 namespace base { |
18 namespace win { | 54 namespace win { |
19 | 55 |
56 namespace { | |
57 | |
58 // Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release. | |
59 base::win::Version MajorMinorBuildToVersion(int major, int minor, int build) { | |
grt (UTC plus 2)
2016/03/10 19:43:44
is base::win:: needed to disambiguate? this is wit
scottmg
2016/03/10 23:51:39
Done.
| |
60 if ((major == 5) && (minor > 0)) { | |
61 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. | |
62 return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003; | |
63 } else if (major == 6) { | |
64 switch (minor) { | |
65 case 0: | |
66 // Treat Windows Server 2008 the same as Windows Vista. | |
67 return VERSION_VISTA; | |
68 case 1: | |
69 // Treat Windows Server 2008 R2 the same as Windows 7. | |
70 return VERSION_WIN7; | |
71 case 2: | |
72 // Treat Windows Server 2012 the same as Windows 8. | |
73 return VERSION_WIN8; | |
74 default: | |
75 DCHECK_EQ(minor, 3); | |
76 return VERSION_WIN8_1; | |
77 } | |
78 } else if (major == 10) { | |
79 if (build < 10586) { | |
80 return VERSION_WIN10; | |
81 } else { | |
82 return VERSION_WIN10_TH2; | |
83 } | |
84 } else if (major > 6) { | |
85 NOTREACHED(); | |
86 return VERSION_WIN_LAST; | |
87 } | |
88 | |
89 NOTREACHED(); | |
90 return VERSION_WIN_LAST; | |
91 } | |
92 | |
93 // Retrieve a version from kernel32. This is useful because when running in | |
94 // compatibility mode for a down-level version of the OS, the file version of | |
95 // kernel32 will still be the "real" version. | |
96 base::win::Version GetVersionFromKernel32() { | |
97 const wchar_t kSystemDll[] = L"kernel32.dll"; | |
grt (UTC plus 2)
2016/03/10 19:43:44
nit: static const wchar_t to avoid the weight of p
scottmg
2016/03/10 23:51:39
Done.
| |
98 VS_FIXEDFILEINFO ffi; | |
robliao
2016/03/10 19:28:51
Nit: fixed_file_info. This looks very hungarian :-
scottmg
2016/03/10 23:51:39
Done.
| |
99 if (::GetModuleVersionAndType(base::FilePath(kSystemDll), &ffi)) { | |
grt (UTC plus 2)
2016/03/10 19:43:44
can you use FileVersionInfoWin to get rid of the d
scottmg
2016/03/10 23:51:39
Done.
| |
100 const int major = ffi.dwFileVersionMS >> 16; | |
grt (UTC plus 2)
2016/03/10 19:43:44
HIWORD and LOWORD?
scottmg
2016/03/10 23:51:39
Done.
| |
101 const int minor = ffi.dwFileVersionMS & 0xffff; | |
102 const int build = ffi.dwFileVersionLS >> 16; | |
103 return MajorMinorBuildToVersion(major, minor, build); | |
104 } | |
105 | |
106 NOTREACHED(); | |
107 return VERSION_WIN_LAST; | |
108 } | |
109 | |
110 } // namespace | |
111 | |
20 // static | 112 // static |
21 OSInfo* OSInfo::GetInstance() { | 113 OSInfo* OSInfo::GetInstance() { |
22 // Note: we don't use the Singleton class because it depends on AtExitManager, | 114 // 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 | 115 // and it's convenient for other modules to use this classs without it. This |
24 // pattern is copied from gurl.cc. | 116 // pattern is copied from gurl.cc. |
25 static OSInfo* info; | 117 static OSInfo* info; |
26 if (!info) { | 118 if (!info) { |
27 OSInfo* new_info = new OSInfo(); | 119 OSInfo* new_info = new OSInfo(); |
28 if (InterlockedCompareExchangePointer( | 120 if (InterlockedCompareExchangePointer( |
29 reinterpret_cast<PVOID*>(&info), new_info, NULL)) { | 121 reinterpret_cast<PVOID*>(&info), new_info, NULL)) { |
30 delete new_info; | 122 delete new_info; |
31 } | 123 } |
32 } | 124 } |
33 return info; | 125 return info; |
34 } | 126 } |
35 | 127 |
36 OSInfo::OSInfo() | 128 OSInfo::OSInfo() |
37 : version_(VERSION_PRE_XP), | 129 : version_(VERSION_PRE_XP), |
130 kernel32_version_(VERSION_PRE_XP), | |
131 got_kernel32_version_(false), | |
38 architecture_(OTHER_ARCHITECTURE), | 132 architecture_(OTHER_ARCHITECTURE), |
39 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { | 133 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { |
40 OSVERSIONINFOEX version_info = { sizeof version_info }; | 134 OSVERSIONINFOEX version_info = { sizeof version_info }; |
41 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); | 135 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); |
42 version_number_.major = version_info.dwMajorVersion; | 136 version_number_.major = version_info.dwMajorVersion; |
43 version_number_.minor = version_info.dwMinorVersion; | 137 version_number_.minor = version_info.dwMinorVersion; |
44 version_number_.build = version_info.dwBuildNumber; | 138 version_number_.build = version_info.dwBuildNumber; |
45 if ((version_number_.major == 5) && (version_number_.minor > 0)) { | 139 version_ = MajorMinorBuildToVersion( |
46 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. | 140 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; | 141 service_pack_.major = version_info.wServicePackMajor; |
78 service_pack_.minor = version_info.wServicePackMinor; | 142 service_pack_.minor = version_info.wServicePackMinor; |
79 | 143 |
80 SYSTEM_INFO system_info = {}; | 144 SYSTEM_INFO system_info = {}; |
81 ::GetNativeSystemInfo(&system_info); | 145 ::GetNativeSystemInfo(&system_info); |
82 switch (system_info.wProcessorArchitecture) { | 146 switch (system_info.wProcessorArchitecture) { |
83 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; | 147 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; |
84 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; | 148 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; |
85 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; | 149 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; |
86 } | 150 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 version_type_ = SUITE_PROFESSIONAL; | 206 version_type_ = SUITE_PROFESSIONAL; |
143 } else { | 207 } else { |
144 // Windows is pre XP so we don't care but pick a safe default. | 208 // Windows is pre XP so we don't care but pick a safe default. |
145 version_type_ = SUITE_HOME; | 209 version_type_ = SUITE_HOME; |
146 } | 210 } |
147 } | 211 } |
148 | 212 |
149 OSInfo::~OSInfo() { | 213 OSInfo::~OSInfo() { |
150 } | 214 } |
151 | 215 |
216 Version OSInfo::Kernel32Version() const { | |
217 if (!got_kernel32_version_) { | |
218 kernel32_version_ = GetVersionFromKernel32(); | |
219 got_kernel32_version_ = true; | |
220 } | |
221 return kernel32_version_; | |
222 } | |
223 | |
152 std::string OSInfo::processor_model_name() { | 224 std::string OSInfo::processor_model_name() { |
153 if (processor_model_name_.empty()) { | 225 if (processor_model_name_.empty()) { |
154 const wchar_t kProcessorNameString[] = | 226 const wchar_t kProcessorNameString[] = |
155 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; | 227 L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; |
156 base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); | 228 base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); |
157 string16 value; | 229 string16 value; |
158 key.ReadValue(L"ProcessorNameString", &value); | 230 key.ReadValue(L"ProcessorNameString", &value); |
159 processor_model_name_ = UTF16ToUTF8(value); | 231 processor_model_name_ = UTF16ToUTF8(value); |
160 } | 232 } |
161 return processor_model_name_; | 233 return processor_model_name_; |
(...skipping 11 matching lines...) Expand all Loading... | |
173 return WOW64_UNKNOWN; | 245 return WOW64_UNKNOWN; |
174 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; | 246 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; |
175 } | 247 } |
176 | 248 |
177 Version GetVersion() { | 249 Version GetVersion() { |
178 return OSInfo::GetInstance()->version(); | 250 return OSInfo::GetInstance()->version(); |
179 } | 251 } |
180 | 252 |
181 } // namespace win | 253 } // namespace win |
182 } // namespace base | 254 } // namespace base |
OLD | NEW |