File: data_decoder_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (124 lines) | stat: -rw-r--r-- 4,017 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2025 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/interest_group/data_decoder_manager.h"

#include <map>
#include <memory>
#include <optional>
#include <tuple>
#include <utility>

#include "base/check.h"
#include "base/containers/span.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/types/pass_key.h"
#include "content/common/content_export.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "url/origin.h"

namespace content {

DataDecoderManager::Handle::Handle(base::PassKey<DataDecoderManager>,
                                   DataDecoderManager* manager,
                                   DecoderMap::iterator decoder_it)
    : manager_(manager), decoder_it_(decoder_it) {
  ++decoder_it_->second.num_handles;
}

DataDecoderManager::Handle::~Handle() {
  CHECK_GT(decoder_it_->second.num_handles, 0u);
  --decoder_it_->second.num_handles;
  manager_->OnHandleDestroyed(decoder_it_);
}

data_decoder::DataDecoder& DataDecoderManager::Handle::data_decoder() {
  return decoder_it_->second.decoder;
}

DataDecoderManager::DataDecoderManager() = default;

DataDecoderManager::~DataDecoderManager() {
  // Check that no decoders have any outstanding handles.
  for (const auto& decoder : decoder_map_) {
    CHECK_EQ(decoder.second.num_handles, 0u);
  }
}

std::unique_ptr<DataDecoderManager::Handle> DataDecoderManager::GetHandle(
    const url::Origin& main_frame_origin,
    const url::Origin& owner_origin) {
  auto [decoder_it, unused_inserted] =
      decoder_map_.try_emplace({main_frame_origin, owner_origin});
  decoder_it->second.expected_service_teardown_time.reset();
  return std::make_unique<Handle>(base::PassKey<DataDecoderManager>(), this,
                                  decoder_it);
}

size_t DataDecoderManager::NumDecodersForTesting() const {
  return decoder_map_.size();
}

std::optional<size_t> DataDecoderManager::GetHandleCountForTesting(
    const url::Origin& main_frame_origin,
    const url::Origin& owner_origin) const {
  auto decoder_it = decoder_map_.find({main_frame_origin, owner_origin});
  if (decoder_it == decoder_map_.end()) {
    return std::nullopt;
  }
  return decoder_it->second.num_handles;
}

void DataDecoderManager::OnHandleDestroyed(DecoderMap::iterator decoder_it) {
  if (decoder_it->second.num_handles > 0) {
    return;
  }

  decoder_it->second.expected_service_teardown_time =
      base::TimeTicks::Now() + kIdleTimeout;
  if (!timer_.IsRunning()) {
    timer_.Start(FROM_HERE, kIdleTimeout,
                 base::BindOnce(&DataDecoderManager::CleanUpIdleDecoders,
                                base::Unretained(this)));
  }
}

void DataDecoderManager::CleanUpIdleDecoders() {
  bool should_restart_timer = false;
  base::TimeTicks now = base::TimeTicks::Now();
  for (auto next_it = decoder_map_.begin(); next_it != decoder_map_.end();) {
    auto it = next_it;
    ++next_it;
    // For decoders with live Handles, nothing to do.
    if (!it->second.expected_service_teardown_time) {
      DCHECK_GT(it->second.num_handles, 0u);
      continue;
    }

    DCHECK_EQ(it->second.num_handles, 0u);

    // Destroy expired decoders.
    if (*it->second.expected_service_teardown_time <= now) {
      decoder_map_.erase(it);
      continue;
    }

    // If there are any decoders with an expiration time, but that expiration
    // time has not yet been reached, need to queue a cleanup task. Such
    // decoders should have a teardown time within `kIdleTimeout` of now.
    DCHECK_LE(*it->second.expected_service_teardown_time, now + kIdleTimeout);
    should_restart_timer = true;
  }

  if (should_restart_timer) {
    timer_.Start(FROM_HERE, kIdleTimeout,
                 base::BindOnce(&DataDecoderManager::CleanUpIdleDecoders,
                                base::Unretained(this)));
  }
}

}  // namespace content