File: oom_intervention_decider.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (187 lines) | stat: -rw-r--r-- 6,378 bytes parent folder | download | duplicates (6)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/oom_intervention/oom_intervention_decider.h"

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "components/metrics/metrics_service.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/scoped_user_pref_update.h"

namespace {

const char kOomInterventionDecider[] = "oom_intervention.decider";

// Deprecated: Replaced with `kBlocklist`.
// TODO(crbug.com/40744119): Remove this after M92 once existing
// clients have migrated to the new pref.
const char kBlacklist[] = "oom_intervention.blacklist";
// Pref path for blocklist. If a hostname is in the blocklist we never trigger
// intervention on the host.
const char kBlocklist[] = "oom_intervention.blocklist";
// Pref path for declined host list. If a hostname is in the declined host list
// we don't trigger intervention until a OOM crash happens on the host.
const char kDeclinedHostList[] = "oom_intervention.declined_host_list";
// Pref path for OOM detected host list. When an OOM crash is observed on
// a host the hostname is added to the list.
const char kOomDetectedHostList[] = "oom_intervention.oom_detected_host_list";

class DelegateImpl : public OomInterventionDecider::Delegate {
 public:
  bool WasLastShutdownClean() override {
    if (!g_browser_process || !g_browser_process->metrics_service())
      return true;
    return g_browser_process->metrics_service()->WasLastShutdownClean();
  }
};

}  // namespace

const size_t OomInterventionDecider::kMaxListSize = 10;
const size_t OomInterventionDecider::kMaxBlocklistSize = 6;

// static
void OomInterventionDecider::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterListPref(kBlocklist);
  registry->RegisterListPref(kDeclinedHostList);
  registry->RegisterListPref(kOomDetectedHostList);

  // Continue to register the old preference to migrate its value.
  registry->RegisterListPref(kBlacklist);
}

// static
OomInterventionDecider* OomInterventionDecider::GetForBrowserContext(
    content::BrowserContext* context) {
  // The OomIntervetnionDecider is disabled in incognito mode because it is
  // written in such a way that hostnames would be persisted in preferences on
  // disk which is not acceptable for incognito mode.
  if (context->IsOffTheRecord())
    return nullptr;

  if (!context->GetUserData(kOomInterventionDecider)) {
    PrefService* prefs = Profile::FromBrowserContext(context)->GetPrefs();
    context->SetUserData(kOomInterventionDecider,
                         base::WrapUnique(new OomInterventionDecider(
                             std::make_unique<DelegateImpl>(), prefs)));
  }
  return static_cast<OomInterventionDecider*>(
      context->GetUserData(kOomInterventionDecider));
}

OomInterventionDecider::OomInterventionDecider(
    std::unique_ptr<Delegate> delegate,
    PrefService* prefs)
    : delegate_(std::move(delegate)), prefs_(prefs) {
  DCHECK(delegate_);

  PrefService::PrefInitializationStatus pref_status =
      prefs_->GetInitializationStatus();
  if (pref_status == PrefService::INITIALIZATION_STATUS_WAITING) {
    prefs_->AddPrefInitObserver(base::BindOnce(
        &OomInterventionDecider::OnPrefInitialized, base::Unretained(this)));
  } else {
    OnPrefInitialized(pref_status ==
                      PrefService::INITIALIZATION_STATUS_SUCCESS);
  }
}

OomInterventionDecider::~OomInterventionDecider() = default;

bool OomInterventionDecider::CanTriggerIntervention(
    const std::string& host) const {
  if (IsOptedOut(host))
    return false;

  // Check whether OOM was observed before checking declined host list in favor
  // of triggering intervention after OOM.
  if (IsInList(kOomDetectedHostList, host))
    return true;

  if (IsInList(kDeclinedHostList, host))
    return false;

  return true;
}

void OomInterventionDecider::OnInterventionDeclined(const std::string& host) {
  if (IsOptedOut(host))
    return;

  if (IsInList(kDeclinedHostList, host)) {
    AddToList(kBlocklist, host);
  } else {
    AddToList(kDeclinedHostList, host);
  }
}

void OomInterventionDecider::OnOomDetected(const std::string& host) {
  if (IsOptedOut(host))
    return;
  AddToList(kOomDetectedHostList, host);
}

void OomInterventionDecider::ClearData() {
  prefs_->ClearPref(kBlocklist);
  prefs_->ClearPref(kDeclinedHostList);
  prefs_->ClearPref(kOomDetectedHostList);
}

void OomInterventionDecider::OnPrefInitialized(bool success) {
  if (!success)
    return;

  // Migrate `kBlacklist` to `kBlocklist`.
  const base::Value::List& old_pref_value = prefs_->GetList(kBlacklist);
  if (!old_pref_value.empty()) {
    prefs_->SetList(kBlocklist, old_pref_value.Clone());
    prefs_->SetList(kBlacklist, base::Value::List());
  }

  if (delegate_->WasLastShutdownClean())
    return;

  const base::Value::List& declined_list = prefs_->GetList(kDeclinedHostList);
  if (!declined_list.empty()) {
    const std::string& last_declined = declined_list.back().GetString();
    if (!IsInList(kBlocklist, last_declined))
      AddToList(kOomDetectedHostList, last_declined);
  }
}

bool OomInterventionDecider::IsOptedOut(const std::string& host) const {
  if (prefs_->GetList(kBlocklist).size() >= kMaxBlocklistSize)
    return true;

  return IsInList(kBlocklist, host);
}

bool OomInterventionDecider::IsInList(const char* list_name,
                                      const std::string& host) const {
  for (const auto& value : prefs_->GetList(list_name)) {
    if (value.GetString() == host)
      return true;
  }
  return false;
}

void OomInterventionDecider::AddToList(const char* list_name,
                                       const std::string& host) {
  if (IsInList(list_name, host))
    return;
  ScopedListPrefUpdate update(prefs_, list_name);
  base::Value::List& update_list = update.Get();
  update_list.Append(host);
  if (update_list.size() > kMaxListSize)
    update_list.erase(update_list.begin());

  // Save the list immediately because we typically modify lists under high
  // memory pressure, in which the browser process can be killed by the OS
  // soon.
  prefs_->CommitPendingWrite();
}