Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <winioctl.h> | |
| 8 | 9 |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/win/scoped_handle.h" | |
| 14 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 int64 AmountOfMemory(DWORDLONG MEMORYSTATUSEX::* memory_field) { | 20 int64 AmountOfMemory(DWORDLONG MEMORYSTATUSEX::* memory_field) { |
| 19 MEMORYSTATUSEX memory_info; | 21 MEMORYSTATUSEX memory_info; |
| 20 memory_info.dwLength = sizeof(memory_info); | 22 memory_info.dwLength = sizeof(memory_info); |
| 21 if (!GlobalMemoryStatusEx(&memory_info)) { | 23 if (!GlobalMemoryStatusEx(&memory_info)) { |
| 22 NOTREACHED(); | 24 NOTREACHED(); |
| 23 return 0; | 25 return 0; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 48 return AmountOfMemory(&MEMORYSTATUSEX::ullAvailPhys); | 50 return AmountOfMemory(&MEMORYSTATUSEX::ullAvailPhys); |
| 49 } | 51 } |
| 50 | 52 |
| 51 // static | 53 // static |
| 52 int64 SysInfo::AmountOfVirtualMemory() { | 54 int64 SysInfo::AmountOfVirtualMemory() { |
| 53 return 0; | 55 return 0; |
| 54 } | 56 } |
| 55 | 57 |
| 56 // static | 58 // static |
| 57 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { | 59 int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
| 58 base::ThreadRestrictions::AssertIOAllowed(); | 60 ThreadRestrictions::AssertIOAllowed(); |
| 59 | 61 |
| 60 ULARGE_INTEGER available, total, free; | 62 ULARGE_INTEGER available, total, free; |
| 61 if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free)) { | 63 if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free)) |
| 62 return -1; | 64 return -1; |
| 65 | |
| 66 int64 rv = static_cast<int64>(available.QuadPart); | |
| 67 return rv < 0 ? kint64max : rv; | |
| 68 } | |
| 69 | |
| 70 bool SysInfo::HasSeekPenalty(const FilePath& path, bool* has_seek_penalty) { | |
| 71 ThreadRestrictions::AssertIOAllowed(); | |
| 72 | |
| 73 if (!path.IsAbsolute() || !has_seek_penalty) | |
|
rvargas (doing something else)
2015/03/12 22:01:12
I would probably also DCHECK the first condition g
Dan Beam
2015/03/13 19:31:32
Done.
| |
| 74 return false; | |
| 75 | |
| 76 std::vector<FilePath::StringType> components; | |
| 77 path.GetComponents(&components); | |
| 78 | |
| 79 FilePath::StringType drive = L"\\\\.\\" + components[0]; | |
|
rvargas (doing something else)
2015/03/12 22:01:12
This syntax is not supported before win7
Dan Beam
2015/03/13 19:31:32
Acknowledged.
| |
| 80 win::ScopedHandle handle(CreateFile( | |
|
rvargas (doing something else)
2015/03/12 22:01:12
Looks like a job for base::File
Dan Beam
2015/03/13 19:31:32
Done.
| |
| 81 drive.c_str(), | |
| 82 0, | |
| 83 FILE_SHARE_READ | FILE_SHARE_WRITE, | |
| 84 NULL, | |
| 85 OPEN_EXISTING, | |
| 86 0, | |
| 87 NULL)); | |
| 88 | |
| 89 if (!handle.IsValid()) | |
| 90 return false; | |
| 91 | |
| 92 STORAGE_PROPERTY_QUERY query; | |
| 93 query.QueryType = PropertyStandardQuery; | |
| 94 query.PropertyId = StorageDeviceSeekPenaltyProperty; | |
|
rvargas (doing something else)
2015/03/12 22:01:11
Not supported on XP
Dan Beam
2015/03/13 19:31:32
Acknowledged.
| |
| 95 | |
| 96 DEVICE_SEEK_PENALTY_DESCRIPTOR result; | |
| 97 DWORD unused_bytes_returned; | |
| 98 | |
| 99 if (!DeviceIoControl(handle.Get(), | |
| 100 IOCTL_STORAGE_QUERY_PROPERTY, | |
| 101 &query, sizeof(query), | |
| 102 &result, sizeof(result), | |
| 103 &unused_bytes_returned, | |
|
rvargas (doing something else)
2015/03/12 22:01:12
Verify that this is >= sizeof(result) before acces
Dan Beam
2015/03/13 19:31:32
Done.
| |
| 104 NULL)) { | |
| 105 return false; | |
| 63 } | 106 } |
| 64 int64 rv = static_cast<int64>(available.QuadPart); | 107 |
| 65 if (rv < 0) | 108 *has_seek_penalty = !!result.IncursSeekPenalty; |
|
rvargas (doing something else)
2015/03/12 22:01:12
nit: != FALSE? (I hate using "!!" to do this).
Dan Beam
2015/03/13 19:31:32
why not == TRUE?
| |
| 66 rv = kint64max; | 109 return true; |
| 67 return rv; | |
| 68 } | 110 } |
| 69 | 111 |
| 70 // static | 112 // static |
| 71 std::string SysInfo::OperatingSystemName() { | 113 std::string SysInfo::OperatingSystemName() { |
| 72 return "Windows NT"; | 114 return "Windows NT"; |
| 73 } | 115 } |
| 74 | 116 |
| 75 // static | 117 // static |
| 76 std::string SysInfo::OperatingSystemVersion() { | 118 std::string SysInfo::OperatingSystemVersion() { |
| 77 win::OSInfo* os_info = win::OSInfo::GetInstance(); | 119 win::OSInfo* os_info = win::OSInfo::GetInstance(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, | 163 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, |
| 122 int32* minor_version, | 164 int32* minor_version, |
| 123 int32* bugfix_version) { | 165 int32* bugfix_version) { |
| 124 win::OSInfo* os_info = win::OSInfo::GetInstance(); | 166 win::OSInfo* os_info = win::OSInfo::GetInstance(); |
| 125 *major_version = os_info->version_number().major; | 167 *major_version = os_info->version_number().major; |
| 126 *minor_version = os_info->version_number().minor; | 168 *minor_version = os_info->version_number().minor; |
| 127 *bugfix_version = 0; | 169 *bugfix_version = 0; |
| 128 } | 170 } |
| 129 | 171 |
| 130 } // namespace base | 172 } // namespace base |
| OLD | NEW |