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

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: Remove tab backgrounding from field trial 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 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
7 #include "base/mac/mac_util.h"
8 #include "base/mac/mach_logging.h"
9
10 #include <mach/mach.h>
11
12 // The following was added to <mach/task_policy.h> after 10.8.
13 #ifndef TASK_OVERRIDE_QOS_POLICY
14
15 #define TASK_OVERRIDE_QOS_POLICY 9
16
17 typedef struct task_category_policy task_category_policy_data_t;
18 typedef struct task_category_policy* task_category_policy_t;
19
20 enum task_latency_qos {
21 LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
22 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
23 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
24 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
25 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
26 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
27 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
28 };
29 typedef integer_t task_latency_qos_t;
30 enum task_throughput_qos {
31 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
32 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
33 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
34 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
35 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
36 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
37 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
38 };
39
40 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
41 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
42
43 typedef integer_t task_throughput_qos_t;
44
45 struct task_qos_policy {
46 task_latency_qos_t task_latency_qos_tier;
47 task_throughput_qos_t task_throughput_qos_tier;
48 };
49
50 typedef struct task_qos_policy* task_qos_policy_t;
51 #define TASK_QOS_POLICY_COUNT \
52 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
53
54 #endif // TASK_OVERRIDE_QOS_POLICY
55
56 namespace base {
57
58 bool Process::CanBackgroundProcesses() {
59 return true;
60 }
61
62 bool Process::IsProcessBackgrounded(mach_port_t task_port) const {
63 // See SetProcessBackgrounded().
64 DCHECK(IsValid());
65
66 task_category_policy_data_t category_policy;
67 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
68 boolean_t get_default = FALSE;
69
70 kern_return_t result =
71 task_policy_get(task_port, TASK_CATEGORY_POLICY,
72 reinterpret_cast<task_policy_t>(&category_policy),
73 &task_info_count, &get_default);
74 MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result) <<
75 "task_policy_get TASK_CATEGORY_POLICY";
76
77 if (result == KERN_SUCCESS && get_default == FALSE) {
78 return category_policy.role == TASK_BACKGROUND_APPLICATION;
79 }
80 return false;
81 }
82
83 bool Process::SetProcessBackgrounded(mach_port_t task_port, bool background) {
84 DCHECK(IsValid());
85
86 if (!CanBackgroundProcesses() || task_port == TASK_NULL) {
87 return false;
88 }
89
90 task_category_policy category_poilcy;
Avi (use Gerrit) 2015/03/18 16:56:30 typo in variable name
91 category_poilcy.role =
92 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
93 kern_return_t result =
94 task_policy_set(task_port, TASK_CATEGORY_POLICY,
95 reinterpret_cast<task_policy_t>(&category_poilcy),
96 TASK_CATEGORY_POLICY_COUNT);
97
98 if (result != KERN_SUCCESS) {
99 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY";
100 return false;
101 } else if (!mac::IsOSMavericksOrLater()) {
102 return true;
103 }
104
105 // Latency QoS regulates timer throttling/accuracy. Select default tier
106 // on foreground because precise timer firing isn't needed.
107 struct task_qos_policy qos_policy = {
108 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_LAUNCH_DEFAULT_TIER,
109 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER
110 };
111 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY,
112 reinterpret_cast<task_policy_t>(&qos_policy),
113 TASK_QOS_POLICY_COUNT);
114 if (result != KERN_SUCCESS) {
115 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY";
116 return false;
117 }
118
119 return true;
120 }
121
122 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698