Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: base/win/windows_version.cc

Issue 1784623003: Add histograms to compare GetVersionEx() with VerQueryValue() of kernel32 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/win/registry.h" 12 #include "base/win/registry.h"
12 13
13 namespace { 14 namespace {
15
14 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); 16 typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD);
17
18 // Retrieve the type and version information from a given module. This function
19 // calls GetFileVersionInfo() which can implicitly call LoadLibrary().
20 bool GetModuleVersionAndType(const base::FilePath& path,
21 VS_FIXEDFILEINFO* vs_fixedfileinfo) {
22 DWORD size = ::GetFileVersionInfoSize(path.value().c_str(), nullptr);
Will Harris 2016/03/09 23:02:50 maybe assert IO is allowed for thread, given this
scottmg 2016/03/10 00:41:31 I don't think I can do that because there's versio
23 if (!size) {
24 PLOG_IF(WARNING, GetLastError() != ERROR_RESOURCE_TYPE_NOT_FOUND)
25 << "GetFileVersionInfoSize: " << base::UTF16ToUTF8(path.value());
26 return false;
27 }
28
29 scoped_ptr<uint8_t[]> data(new uint8_t[size]);
30 if (!::GetFileVersionInfo(path.value().c_str(), 0, size, data.get())) {
31 PLOG(WARNING) << "GetFileVersionInfo: " << base::UTF16ToUTF8(path.value());
32 return false;
33 }
34
35 VS_FIXEDFILEINFO* fixed_file_info;
36 UINT ffi_size;
37 if (!::VerQueryValue(data.get(), L"\\",
38 reinterpret_cast<void**>(&fixed_file_info), &ffi_size)) {
39 PLOG(WARNING) << "VerQueryValue";
40 return false;
41 }
42
43 *vs_fixedfileinfo = *fixed_file_info;
44 vs_fixedfileinfo->dwFileFlags &= vs_fixedfileinfo->dwFileFlagsMask;
45 return true;
15 } 46 }
16 47
48 } // namespace
49
17 namespace base { 50 namespace base {
18 namespace win { 51 namespace win {
19 52
53 namespace {
54
55 // Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release.
56 base::win::Version MajorMinorBuildToVersion(int major, int minor, int build) {
57 if ((major == 5) && (minor > 0)) {
58 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003.
59 return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003;
60 } else if (major == 6) {
61 switch (minor) {
62 case 0:
63 // Treat Windows Server 2008 the same as Windows Vista.
64 return VERSION_VISTA;
65 case 1:
66 // Treat Windows Server 2008 R2 the same as Windows 7.
67 return VERSION_WIN7;
68 case 2:
69 // Treat Windows Server 2012 the same as Windows 8.
70 return VERSION_WIN8;
71 default:
72 DCHECK_EQ(minor, 3);
73 return VERSION_WIN8_1;
74 }
75 } else if (major == 10) {
76 if (build < 10586) {
77 return VERSION_WIN10;
78 } else {
79 return VERSION_WIN10_TH2;
80 }
81 } else if (major > 6) {
82 NOTREACHED();
83 return VERSION_WIN_LAST;
84 }
85
86 NOTREACHED();
87 return VERSION_WIN_LAST;
88 }
89
90 // Retrieve a version from kernel32. This is useful because when running in
91 // compatibility mode for a down-level version of the OS, the file version of
92 // kernel32 will still be the "real" version.
93 base::win::Version GetVersionFromKernel32() {
94 const wchar_t kSystemDll[] = L"kernel32.dll";
95 VS_FIXEDFILEINFO ffi;
96 if (::GetModuleVersionAndType(base::FilePath(kSystemDll), &ffi)) {
97 const int major = ffi.dwFileVersionMS >> 16;
98 const int minor = ffi.dwFileVersionMS & 0xffff;
99 const int build = ffi.dwFileVersionLS >> 16;
100 return MajorMinorBuildToVersion(major, minor, build);
101 }
102
103 NOTREACHED();
104 return VERSION_WIN_LAST;
105 }
106
107 } // namespace
108
20 // static 109 // static
21 OSInfo* OSInfo::GetInstance() { 110 OSInfo* OSInfo::GetInstance() {
22 // Note: we don't use the Singleton class because it depends on AtExitManager, 111 // 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 112 // and it's convenient for other modules to use this classs without it. This
24 // pattern is copied from gurl.cc. 113 // pattern is copied from gurl.cc.
25 static OSInfo* info; 114 static OSInfo* info;
26 if (!info) { 115 if (!info) {
27 OSInfo* new_info = new OSInfo(); 116 OSInfo* new_info = new OSInfo();
28 if (InterlockedCompareExchangePointer( 117 if (InterlockedCompareExchangePointer(
29 reinterpret_cast<PVOID*>(&info), new_info, NULL)) { 118 reinterpret_cast<PVOID*>(&info), new_info, NULL)) {
30 delete new_info; 119 delete new_info;
31 } 120 }
32 } 121 }
33 return info; 122 return info;
34 } 123 }
35 124
36 OSInfo::OSInfo() 125 OSInfo::OSInfo()
37 : version_(VERSION_PRE_XP), 126 : version_(VERSION_PRE_XP),
38 architecture_(OTHER_ARCHITECTURE), 127 architecture_(OTHER_ARCHITECTURE),
39 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { 128 wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
40 OSVERSIONINFOEX version_info = { sizeof version_info }; 129 OSVERSIONINFOEX version_info = { sizeof version_info };
41 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); 130 ::GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
42 version_number_.major = version_info.dwMajorVersion; 131 version_number_.major = version_info.dwMajorVersion;
43 version_number_.minor = version_info.dwMinorVersion; 132 version_number_.minor = version_info.dwMinorVersion;
44 version_number_.build = version_info.dwBuildNumber; 133 version_number_.build = version_info.dwBuildNumber;
45 if ((version_number_.major == 5) && (version_number_.minor > 0)) { 134 version_ = MajorMinorBuildToVersion(
46 // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. 135 version_number_.major, version_number_.minor, version_number_.build);
47 version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; 136 kernel32_version_ = GetVersionFromKernel32();
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; 137 service_pack_.major = version_info.wServicePackMajor;
78 service_pack_.minor = version_info.wServicePackMinor; 138 service_pack_.minor = version_info.wServicePackMinor;
79 139
80 SYSTEM_INFO system_info = {}; 140 SYSTEM_INFO system_info = {};
81 ::GetNativeSystemInfo(&system_info); 141 ::GetNativeSystemInfo(&system_info);
82 switch (system_info.wProcessorArchitecture) { 142 switch (system_info.wProcessorArchitecture) {
83 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; 143 case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break;
84 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; 144 case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break;
85 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; 145 case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break;
86 } 146 }
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 return WOW64_UNKNOWN; 233 return WOW64_UNKNOWN;
174 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; 234 return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
175 } 235 }
176 236
177 Version GetVersion() { 237 Version GetVersion() {
178 return OSInfo::GetInstance()->version(); 238 return OSInfo::GetInstance()->version();
179 } 239 }
180 240
181 } // namespace win 241 } // namespace win
182 } // namespace base 242 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698