File: bfcache_policy.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (149 lines) | stat: -rw-r--r-- 5,571 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/performance_manager/graph/policies/bfcache_policy.h"

#include "base/functional/bind.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/task/task_traits.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/page_node.h"
#include "content/public/browser/back_forward_cache.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"

namespace performance_manager::policies {

namespace {

// The foregrounded tab's cache limit on moderate memory pressure. The negative
// value means no limit.
int ForegroundCacheSizeOnModeratePressure() {
  static constexpr base::FeatureParam<int>
      foreground_cache_size_on_moderate_pressure{
          &features::kBFCachePerformanceManagerPolicy,
          "foreground_cache_size_on_moderate_pressure", 3};
  return foreground_cache_size_on_moderate_pressure.Get();
}

// The backgrounded tab's cache limit on moderate memory pressure. The negative
// value means no limit.
int BackgroundCacheSizeOnModeratePressure() {
  static constexpr base::FeatureParam<int>
      background_cache_size_on_moderate_pressure{
          &features::kBFCachePerformanceManagerPolicy,
          "background_cache_size_on_moderate_pressure", 1};
  return background_cache_size_on_moderate_pressure.Get();
}

// The foregrounded tab's cache limit on critical memory pressure. The negative
// value means no limit.
int ForegroundCacheSizeOnCriticalPressure() {
  static constexpr base::FeatureParam<int>
      foreground_cache_size_on_critical_pressure{
          &features::kBFCachePerformanceManagerPolicy,
          "foreground_cache_size_on_critical_pressure", 0};
  return foreground_cache_size_on_critical_pressure.Get();
}

// The backgrounded tab's cache limit on critical memory pressure. The negative
// value means no limit.
int BackgroundCacheSizeOnCriticalPressure() {
  static constexpr base::FeatureParam<int>
      background_cache_size_on_critical_pressure{
          &features::kBFCachePerformanceManagerPolicy,
          "background_cache_size_on_critical_pressure", 0};
  return background_cache_size_on_critical_pressure.Get();
}

bool PageMightHaveFramesInBFCache(const PageNode* page_node) {
  // TODO(crbug.com/40182881): Use PageState when that actually works.
  auto main_frame_nodes = page_node->GetMainFrameNodes();
  if (main_frame_nodes.size() == 1)
    return false;
  for (const FrameNode* main_frame_node : main_frame_nodes) {
    if (!main_frame_node->IsCurrent())
      return true;
  }
  return false;
}

using MemoryPressureLevel = base::MemoryPressureListener::MemoryPressureLevel;

void MaybeFlushBFCacheImpl(content::WebContents* contents,
                           MemoryPressureLevel memory_pressure_level) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(contents);

  int cache_size = -1;
  content::BackForwardCache::NotRestoredReason reason;
  bool foregrounded =
      (contents->GetVisibility() == content::Visibility::VISIBLE);
  switch (memory_pressure_level) {
    case MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_MODERATE:
      cache_size = foregrounded ? ForegroundCacheSizeOnModeratePressure()
                                : BackgroundCacheSizeOnModeratePressure();
      reason = content::BackForwardCache::NotRestoredReason::
          kCacheLimitPrunedOnModerateMemoryPressure;
      break;
    case MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_CRITICAL:
      cache_size = foregrounded ? ForegroundCacheSizeOnCriticalPressure()
                                : BackgroundCacheSizeOnCriticalPressure();
      reason = content::BackForwardCache::NotRestoredReason::
          kCacheLimitPrunedOnCriticalMemoryPressure;
      break;
    default:
      NOTREACHED();
  }
  // Do not flush BFCache if cache_size is negative (such as -1).
  if (cache_size < 0)
    return;

  // Do not flush the BFCache if there's a pending navigation as this could stop
  // it.
  // TODO(sebmarchand): Check if this is really needed.
  auto& navigation_controller = contents->GetController();
  if (!navigation_controller.GetPendingEntry())
    navigation_controller.GetBackForwardCache().Prune(cache_size, reason);
}

}  // namespace

void BFCachePolicy::MaybeFlushBFCache(
    const PageNode* page_node,
    MemoryPressureLevel memory_pressure_level) {
  DCHECK(page_node);
  MaybeFlushBFCacheImpl(page_node->GetWebContents().get(),
                        memory_pressure_level);
}

void BFCachePolicy::OnPassedToGraph(Graph* graph) {
  DCHECK(graph->HasOnlySystemNode());
  graph->AddSystemNodeObserver(this);
}

void BFCachePolicy::OnTakenFromGraph(Graph* graph) {
  graph->RemoveSystemNodeObserver(this);
}

void BFCachePolicy::OnMemoryPressure(MemoryPressureLevel new_level) {
  // This shouldn't happen but add the check anyway in case the API changes.
  if (new_level == MemoryPressureLevel::MEMORY_PRESSURE_LEVEL_NONE) {
    return;
  }

  // Apply the cache limit to all pages.
  for (const PageNode* page_node : GetOwningGraph()->GetAllPageNodes()) {
    if (PageMightHaveFramesInBFCache(page_node)) {
      MaybeFlushBFCache(page_node, new_level);
    }
  }
}

}  // namespace performance_manager::policies