Upload upstream chromium 69.0.3497
[platform/framework/web/chromium-efl.git] / base / sys_info_win.cc
1 // Copyright 2015 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.
4
5 #include "base/sys_info.h"
6
7 #include <windows.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <limits>
12
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/process/process_metrics.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/win/windows_version.h"
19
20 namespace {
21
22 int64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) {
23   MEMORYSTATUSEX memory_info;
24   memory_info.dwLength = sizeof(memory_info);
25   if (!GlobalMemoryStatusEx(&memory_info)) {
26     NOTREACHED();
27     return 0;
28   }
29
30   int64_t rv = static_cast<int64_t>(memory_info.*memory_field);
31   return rv < 0 ? std::numeric_limits<int64_t>::max() : rv;
32 }
33
34 bool GetDiskSpaceInfo(const base::FilePath& path,
35                       int64_t* available_bytes,
36                       int64_t* total_bytes) {
37   ULARGE_INTEGER available;
38   ULARGE_INTEGER total;
39   ULARGE_INTEGER free;
40   if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free))
41     return false;
42
43   if (available_bytes) {
44     *available_bytes = static_cast<int64_t>(available.QuadPart);
45     if (*available_bytes < 0)
46       *available_bytes = std::numeric_limits<int64_t>::max();
47   }
48   if (total_bytes) {
49     *total_bytes = static_cast<int64_t>(total.QuadPart);
50     if (*total_bytes < 0)
51       *total_bytes = std::numeric_limits<int64_t>::max();
52   }
53   return true;
54 }
55
56 }  // namespace
57
58 namespace base {
59
60 // static
61 int SysInfo::NumberOfProcessors() {
62   return win::OSInfo::GetInstance()->processors();
63 }
64
65 // static
66 int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
67   return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys);
68 }
69
70 // static
71 int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
72   SystemMemoryInfoKB info;
73   if (!GetSystemMemoryInfo(&info))
74     return 0;
75   return static_cast<int64_t>(info.avail_phys) * 1024;
76 }
77
78 // static
79 int64_t SysInfo::AmountOfVirtualMemory() {
80   return AmountOfMemory(&MEMORYSTATUSEX::ullTotalVirtual);
81 }
82
83 // static
84 int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
85   AssertBlockingAllowed();
86
87   int64_t available;
88   if (!GetDiskSpaceInfo(path, &available, nullptr))
89     return -1;
90   return available;
91 }
92
93 // static
94 int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) {
95   AssertBlockingAllowed();
96
97   int64_t total;
98   if (!GetDiskSpaceInfo(path, nullptr, &total))
99     return -1;
100   return total;
101 }
102
103 std::string SysInfo::OperatingSystemName() {
104   return "Windows NT";
105 }
106
107 // static
108 std::string SysInfo::OperatingSystemVersion() {
109   win::OSInfo* os_info = win::OSInfo::GetInstance();
110   win::OSInfo::VersionNumber version_number = os_info->version_number();
111   std::string version(StringPrintf("%d.%d.%d", version_number.major,
112                                    version_number.minor,
113                                    version_number.build));
114   win::OSInfo::ServicePack service_pack = os_info->service_pack();
115   if (service_pack.major != 0) {
116     version += StringPrintf(" SP%d", service_pack.major);
117     if (service_pack.minor != 0)
118       version += StringPrintf(".%d", service_pack.minor);
119   }
120   return version;
121 }
122
123 // TODO: Implement OperatingSystemVersionComplete, which would include
124 // patchlevel/service pack number.
125 // See chrome/browser/feedback/feedback_util.h, FeedbackUtil::SetOSVersion.
126
127 // static
128 std::string SysInfo::OperatingSystemArchitecture() {
129   win::OSInfo::WindowsArchitecture arch =
130       win::OSInfo::GetInstance()->architecture();
131   switch (arch) {
132     case win::OSInfo::X86_ARCHITECTURE:
133       return "x86";
134     case win::OSInfo::X64_ARCHITECTURE:
135       return "x86_64";
136     case win::OSInfo::IA64_ARCHITECTURE:
137       return "ia64";
138     default:
139       return "";
140   }
141 }
142
143 // static
144 std::string SysInfo::CPUModelName() {
145   return win::OSInfo::GetInstance()->processor_model_name();
146 }
147
148 // static
149 size_t SysInfo::VMAllocationGranularity() {
150   return win::OSInfo::GetInstance()->allocation_granularity();
151 }
152
153 // static
154 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
155                                             int32_t* minor_version,
156                                             int32_t* bugfix_version) {
157   win::OSInfo* os_info = win::OSInfo::GetInstance();
158   *major_version = os_info->version_number().major;
159   *minor_version = os_info->version_number().minor;
160   *bugfix_version = 0;
161 }
162
163 }  // namespace base