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