Commit afbbeff6 authored by Chris Hamilton's avatar Chris Hamilton Committed by Commit Bot

[PM] Migrate ExecutionContextRegistry to use blink tokens.

BUG=1096617

Change-Id: I6fe9a8c8597520bc05f2e74d2d8ce969436ce69c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2399766Reviewed-by: default avatarJoe Mason <joenotcharles@chromium.org>
Commit-Queue: Joe Mason <joenotcharles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806283}
parent 664b97cc
......@@ -90,7 +90,6 @@ static_library("performance_manager") {
"public/decorators/tab_properties_decorator.h",
"public/execution_context/execution_context.h",
"public/execution_context/execution_context_registry.h",
"public/execution_context/execution_context_token.h",
"public/features.h",
"public/frame_priority/boosting_vote_aggregator.h",
"public/frame_priority/frame_priority.h",
......
......@@ -8,6 +8,7 @@
#include "base/util/type_safety/pass_key.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/node_attached_data_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/graph/worker_node_impl.h"
#include "components/performance_manager/public/execution_context/execution_context.h"
......@@ -52,11 +53,21 @@ class ExecutionContextImpl : public ExecutionContext,
return kExecutionContextType;
}
Graph* GetGraph() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return node_->graph();
}
const GURL& GetUrl() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return node_->url();
}
const ProcessNode* GetProcessNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return node_->process_node();
}
const FrameNode* GetFrameNode() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (std::is_same<FrameNodeImpl, NodeImplType>::value)
......@@ -97,12 +108,9 @@ class FrameExecutionContext
// Remaining ExecutionContext implementation not provided by
// ExecutionContextImpl:
const ExecutionContextToken& GetToken() const override {
blink::ExecutionContextToken GetToken() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The casting is safe because ExecutionContext guarantees it has the same
// layout as base::UnguessableToken.
return *reinterpret_cast<const ExecutionContextToken*>(
&node_->frame_token().value());
return blink::ExecutionContextToken(node_->frame_token());
}
protected:
......@@ -123,12 +131,9 @@ class WorkerExecutionContext
// Remaining ExecutionContext implementation not provided by
// ExecutionContextImpl:
const ExecutionContextToken& GetToken() const override {
blink::ExecutionContextToken GetToken() const override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The casting is safe because ExecutionContext guarantees it has the same
// layout as base::UnguessableToken.
return *reinterpret_cast<const ExecutionContextToken*>(
&node_->worker_token().value());
return ToExecutionContextToken(node_->worker_token());
}
protected:
......@@ -139,6 +144,28 @@ class WorkerExecutionContext
} // namespace
// Declared in execution_context.h.
blink::ExecutionContextToken ToExecutionContextToken(
const blink::WorkerToken& token) {
if (token.Is<blink::DedicatedWorkerToken>()) {
return blink::ExecutionContextToken(
token.GetAs<blink::DedicatedWorkerToken>());
}
if (token.Is<blink::ServiceWorkerToken>()) {
return blink::ExecutionContextToken(
token.GetAs<blink::ServiceWorkerToken>());
}
if (token.Is<blink::SharedWorkerToken>()) {
return blink::ExecutionContextToken(
token.GetAs<blink::SharedWorkerToken>());
}
// Unfortunately there's no enum of input types, so no way to ensure that
// all types are handled at compile time. This at least ensures via the CQ
// that all types are handled.
NOTREACHED();
return blink::ExecutionContextToken();
}
const ExecutionContext* GetOrCreateExecutionContextForFrameNode(
const FrameNode* frame_node) {
DCHECK(frame_node);
......
......@@ -18,7 +18,8 @@ namespace {
// a custom ExecutionContext wrapper for the time being.
class DummyExecutionContextForLookup : public ExecutionContext {
public:
explicit DummyExecutionContextForLookup(const ExecutionContextToken& token)
explicit DummyExecutionContextForLookup(
const blink::ExecutionContextToken& token)
: token_(token) {}
DummyExecutionContextForLookup(const DummyExecutionContextForLookup&) =
delete;
......@@ -33,7 +34,12 @@ class DummyExecutionContextForLookup : public ExecutionContext {
return ExecutionContextType::kFrameNode;
}
const ExecutionContextToken& GetToken() const override { return token_; }
blink::ExecutionContextToken GetToken() const override { return token_; }
Graph* GetGraph() const override {
NOTREACHED();
return nullptr;
}
const GURL& GetUrl() const override {
NOTREACHED();
......@@ -41,6 +47,11 @@ class DummyExecutionContextForLookup : public ExecutionContext {
return kUrl;
}
const ProcessNode* GetProcessNode() const override {
NOTREACHED();
return nullptr;
}
const FrameNode* GetFrameNode() const override {
NOTREACHED();
return nullptr;
......@@ -52,7 +63,7 @@ class DummyExecutionContextForLookup : public ExecutionContext {
}
private:
const ExecutionContextToken& token_;
const blink::ExecutionContextToken& token_;
};
} // namespace
......@@ -91,7 +102,7 @@ void ExecutionContextRegistryImpl::RemoveObserver(
const ExecutionContext*
ExecutionContextRegistryImpl::GetExecutionContextByToken(
const ExecutionContextToken& token) {
const blink::ExecutionContextToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (token.value().is_empty())
return nullptr;
......@@ -105,10 +116,7 @@ ExecutionContextRegistryImpl::GetExecutionContextByToken(
const FrameNode* ExecutionContextRegistryImpl::GetFrameNodeByFrameToken(
const blink::LocalFrameToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The casting is safe because ExecutionContextToken guarantees it has the
// same layout as base::UnguessableToken.
auto* ec = GetExecutionContextByToken(
*reinterpret_cast<const ExecutionContextToken*>(&token.value()));
auto* ec = GetExecutionContextByToken(blink::ExecutionContextToken(token));
if (!ec)
return nullptr;
return ec->GetFrameNode();
......@@ -117,10 +125,7 @@ const FrameNode* ExecutionContextRegistryImpl::GetFrameNodeByFrameToken(
const WorkerNode* ExecutionContextRegistryImpl::GetWorkerNodeByWorkerToken(
const blink::WorkerToken& token) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// The casting is safe because ExecutionContextToken guarantees it has the
// same layout as base::UnguessableToken.
auto* ec = GetExecutionContextByToken(
*reinterpret_cast<const ExecutionContextToken*>(&token.value()));
auto* ec = GetExecutionContextByToken(ToExecutionContextToken(token));
if (!ec)
return nullptr;
return ec->GetWorkerNode();
......
......@@ -8,11 +8,11 @@
#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/execution_context/execution_context_token.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 {
......@@ -40,7 +40,7 @@ class ExecutionContextRegistryImpl
void AddObserver(ExecutionContextObserver* observer) override;
void RemoveObserver(ExecutionContextObserver* observer) override;
const ExecutionContext* GetExecutionContextByToken(
const ExecutionContextToken& token) override;
const blink::ExecutionContextToken& token) override;
const FrameNode* GetFrameNodeByFrameToken(
const blink::LocalFrameToken& token) override;
const WorkerNode* GetWorkerNodeByWorkerToken(
......
......@@ -13,6 +13,7 @@
#include "components/performance_manager/test_support/mock_graphs.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/tokens/tokens.h"
namespace performance_manager {
namespace execution_context {
......@@ -112,6 +113,7 @@ TEST_F(ExecutionContextRegistryImplTest, RegistryWorks) {
EXPECT_EQ(ExecutionContextType::kFrameNode, frame1_ec->GetType());
EXPECT_EQ(frame1->frame_token().value(), frame1_ec->GetToken().value());
EXPECT_EQ(frame1->url(), frame1_ec->GetUrl());
EXPECT_EQ(frame1->process_node(), frame1_ec->GetProcessNode());
EXPECT_EQ(frame1, frame1_ec->GetFrameNode());
EXPECT_FALSE(frame1_ec->GetWorkerNode());
......@@ -119,6 +121,7 @@ TEST_F(ExecutionContextRegistryImplTest, RegistryWorks) {
EXPECT_EQ(ExecutionContextType::kWorkerNode, worker_ec->GetType());
EXPECT_EQ(worker->worker_token().value(), worker_ec->GetToken().value());
EXPECT_EQ(worker->url(), worker_ec->GetUrl());
EXPECT_EQ(worker->process_node(), worker_ec->GetProcessNode());
EXPECT_FALSE(worker_ec->GetFrameNode());
EXPECT_EQ(worker, worker_ec->GetWorkerNode());
......@@ -142,10 +145,11 @@ TEST_F(ExecutionContextRegistryImplTest, RegistryWorks) {
registry_->GetWorkerNodeByWorkerToken(worker->worker_token()));
// Querying an invalid token or a random token should fail.
EXPECT_FALSE(registry_->GetExecutionContextByToken(
ExecutionContextToken(base::UnguessableToken::Null())));
EXPECT_FALSE(registry_->GetExecutionContextByToken(
ExecutionContextToken(base::UnguessableToken::Create())));
EXPECT_FALSE(
registry_->GetExecutionContextByToken(blink::ExecutionContextToken(
blink::LocalFrameToken(base::UnguessableToken::Null()))));
EXPECT_FALSE(
registry_->GetExecutionContextByToken(blink::ExecutionContextToken()));
EXPECT_FALSE(registry_->GetFrameNodeByFrameToken(blink::LocalFrameToken()));
EXPECT_FALSE(registry_->GetWorkerNodeByWorkerToken(blink::WorkerToken()));
......
......@@ -6,13 +6,15 @@
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
#include "base/observer_list_types.h"
#include "components/performance_manager/public/execution_context/execution_context_token.h"
#include "third_party/blink/public/common/tokens/tokens.h"
class GURL;
namespace performance_manager {
class FrameNode;
class Graph;
class ProcessNode;
class WorkerNode;
namespace execution_context {
......@@ -55,8 +57,11 @@ class ExecutionContext {
// Returns the unique token associated with this ExecutionContext. This is a
// constant over the lifetime of the context. Tokens are unique for all time
// and should never be reused.
virtual const ExecutionContextToken& GetToken() const = 0;
// and will never be reused.
virtual blink::ExecutionContextToken GetToken() const = 0;
// Returns the graph to which this ExecutionContext belongs.
virtual Graph* GetGraph() const = 0;
// Returns the final post-redirect committed URL associated with this
// ExecutionContext. This is the URL of the HTML document (not the javascript)
......@@ -64,6 +69,10 @@ class ExecutionContext {
// of a WorkerNode.
virtual const GURL& GetUrl() const = 0;
// Returns the ProcessNode corresponding to the process in which this
// ExecutionContext is hosted. This will never return nullptr.
virtual const ProcessNode* GetProcessNode() const = 0;
// Returns the underlying FrameNode, if this context is a FrameNode, or
// nullptr otherwise.
virtual const FrameNode* GetFrameNode() const = 0;
......@@ -106,7 +115,14 @@ class ExecutionContextObserverDefaultImpl : public ExecutionContextObserver {
void OnBeforeExecutionContextRemoved(const ExecutionContext* ec) override {}
};
// Helper function for converting from a WorkerToken to an
// ExecutionContextToken.
// TODO(crbug.com/1126285): Get rid of this once MultiToken handles compatible
// assignment.
blink::ExecutionContextToken ToExecutionContextToken(
const blink::WorkerToken& token);
} // namespace execution_context
} // namespace performance_manager
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
\ No newline at end of file
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
......@@ -5,7 +5,6 @@
#ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_REGISTRY_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_REGISTRY_H_
#include "components/performance_manager/public/execution_context/execution_context_token.h"
#include "components/performance_manager/public/graph/frame_node.h"
#include "components/performance_manager/public/graph/worker_node.h"
#include "third_party/blink/public/common/tokens/tokens.h"
......@@ -44,7 +43,7 @@ class ExecutionContextRegistry {
// Looks up an ExecutionContext by token. Returns nullptr if no such context
// exists.
virtual const ExecutionContext* GetExecutionContextByToken(
const ExecutionContextToken& token) = 0;
const blink::ExecutionContextToken& token) = 0;
// Does a typed lookup of a FrameNode ExecutionContext by FrameToken, returns
// nullptr if no such FrameNode exists.
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_TOKEN_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_TOKEN_H_
#include "base/unguessable_token.h"
#include "base/util/type_safety/token_type.h"
namespace performance_manager {
namespace execution_context {
using ExecutionContextToken = util::TokenType<class ExecutionContextTokenTag>;
// These objects should have the identical size and layout; the StrongAlias
// wrapper is simply there to provide type safety. This allows us to
// safely reinterpret_cast behind the scenes, so we can continue to return
// references and pointers.
static_assert(sizeof(ExecutionContextToken) == sizeof(base::UnguessableToken),
"TokenType should not change object layout");
} // namespace execution_context
} // namespace performance_manager
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_TOKEN_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment