chromium/components/performance_manager/v8_memory/v8_context_tracker_helpers.h

// 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_V8_MEMORY_V8_CONTEXT_TRACKER_HELPERS_H_
#define COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_CONTEXT_TRACKER_HELPERS_H_

#include <optional>
#include <string>

#include "third_party/blink/public/common/tokens/tokens.h"

namespace performance_manager {

class FrameNode;
class Graph;

namespace execution_context {
class ExecutionContext;
}  // namespace execution_context

namespace mojom {
class V8ContextDescription;
}  // namespace mojom

namespace v8_memory {

// Determines if the provided frame has a cross-process parent frame.
[[nodiscard]] bool HasCrossProcessParent(const FrameNode* frame_node);

// Determines if a string is a valid extension ID.
// TODO(crbug.com/40136290): The extension ID should be strongly typed, with
// built-in validation, mojo type-mapping, etc. Ideally this would be done
// directly in extensions/common/extension_id.h.
[[nodiscard]] bool IsValidExtensionId(const std::string& s);

// Returns true if an ExecutionContextToken corresponds to a worklet.
[[nodiscard]] bool IsWorkletToken(const blink::ExecutionContextToken& token);

// Returns true if an ExecutionContextToken corresponds to a worker.
[[nodiscard]] bool IsWorkerToken(const blink::ExecutionContextToken& token);

// Looks up the execution context corresponding to the given token. Note that
// the ExecutionContextRegistry must be installed on the graph.
[[nodiscard]] const execution_context::ExecutionContext* GetExecutionContext(
    const blink::ExecutionContextToken& token,
    Graph* graph);

// Return type for V8ContextDescription validation.
enum class V8ContextDescriptionStatus {};

// Validates the given V8ContextDescription.
[[nodiscard]] V8ContextDescriptionStatus ValidateV8ContextDescription(
    const mojom::V8ContextDescription& description);

// Determines whether or not IframeAttributionData is expected to accompany the
// provided V8ContextDescription. This is not always able to be determined, in
// which case std::nullopt will be returned. It is assumed that the
// |description| has previously been validated.
[[nodiscard]] std::optional<bool>
ExpectIframeAttributionDataForV8ContextDescription(
    const mojom::V8ContextDescription& description,
    Graph* graph);

// Small helper class for maintaining a count of objects that are optionally
// "marked".
class MarkedObjectCount {};

// Helper class for maintaining a pair of context counts for both
// ExecutionContexts and V8Contexts.
class ContextCounts {};

}  // namespace v8_memory
}  // namespace performance_manager

#endif  // COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_V8_CONTEXT_TRACKER_HELPERS_H_