OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 #if defined(OS_CHROMEOS) | 127 #if defined(OS_CHROMEOS) |
128 if (cgroups.Get().enabled) | 128 if (cgroups.Get().enabled) |
129 return true; | 129 return true; |
130 #endif | 130 #endif |
131 | 131 |
132 static LazyInstance<CheckForNicePermission> check_for_nice_permission = | 132 static LazyInstance<CheckForNicePermission> check_for_nice_permission = |
133 LAZY_INSTANCE_INITIALIZER; | 133 LAZY_INSTANCE_INITIALIZER; |
134 return check_for_nice_permission.Get().can_reraise_priority; | 134 return check_for_nice_permission.Get().can_reraise_priority; |
135 } | 135 } |
136 | 136 |
| 137 // TODO(rvargas) crbug.com/417532: Remove Process::* |
| 138 // ----------------------------------------------------------------------------- |
| 139 |
| 140 // static |
| 141 bool ProcessObject::CanBackgroundProcesses() { |
| 142 #if defined(OS_CHROMEOS) |
| 143 if (cgroups.Get().enabled) |
| 144 return true; |
| 145 #endif |
| 146 |
| 147 static LazyInstance<CheckForNicePermission> check_for_nice_permission = |
| 148 LAZY_INSTANCE_INITIALIZER; |
| 149 return check_for_nice_permission.Get().can_reraise_priority; |
| 150 } |
| 151 |
| 152 bool ProcessObject::IsProcessBackgrounded() const { |
| 153 DCHECK(IsValid()); |
| 154 |
| 155 #if defined(OS_CHROMEOS) |
| 156 if (cgroups.Get().enabled) { |
| 157 std::string proc; |
| 158 if (base::ReadFileToString( |
| 159 base::FilePath(StringPrintf(kProcPath, process_)), |
| 160 &proc)) { |
| 161 std::vector<std::string> proc_parts; |
| 162 base::SplitString(proc, ':', &proc_parts); |
| 163 DCHECK(proc_parts.size() == 3); |
| 164 bool ret = proc_parts[2] == std::string(kBackground); |
| 165 return ret; |
| 166 } else { |
| 167 return false; |
| 168 } |
| 169 } |
| 170 #endif |
| 171 return GetPriority() == kBackgroundPriority; |
| 172 } |
| 173 |
| 174 bool ProcessObject::SetProcessBackgrounded(bool background) { |
| 175 DCHECK(IsValid()); |
| 176 |
| 177 #if defined(OS_CHROMEOS) |
| 178 if (cgroups.Get().enabled) { |
| 179 std::string pid = StringPrintf("%d", process_); |
| 180 const base::FilePath file = |
| 181 background ? |
| 182 cgroups.Get().background_file : cgroups.Get().foreground_file; |
| 183 return base::WriteFile(file, pid.c_str(), pid.size()) > 0; |
| 184 } |
| 185 #endif // OS_CHROMEOS |
| 186 |
| 187 if (!CanBackgroundProcesses()) |
| 188 return false; |
| 189 |
| 190 int priority = background ? kBackgroundPriority : kForegroundPriority; |
| 191 int result = setpriority(PRIO_PROCESS, process_, priority); |
| 192 DPCHECK(result == 0); |
| 193 return result == 0; |
| 194 } |
| 195 |
137 } // namespace base | 196 } // namespace base |
OLD | NEW |