summaryrefslogtreecommitdiff
path: root/chromium/base/process/process_mac.cc
blob: f83fbb9991e952e3859e6db9dc8811761b1ea4b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/process/process.h"

#include <mach/mach.h>

#include "base/feature_list.h"
#include "base/mac/mach_logging.h"

namespace base {

// Enables backgrounding hidden renderers on Mac.
const Feature kMacAllowBackgroundingProcesses{"MacAllowBackgroundingProcesses",
                                              FEATURE_DISABLED_BY_DEFAULT};

bool Process::CanBackgroundProcesses() {
  return FeatureList::IsEnabled(kMacAllowBackgroundingProcesses);
}

bool Process::IsProcessBackgrounded(PortProvider* port_provider) const {
  DCHECK(IsValid());
  if (port_provider == nullptr || !CanBackgroundProcesses())
    return false;

  mach_port_t task_port = port_provider->TaskForPid(Pid());
  if (task_port == TASK_NULL)
    return false;

  task_category_policy_data_t category_policy;
  mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
  boolean_t get_default = FALSE;

  kern_return_t result =
      task_policy_get(task_port, TASK_CATEGORY_POLICY,
                      reinterpret_cast<task_policy_t>(&category_policy),
                      &task_info_count, &get_default);
  MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result)
      << "task_policy_get TASK_CATEGORY_POLICY";

  if (result == KERN_SUCCESS && get_default == FALSE) {
    return category_policy.role == TASK_BACKGROUND_APPLICATION;
  }
  return false;
}

bool Process::SetProcessBackgrounded(PortProvider* port_provider,
                                     bool background) {
  DCHECK(IsValid());
  if (port_provider == nullptr || !CanBackgroundProcesses())
    return false;

  mach_port_t task_port = port_provider->TaskForPid(Pid());
  if (task_port == TASK_NULL)
    return false;

  if (IsProcessBackgrounded(port_provider) == background)
    return true;

  task_category_policy category_policy;
  category_policy.role =
      background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
  kern_return_t result =
      task_policy_set(task_port, TASK_CATEGORY_POLICY,
                      reinterpret_cast<task_policy_t>(&category_policy),
                      TASK_CATEGORY_POLICY_COUNT);

  if (result != KERN_SUCCESS) {
    MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY";
    return false;
  }

  // Latency QoS regulates timer throttling/accuracy. Select default tier
  // on foreground because precise timer firing isn't needed.
  struct task_qos_policy qos_policy = {
      background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED,
      background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED};
  result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY,
                           reinterpret_cast<task_policy_t>(&qos_policy),
                           TASK_QOS_POLICY_COUNT);
  if (result != KERN_SUCCESS) {
    MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY";
    return false;
  }

  return true;
}

}  // namespace base