Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/win/windows_version.h" |
michael@0 | 6 | |
michael@0 | 7 | #include <windows.h> |
michael@0 | 8 | |
michael@0 | 9 | #include "base/logging.h" |
michael@0 | 10 | #include "base/strings/utf_string_conversions.h" |
michael@0 | 11 | #include "base/win/registry.h" |
michael@0 | 12 | |
michael@0 | 13 | namespace base { |
michael@0 | 14 | namespace win { |
michael@0 | 15 | |
michael@0 | 16 | // static |
michael@0 | 17 | OSInfo* OSInfo::GetInstance() { |
michael@0 | 18 | // Note: we don't use the Singleton class because it depends on AtExitManager, |
michael@0 | 19 | // and it's convenient for other modules to use this classs without it. This |
michael@0 | 20 | // pattern is copied from gurl.cc. |
michael@0 | 21 | static OSInfo* info; |
michael@0 | 22 | if (!info) { |
michael@0 | 23 | OSInfo* new_info = new OSInfo(); |
michael@0 | 24 | if (InterlockedCompareExchangePointer( |
michael@0 | 25 | reinterpret_cast<PVOID*>(&info), new_info, NULL)) { |
michael@0 | 26 | delete new_info; |
michael@0 | 27 | } |
michael@0 | 28 | } |
michael@0 | 29 | return info; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | OSInfo::OSInfo() |
michael@0 | 33 | : version_(VERSION_PRE_XP), |
michael@0 | 34 | architecture_(OTHER_ARCHITECTURE), |
michael@0 | 35 | wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) { |
michael@0 | 36 | OSVERSIONINFOEX version_info = { sizeof version_info }; |
michael@0 | 37 | GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info)); |
michael@0 | 38 | version_number_.major = version_info.dwMajorVersion; |
michael@0 | 39 | version_number_.minor = version_info.dwMinorVersion; |
michael@0 | 40 | version_number_.build = version_info.dwBuildNumber; |
michael@0 | 41 | if ((version_number_.major == 5) && (version_number_.minor > 0)) { |
michael@0 | 42 | // Treat XP Pro x64, Home Server, and Server 2003 R2 as Server 2003. |
michael@0 | 43 | version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003; |
michael@0 | 44 | } else if (version_number_.major == 6) { |
michael@0 | 45 | switch (version_number_.minor) { |
michael@0 | 46 | case 0: |
michael@0 | 47 | // Treat Windows Server 2008 the same as Windows Vista. |
michael@0 | 48 | version_ = VERSION_VISTA; |
michael@0 | 49 | break; |
michael@0 | 50 | case 1: |
michael@0 | 51 | // Treat Windows Server 2008 R2 the same as Windows 7. |
michael@0 | 52 | version_ = VERSION_WIN7; |
michael@0 | 53 | break; |
michael@0 | 54 | case 2: |
michael@0 | 55 | // Treat Windows Server 2012 the same as Windows 8. |
michael@0 | 56 | version_ = VERSION_WIN8; |
michael@0 | 57 | break; |
michael@0 | 58 | default: |
michael@0 | 59 | DCHECK_EQ(version_number_.minor, 3); |
michael@0 | 60 | version_ = VERSION_WIN8_1; |
michael@0 | 61 | break; |
michael@0 | 62 | } |
michael@0 | 63 | } else if (version_number_.major > 6) { |
michael@0 | 64 | NOTREACHED(); |
michael@0 | 65 | version_ = VERSION_WIN_LAST; |
michael@0 | 66 | } |
michael@0 | 67 | service_pack_.major = version_info.wServicePackMajor; |
michael@0 | 68 | service_pack_.minor = version_info.wServicePackMinor; |
michael@0 | 69 | |
michael@0 | 70 | SYSTEM_INFO system_info = { 0 }; |
michael@0 | 71 | GetNativeSystemInfo(&system_info); |
michael@0 | 72 | switch (system_info.wProcessorArchitecture) { |
michael@0 | 73 | case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break; |
michael@0 | 74 | case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break; |
michael@0 | 75 | case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break; |
michael@0 | 76 | } |
michael@0 | 77 | processors_ = system_info.dwNumberOfProcessors; |
michael@0 | 78 | allocation_granularity_ = system_info.dwAllocationGranularity; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | OSInfo::~OSInfo() { |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | std::string OSInfo::processor_model_name() { |
michael@0 | 85 | if (processor_model_name_.empty()) { |
michael@0 | 86 | const wchar_t kProcessorNameString[] = |
michael@0 | 87 | L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"; |
michael@0 | 88 | base::win::RegKey key(HKEY_LOCAL_MACHINE, kProcessorNameString, KEY_READ); |
michael@0 | 89 | string16 value; |
michael@0 | 90 | key.ReadValue(L"ProcessorNameString", &value); |
michael@0 | 91 | processor_model_name_ = UTF16ToUTF8(value); |
michael@0 | 92 | } |
michael@0 | 93 | return processor_model_name_; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | // static |
michael@0 | 97 | OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) { |
michael@0 | 98 | typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL); |
michael@0 | 99 | IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>( |
michael@0 | 100 | GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process")); |
michael@0 | 101 | if (!is_wow64_process) |
michael@0 | 102 | return WOW64_DISABLED; |
michael@0 | 103 | BOOL is_wow64 = FALSE; |
michael@0 | 104 | if (!(*is_wow64_process)(process_handle, &is_wow64)) |
michael@0 | 105 | return WOW64_UNKNOWN; |
michael@0 | 106 | return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | Version GetVersion() { |
michael@0 | 110 | return OSInfo::GetInstance()->version(); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | } // namespace win |
michael@0 | 114 | } // namespace base |