summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/scheduler/common/features.cc
blob: dc69dfc189bf485b1f3e99295cd7fe8bcdbd3aa6 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 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 "third_party/blink/renderer/platform/scheduler/common/features.h"

#include "base/command_line.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/switches.h"

namespace blink {
namespace scheduler {

namespace {

enum class PolicyOverride { NO_OVERRIDE, FORCE_DISABLE, FORCE_ENABLE };

bool g_intensive_wake_up_throttling_policy_override_cached_ = false;

// Returns the IntensiveWakeUpThrottling policy settings. This is checked once
// on first access and cached. Note that that this is *not* thread-safe!
PolicyOverride GetIntensiveWakeUpThrottlingPolicyOverride() {
  static PolicyOverride policy = PolicyOverride::NO_OVERRIDE;
  if (g_intensive_wake_up_throttling_policy_override_cached_)
    return policy;

  // Otherwise, check the command-line. Only values of "0" and "1" are valid,
  // anything else is ignored (and allows the base::Feature to control the
  // feature). This slow path will only be hit once per renderer process.
  g_intensive_wake_up_throttling_policy_override_cached_ = true;
  std::string value =
      base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kIntensiveWakeUpThrottlingPolicy);
  if (value == switches::kIntensiveWakeUpThrottlingPolicy_ForceEnable) {
    policy = PolicyOverride::FORCE_ENABLE;
  } else if (value == switches::kIntensiveWakeUpThrottlingPolicy_ForceDisable) {
    policy = PolicyOverride::FORCE_DISABLE;
  } else {
    // Necessary in testing configurations, as the policy can be parsed
    // repeatedly.
    policy = PolicyOverride::NO_OVERRIDE;
  }

  return policy;
}

}  // namespace

void ClearIntensiveWakeUpThrottlingPolicyOverrideCacheForTesting() {
  g_intensive_wake_up_throttling_policy_override_cached_ = false;
}

bool IsIntensiveWakeUpThrottlingEnabled() {
  // If policy is present then respect it.
  auto policy = GetIntensiveWakeUpThrottlingPolicyOverride();
  if (policy != PolicyOverride::NO_OVERRIDE)
    return policy == PolicyOverride::FORCE_ENABLE;
  // Otherwise respect the base::Feature.
  return base::FeatureList::IsEnabled(features::kIntensiveWakeUpThrottling);
}

// If a policy override is specified then stick to the published defaults so
// that admins get consistent behaviour that clients can't override. Otherwise
// use the base::FeatureParams.

base::TimeDelta GetIntensiveWakeUpThrottlingGracePeriod() {
  // Controls the time that elapses after a page is backgrounded before the
  // throttling policy takes effect.
  static const base::FeatureParam<int>
      kIntensiveWakeUpThrottling_GracePeriodSeconds{
          &features::kIntensiveWakeUpThrottling,
          features::kIntensiveWakeUpThrottling_GracePeriodSeconds_Name,
          kIntensiveWakeUpThrottling_GracePeriodSeconds_Default};

  int seconds = kIntensiveWakeUpThrottling_GracePeriodSeconds_Default;
  if (GetIntensiveWakeUpThrottlingPolicyOverride() ==
      PolicyOverride::NO_OVERRIDE) {
    seconds = kIntensiveWakeUpThrottling_GracePeriodSeconds.Get();
  }
  return base::TimeDelta::FromSeconds(seconds);
}

const base::Feature kThrottleForegroundTimers{
    "ThrottleForegroundTimers", base::FEATURE_DISABLED_BY_DEFAULT};

base::TimeDelta GetForegroundTimersThrottledWakeUpInterval() {
  constexpr int kForegroundTimersThrottling_WakeUpIntervalMillis_Default = 100;
  static const base::FeatureParam<int>
      kForegroundTimersThrottledWakeUpIntervalMills{
          &kThrottleForegroundTimers,
          "ForegroundTimersThrottledWakeUpIntervalMills",
          kForegroundTimersThrottling_WakeUpIntervalMillis_Default};
  return base::TimeDelta::FromMilliseconds(
      kForegroundTimersThrottledWakeUpIntervalMills.Get());
}

const base::Feature kDeprioritizeDOMTimersDuringPageLoading{
    "DeprioritizeDOMTimersDuringPageLoading",
    base::FEATURE_DISABLED_BY_DEFAULT};

const base::FeatureParam<DeprioritizeDOMTimersPhase>::Option
    kDeprioritizeDOMTimersPhaseOptions[] = {
        {DeprioritizeDOMTimersPhase::kOnDOMContentLoaded, "ondomcontentloaded"},
        {DeprioritizeDOMTimersPhase::kFirstContentfulPaint, "fcp"},
        {DeprioritizeDOMTimersPhase::kOnLoad, "onload"}};

const base::FeatureParam<DeprioritizeDOMTimersPhase>
    kDeprioritizeDOMTimersPhase{&kDeprioritizeDOMTimersDuringPageLoading,
                                "phase",
                                DeprioritizeDOMTimersPhase::kOnDOMContentLoaded,
                                &kDeprioritizeDOMTimersPhaseOptions};
}  // namespace scheduler
}  // namespace blink