File: frame_data_utils.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 (102 lines) | stat: -rw-r--r-- 3,927 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2021 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/page_load_metrics/browser/observers/ad_metrics/frame_data_utils.h"

#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
#include "net/base/mime_util.h"
#include "third_party/blink/public/common/mime_util/mime_util.h"

namespace page_load_metrics {

ResourceLoadAggregator::ResourceLoadAggregator() = default;
ResourceLoadAggregator::~ResourceLoadAggregator() = default;

// static
ResourceMimeType ResourceLoadAggregator::GetResourceMimeType(
    const mojom::ResourceDataUpdatePtr& resource) {
  if (blink::IsSupportedImageMimeType(resource->mime_type))
    return ResourceMimeType::kImage;
  if (blink::IsSupportedJavascriptMimeType(resource->mime_type))
    return ResourceMimeType::kJavascript;

  std::string top_level_type;
  std::string subtype;
  // Categorize invalid mime types as "Other".
  if (!net::ParseMimeTypeWithoutParameter(resource->mime_type, &top_level_type,
                                          &subtype)) {
    return ResourceMimeType::kOther;
  }
  if (top_level_type.compare("video") == 0)
    return ResourceMimeType::kVideo;
  if (top_level_type.compare("text") == 0 && subtype.compare("css") == 0)
    return ResourceMimeType::kCss;
  if (top_level_type.compare("text") == 0 && subtype.compare("html") == 0)
    return ResourceMimeType::kHtml;
  return ResourceMimeType::kOther;
}

void ResourceLoadAggregator::ProcessResourceLoad(
    const mojom::ResourceDataUpdatePtr& resource) {
  bytes_ += resource->delta_bytes;
  network_bytes_ += resource->delta_bytes;

  // Report cached resource body bytes to overall frame bytes.
  if (resource->is_complete &&
      resource->cache_type != mojom::CacheType::kNotCached) {
    bytes_ += resource->encoded_body_length;
  }

  if (resource->reported_as_ad_resource) {
    ad_network_bytes_ += resource->delta_bytes;
    ad_bytes_ += resource->delta_bytes;
    // Report cached resource body bytes to overall frame bytes.
    if (resource->is_complete &&
        resource->cache_type != mojom::CacheType::kNotCached)
      ad_bytes_ += resource->encoded_body_length;

    ResourceMimeType mime_type = GetResourceMimeType(resource);
    ad_bytes_by_mime_[static_cast<size_t>(mime_type)] += resource->delta_bytes;
  }
}

void ResourceLoadAggregator::AdjustAdBytes(int64_t unaccounted_ad_bytes,
                                           ResourceMimeType mime_type) {
  ad_network_bytes_ += unaccounted_ad_bytes;
  ad_bytes_ += unaccounted_ad_bytes;
  ad_bytes_by_mime_[static_cast<size_t>(mime_type)] += unaccounted_ad_bytes;
}

PeakCpuAggregator::PeakCpuAggregator() = default;
PeakCpuAggregator::~PeakCpuAggregator() = default;

// static
constexpr base::TimeDelta PeakCpuAggregator::kWindowSize;

void PeakCpuAggregator::UpdatePeakWindowedPercent(
    base::TimeDelta cpu_usage_update,
    base::TimeTicks update_time) {
  current_window_total_ += cpu_usage_update;
  current_window_updates_.push(CpuUpdateData(update_time, cpu_usage_update));
  base::TimeTicks cutoff_time = update_time - kWindowSize;
  while (!current_window_updates_.empty() &&
         current_window_updates_.front().update_time < cutoff_time) {
    current_window_total_ -= current_window_updates_.front().usage_info;
    current_window_updates_.pop();
  }
  int current_windowed_percent = 100 * current_window_total_.InMilliseconds() /
                                 kWindowSize.InMilliseconds();
  if (current_windowed_percent > peak_windowed_percent_)
    peak_windowed_percent_ = current_windowed_percent;
}

void MemoryUsageAggregator::UpdateUsage(int64_t delta_bytes) {
  current_bytes_used_ += delta_bytes;
  if (current_bytes_used_ > max_bytes_used_)
    max_bytes_used_ = current_bytes_used_;
}

}  // namespace page_load_metrics