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

Side by Side Diff: base/process/process_mac.cc

Issue 989703002: Add support for backgrounding processes on the Mac (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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/process/process.h"
6 #include "base/mac/mac_util.h"
7 #include <mach/mach.h>
8
9 // The following was added to <mach/task_policy.h> after 10.8
10 #ifndef TASK_OVERRIDE_QOS_POLICY
11
12 #define TASK_OVERRIDE_QOS_POLICY 9
13
14 typedef struct task_category_policy task_category_policy_data_t;
15 typedef struct task_category_policy* task_category_policy_t;
16
17 #define TASK_CATEGORY_POLICY_COUNT \
18 ((mach_msg_type_number_t)(sizeof(task_category_policy_data_t) / \
19 sizeof(integer_t)))
20
21 enum task_latency_qos {
22 LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
23 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
24 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
25 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
26 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
27 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
28 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
29 };
30 typedef integer_t task_latency_qos_t;
31 enum task_throughput_qos {
32 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
33 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
34 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
35 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
36 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
37 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
38 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
39 };
40
41 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
42 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
43
44 typedef integer_t task_throughput_qos_t;
45
46 struct task_qos_policy {
47 task_latency_qos_t task_latency_qos_tier;
48 task_throughput_qos_t task_throughput_qos_tier;
49 };
50
51 typedef struct task_qos_policy* task_qos_policy_t;
52 #define TASK_QOS_POLICY_COUNT \
53 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
54
55 #endif // TASK_OVERRIDE_QOS_POLICY
56
57 namespace base {
58
59 bool Process::CanBackgroundProcesses() {
60 return true;
61 }
62
63 bool Process::IsProcessBackgrounded(mach_port_t process_port) const {
64 // See SetProcessBackgrounded().
65 DCHECK(IsValid());
66
67 task_category_policy_data_t category_policy;
68 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
69 boolean_t get_default = FALSE;
70
71 kern_return_t result = task_policy_get(process_port, TASK_CATEGORY_POLICY,
72 (task_policy_t)&category_policy,
73 &task_info_count, &get_default);
74 DPCHECK(result == KERN_SUCCESS);
75
76 if (result == KERN_SUCCESS && get_default == FALSE) {
77 return category_policy.role == TASK_BACKGROUND_APPLICATION;
78 }
79 return false;
80 }
81
82 bool Process::SetProcessBackgrounded(mach_port_t process_port,
83 bool background) {
84 DCHECK(IsValid());
85
86 if (!CanBackgroundProcesses() || process_port == 0) {
87 return false;
88 }
89
90 struct task_category_policy taskCatPolicy;
91 taskCatPolicy.role =
92 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
93 kern_return_t result = task_policy_set(process_port, TASK_CATEGORY_POLICY,
94 (thread_policy_t)&taskCatPolicy,
95 TASK_CATEGORY_POLICY_COUNT);
96 DPCHECK(result == KERN_SUCCESS);
97
98 if (result != KERN_SUCCESS) {
99 return false;
100 } else if (!mac::IsOSMavericksOrLater()) {
101 return true;
102 }
103
104 // Latency QoS regulates timer throttling/accuracy. Select default tier
105 // on foreground because precise timer firing isn't needed
106 struct task_qos_policy qosinfo = {
107 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_LAUNCH_DEFAULT_TIER,
108 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER};
109 result = task_policy_set(process_port, TASK_OVERRIDE_QOS_POLICY,
110 (task_policy_t)&qosinfo, TASK_QOS_POLICY_COUNT);
111 DPCHECK(result == KERN_SUCCESS);
112
113 return result == KERN_SUCCESS;
114 }
115
116 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698