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

#ifndef COMPONENTS_PERFORMANCE_MANAGER_SCENARIO_API_PERFORMANCE_SCENARIO_OBSERVER_H_
#define COMPONENTS_PERFORMANCE_MANAGER_SCENARIO_API_PERFORMANCE_SCENARIO_OBSERVER_H_

#include <array>

#include "base/component_export.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/observer_list_threadsafe.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation_traits.h"
#include "base/sequence_checker.h"
#include "base/synchronization/lock.h"
#include "base/types/pass_key.h"
#include "components/performance_manager/scenario_api/performance_scenario_memory_forward.h"
#include "components/performance_manager/scenario_api/performance_scenarios.h"

namespace performance_scenarios {

class PerformanceScenarioObserverList;

// An observer that watches for changes to values held in
// ScopedReadOnlyScenarioMemory.
class COMPONENT_EXPORT(SCENARIO_API) PerformanceScenarioObserver
    : public base::CheckedObserver {
 public:
  // Invoked whenever the given scenario changes for `scope`.
  virtual void OnLoadingScenarioChanged(ScenarioScope scope,
                                        LoadingScenario old_scenario,
                                        LoadingScenario new_scenario) {}
  virtual void OnInputScenarioChanged(ScenarioScope scope,
                                      InputScenario old_scenario,
                                      InputScenario new_scenario) {}
};

// An observer that watches for the scenario values held in
// ScopedReadOnlyScenarioMemory to start or stop matching a ScenarioPattern.
class COMPONENT_EXPORT(SCENARIO_API) MatchingScenarioObserver
    : public base::CheckedObserver {
 public:
  explicit MatchingScenarioObserver(ScenarioPattern pattern);

  ~MatchingScenarioObserver() override;

  MatchingScenarioObserver(const MatchingScenarioObserver&) = delete;
  MatchingScenarioObserver& operator=(const MatchingScenarioObserver&) = delete;

  // Returns the pattern that this observer matches.
  ScenarioPattern scenario_pattern() const { return pattern_; }

  // Invoked whenever the scenarios for `scope` change in a way that causes them
  // to start or stop matching the ScenarioPattern. `matches_pattern` will be
  // true if they now match the pattern, false otherwise.
  virtual void OnScenarioMatchChanged(ScenarioScope scope,
                                      bool matches_pattern) = 0;

  // Allows PerformanceScenarioObserverList to notify of scenario changes. Will
  // invoke OnScenarioMatchChanged if necessary.
  void NotifyIfScenarioMatchChanged(ScenarioScope scope,
                                    LoadingScenario loading_scenario,
                                    InputScenario input_scenario);

 private:
  // Returns a reference into `last_match_notifications_` for `scope`.
  bool& LastMatchNotification(ScenarioScope scope);

  const ScenarioPattern pattern_;

  // This will be bound to whichever sequence calls
  // PerformanceScenarioObserverList::AddMatchingObserver, to validate that the
  // observer list always accesses this class on that sequence.
  SEQUENCE_CHECKER(sequence_checker_);

  // The last match notification that was sent for each scope.
  std::array<bool, ScenarioScopes::kValueCount> last_match_notifications_
      GUARDED_BY_CONTEXT(sequence_checker_);
};

// Central list of PerformanceScenarioObservers for a scope, wrapping an
// ObserverListThreadSafe. The lifetime is managed by
// ScopedReadOnlyScenarioMemory on the main thread, but it's refcounted so
// GetForScope() can be called from any sequence. Callers on other sequences
// will extend the lifetime until they drop their reference.
//
// All methods can be called from any sequence.
class COMPONENT_EXPORT(SCENARIO_API) PerformanceScenarioObserverList
    : public base::RefCountedThreadSafe<PerformanceScenarioObserverList> {
 public:
  // Returns the object that notifies observers for `scope`, or nullptr if no
  // ScopedReadOnlyScenarioMemory exists for `scope`.
  static scoped_refptr<PerformanceScenarioObserverList> GetForScope(
      ScenarioScope scope);

  PerformanceScenarioObserverList(const PerformanceScenarioObserverList&) =
      delete;
  PerformanceScenarioObserverList& operator=(
      const PerformanceScenarioObserverList&) = delete;

  // Adds `observer` to the list. The observer will be notified on the calling
  // sequence.
  void AddObserver(PerformanceScenarioObserver* observer);

  // Removes `observer` from the list.
  void RemoveObserver(PerformanceScenarioObserver* observer);

  // Adds `matching_observer` to the list. The observer will be notified on the
  // calling sequence.
  void AddMatchingObserver(MatchingScenarioObserver* matching_observer);

  // Removes `matching_observer` from the list.
  void RemoveMatchingObserver(MatchingScenarioObserver* matching_observer);

  // Notifies observers of scenarios that have changed for this scope since the
  // last call.
  void NotifyIfScenarioChanged(
      base::Location location = base::Location::Current());

  // Notifies observers for all scopes of scenarios that have changed since the
  // last call.
  static void NotifyAllScopes(
      base::Location location = base::Location::Current());

  // Lets ScopedReadOnlyScenarioMemory create and destroy the notifier for
  // `scope`.
  static void CreateForScope(base::PassKey<ScopedReadOnlyScenarioMemory>,
                             ScenarioScope scope);
  static void DestroyForScope(base::PassKey<ScopedReadOnlyScenarioMemory>,
                              ScenarioScope scope);

 private:
  friend class base::RefCountedThreadSafe<PerformanceScenarioObserverList>;

  explicit PerformanceScenarioObserverList(ScenarioScope scope);
  ~PerformanceScenarioObserverList();

  const ScenarioScope scope_;

  // The last scenario values that were notified.
  base::Lock lock_;
  LoadingScenario last_loading_scenario_ GUARDED_BY(lock_);
  InputScenario last_input_scenario_ GUARDED_BY(lock_);

  // kAddingSequenceOnly is safer than the default so use it for all lists.
  template <typename T>
  using ObserverList = base::ObserverListThreadSafe<
      T,
      base::RemoveObserverPolicy::kAddingSequenceOnly>;

  // ObserverListThreadSafe must be held in a scoped_refptr because its
  // destructor is private, but the pointer should never be reassigned.
  const scoped_refptr<ObserverList<PerformanceScenarioObserver>> observers_ =
      base::MakeRefCounted<ObserverList<PerformanceScenarioObserver>>();
  const scoped_refptr<ObserverList<MatchingScenarioObserver>>
      matching_observers_ =
          base::MakeRefCounted<ObserverList<MatchingScenarioObserver>>();
};

}  // namespace performance_scenarios

namespace base {

// Specialize ScopedObservation to invoke the correct add and remove methods for
// MatchingScenarioObserver. These must be in the same namespace as
// base::ScopedObservationTraits.

template <>
struct ScopedObservationTraits<
    performance_scenarios::PerformanceScenarioObserverList,
    performance_scenarios::MatchingScenarioObserver> {
  static void AddObserver(
      performance_scenarios::PerformanceScenarioObserverList* source,
      performance_scenarios::MatchingScenarioObserver* observer) {
    source->AddMatchingObserver(observer);
  }
  static void RemoveObserver(
      performance_scenarios::PerformanceScenarioObserverList* source,
      performance_scenarios::MatchingScenarioObserver* observer) {
    source->RemoveMatchingObserver(observer);
  }
};

}  // namespace base

#endif  // COMPONENTS_PERFORMANCE_MANAGER_SCENARIO_API_PERFORMANCE_SCENARIO_OBSERVER_H_