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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
// 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.
#include "components/performance_manager/execution_context/execution_context_registry_impl.h"
#include "base/check.h"
#include "base/memory/raw_ref.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "components/performance_manager/execution_context/execution_context_impl.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/worker_node_impl.h"
#include "components/performance_manager/public/execution_context/execution_context.h"
#include "url/gurl.h"
namespace performance_manager {
namespace execution_context {
////////////////////////////////////////////////////////////////////////////////
// ExecutionContextRegistry
ExecutionContextRegistry::ExecutionContextRegistry() = default;
ExecutionContextRegistry::~ExecutionContextRegistry() = default;
// static
ExecutionContextRegistry* ExecutionContextRegistry::GetFromGraph(Graph* graph) {
return GraphRegisteredImpl<ExecutionContextRegistryImpl>::GetFromGraph(graph);
}
// static
const ExecutionContext*
ExecutionContextRegistry::GetExecutionContextForFrameNode(
const FrameNode* frame_node) {
auto* ec_registry = GetFromGraph(frame_node->GetGraph());
DCHECK(ec_registry);
return ec_registry->GetExecutionContextForFrameNodeImpl(frame_node);
}
// static
const ExecutionContext*
ExecutionContextRegistry::GetExecutionContextForWorkerNode(
const WorkerNode* worker_node) {
auto* ec_registry = GetFromGraph(worker_node->GetGraph());
DCHECK(ec_registry);
return ec_registry->GetExecutionContextForWorkerNodeImpl(worker_node);
}
////////////////////////////////////////////////////////////////////////////////
// ExecutionContextRegistryImpl
ExecutionContextRegistryImpl::ExecutionContextRegistryImpl() {
DETACH_FROM_SEQUENCE(sequence_checker_);
}
ExecutionContextRegistryImpl::~ExecutionContextRegistryImpl() = default;
void ExecutionContextRegistryImpl::AddObserver(
ExecutionContextObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observers_.AddObserver(observer);
}
bool ExecutionContextRegistryImpl::HasObserver(
ExecutionContextObserver* observer) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return observers_.HasObserver(observer);
}
void ExecutionContextRegistryImpl::RemoveObserver(
ExecutionContextObserver* observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(observers_.HasObserver(observer));
observers_.RemoveObserver(observer);
}
const ExecutionContext*
ExecutionContextRegistryImpl::GetExecutionContextByToken(
const blink::ExecutionContextToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (token.value().is_empty())
return nullptr;
auto it = execution_contexts_.find(token);
if (it == execution_contexts_.end())
return nullptr;
return *it;
}
const FrameNode* ExecutionContextRegistryImpl::GetFrameNodeByFrameToken(
const blink::LocalFrameToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextByToken(token);
if (!ec)
return nullptr;
return ec->GetFrameNode();
}
const WorkerNode* ExecutionContextRegistryImpl::GetWorkerNodeByWorkerToken(
const blink::WorkerToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextByToken(blink::ExecutionContextToken(token));
if (!ec)
return nullptr;
return ec->GetWorkerNode();
}
const ExecutionContext*
ExecutionContextRegistryImpl::GetExecutionContextForFrameNodeImpl(
const FrameNode* frame_node) {
return &FrameExecutionContext::Get(FrameNodeImpl::FromNode(frame_node));
}
const ExecutionContext*
ExecutionContextRegistryImpl::GetExecutionContextForWorkerNodeImpl(
const WorkerNode* worker_node) {
return &WorkerExecutionContext::Get(WorkerNodeImpl::FromNode(worker_node));
}
void ExecutionContextRegistryImpl::SetUp(Graph* graph) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(graph->HasOnlySystemNode());
graph->RegisterObject(this);
graph->AddFrameNodeObserver(this);
graph->AddWorkerNodeObserver(this);
}
void ExecutionContextRegistryImpl::TearDown(Graph* graph) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
graph->RemoveWorkerNodeObserver(this);
graph->RemoveFrameNodeObserver(this);
graph->UnregisterObject(this);
}
void ExecutionContextRegistryImpl::OnFrameNodeAdded(
const FrameNode* frame_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForFrameNodeImpl(frame_node);
DCHECK(ec);
auto result = execution_contexts_.insert(ec);
DCHECK(result.second); // Inserted.
for (auto& observer : observers_)
observer.OnExecutionContextAdded(ec);
}
void ExecutionContextRegistryImpl::OnBeforeFrameNodeRemoved(
const FrameNode* frame_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForFrameNodeImpl(frame_node);
DCHECK(ec);
for (auto& observer : observers_)
observer.OnBeforeExecutionContextRemoved(ec);
size_t erased = execution_contexts_.erase(ec);
DCHECK_EQ(1u, erased);
}
void ExecutionContextRegistryImpl::OnPriorityAndReasonChanged(
const FrameNode* frame_node,
const PriorityAndReason& previous_value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForFrameNodeImpl(frame_node);
DCHECK(ec);
for (auto& observer : observers_)
observer.OnPriorityAndReasonChanged(ec, previous_value);
}
void ExecutionContextRegistryImpl::OnWorkerNodeAdded(
const WorkerNode* worker_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForWorkerNodeImpl(worker_node);
DCHECK(ec);
auto result = execution_contexts_.insert(ec);
DCHECK(result.second); // Inserted.
for (auto& observer : observers_)
observer.OnExecutionContextAdded(ec);
}
void ExecutionContextRegistryImpl::OnBeforeWorkerNodeRemoved(
const WorkerNode* worker_node) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForWorkerNodeImpl(worker_node);
DCHECK(ec);
for (auto& observer : observers_)
observer.OnBeforeExecutionContextRemoved(ec);
size_t erased = execution_contexts_.erase(ec);
DCHECK_EQ(1u, erased);
}
void ExecutionContextRegistryImpl::OnPriorityAndReasonChanged(
const WorkerNode* worker_node,
const PriorityAndReason& previous_value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto* ec = GetExecutionContextForWorkerNodeImpl(worker_node);
DCHECK(ec);
for (auto& observer : observers_)
observer.OnPriorityAndReasonChanged(ec, previous_value);
}
////////////////////////////////////////////////////////////////////////////////
// ExecutionContextRegistryImpl::ExecutionContextHash
size_t ExecutionContextRegistryImpl::ExecutionContextHash::operator()(
const blink::ExecutionContextToken& token) const {
return base::UnguessableTokenHash()(token.value());
}
size_t ExecutionContextRegistryImpl::ExecutionContextHash::operator()(
const ExecutionContext* ec) const {
return base::UnguessableTokenHash()(ec->GetToken().value());
}
////////////////////////////////////////////////////////////////////////////////
// ExecutionContextRegistryImpl::ExecutionContextKeyEqual
bool ExecutionContextRegistryImpl::ExecutionContextKeyEqual::operator()(
const ExecutionContext* ec1,
const ExecutionContext* ec2) const {
return ec1->GetToken() == ec2->GetToken();
}
bool ExecutionContextRegistryImpl::ExecutionContextKeyEqual::operator()(
const ExecutionContext* ec,
const blink::ExecutionContextToken& token) const {
return ec->GetToken() == token;
}
bool ExecutionContextRegistryImpl::ExecutionContextKeyEqual::operator()(
const blink::ExecutionContextToken& token,
const ExecutionContext* ec) const {
return token == ec->GetToken();
}
} // namespace execution_context
} // namespace performance_manager
|