File: execution_context_registry_impl.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (111 lines) | stat: -rw-r--r-- 4,834 bytes parent folder | download | duplicates (4)
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
// 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 COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_EXECUTION_CONTEXT_REGISTRY_IMPL_H_
#define COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_EXECUTION_CONTEXT_REGISTRY_IMPL_H_

#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/sequence_checker.h"
#include "components/performance_manager/public/execution_context/execution_context_registry.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/graph/graph_registered.h"
#include "components/performance_manager/public/graph/worker_node.h"
#include "third_party/blink/public/common/tokens/tokens.h"

namespace performance_manager {
namespace execution_context {

class ExecutionContext;

// The ExecutionContextRegistry is a GraphRegistered class that allows for
// observers to be registered, and for ExecutionContexts to be looked up by
// their tokens. SetUp() must be called prior to any nodes being created.
class ExecutionContextRegistryImpl
    : public ExecutionContextRegistry,
      public GraphRegisteredImpl<ExecutionContextRegistryImpl>,
      public FrameNodeObserver,
      public WorkerNodeObserver {
 public:
  ExecutionContextRegistryImpl();
  ExecutionContextRegistryImpl(const ExecutionContextRegistryImpl&) = delete;
  ExecutionContextRegistryImpl& operator=(const ExecutionContextRegistryImpl&) =
      delete;
  ~ExecutionContextRegistryImpl() override;

  // Sets up/tears down the instance on the graph.
  void SetUp(Graph* graph);
  void TearDown(Graph* graph);

  // ExecutionContextRegistry implementation:
  void AddObserver(ExecutionContextObserver* observer) override;
  bool HasObserver(ExecutionContextObserver* observer) const override;
  void RemoveObserver(ExecutionContextObserver* observer) override;
  const ExecutionContext* GetExecutionContextByToken(
      const blink::ExecutionContextToken& token) override;
  const FrameNode* GetFrameNodeByFrameToken(
      const blink::LocalFrameToken& token) override;
  const WorkerNode* GetWorkerNodeByWorkerToken(
      const blink::WorkerToken& token) override;
  const ExecutionContext* GetExecutionContextForFrameNodeImpl(
      const FrameNode* frame_node) override;
  const ExecutionContext* GetExecutionContextForWorkerNodeImpl(
      const WorkerNode* worker_node) override;

  size_t GetExecutionContextCountForTesting() const {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    return execution_contexts_.size();
  }

 private:
  // FrameNodeObserver:
  void OnFrameNodeAdded(const FrameNode* frame_node) override;
  void OnBeforeFrameNodeRemoved(const FrameNode* frame_node) override;
  void OnPriorityAndReasonChanged(
      const FrameNode* frame_node,
      const PriorityAndReason& previous_value) override;

  // WorkerNodeObserver:
  void OnWorkerNodeAdded(const WorkerNode* worker_node) override;
  void OnBeforeWorkerNodeRemoved(const WorkerNode* worker_node) override;
  void OnPriorityAndReasonChanged(
      const WorkerNode* worker_node,
      const PriorityAndReason& previous_value) override;

  // Maintains the collection of all currently known ExecutionContexts in the
  // Graph. It is expected that there are O(100s) to O(1000s) of these being
  // tracked. This is stored as a hash set keyed by the token, so that the
  // token itself doesn't have to be duplicated as would be the case with a map.
  struct ExecutionContextHash {
    using is_transparent = void;
    size_t operator()(const blink::ExecutionContextToken& token) const;
    size_t operator()(const ExecutionContext* ec) const;
  };
  struct ExecutionContextKeyEqual {
    using is_transparent = void;
    bool operator()(const ExecutionContext* ec1,
                    const ExecutionContext* ec2) const;
    bool operator()(const ExecutionContext* ec,
                    const blink::ExecutionContextToken& token) const;
    bool operator()(const blink::ExecutionContextToken& token,
                    const ExecutionContext* ec) const;
  };
  std::unordered_set<raw_ptr<const ExecutionContext, CtnExperimental>,
                     ExecutionContextHash,
                     ExecutionContextKeyEqual>
      execution_contexts_ GUARDED_BY_CONTEXT(sequence_checker_);

  base::ObserverList<ExecutionContextObserver,
                     /* check_empty = */ true,
                     /* allow_reentrancy */ false>
      observers_ GUARDED_BY_CONTEXT(sequence_checker_);

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace execution_context
}  // namespace performance_manager

#endif  // COMPONENTS_PERFORMANCE_MANAGER_EXECUTION_CONTEXT_EXECUTION_CONTEXT_REGISTRY_IMPL_H_