File: frame_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 (127 lines) | stat: -rw-r--r-- 4,440 bytes parent folder | download | duplicates (6)
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
// 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/frame_context.h"

#include <optional>
#include <sstream>
#include <utility>

#include "base/check.h"
#include "base/memory/weak_ptr.h"
#include "components/performance_manager/graph/frame_node_impl.h"
#include "components/performance_manager/graph/process_node_impl.h"
#include "components/performance_manager/public/graph/frame_node.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_thread.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"

namespace resource_attribution {

namespace {

bool IsValidId(content::GlobalRenderFrameHostId id) {
  return !RenderProcessHostId(id.child_id).is_null() &&
         id.frame_routing_id != MSG_ROUTING_NONE;
}

}  // namespace

FrameContext::FrameContext(content::GlobalRenderFrameHostId id,
                           base::WeakPtr<FrameNode> weak_node)
    : id_(id), weak_node_(weak_node) {}

FrameContext::~FrameContext() = default;

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

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

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

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

// static
std::optional<FrameContext> FrameContext::FromRenderFrameHost(
    content::RenderFrameHost* host) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  CHECK(host);
  CHECK(IsValidId(host->GetGlobalId()));
  base::WeakPtr<FrameNode> frame_node =
      PerformanceManager::GetFrameNodeForRenderFrameHost(host);
  if (!frame_node.MaybeValid()) {
    // GetFrameNodeForRenderFrameHost returns an explicit null WeakPtr if
    // PerformanceManager is not started or `host` has no FrameNode yet. In this
    // case MaybeValid() is guaranteed false and this should return nullopt.
    //
    // Otherwise this should return a FrameContext containing a WeakPtr, which
    // will become invalid once the FrameNode dies. FrameNode outlives the
    // RenderFrameHost, and the caller had a valid RenderFrameHost on the UI
    // thread, so the WeakPtr is guaranteed to be valid at this point. Therefore
    // MaybeValid() can only be false in the first case.
    return std::nullopt;
  }
  return FrameContext(host->GetGlobalId(), std::move(frame_node));
}

content::RenderFrameHost* FrameContext::GetRenderFrameHost() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return content::RenderFrameHost::FromID(id_);
}

content::GlobalRenderFrameHostId FrameContext::GetRenderFrameHostId() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return id_;
}

base::WeakPtr<FrameNode> FrameContext::GetWeakFrameNode() const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  return weak_node_;
}

// static
FrameContext FrameContext::FromFrameNode(const FrameNode* node) {
  CHECK(node);
  DCHECK_ON_GRAPH_SEQUENCE(node->GetGraph());
  auto* node_impl = FrameNodeImpl::FromNode(node);
  CHECK(node_impl->process_node());
  content::GlobalRenderFrameHostId global_id(
      node_impl->process_node()->GetRenderProcessHostId().GetUnsafeValue(),
      node_impl->render_frame_id());
  CHECK(IsValidId(global_id));
  return FrameContext(global_id, node_impl->GetWeakPtr());
}

// static
std::optional<FrameContext> FrameContext::FromWeakFrameNode(
    base::WeakPtr<FrameNode> node) {
  if (!node) {
    return std::nullopt;
  }
  return FromFrameNode(node.get());
}

FrameNode* FrameContext::GetFrameNode() 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 FrameContext::ToString() const {
  // Using stringstream instead of StrCat because `id_` has a streaming
  // operator.
  std::stringstream s;
  s << "FrameContext:" << id_;
  return s.str();
}

}  // namespace resource_attribution