michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/win/windows_version.h" michael@0: michael@0: #include michael@0: michael@0: #include "base/logging.h" michael@0: #include "base/strings/utf_string_conversions.h" michael@0: #include "base/win/registry.h" michael@0: michael@0: namespace base { michael@0: namespace win { michael@0: michael@0: // static michael@0: OSInfo* OSInfo::GetInstance() { michael@0: // Note: we don't use the Singleton class because it depends on AtExitManager, michael@0: // and it's convenient for other modules to use this classs without it. This michael@0: // pattern is copied from gurl.cc. michael@0: static OSInfo* info; michael@0: if (!info) { michael@0: OSInfo* new_info = new OSInfo(); michael@0: if (InterlockedCompareExchangePointer( michael@0: reinterpret_cast(&info), new_info, NULL)) { michael@0: delete new_info; michael@0: } michael@0: } michael@0: return info; michael@0: } michael@0: michael@0: OSInfo::OSInfo() michael@0: : version_(VERSION_PRE_XP), michael@0: architecture_(OTHER_ARCHITECTURE), michael@0: wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { michael@0: OSVERSIONINFOEX version_info = { sizeof version_info }; michael@0: GetVersionEx(reinterpret_cast(&version_info)); michael@0: version_number_.major = version_info.dwMajorVersion; michael@0: version_number_.minor = version_info.dwMinorVersion; michael@0: version_number_.build = version_info.dwBuildNumber; michael@0: if ((version_number_.major == 5) && (version_number_.minor > 0)) { michael@0: // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. michael@0: version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; michael@0: } else if (version_number_.major == 6) { michael@0: switch (version_number_.minor) { michael@0: case 0: michael@0: // Treat Windows Server 2008 the same as Windows Vista. michael@0: version_ = VERSION_VISTA; michael@0: break; michael@0: case 1: michael@0: // Treat Windows Server 2008 R2 the same as Windows 7. michael@0: version_ = VERSION_WIN7; michael@0: break; michael@0: case 2: michael@0: // Treat Windows Server 2012 the same as Windows 8. michael@0: version_ = VERSION_WIN8; michael@0: break; michael@0: default: michael@0: DCHECK_EQ(version_number_.minor, 3); michael@0: version_ = VERSION_WIN8_1; michael@0: break; michael@0: } michael@0: } else if (version_number_.major > 6) { michael@0: NOTREACHED(); michael@0: version_ = VERSION_WIN_LAST; michael@0: } michael@0: service_pack_.major = version_info.wServicePackMajor; michael@0: service_pack_.minor = version_info.wServicePackMinor; michael@0: michael@0: SYSTEM_INFO system_info = { 0 }; michael@0: GetNativeSystemInfo(&system_info); michael@0: switch (system_info.wProcessorArchitecture) { michael@0: case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; michael@0: case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; michael@0: case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; michael@0: } michael@0: processors_ = system_info.dwNumberOfProcessors; michael@0: allocation_granularity_ = system_info.dwAllocationGranularity; michael@0: } michael@0: michael@0: OSInfo::~OSInfo() { michael@0: } michael@0: michael@0: std::string OSInfo::processor_model_name() { michael@0: if (processor_model_name_.empty()) { michael@0: const wchar_t kProcessorNameString[] = michael@0: L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; michael@0: base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); michael@0: string16 value; michael@0: key.ReadValue(L"ProcessorNameString", &value); michael@0: processor_model_name_ = UTF16ToUTF8(value); michael@0: } michael@0: return processor_model_name_; michael@0: } michael@0: michael@0: // static michael@0: OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) { michael@0: typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL); michael@0: IsWow64ProcessFunc is_wow64_process = reinterpret_cast( michael@0: GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process")); michael@0: if (!is_wow64_process) michael@0: return WOW64_DISABLED; michael@0: BOOL is_wow64 = FALSE; michael@0: if (!(*is_wow64_process)(process_handle, &is_wow64)) michael@0: return WOW64_UNKNOWN; michael@0: return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; michael@0: } michael@0: michael@0: Version GetVersion() { michael@0: return OSInfo::GetInstance()->version(); michael@0: } michael@0: michael@0: } // namespace win michael@0: } // namespace base