1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/win/windows_version.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,114 @@ 1.4 +// Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "base/win/windows_version.h" 1.9 + 1.10 +#include <windows.h> 1.11 + 1.12 +#include "base/logging.h" 1.13 +#include "base/strings/utf_string_conversions.h" 1.14 +#include "base/win/registry.h" 1.15 + 1.16 +namespace base { 1.17 +namespace win { 1.18 + 1.19 +// static 1.20 +OSInfo* OSInfo::GetInstance() { 1.21 + // Note: we don't use the Singleton class because it depends on AtExitManager, 1.22 + // and it's convenient for other modules to use this classs without it. This 1.23 + // pattern is copied from gurl.cc. 1.24 + static OSInfo* info; 1.25 + if (!info) { 1.26 + OSInfo* new_info = new OSInfo(); 1.27 + if (InterlockedCompareExchangePointer( 1.28 + reinterpret_cast<PVOID*>(&info), new_info, NULL)) { 1.29 + delete new_info; 1.30 + } 1.31 + } 1.32 + return info; 1.33 +} 1.34 + 1.35 +OSInfo::OSInfo() 1.36 + : version_(VERSION_PRE_XP), 1.37 + architecture_(OTHER_ARCHITECTURE), 1.38 + wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { 1.39 + OSVERSIONINFOEX version_info = { sizeof version_info }; 1.40 + GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); 1.41 + version_number_.major = version_info.dwMajorVersion; 1.42 + version_number_.minor = version_info.dwMinorVersion; 1.43 + version_number_.build = version_info.dwBuildNumber; 1.44 + if ((version_number_.major == 5) && (version_number_.minor > 0)) { 1.45 + // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. 1.46 + version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; 1.47 + } else if (version_number_.major == 6) { 1.48 + switch (version_number_.minor) { 1.49 + case 0: 1.50 + // Treat Windows Server 2008 the same as Windows Vista. 1.51 + version_ = VERSION_VISTA; 1.52 + break; 1.53 + case 1: 1.54 + // Treat Windows Server 2008 R2 the same as Windows 7. 1.55 + version_ = VERSION_WIN7; 1.56 + break; 1.57 + case 2: 1.58 + // Treat Windows Server 2012 the same as Windows 8. 1.59 + version_ = VERSION_WIN8; 1.60 + break; 1.61 + default: 1.62 + DCHECK_EQ(version_number_.minor, 3); 1.63 + version_ = VERSION_WIN8_1; 1.64 + break; 1.65 + } 1.66 + } else if (version_number_.major > 6) { 1.67 + NOTREACHED(); 1.68 + version_ = VERSION_WIN_LAST; 1.69 + } 1.70 + service_pack_.major = version_info.wServicePackMajor; 1.71 + service_pack_.minor = version_info.wServicePackMinor; 1.72 + 1.73 + SYSTEM_INFO system_info = { 0 }; 1.74 + GetNativeSystemInfo(&system_info); 1.75 + switch (system_info.wProcessorArchitecture) { 1.76 + case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; 1.77 + case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; 1.78 + case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; 1.79 + } 1.80 + processors_ = system_info.dwNumberOfProcessors; 1.81 + allocation_granularity_ = system_info.dwAllocationGranularity; 1.82 +} 1.83 + 1.84 +OSInfo::~OSInfo() { 1.85 +} 1.86 + 1.87 +std::string OSInfo::processor_model_name() { 1.88 + if (processor_model_name_.empty()) { 1.89 + const wchar_t kProcessorNameString[] = 1.90 + L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; 1.91 + base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); 1.92 + string16 value; 1.93 + key.ReadValue(L"ProcessorNameString", &value); 1.94 + processor_model_name_ = UTF16ToUTF8(value); 1.95 + } 1.96 + return processor_model_name_; 1.97 +} 1.98 + 1.99 +// static 1.100 +OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) { 1.101 + typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL); 1.102 + IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>( 1.103 + GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process")); 1.104 + if (!is_wow64_process) 1.105 + return WOW64_DISABLED; 1.106 + BOOL is_wow64 = FALSE; 1.107 + if (!(*is_wow64_process)(process_handle, &is_wow64)) 1.108 + return WOW64_UNKNOWN; 1.109 + return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; 1.110 +} 1.111 + 1.112 +Version GetVersion() { 1.113 + return OSInfo::GetInstance()->version(); 1.114 +} 1.115 + 1.116 +} // namespace win 1.117 +} // namespace base