Add ICU message format support
[chromium-blink-merge.git] / base / process / process_mac.cc
blob1913cc378e837c132b41dcd5ad447e0af21e342a
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.
5 #include "base/process/process.h"
7 #include "base/mac/mac_util.h"
8 #include "base/mac/mach_logging.h"
10 #include <mach/mach.h>
12 // The following was added to <mach/task_policy.h> after 10.8.
13 // TODO(shrike): Remove the TASK_OVERRIDE_QOS_POLICY ifndef once builders
14 // reach 10.9 or higher.
15 #ifndef TASK_OVERRIDE_QOS_POLICY
17 #define TASK_OVERRIDE_QOS_POLICY 9
19 typedef struct task_category_policy task_category_policy_data_t;
20 typedef struct task_category_policy* task_category_policy_t;
22 enum task_latency_qos {
23 LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
24 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
25 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
26 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
27 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
28 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
29 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
31 typedef integer_t task_latency_qos_t;
32 enum task_throughput_qos {
33 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
34 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
35 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
36 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
37 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
38 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
39 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
42 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
43 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
45 typedef integer_t task_throughput_qos_t;
47 struct task_qos_policy {
48 task_latency_qos_t task_latency_qos_tier;
49 task_throughput_qos_t task_throughput_qos_tier;
52 typedef struct task_qos_policy* task_qos_policy_t;
53 #define TASK_QOS_POLICY_COUNT \
54 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
56 #endif // TASK_OVERRIDE_QOS_POLICY
58 namespace base {
60 bool Process::CanBackgroundProcesses() {
61 return true;
64 bool Process::IsProcessBackgrounded(mach_port_t task_port) const {
65 // See SetProcessBackgrounded().
66 DCHECK(IsValid());
67 DCHECK_NE(task_port, TASK_NULL);
69 task_category_policy_data_t category_policy;
70 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
71 boolean_t get_default = FALSE;
73 kern_return_t result =
74 task_policy_get(task_port, TASK_CATEGORY_POLICY,
75 reinterpret_cast<task_policy_t>(&category_policy),
76 &task_info_count, &get_default);
77 MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result) <<
78 "task_policy_get TASK_CATEGORY_POLICY";
80 if (result == KERN_SUCCESS && get_default == FALSE) {
81 return category_policy.role == TASK_BACKGROUND_APPLICATION;
83 return false;
86 bool Process::SetProcessBackgrounded(mach_port_t task_port, bool background) {
87 DCHECK(IsValid());
88 DCHECK_NE(task_port, TASK_NULL);
90 if (!CanBackgroundProcesses()) {
91 return false;
92 } else if (IsProcessBackgrounded(task_port) == background) {
93 return true;
96 task_category_policy category_policy;
97 category_policy.role =
98 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
99 kern_return_t result =
100 task_policy_set(task_port, TASK_CATEGORY_POLICY,
101 reinterpret_cast<task_policy_t>(&category_policy),
102 TASK_CATEGORY_POLICY_COUNT);
104 if (result != KERN_SUCCESS) {
105 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY";
106 return false;
107 } else if (!mac::IsOSMavericksOrLater()) {
108 return true;
111 // Latency QoS regulates timer throttling/accuracy. Select default tier
112 // on foreground because precise timer firing isn't needed.
113 struct task_qos_policy qos_policy = {
114 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED,
115 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED
117 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY,
118 reinterpret_cast<task_policy_t>(&qos_policy),
119 TASK_QOS_POLICY_COUNT);
120 if (result != KERN_SUCCESS) {
121 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY";
122 return false;
125 return true;
128 } // namespace base