File: devtools_issue_storage.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 (88 lines) | stat: -rw-r--r-- 3,253 bytes parent folder | download | duplicates (5)
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
// 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.

#include "content/browser/devtools/devtools_issue_storage.h"

#include <bit>
#include <cstdint>

#include "content/browser/devtools/protocol/audits.h"
#include "content/browser/devtools/render_frame_devtools_agent_host.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/page_transition_types.h"

namespace content {

static const unsigned kMaxIssueCount = 1000;

PAGE_USER_DATA_KEY_IMPL(DevToolsIssueStorage);

DevToolsIssueStorage::DevToolsIssueStorage(Page& page)
    : PageUserData<DevToolsIssueStorage>(page) {
  // DevToolsIssueStorage is only created for outermost pages.
  DCHECK(!page.GetMainDocument().GetParentOrOuterDocument());
}

DevToolsIssueStorage::~DevToolsIssueStorage() {
  // TOOD(1351587): remove explicit destructor once the bug is fixed.
  // This is so that crash key is scoped to issue destruction.
  SCOPED_CRASH_KEY_NUMBER("devtools", "audit_issue_count",
                          std::bit_width<uint32_t>(total_added_issues_) - 1);
  issues_.clear();
}

void DevToolsIssueStorage::AddInspectorIssue(
    RenderFrameHost* rfh,
    std::unique_ptr<protocol::Audits::InspectorIssue> issue) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  DCHECK_LE(issues_.size(), kMaxIssueCount);
  if (issues_.size() == kMaxIssueCount) {
    issues_.pop_front();
  }
  total_added_issues_++;
  issues_.emplace_back(rfh->GetGlobalId(), std::move(issue));
}

std::vector<const protocol::Audits::InspectorIssue*>
DevToolsIssueStorage::FindIssuesForAgentOf(
    RenderFrameHost* render_frame_host) const {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  RenderFrameHostImpl* render_frame_host_impl =
      static_cast<RenderFrameHostImpl*>(render_frame_host);
  RenderFrameHostImpl* main_rfh =
      static_cast<RenderFrameHostImpl*>(&page().GetMainDocument());
  DevToolsAgentHostImpl* agent_host =
      RenderFrameDevToolsAgentHost::GetFor(render_frame_host_impl);
  DCHECK_EQ(&render_frame_host->GetOutermostMainFrame()->GetPage(), &page());
  DCHECK(RenderFrameDevToolsAgentHost::ShouldCreateDevToolsForHost(
      render_frame_host_impl));
  DCHECK(agent_host);
  bool is_main_agent = render_frame_host_impl == main_rfh;

  std::vector<const protocol::Audits::InspectorIssue*> issues;
  for (const auto& entry : issues_) {
    bool should_add;
    RenderFrameHostImpl* issue_rfh = RenderFrameHostImpl::FromID(entry.first);
    if (!issue_rfh) {
      // Issues that fall in this category are either associated with |main_rfh|
      // or with deleted subframe RFHs of |main_rfh|. In both cases, we only
      // want to retrieve them for |main_rfh|'s agent.
      // Note: This means that issues for deleted subframe RFHs get reparented
      // to |main_rfh| after deletion.
      should_add = is_main_agent;
    } else {
      should_add =
          RenderFrameDevToolsAgentHost::GetFor(issue_rfh) == agent_host;
    }
    if (should_add)
      issues.push_back(entry.second.get());
  }
  return issues;
}

}  // namespace content