File: process_context.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (193 lines) | stat: -rw-r--r-- 6,872 bytes parent folder | download | duplicates (2)
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
// 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.

#include "components/performance_manager/public/resource_attribution/process_context.h"

#include <optional>
#include <utility>
#include <variant>

#include "base/check.h"
#include "base/functional/overloaded.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/browser_child_process_host_id.h"
#include "components/performance_manager/public/graph/graph.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/public/render_process_host_id.h"
#include "components/performance_manager/resource_attribution/performance_manager_aliases.h"
#include "content/public/browser/browser_child_process_host.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/render_process_host.h"

namespace resource_attribution {

ProcessContext::ProcessContext(AnyProcessHostId id,
                               base::WeakPtr<ProcessNode> weak_node)
    : id_(id), weak_node_(weak_node) {}

ProcessContext::~ProcessContext() = default;

ProcessContext::ProcessContext(const ProcessContext& other) = default;

ProcessContext& ProcessContext::operator=(const ProcessContext& other) =
    default;

ProcessContext::ProcessContext(ProcessContext&& other) = default;

ProcessContext& ProcessContext::operator=(ProcessContext&& other) = default;

// static
std::optional<ProcessContext> ProcessContext::FromBrowserProcess() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  base::WeakPtr<ProcessNode> process_node =
      PerformanceManager::GetProcessNodeForBrowserProcess();
  if (!process_node.MaybeValid()) {
    return std::nullopt;
  }
  return ProcessContext(BrowserProcessTag{}, std::move(process_node));
}

// static
std::optional<ProcessContext> ProcessContext::FromRenderProcessHost(
    content::RenderProcessHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(host);
  RenderProcessHostId id(host->GetID());
  CHECK(!id.is_null());
  base::WeakPtr<ProcessNode> process_node =
      PerformanceManager::GetProcessNodeForRenderProcessHost(host);
  if (!process_node.MaybeValid()) {
    return std::nullopt;
  }
  return ProcessContext(std::move(id), std::move(process_node));
}

// static
std::optional<ProcessContext> ProcessContext::FromBrowserChildProcessHost(
    content::BrowserChildProcessHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(host);
  BrowserChildProcessHostId id(host->GetData().id);
  CHECK(!id.is_null());
  base::WeakPtr<ProcessNode> process_node =
      PerformanceManager::GetProcessNodeForBrowserChildProcessHost(host);
  if (!process_node.MaybeValid()) {
    return std::nullopt;
  }
  return ProcessContext(std::move(id), std::move(process_node));
}

bool ProcessContext::IsBrowserProcessContext() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return std::holds_alternative<BrowserProcessTag>(id_);
}

bool ProcessContext::IsRenderProcessContext() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return std::holds_alternative<RenderProcessHostId>(id_);
}

bool ProcessContext::IsBrowserChildProcessContext() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return std::holds_alternative<BrowserChildProcessHostId>(id_);
}

content::RenderProcessHost* ProcessContext::GetRenderProcessHost() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  const auto* rph_id = std::get_if<RenderProcessHostId>(&id_);
  return rph_id ? content::RenderProcessHost::FromID(rph_id->GetUnsafeValue())
                : nullptr;
}

RenderProcessHostId ProcessContext::GetRenderProcessHostId() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  const auto* rph_id = std::get_if<RenderProcessHostId>(&id_);
  return rph_id ? *rph_id : RenderProcessHostId();
}

content::BrowserChildProcessHost* ProcessContext::GetBrowserChildProcessHost()
    const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  const auto* bcph_id = std::get_if<BrowserChildProcessHostId>(&id_);
  return bcph_id ? content::BrowserChildProcessHost::FromID(
                       bcph_id->GetUnsafeValue())
                 : nullptr;
}

BrowserChildProcessHostId ProcessContext::GetBrowserChildProcessHostId() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  const auto* bcph_id = std::get_if<BrowserChildProcessHostId>(&id_);
  return bcph_id ? *bcph_id : BrowserChildProcessHostId();
}

base::WeakPtr<ProcessNode> ProcessContext::GetWeakProcessNode() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return weak_node_;
}

// static
ProcessContext ProcessContext::FromProcessNode(const ProcessNode* node) {
  CHECK(node);
  DCHECK_ON_GRAPH_SEQUENCE(node->GetGraph());
  auto* node_impl = ProcessNodeImpl::FromNode(node);
  AnyProcessHostId id;
  switch (node_impl->GetProcessType()) {
    case content::PROCESS_TYPE_BROWSER:
      id = BrowserProcessTag{};
      break;
    case content::PROCESS_TYPE_RENDERER:
      id = node_impl->GetRenderProcessHostId();
      CHECK(!std::get<RenderProcessHostId>(id).is_null());
      break;
    default:
      id = node_impl->GetBrowserChildProcessHostProxy()
               .browser_child_process_host_id();
      CHECK(!std::get<BrowserChildProcessHostId>(id).is_null());
      break;
  }
  return ProcessContext(std::move(id), node_impl->GetWeakPtr());
}

// static
std::optional<ProcessContext> ProcessContext::FromWeakProcessNode(
    base::WeakPtr<ProcessNode> node) {
  if (!node) {
    return std::nullopt;
  }
  return FromProcessNode(node.get());
}

ProcessNode* ProcessContext::GetProcessNode() const {
  if (weak_node_) {
    // `weak_node` will check anyway if dereferenced from the wrong sequence,
    // but let's be explicit.
    DCHECK_ON_GRAPH_SEQUENCE(weak_node_->GetGraph());
    return weak_node_.get();
  }
  return nullptr;
}

std::string ProcessContext::ToString() const {
  return std::visit(
      base::Overloaded{
          [](const BrowserProcessTag&) -> std::string {
            return "ProcessContext:Browser";
          },
          [](const RenderProcessHostId& id) -> std::string {
            return base::StrCat({"ProcessContext:Renderer:",
                                 base::NumberToString(id.GetUnsafeValue())});
          },
          [](const BrowserChildProcessHostId& id) -> std::string {
            return base::StrCat({"ProcessContext:BrowserChild:",
                                 base::NumberToString(id.GetUnsafeValue())});
          },
      },
      id_);
}

}  // namespace resource_attribution