Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3043)

Unified Diff: base/process/process_linux.cc

Issue 651253002: Enforce handle ownership in base::Process (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/process/process_linux.cc
diff --git a/base/process/process_linux.cc b/base/process/process_linux.cc
index 2c22d26484294024453b607b689309079b46999a..0560186ecf3129f5b437d5d392445103da33dd28 100644
--- a/base/process/process_linux.cc
+++ b/base/process/process_linux.cc
@@ -134,4 +134,63 @@ bool Process::CanBackgroundProcesses() {
return check_for_nice_permission.Get().can_reraise_priority;
}
+// TODO(rvargas) crbug.com/417532: Remove Process::*
+// -----------------------------------------------------------------------------
+
+// static
+bool ProcessObject::CanBackgroundProcesses() {
+#if defined(OS_CHROMEOS)
+ if (cgroups.Get().enabled)
+ return true;
+#endif
+
+ static LazyInstance<CheckForNicePermission> check_for_nice_permission =
+ LAZY_INSTANCE_INITIALIZER;
+ return check_for_nice_permission.Get().can_reraise_priority;
+}
+
+bool ProcessObject::IsProcessBackgrounded() const {
+ DCHECK(IsValid());
+
+#if defined(OS_CHROMEOS)
+ if (cgroups.Get().enabled) {
+ std::string proc;
+ if (base::ReadFileToString(
+ base::FilePath(StringPrintf(kProcPath, process_)),
+ &proc)) {
+ std::vector<std::string> proc_parts;
+ base::SplitString(proc, ':', &proc_parts);
+ DCHECK(proc_parts.size() == 3);
+ bool ret = proc_parts[2] == std::string(kBackground);
+ return ret;
+ } else {
+ return false;
+ }
+ }
+#endif
+ return GetPriority() == kBackgroundPriority;
+}
+
+bool ProcessObject::SetProcessBackgrounded(bool background) {
+ DCHECK(IsValid());
+
+#if defined(OS_CHROMEOS)
+ if (cgroups.Get().enabled) {
+ std::string pid = StringPrintf("%d", process_);
+ const base::FilePath file =
+ background ?
+ cgroups.Get().background_file : cgroups.Get().foreground_file;
+ return base::WriteFile(file, pid.c_str(), pid.size()) > 0;
+ }
+#endif // OS_CHROMEOS
+
+ if (!CanBackgroundProcesses())
+ return false;
+
+ int priority = background ? kBackgroundPriority : kForegroundPriority;
+ int result = setpriority(PRIO_PROCESS, process_, priority);
+ DPCHECK(result == 0);
+ return result == 0;
+}
+
} // namespace base

Powered by Google App Engine
This is Rietveld 408576698