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
|
// 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_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
#include "base/observer_list_types.h"
#include "components/performance_manager/public/execution_context_priority/execution_context_priority.h"
#include "components/performance_manager/public/graph/node_state.h"
#include "third_party/blink/public/common/tokens/tokens.h"
class GURL;
namespace performance_manager {
class FrameNode;
class Graph;
class ProcessNode;
class WorkerNode;
using execution_context_priority::PriorityAndReason;
namespace execution_context {
class ExecutionContextObserver;
class ExecutionContextObserverDefaultImpl;
// The types of ExecutionContexts that are defined.
enum class ExecutionContextType {
kFrameNode,
kWorkerNode,
};
// An ExecutionContext is a concept from Blink, which denotes a context in
// which Javascript can be executed. Roughly speaking, both FrameNodes and
// WorkerNodes correspond to ExecutionContexts in the PM world-view. This class
// allows external observers to track ExecutionContexts abstractly, without
// having to deal with multiple types at runtime, and duplicating lots of code.
//
// A decorator is responsible for augmenting each instance of a WorkerNode or a
// FrameNode with an instance of ExecutionContext which wraps it, and provides
// accessors for concepts that are common between these two node types. It also
// provides an observer which adapts FrameNodeObserver and WorkerNodeObserver
// behind the scenes, so you don't have to.
//
// Instances of this object are managed by the ExecutionContextRegistry. Their
// lifetimes are the same as those of the underlying nodes they wrap.
class ExecutionContext {
public:
using Observer = ExecutionContextObserver;
using ObserverDefaultImpl = ExecutionContextObserverDefaultImpl;
// Syntactic sugar for converting from a node to the corresponding
// ExecutionContext.
static const ExecutionContext* From(const ExecutionContext* ec) { return ec; }
static const ExecutionContext* From(const FrameNode* frame_node);
static const ExecutionContext* From(const WorkerNode* worker_node);
virtual ~ExecutionContext() = default;
// Returns the type of this ExecutionContext.
virtual ExecutionContextType GetType() const = 0;
// 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 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 state of this node.
virtual NodeState GetNodeState() const = 0;
// Returns the final post-redirect committed URL associated with this
// ExecutionContext. This is the URL of the HTML document (not the javascript)
// in the case of a FrameNode, or the URL of the worker javascript in the case
// 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 current priority of the execution context, and the reason for
// the execution context having that particular priority.
virtual const PriorityAndReason& GetPriorityAndReason() const = 0;
// Returns the underlying FrameNode, if this context is a FrameNode, or
// nullptr otherwise.
virtual const FrameNode* GetFrameNode() const = 0;
// Returns the underlying WorkerNode, if this context is a WorkerNode, or
// nullptr otherwise.
virtual const WorkerNode* GetWorkerNode() const = 0;
};
// An observer for ExecutionContexts.
class ExecutionContextObserver : public base::CheckedObserver {
public:
ExecutionContextObserver() = default;
ExecutionContextObserver(const ExecutionContextObserver&) = delete;
ExecutionContextObserver& operator=(const ExecutionContextObserver&) = delete;
~ExecutionContextObserver() override = default;
// Called when an ExecutionContext is added. The provided pointer is valid
// until a call to OnBeforeExecutionContextRemoved().
virtual void OnExecutionContextAdded(const ExecutionContext* ec) = 0;
// Called when an ExecutionContext is about to be removed. The pointer |ec|
// becomes invalid immediately after this returns.
virtual void OnBeforeExecutionContextRemoved(const ExecutionContext* ec) = 0;
// Invoked when the execution context priority and reason changes.
virtual void OnPriorityAndReasonChanged(
const ExecutionContext* ec,
const PriorityAndReason& previous_value) = 0;
};
// A default implementation of ExecutionContextObserver with empty stubs for all
// functions.
class ExecutionContextObserverDefaultImpl : public ExecutionContextObserver {
public:
ExecutionContextObserverDefaultImpl() = default;
ExecutionContextObserverDefaultImpl(
const ExecutionContextObserverDefaultImpl&) = delete;
ExecutionContextObserverDefaultImpl& operator=(
const ExecutionContextObserverDefaultImpl&) = delete;
~ExecutionContextObserverDefaultImpl() override = default;
// ExecutionContextObserver implementation:
void OnExecutionContextAdded(const ExecutionContext* ec) override {}
void OnBeforeExecutionContextRemoved(const ExecutionContext* ec) override {}
void OnPriorityAndReasonChanged(
const ExecutionContext* ec,
const PriorityAndReason& previous_value) override {}
};
} // namespace execution_context
} // namespace performance_manager
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_EXECUTION_CONTEXT_EXECUTION_CONTEXT_H_
|