HttpServer: don't crash in SetReceiveBufferSize/SetSendBufferSize if connection is...
[chromium-blink-merge.git] / base / sys_info_win.cc
blob817992c4f7a5705ce700568bae49da3696ead843
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/sys_info.h"
7 #include <windows.h>
8 #include <winioctl.h>
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/win/windows_version.h"
18 namespace {
20 int64 AmountOfMemory(DWORDLONG MEMORYSTATUSEX::* memory_field) {
21 MEMORYSTATUSEX memory_info;
22 memory_info.dwLength = sizeof(memory_info);
23 if (!GlobalMemoryStatusEx(&memory_info)) {
24 NOTREACHED();
25 return 0;
28 int64 rv = static_cast<int64>(memory_info.*memory_field);
29 return rv < 0 ? kint64max : rv;
32 } // namespace
34 namespace base {
36 // static
37 int SysInfo::NumberOfProcessors() {
38 return win::OSInfo::GetInstance()->processors();
41 // static
42 int64 SysInfo::AmountOfPhysicalMemory() {
43 return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys);
46 // static
47 int64 SysInfo::AmountOfAvailablePhysicalMemory() {
48 return AmountOfMemory(&MEMORYSTATUSEX::ullAvailPhys);
51 // static
52 int64 SysInfo::AmountOfVirtualMemory() {
53 return 0;
56 // static
57 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
58 ThreadRestrictions::AssertIOAllowed();
60 ULARGE_INTEGER available, total, free;
61 if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free))
62 return -1;
64 int64 rv = static_cast<int64>(available.QuadPart);
65 return rv < 0 ? kint64max : rv;
68 bool SysInfo::HasSeekPenalty(const FilePath& path, bool* has_seek_penalty) {
69 ThreadRestrictions::AssertIOAllowed();
71 DCHECK(path.IsAbsolute());
72 DCHECK(has_seek_penalty);
74 // TODO(dbeam): Vista, XP support.
75 if (win::GetVersion() < win::VERSION_WIN7)
76 return false;
78 std::vector<FilePath::StringType> components;
79 path.GetComponents(&components);
81 File drive(FilePath(L"\\\\.\\" + components[0]), File::FLAG_OPEN);
82 if (!drive.IsValid())
83 return false;
85 STORAGE_PROPERTY_QUERY query = {};
86 query.QueryType = PropertyStandardQuery;
87 query.PropertyId = StorageDeviceSeekPenaltyProperty;
89 DEVICE_SEEK_PENALTY_DESCRIPTOR result;
90 DWORD bytes_returned;
92 BOOL success = DeviceIoControl(drive.GetPlatformFile(),
93 IOCTL_STORAGE_QUERY_PROPERTY,
94 &query, sizeof(query),
95 &result, sizeof(result),
96 &bytes_returned,
97 NULL);
98 if (success == FALSE || bytes_returned < sizeof(result))
99 return false;
101 *has_seek_penalty = result.IncursSeekPenalty != FALSE;
102 return true;
105 // static
106 std::string SysInfo::OperatingSystemName() {
107 return "Windows NT";
110 // static
111 std::string SysInfo::OperatingSystemVersion() {
112 win::OSInfo* os_info = win::OSInfo::GetInstance();
113 win::OSInfo::VersionNumber version_number = os_info->version_number();
114 std::string version(StringPrintf("%d.%d", version_number.major,
115 version_number.minor));
116 win::OSInfo::ServicePack service_pack = os_info->service_pack();
117 if (service_pack.major != 0) {
118 version += StringPrintf(" SP%d", service_pack.major);
119 if (service_pack.minor != 0)
120 version += StringPrintf(".%d", service_pack.minor);
122 return version;
125 // TODO: Implement OperatingSystemVersionComplete, which would include
126 // patchlevel/service pack number.
127 // See chrome/browser/feedback/feedback_util.h, FeedbackUtil::SetOSVersion.
129 // static
130 std::string SysInfo::OperatingSystemArchitecture() {
131 win::OSInfo::WindowsArchitecture arch =
132 win::OSInfo::GetInstance()->architecture();
133 switch (arch) {
134 case win::OSInfo::X86_ARCHITECTURE:
135 return "x86";
136 case win::OSInfo::X64_ARCHITECTURE:
137 return "x86_64";
138 case win::OSInfo::IA64_ARCHITECTURE:
139 return "ia64";
140 default:
141 return "";
145 // static
146 std::string SysInfo::CPUModelName() {
147 return win::OSInfo::GetInstance()->processor_model_name();
150 // static
151 size_t SysInfo::VMAllocationGranularity() {
152 return win::OSInfo::GetInstance()->allocation_granularity();
155 // static
156 void SysInfo::OperatingSystemVersionNumbers(int32* major_version,
157 int32* minor_version,
158 int32* bugfix_version) {
159 win::OSInfo* os_info = win::OSInfo::GetInstance();
160 *major_version = os_info->version_number().major;
161 *minor_version = os_info->version_number().minor;
162 *bugfix_version = 0;
165 } // namespace base