summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-07 18:49:09 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-04-07 18:49:09 +0000
commit517617476b7231f63b2f900d9dc825db2ee4161d (patch)
treea09d1e81c9796b0859a01dfc467710724046e8ce /base
parentd91ddfc676314764d240a9ad0da06063bb265d76 (diff)
downloadchromium_src-517617476b7231f63b2f900d9dc825db2ee4161d.zip
chromium_src-517617476b7231f63b2f900d9dc825db2ee4161d.tar.gz
chromium_src-517617476b7231f63b2f900d9dc825db2ee4161d.tar.bz2
Make the windows_version.h functions threadsafe by using a singleton. Add accessors to the singleton for more values that various code wants, then convert almost everyone using OSVERSIONINFO or SYSTEM_INFO structs to calling these accessors. Declare an AtExitManager in the out-of-process test runner since it didn't have one and that breaks singleton-using code in the test executable (as opposed to in chrome.dll).
A few other minor cleanups along the way (binding of "*", shorter code, etc.). Because I ran into problems with it while modifying gcapi.cc, I cleaned up our usage of strsafe.h a bit, so that files that don't need it don't include it and files that do use STRSAFE_NO_DEPRECATE instead of a modified #include order. BUG=none TEST=none Review URL: http://codereview.chromium.org/6713107 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80819 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_version_info_win.cc5
-rw-r--r--base/process_util_win.cc12
-rw-r--r--base/sys_info.h12
-rw-r--r--base/sys_info_chromeos.cc12
-rw-r--r--base/sys_info_mac.cc8
-rw-r--r--base/sys_info_win.cc41
-rw-r--r--base/win/windows_version.cc100
-rw-r--r--base/win/windows_version.h117
8 files changed, 156 insertions, 151 deletions
diff --git a/base/file_version_info_win.cc b/base/file_version_info_win.cc
index 953caa9..8c6820e 100644
--- a/base/file_version_info_win.cc
+++ b/base/file_version_info_win.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -12,9 +12,6 @@
#include "base/path_service.h"
#include "base/threading/thread_restrictions.h"
-// This has to be last.
-#include <strsafe.h>
-
FileVersionInfoWin::FileVersionInfoWin(void* data, int language, int code_page)
: language_(language), code_page_(code_page) {
base::ThreadRestrictions::AssertIOAllowed();
diff --git a/base/process_util_win.cc b/base/process_util_win.cc
index 8ade06e..a49b78c 100644
--- a/base/process_util_win.cc
+++ b/base/process_util_win.cc
@@ -17,6 +17,7 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/metrics/histogram.h"
+#include "base/sys_info.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_version.h"
@@ -554,12 +555,11 @@ bool CleanupProcesses(const std::wstring& executable_name,
///////////////////////////////////////////////////////////////////////////////
// ProcesMetrics
-ProcessMetrics::ProcessMetrics(ProcessHandle process) : process_(process),
- last_time_(0),
- last_system_time_(0) {
- SYSTEM_INFO system_info;
- GetSystemInfo(&system_info);
- processor_count_ = system_info.dwNumberOfProcessors;
+ProcessMetrics::ProcessMetrics(ProcessHandle process)
+ : process_(process),
+ processor_count_(base::SysInfo::NumberOfProcessors()),
+ last_time_(0),
+ last_system_time_(0) {
}
// static
diff --git a/base/sys_info.h b/base/sys_info.h
index 1d2939e..863e068 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -41,9 +41,9 @@ class BASE_API SysInfo {
// Retrieves detailed numeric values for the OS version.
// TODO(port): Implement a Linux version of this method and enable the
// corresponding unit test.
- static void OperatingSystemVersionNumbers(int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version);
+ static void OperatingSystemVersionNumbers(int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version);
// Returns the CPU architecture of the system. Exact return value may differ
// across platforms.
@@ -73,9 +73,9 @@ class BASE_API SysInfo {
// Parses /etc/lsb-release to get version information for Google Chrome OS.
// Declared here so it can be exposed for unit testing.
static void ParseLsbRelease(const std::string& lsb_release,
- int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version);
+ int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version);
#endif
};
diff --git a/base/sys_info_chromeos.cc b/base/sys_info_chromeos.cc
index b15d71b..cd17c18 100644
--- a/base/sys_info_chromeos.cc
+++ b/base/sys_info_chromeos.cc
@@ -43,9 +43,9 @@ static base::LazyInstance<ChromeOSVersionNumbers>
g_chrome_os_version_numbers(base::LINKER_INITIALIZED);
// static
-void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version) {
+void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version) {
if (!g_chrome_os_version_numbers.Get().parsed) {
// The other implementations of SysInfo don't block on the disk.
// See http://code.google.com/p/chromium/issues/detail?id=60394
@@ -75,9 +75,9 @@ std::string SysInfo::GetLinuxStandardBaseVersionKey() {
// static
void SysInfo::ParseLsbRelease(const std::string& lsb_release,
- int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version) {
+ int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version) {
size_t version_key_index = std::string::npos;
for (int i = 0; kLinuxStandardBaseVersionKeys[i] != NULL; ++i) {
version_key_index = lsb_release.find(kLinuxStandardBaseVersionKeys[i]);
diff --git a/base/sys_info_mac.cc b/base/sys_info_mac.cc
index 79caf55..e668421 100644
--- a/base/sys_info_mac.cc
+++ b/base/sys_info_mac.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -14,9 +14,9 @@
namespace base {
// static
-void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version) {
+void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version) {
Gestalt(gestaltSystemVersionMajor,
reinterpret_cast<SInt32*>(major_version));
Gestalt(gestaltSystemVersionMinor,
diff --git a/base/sys_info_win.cc b/base/sys_info_win.cc
index ce8376a..045d516 100644
--- a/base/sys_info_win.cc
+++ b/base/sys_info_win.cc
@@ -10,14 +10,13 @@
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
+#include "base/win/windows_version.h"
namespace base {
// static
int SysInfo::NumberOfProcessors() {
- SYSTEM_INFO info;
- GetSystemInfo(&info);
- return static_cast<int>(info.dwNumberOfProcessors);
+ return win::OSInfo::GetInstance()->processors();
}
// static
@@ -54,12 +53,17 @@ std::string SysInfo::OperatingSystemName() {
// static
std::string SysInfo::OperatingSystemVersion() {
- OSVERSIONINFO info = {0};
- info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
- GetVersionEx(&info);
-
- return base::StringPrintf("%lu.%lu",
- info.dwMajorVersion, info.dwMinorVersion);
+ win::OSInfo* os_info = win::OSInfo::GetInstance();
+ win::OSInfo::VersionNumber version_number = os_info->version_number();
+ std::string version(StringPrintf("%d.%d", version_number.major,
+ version_number.minor));
+ win::OSInfo::ServicePack service_pack = os_info->service_pack();
+ if (service_pack.major != 0) {
+ version += StringPrintf(" SP%d", service_pack.major);
+ if (service_pack.minor != 0)
+ version += StringPrintf(".%d", service_pack.minor);
+ }
+ return version;
}
// TODO: Implement OperatingSystemVersionComplete, which would include
@@ -88,21 +92,16 @@ int SysInfo::DisplayCount() {
// static
size_t SysInfo::VMAllocationGranularity() {
- SYSTEM_INFO sysinfo;
- GetSystemInfo(&sysinfo);
-
- return sysinfo.dwAllocationGranularity;
+ return win::OSInfo::GetInstance()->allocation_granularity();
}
// static
-void SysInfo::OperatingSystemVersionNumbers(int32 *major_version,
- int32 *minor_version,
- int32 *bugfix_version) {
- OSVERSIONINFO info = {0};
- info.dwOSVersionInfoSize = sizeof(info);
- GetVersionEx(&info);
- *major_version = info.dwMajorVersion;
- *minor_version = info.dwMinorVersion;
+void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
+ int32* minor_version,
+ int32* bugfix_version) {
+ win::OSInfo* os_info = win::OSInfo::GetInstance();
+ *major_version = os_info->version_number().major;
+ *minor_version = os_info->version_number().minor;
*bugfix_version = 0;
}
diff --git a/base/win/windows_version.cc b/base/win/windows_version.cc
index d53148d..8dc2d93 100644
--- a/base/win/windows_version.cc
+++ b/base/win/windows_version.cc
@@ -11,81 +11,49 @@
namespace base {
namespace win {
-Version GetVersion() {
- static bool checked_version = false;
- static Version win_version = VERSION_PRE_2000;
- if (!checked_version) {
- OSVERSIONINFOEX version_info;
- version_info.dwOSVersionInfoSize = sizeof version_info;
- GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
- if (version_info.dwMajorVersion == 5) {
- switch (version_info.dwMinorVersion) {
- case 0:
- win_version = VERSION_2000;
- break;
- case 1:
- win_version = VERSION_XP;
- break;
- case 2:
- default:
- win_version = VERSION_SERVER_2003;
- break;
- }
- } else if (version_info.dwMajorVersion == 6) {
- if (version_info.wProductType != VER_NT_WORKSTATION) {
- // 2008 is 6.0, and 2008 R2 is 6.1.
- win_version = VERSION_2008;
- } else {
- if (version_info.dwMinorVersion == 0) {
- win_version = VERSION_VISTA;
- } else {
- win_version = VERSION_WIN7;
- }
- }
- } else if (version_info.dwMajorVersion > 6) {
- win_version = VERSION_WIN7;
- }
- checked_version = true;
- }
- return win_version;
+// static
+OSInfo* OSInfo::GetInstance() {
+ return Singleton<OSInfo>::get();
}
-void GetServicePackLevel(int* major, int* minor) {
- DCHECK(major && minor);
- static bool checked_version = false;
- static int service_pack_major = -1;
- static int service_pack_minor = -1;
- if (!checked_version) {
- OSVERSIONINFOEX version_info = {0};
- version_info.dwOSVersionInfoSize = sizeof(version_info);
- GetVersionEx(reinterpret_cast<OSVERSIONINFOW*>(&version_info));
- service_pack_major = version_info.wServicePackMajor;
- service_pack_minor = version_info.wServicePackMinor;
- checked_version = true;
+OSInfo::OSInfo()
+ : version_(VERSION_PRE_XP),
+ architecture_(OTHER_ARCHITECTURE),
+ wow64_status_(GetWOW64StatusForProcess(GetCurrentProcess())) {
+ OSVERSIONINFOEX version_info = { sizeof version_info };
+ GetVersionEx(reinterpret_cast<OSVERSIONINFO*>(&version_info));
+ 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)) {
+ version_ = (version_number_.minor == 1) ? VERSION_XP : VERSION_SERVER_2003;
+ } else if (version_number_.major == 6) {
+ if (version_info.wProductType == VER_NT_WORKSTATION)
+ version_ = (version_number_.minor == 0) ? VERSION_VISTA : VERSION_WIN7;
+ else
+ version_ = VERSION_SERVER_2008;
+ } else if (version_number_.major > 6) {
+ version_ = VERSION_WIN7;
}
+ service_pack_.major = version_info.wServicePackMajor;
+ service_pack_.minor = version_info.wServicePackMinor;
- *major = service_pack_major;
- *minor = service_pack_minor;
-}
-
-WindowsArchitecture GetWindowsArchitecture() {
- SYSTEM_INFO system_info;
+ SYSTEM_INFO system_info = { 0 };
GetNativeSystemInfo(&system_info);
switch (system_info.wProcessorArchitecture) {
- case PROCESSOR_ARCHITECTURE_INTEL: return X86_ARCHITECTURE;
- case PROCESSOR_ARCHITECTURE_AMD64: return X64_ARCHITECTURE;
- case PROCESSOR_ARCHITECTURE_IA64: return IA64_ARCHITECTURE;
- default: return OTHER_ARCHITECTURE;
+ case PROCESSOR_ARCHITECTURE_INTEL: architecture_ = X86_ARCHITECTURE; break;
+ case PROCESSOR_ARCHITECTURE_AMD64: architecture_ = X64_ARCHITECTURE; break;
+ case PROCESSOR_ARCHITECTURE_IA64: architecture_ = IA64_ARCHITECTURE; break;
}
+ processors_ = system_info.dwNumberOfProcessors;
+ allocation_granularity_ = system_info.dwAllocationGranularity;
}
-WOW64Status GetWOW64Status() {
- static WOW64Status wow64_status =
- GetWOW64StatusForProcess(GetCurrentProcess());
- return wow64_status;
+OSInfo::~OSInfo() {
}
-WOW64Status GetWOW64StatusForProcess(HANDLE process_handle) {
+// static
+OSInfo::WOW64Status OSInfo::GetWOW64StatusForProcess(HANDLE process_handle) {
typedef BOOL (WINAPI* IsWow64ProcessFunc)(HANDLE, PBOOL);
IsWow64ProcessFunc is_wow64_process = reinterpret_cast<IsWow64ProcessFunc>(
GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process"));
@@ -97,5 +65,9 @@ WOW64Status GetWOW64StatusForProcess(HANDLE process_handle) {
return is_wow64 ? WOW64_ENABLED : WOW64_DISABLED;
}
+Version GetVersion() {
+ return OSInfo::GetInstance()->version();
+}
+
} // namespace win
} // namespace base
diff --git a/base/win/windows_version.h b/base/win/windows_version.h
index 1d4b9bf..96f3792 100644
--- a/base/win/windows_version.h
+++ b/base/win/windows_version.h
@@ -7,61 +7,98 @@
#pragma once
#include "base/base_api.h"
+#include "base/memory/singleton.h"
typedef void* HANDLE;
namespace base {
namespace win {
+// The running version of Windows. This is declared outside OSInfo for
+// syntactic sugar reasons; see the declaration of GetVersion() below.
// NOTE: Keep these in order so callers can do things like
-// "if (GetWinVersion() > WINVERSION_2000) ...". It's OK to change the values,
-// though.
+// "if (base::win::GetVersion() >= base::win::VERSION_VISTA) ...".
enum Version {
- VERSION_PRE_2000 = 0, // Not supported
- VERSION_2000 = 1, // Not supported
- VERSION_XP = 2,
- VERSION_SERVER_2003 = 3, // Also includes Windows XP Professional x64 edition
- VERSION_VISTA = 4,
- VERSION_2008 = 5,
- VERSION_WIN7 = 6,
+ VERSION_PRE_XP = 0, // Not supported.
+ VERSION_XP,
+ VERSION_SERVER_2003, // Also includes Windows XP Professional x64.
+ VERSION_VISTA,
+ VERSION_SERVER_2008,
+ VERSION_WIN7,
};
-// Returns the running version of Windows.
-BASE_API Version GetVersion();
+// A Singleton that can be used to query various pieces of information about the
+// OS and process state.
+class BASE_API OSInfo {
+ public:
+ struct VersionNumber {
+ int major;
+ int minor;
+ int build;
+ };
-// Returns the major and minor version of the service pack installed.
-BASE_API void GetServicePackLevel(int* major, int* minor);
+ struct ServicePack {
+ int major;
+ int minor;
+ };
-enum WindowsArchitecture {
- X86_ARCHITECTURE,
- X64_ARCHITECTURE,
- IA64_ARCHITECTURE,
- OTHER_ARCHITECTURE,
-};
+ // The processor architecture this copy of Windows natively uses. For
+ // example, given an x64-capable processor, we have three possibilities:
+ // 32-bit Chrome running on 32-bit Windows: X86_ARCHITECTURE
+ // 32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
+ // 64-bit Chrome running on 64-bit Windows: X64_ARCHITECTURE
+ enum WindowsArchitecture {
+ X86_ARCHITECTURE,
+ X64_ARCHITECTURE,
+ IA64_ARCHITECTURE,
+ OTHER_ARCHITECTURE,
+ };
-// Returns the processor architecture this copy of Windows natively uses.
-// For example, given an x64-capable processor, we have three possibilities:
-// 32-bit Chrome running on 32-bit Windows: X86_ARCHITECTURE
-// 32-bit Chrome running on 64-bit Windows via WOW64: X64_ARCHITECTURE
-// 64-bit Chrome running on 64-bit Windows: X64_ARCHITECTURE
-BASE_API WindowsArchitecture GetWindowsArchitecture();
-
-enum WOW64Status {
- WOW64_DISABLED,
- WOW64_ENABLED,
- WOW64_UNKNOWN,
-};
+ // Whether a process is running under WOW64 (the wrapper that allows 32-bit
+ // processes to run on 64-bit versions of Windows). This will return
+ // WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit
+ // Chrome on 64-bit Windows". WOW64_UNKNOWN means "an error occurred", e.g.
+ // the process does not have sufficient access rights to determine this.
+ enum WOW64Status {
+ WOW64_DISABLED,
+ WOW64_ENABLED,
+ WOW64_UNKNOWN,
+ };
+
+ static OSInfo* GetInstance();
+
+ Version version() const { return version_; }
+ // The next two functions return arrays of values, [major, minor(, build)].
+ VersionNumber version_number() const { return version_number_; }
+ ServicePack service_pack() const { return service_pack_; }
+ WindowsArchitecture architecture() const { return architecture_; }
+ int processors() const { return processors_; }
+ size_t allocation_granularity() const { return allocation_granularity_; }
+ WOW64Status wow64_status() const { return wow64_status_; }
-// Returns whether this process is running under WOW64 (the wrapper that allows
-// 32-bit processes to run on 64-bit versions of Windows). This will return
-// WOW64_DISABLED for both "32-bit Chrome on 32-bit Windows" and "64-bit Chrome
-// on 64-bit Windows". WOW64_UNKNOWN means "an error occurred", e.g. the
-// process does not have sufficient access rights to determine this.
-BASE_API WOW64Status GetWOW64Status();
+ // Like wow64_status(), but for the supplied handle instead of the current
+ // process. This doesn't touch member state, so you can bypass the singleton.
+ static WOW64Status GetWOW64StatusForProcess(HANDLE process_handle);
-// Like GetWOW64Status(), but for the supplied handle instead of the current
-// process.
-BASE_API WOW64Status GetWOW64StatusForProcess(HANDLE process_handle);
+ private:
+ OSInfo();
+ ~OSInfo();
+
+ Version version_;
+ VersionNumber version_number_;
+ ServicePack service_pack_;
+ WindowsArchitecture architecture_;
+ int processors_;
+ size_t allocation_granularity_;
+ WOW64Status wow64_status_;
+
+ friend struct DefaultSingletonTraits<OSInfo>;
+ DISALLOW_COPY_AND_ASSIGN(OSInfo);
+};
+
+// Because this is by far the most commonly-requested value from the above
+// singleton, we add a global-scope accessor here as syntactic sugar.
+BASE_API Version GetVersion();
} // namespace win
} // namespace base