File: page_discarding_helper.h

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 (112 lines) | stat: -rw-r--r-- 4,905 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PERFORMANCE_MANAGER_POLICIES_PAGE_DISCARDING_HELPER_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_POLICIES_PAGE_DISCARDING_HELPER_H_

#include <optional>
#include <string_view>

#include "base/functional/callback.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "chrome/browser/performance_manager/mechanisms/page_discarder.h"
#include "chrome/browser/performance_manager/policies/cannot_discard_reason.h"
#include "chrome/browser/performance_manager/policies/discard_eligibility_policy.h"
#include "chrome/browser/resource_coordinator/lifecycle_unit_state.mojom-shared.h"
#include "components/memory_pressure/reclaim_target.h"
#include "components/memory_pressure/unnecessary_discard_monitor.h"
#include "components/performance_manager/public/features.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/graph_registered.h"
#include "components/performance_manager/public/graph/node_data_describer.h"
#include "components/performance_manager/public/graph/page_node.h"

namespace performance_manager {

namespace mechanism {
class PageDiscarder;
}  // namespace mechanism

namespace policies {

// Helper class to be used by the policies that want to discard tabs.
//
// This is a GraphRegistered object and should be accessed via
// PageDiscardingHelper::GetFromGraph(graph()).
//
// This requires DiscardEligibilityPolicy to be registered in the graph.
class PageDiscardingHelper
    : public GraphOwnedAndRegistered<PageDiscardingHelper>,
      public NodeDataDescriberDefaultImpl,
      public PageNodeObserver {
 public:
  // DiscardCallback passes the time of first discarding is done.
  // If discarding fails or there is no candidate for discarding, this passes
  // nullopt.
  using DiscardCallback =
      base::OnceCallback<void(std::optional<base::TimeTicks>)>;

  PageDiscardingHelper();
  ~PageDiscardingHelper() override;
  PageDiscardingHelper(const PageDiscardingHelper& other) = delete;
  PageDiscardingHelper& operator=(const PageDiscardingHelper&) = delete;

  // Selects and discards a tab. This will try to discard a tab until there's
  // been a successful discard or until there's no more discard candidate.
  // `minimum_time_in_background` is passed to `CanDiscard()`, see comment there
  // about usage. Returns a time taken shortly after the first successful
  // discard, or nullopt if no successful discard occurred.
  std::optional<base::TimeTicks> DiscardAPage(
      DiscardEligibilityPolicy::DiscardReason discard_reason,
      base::TimeDelta minimum_time_in_background =
          kNonVisiblePagesUrgentProtectionTime);

  // Selects and discards multiple tabs to meet the reclaim target. This will
  // keep trying again until there's been at least a single successful discard
  // or until there's no more discard candidate. If |reclaim_target_kb| is
  // nullopt, only discard one tab. If |discard_protected_tabs| is true,
  // protected tabs (CanDiscard() returns kProtected) can also be discarded.
  // `minimum_time_in_background` is passed to `CanDiscard()`, see comment there
  // about usage. Returns a time taken shortly after the first successful
  // discard, or nullopt if no successful discard occurred.
  std::optional<base::TimeTicks> DiscardMultiplePages(
      std::optional<memory_pressure::ReclaimTarget> reclaim_target,
      bool discard_protected_tabs,
      DiscardEligibilityPolicy::DiscardReason discard_reason,
      base::TimeDelta minimum_time_in_background =
          kNonVisiblePagesUrgentProtectionTime);

  // Immediately discards as many pages as possible in `page_nodes`.
  // `minimum_time_in_background` is passed to `CanDiscard()`, see comment there
  // about usage. Returns true if at least one page was successfully discarded.
  bool ImmediatelyDiscardMultiplePages(
      const std::vector<const PageNode*>& page_nodes,
      DiscardEligibilityPolicy::DiscardReason discard_reason,
      base::TimeDelta minimum_time_in_background = base::TimeDelta());

  void SetMockDiscarderForTesting(
      std::unique_ptr<mechanism::PageDiscarder> discarder);

 protected:
  void OnPassedToGraph(Graph* graph) override;
  void OnTakenFromGraph(Graph* graph) override;

 private:
  // NodeDataDescriber implementation:
  base::Value::Dict DescribePageNodeData(const PageNode* node) const override;

  // The mechanism used to do the actual discarding.
  std::unique_ptr<mechanism::PageDiscarder> page_discarder_;

  memory_pressure::UnnecessaryDiscardMonitor unnecessary_discard_monitor_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace policies

}  // namespace performance_manager

#endif  // CHROME_BROWSER_PERFORMANCE_MANAGER_POLICIES_PAGE_DISCARDING_HELPER_H_