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
|
// Copyright 2023 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_RESOURCE_ATTRIBUTION_PROCESS_CONTEXT_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_PROCESS_CONTEXT_H_
#include <compare>
#include <optional>
#include <string>
#include <variant>
#include "base/memory/weak_ptr.h"
#include "components/performance_manager/public/browser_child_process_host_id.h"
#include "components/performance_manager/public/render_process_host_id.h"
namespace content {
class BrowserChildProcessHost;
class RenderProcessHost;
} // namespace content
namespace performance_manager {
class ProcessNode;
}
namespace resource_attribution {
class ProcessContext {
public:
~ProcessContext();
ProcessContext(const ProcessContext& other);
ProcessContext& operator=(const ProcessContext& other);
ProcessContext(ProcessContext&& other);
ProcessContext& operator=(ProcessContext&& other);
// UI thread methods.
// Returns the ProcessContext for the browser process, or nullopt if there is
// none. (This could happen in tests, or before the PerformanceManager
// starts.)
static std::optional<ProcessContext> FromBrowserProcess();
// Returns the ProcessContext for the renderer process hosted in `host`, which
// must be non-null and have a valid RenderProcessHostId. Returns nullopt if
// the RenderProcessHost is not registered with PerformanceManager.
static std::optional<ProcessContext> FromRenderProcessHost(
content::RenderProcessHost* host);
// Returns the ProcessContext for the non-renderer child process hosted in
// `host`, which must be non-null and have a valid BrowserChildProcessHostId.
// Returns nullopt if the BrowserChildProcessHost is not registered with
// PerformanceManager.
static std::optional<ProcessContext> FromBrowserChildProcessHost(
content::BrowserChildProcessHost* host);
// Returns true iff this context refers to the browser process.
bool IsBrowserProcessContext() const;
// Returns true iff this context refers to a renderer process.
bool IsRenderProcessContext() const;
// Returns true iff this context refers to a non-renderer child process.
bool IsBrowserChildProcessContext() const;
// If this context refers to a renderer process, returns its
// RenderProcessHost. Returns nullptr if it is not a renderer process or the
// host no longer exists.
content::RenderProcessHost* GetRenderProcessHost() const;
// If this context refers to a renderer process, returns the
// RenderProcessHostId that was assigned to it, otherwise returns a null
// RenderProcessHostId.
performance_manager::RenderProcessHostId GetRenderProcessHostId() const;
// If this context refers to a non-renderer child process, returns its
// BrowserChildProcessHost. Returns nullptr if it is not a non-renderer child
// process or the host no longer exists.
content::BrowserChildProcessHost* GetBrowserChildProcessHost() const;
// If this context refers to a non-renderer child process, returns
// the BrowserChildProcessHostId that was assigned to it, otherwise returns a
// null BrowserChildProcessHostId.
performance_manager::BrowserChildProcessHostId GetBrowserChildProcessHostId()
const;
// Returns the ProcessNode for this context, or a null WeakPtr if it no longer
// exists.
base::WeakPtr<performance_manager::ProcessNode> GetWeakProcessNode() const;
// PM sequence methods.
// Returns the ProcessContext for `node`. Equivalent to
// node->GetResourceContext().
static ProcessContext FromProcessNode(
const performance_manager::ProcessNode* node);
// Returns the ProcessContext for `node`, or nullopt if `node` is null.
static std::optional<ProcessContext> FromWeakProcessNode(
base::WeakPtr<performance_manager::ProcessNode> node);
// Returns the ProcessNode for this context, or nullptr if it no longer
// exists.
performance_manager::ProcessNode* GetProcessNode() const;
// Returns a string representation of the context for debugging. This matches
// the interface of base::TokenType and base::UnguessableToken, for
// convenience.
std::string ToString() const;
// Compare ProcessContexts by process host id.
constexpr friend auto operator<=>(const ProcessContext& a,
const ProcessContext& b) {
return a.id_ <=> b.id_;
}
// Test ProcessContexts for equality by process host id.
constexpr friend bool operator==(const ProcessContext& a,
const ProcessContext& b) {
return a.id_ == b.id_;
}
private:
// A tag for the browser process which has no id.
struct BrowserProcessTag {
friend constexpr auto operator<=>(const BrowserProcessTag&,
const BrowserProcessTag&) = default;
friend constexpr bool operator==(const BrowserProcessTag&,
const BrowserProcessTag&) = default;
};
static_assert(BrowserProcessTag{} == BrowserProcessTag{},
"empty structs should always compare equal");
using AnyProcessHostId =
std::variant<BrowserProcessTag,
performance_manager::RenderProcessHostId,
performance_manager::BrowserChildProcessHostId>;
ProcessContext(AnyProcessHostId id,
base::WeakPtr<performance_manager::ProcessNode> weak_node);
AnyProcessHostId id_;
base::WeakPtr<performance_manager::ProcessNode> weak_node_;
};
} // namespace resource_attribution
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_RESOURCE_ATTRIBUTION_PROCESS_CONTEXT_H_
|