Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/process/process.h" | |
| 6 | |
| 7 #include "base/feature_list.h" | |
| 8 #include "base/mac/mach_logging.h" | |
| 9 | |
| 10 #include <mach/mach.h> | |
|
Robert Sesek
2016/11/04 16:51:16
nit: goes between lines 5 and 7
https://google.gi
lgrey
2016/11/04 18:23:32
Done.
| |
| 11 | |
| 12 namespace base { | |
| 13 | |
| 14 // Enables backgrounding hidden renderers on Mac. | |
| 15 const Feature kMacAllowBackgroundingProcesses{"MacAllowBackgroundingProcesses", | |
| 16 FEATURE_DISABLED_BY_DEFAULT}; | |
| 17 | |
| 18 bool Process::CanBackgroundProcesses() { | |
| 19 return FeatureList::IsEnabled(kMacAllowBackgroundingProcesses); | |
| 20 } | |
| 21 | |
| 22 bool Process::IsProcessBackgrounded(PortProvider* port_provider) const { | |
| 23 DCHECK(IsValid()); | |
| 24 if (port_provider == nullptr || !CanBackgroundProcesses()) | |
| 25 return false; | |
| 26 | |
| 27 mach_port_t task_port = port_provider->TaskForPid(Pid()); | |
| 28 if (task_port == TASK_NULL) | |
| 29 return false; | |
| 30 | |
| 31 task_category_policy_data_t category_policy; | |
| 32 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT; | |
| 33 boolean_t get_default = FALSE; | |
| 34 | |
| 35 kern_return_t result = | |
| 36 task_policy_get(task_port, TASK_CATEGORY_POLICY, | |
| 37 reinterpret_cast<task_policy_t>(&category_policy), | |
| 38 &task_info_count, &get_default); | |
| 39 MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result) | |
| 40 << "task_policy_get TASK_CATEGORY_POLICY"; | |
| 41 | |
| 42 if (result == KERN_SUCCESS && get_default == FALSE) { | |
| 43 return category_policy.role == TASK_BACKGROUND_APPLICATION; | |
| 44 } | |
| 45 return false; | |
| 46 } | |
| 47 | |
| 48 bool Process::SetProcessBackgrounded(PortProvider* port_provider, | |
| 49 bool background) { | |
| 50 DCHECK(IsValid()); | |
| 51 if (port_provider == nullptr) | |
| 52 return false; | |
| 53 | |
| 54 mach_port_t task_port = port_provider->TaskForPid(Pid()); | |
| 55 if (task_port == TASK_NULL) | |
| 56 return false; | |
| 57 | |
| 58 if (!CanBackgroundProcesses()) { | |
|
Robert Sesek
2016/11/04 16:51:16
Move this check up to line 51, to match IsProcesBa
lgrey
2016/11/04 18:23:32
Done.
| |
| 59 return false; | |
| 60 } else if (IsProcessBackgrounded(port_provider) == background) { | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 task_category_policy category_policy; | |
| 65 category_policy.role = | |
| 66 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION; | |
| 67 kern_return_t result = | |
| 68 task_policy_set(task_port, TASK_CATEGORY_POLICY, | |
| 69 reinterpret_cast<task_policy_t>(&category_policy), | |
| 70 TASK_CATEGORY_POLICY_COUNT); | |
| 71 | |
| 72 if (result != KERN_SUCCESS) { | |
| 73 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY"; | |
| 74 return false; | |
| 75 } | |
| 76 | |
| 77 // Latency QoS regulates timer throttling/accuracy. Select default tier | |
| 78 // on foreground because precise timer firing isn't needed. | |
| 79 struct task_qos_policy qos_policy = { | |
| 80 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED, | |
| 81 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED}; | |
| 82 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY, | |
| 83 reinterpret_cast<task_policy_t>(&qos_policy), | |
| 84 TASK_QOS_POLICY_COUNT); | |
| 85 if (result != KERN_SUCCESS) { | |
| 86 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY"; | |
| 87 return false; | |
| 88 } | |
| 89 | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 } // namespace base | |
| OLD | NEW |