File: page_discarding_utils.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 (129 lines) | stat: -rw-r--r-- 4,730 bytes parent folder | download | duplicates (3)
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
// 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_TEST_SUPPORT_PAGE_DISCARDING_UTILS_H_
#define CHROME_BROWSER_PERFORMANCE_MANAGER_TEST_SUPPORT_PAGE_DISCARDING_UTILS_H_

#include <vector>

#include "base/memory/raw_ptr.h"
#include "chrome/browser/performance_manager/mechanisms/page_discarder.h"
#include "chrome/browser/performance_manager/policies/page_discarding_helper.h"
#include "chrome/browser/performance_manager/test_support/test_user_performance_tuning_manager_environment.h"
#include "components/performance_manager/graph/graph_impl.h"
#include "components/performance_manager/test_support/graph_test_harness.h"
#include "components/prefs/testing_pref_service.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace performance_manager {

class FrameNodeImpl;
class PageNodeImpl;
class ProcessNodeImpl;

namespace testing {

// Make sure that |page_node| is discardable.
void MakePageNodeDiscardable(PageNodeImpl* page_node,
                             content::BrowserTaskEnvironment& task_env);

class GraphTestHarnessWithDiscardablePage : public GraphTestHarness {
 public:
  GraphTestHarnessWithDiscardablePage();
  ~GraphTestHarnessWithDiscardablePage() override;
  GraphTestHarnessWithDiscardablePage(
      const GraphTestHarnessWithDiscardablePage& other) = delete;
  GraphTestHarnessWithDiscardablePage& operator=(
      const GraphTestHarnessWithDiscardablePage&) = delete;

  void SetUp() override;
  void TearDown() override;

 protected:
  // Deletes and recreates page/process/frame nodes.
  void RecreateNodes();

  PageNodeImpl* page_node() { return page_node_.get(); }
  ProcessNodeImpl* process_node() { return process_node_.get(); }
  FrameNodeImpl* frame_node() { return main_frame_node_.get(); }
  void ResetFrameNode() { main_frame_node_.reset(); }

 private:
  performance_manager::TestNodeWrapper<performance_manager::PageNodeImpl>
      page_node_;
  performance_manager::TestNodeWrapper<performance_manager::ProcessNodeImpl>
      process_node_;
  performance_manager::TestNodeWrapper<performance_manager::FrameNodeImpl>
      main_frame_node_;
};

#if !BUILDFLAG(IS_ANDROID)
// Mock version of a performance_manager::mechanism::PageDiscarder.
class LenientMockPageDiscarder
    : public performance_manager::mechanism::PageDiscarder {
 public:
  LenientMockPageDiscarder();
  ~LenientMockPageDiscarder() override;
  LenientMockPageDiscarder(const LenientMockPageDiscarder& other) = delete;
  LenientMockPageDiscarder& operator=(const LenientMockPageDiscarder&) = delete;

  MOCK_METHOD1(DiscardPageNodeImpl, bool(const PageNode* page_node));

 private:
  std::optional<uint64_t> DiscardPageNode(
      const PageNode* page_node,
      ::mojom::LifecycleUnitDiscardReason discard_reason) override;
};
using MockPageDiscarder = ::testing::StrictMock<LenientMockPageDiscarder>;

// Specialization of a GraphTestHarness that uses a MockPageDiscarder to
// do the discard attempts.
class GraphTestHarnessWithMockDiscarder
    : public GraphTestHarnessWithDiscardablePage {
 public:
  GraphTestHarnessWithMockDiscarder();
  ~GraphTestHarnessWithMockDiscarder() override;
  GraphTestHarnessWithMockDiscarder(
      const GraphTestHarnessWithMockDiscarder& other) = delete;
  GraphTestHarnessWithMockDiscarder& operator=(
      const GraphTestHarnessWithMockDiscarder&) = delete;

  void SetUp() override;
  void TearDown() override;

 protected:
  SystemNodeImpl* system_node() { return graph()->GetSystemNodeImpl(); }
  testing::MockPageDiscarder* discarder() { return mock_discarder_; }

 private:
  TestingPrefServiceSimple local_state_;
  performance_manager::user_tuning::TestUserPerformanceTuningManagerEnvironment
      user_performance_tuning_manager_environment_;
  raw_ptr<testing::MockPageDiscarder> mock_discarder_;
};

// Scoped helper object to unconditionally always discard pages in tests, for
// the duration of the object's life. This object is not nestable.
class ScopedSetAllPagesDiscardableForTesting {
 public:
  ScopedSetAllPagesDiscardableForTesting() {
    auto* policy = policies::DiscardEligibilityPolicy::GetFromGraph();
    CHECK(policy);
    policy->set_always_discard_for_testing(true);
  }

  ~ScopedSetAllPagesDiscardableForTesting() {
    auto* policy = policies::DiscardEligibilityPolicy::GetFromGraph();
    CHECK(policy);
    policy->set_always_discard_for_testing(false);
  }
};

#endif  // !BUILDFLAG(IS_ANDROID)

}  // namespace testing
}  // namespace performance_manager

#endif  // CHROME_BROWSER_PERFORMANCE_MANAGER_TEST_SUPPORT_PAGE_DISCARDING_UTILS_H_