Chromium Code Reviews| Index: base/win/windows_version.cc |
| diff --git a/base/win/windows_version.cc b/base/win/windows_version.cc |
| index 7a8b8fdbdbaad6958759cdf603858d2387606bc5..61114cf09b54716aed85ca7e0f0325b2d5db70a9 100644 |
| --- a/base/win/windows_version.cc |
| +++ b/base/win/windows_version.cc |
| @@ -6,17 +6,106 @@ |
| #include <windows.h> |
| +#include "base/files/file_path.h" |
| #include "base/logging.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/win/registry.h" |
| namespace { |
| + |
| typedef BOOL (WINAPI *GetProductInfoPtr)(DWORD, DWORD, DWORD, DWORD, PDWORD); |
| + |
| +// Retrieve the type and version information from a given module. This function |
| +// calls GetFileVersionInfo() which can implicitly call LoadLibrary(). |
| +bool GetModuleVersionAndType(const base::FilePath& path, |
| + VS_FIXEDFILEINFO* vs_fixedfileinfo) { |
| + 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
|
| + if (!size) { |
| + PLOG_IF(WARNING, GetLastError() != ERROR_RESOURCE_TYPE_NOT_FOUND) |
| + << "GetFileVersionInfoSize: " << base::UTF16ToUTF8(path.value()); |
| + return false; |
| + } |
| + |
| + scoped_ptr<uint8_t[]> data(new uint8_t[size]); |
| + if (!::GetFileVersionInfo(path.value().c_str(), 0, size, data.get())) { |
| + PLOG(WARNING) << "GetFileVersionInfo: " << base::UTF16ToUTF8(path.value()); |
| + return false; |
| + } |
| + |
| + VS_FIXEDFILEINFO* fixed_file_info; |
| + UINT ffi_size; |
| + if (!::VerQueryValue(data.get(), L"\\", |
| + reinterpret_cast<void**>(&fixed_file_info), &ffi_size)) { |
| + PLOG(WARNING) << "VerQueryValue"; |
| + return false; |
| + } |
| + |
| + *vs_fixedfileinfo = *fixed_file_info; |
| + vs_fixedfileinfo->dwFileFlags &= vs_fixedfileinfo->dwFileFlagsMask; |
| + return true; |
| } |
| +} // namespace |
| + |
| namespace base { |
| namespace win { |
| +namespace { |
| + |
| +// Helper to map a major.minor.x.build version (e.g. 6.1) to a Windows release. |
| +base::win::Version MajorMinorBuildToVersion(int major, int minor, int build) { |
| + if ((major == 5) && (minor > 0)) { |
| + // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. |
| + return (minor == 1) ? VERSION_XP : VERSION_SERVER_2003; |
| + } else if (major == 6) { |
| + switch (minor) { |
| + case 0: |
| + // Treat Windows Server 2008 the same as Windows Vista. |
| + return VERSION_VISTA; |
| + case 1: |
| + // Treat Windows Server 2008 R2 the same as Windows 7. |
| + return VERSION_WIN7; |
| + case 2: |
| + // Treat Windows Server 2012 the same as Windows 8. |
| + return VERSION_WIN8; |
| + default: |
| + DCHECK_EQ(minor, 3); |
| + return VERSION_WIN8_1; |
| + } |
| + } else if (major == 10) { |
| + if (build < 10586) { |
| + return VERSION_WIN10; |
| + } else { |
| + return VERSION_WIN10_TH2; |
| + } |
| + } else if (major > 6) { |
| + NOTREACHED(); |
| + return VERSION_WIN_LAST; |
| + } |
| + |
| + NOTREACHED(); |
| + return VERSION_WIN_LAST; |
| +} |
| + |
| +// Retrieve a version from kernel32. This is useful because when running in |
| +// compatibility mode for a down-level version of the OS, the file version of |
| +// kernel32 will still be the "real" version. |
| +base::win::Version GetVersionFromKernel32() { |
| + const wchar_t kSystemDll[] = L"kernel32.dll"; |
| + VS_FIXEDFILEINFO ffi; |
| + if (::GetModuleVersionAndType(base::FilePath(kSystemDll), &ffi)) { |
| + const int major = ffi.dwFileVersionMS >> 16; |
| + const int minor = ffi.dwFileVersionMS & 0xffff; |
| + const int build = ffi.dwFileVersionLS >> 16; |
| + return MajorMinorBuildToVersion(major, minor, build); |
| + } |
| + |
| + NOTREACHED(); |
| + return VERSION_WIN_LAST; |
| +} |
| + |
| +} // namespace |
| + |
| // static |
| OSInfo* OSInfo::GetInstance() { |
| // Note: we don't use the Singleton class because it depends on AtExitManager, |
| @@ -42,38 +131,9 @@ OSInfo::OSInfo() |
| version_number_.major = version_info.dwMajorVersion; |
| version_number_.minor = version_info.dwMinorVersion; |
| version_number_.build = version_info.dwBuildNumber; |
| - if ((version_number_.major == 5) && (version_number_.minor > 0)) { |
| - // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. |
| - version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; |
| - } else if (version_number_.major == 6) { |
| - switch (version_number_.minor) { |
| - case 0: |
| - // Treat Windows Server 2008 the same as Windows Vista. |
| - version_ = VERSION_VISTA; |
| - break; |
| - case 1: |
| - // Treat Windows Server 2008 R2 the same as Windows 7. |
| - version_ = VERSION_WIN7; |
| - break; |
| - case 2: |
| - // Treat Windows Server 2012 the same as Windows 8. |
| - version_ = VERSION_WIN8; |
| - break; |
| - default: |
| - DCHECK_EQ(version_number_.minor, 3); |
| - version_ = VERSION_WIN8_1; |
| - break; |
| - } |
| - } else if (version_number_.major == 10) { |
| - if (version_number_.build < 10586) { |
| - version_ = VERSION_WIN10; |
| - } else { |
| - version_ = VERSION_WIN10_TH2; |
| - } |
| - } else if (version_number_.major > 6) { |
| - NOTREACHED(); |
| - version_ = VERSION_WIN_LAST; |
| - } |
| + version_ = MajorMinorBuildToVersion( |
| + version_number_.major, version_number_.minor, version_number_.build); |
| + kernel32_version_ = GetVersionFromKernel32(); |
| service_pack_.major = version_info.wServicePackMajor; |
| service_pack_.minor = version_info.wServicePackMinor; |