File: shared_dictionary_manager_in_memory.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (275 lines) | stat: -rw-r--r-- 9,810 bytes parent folder | download | duplicates (4)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// 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 "services/network/shared_dictionary/shared_dictionary_manager_in_memory.h"

#include <algorithm>

#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "services/network/shared_dictionary/shared_dictionary_storage_in_memory.h"

namespace network {

namespace {

class DictionaryReference {
 public:
  DictionaryReference(
      raw_ptr<SharedDictionaryStorageInMemory> storage,
      raw_ptr<const SharedDictionaryStorageInMemory::DictionaryInfo> dict)
      : storage_(storage), dict_(dict) {}

  DictionaryReference(const DictionaryReference&) = default;
  DictionaryReference& operator=(const DictionaryReference&) = default;
  DictionaryReference(DictionaryReference&& other) = default;
  DictionaryReference& operator=(DictionaryReference&& other) = default;

  ~DictionaryReference() = default;

  raw_ptr<SharedDictionaryStorageInMemory> storage() const { return storage_; }
  const raw_ptr<const SharedDictionaryStorageInMemory::DictionaryInfo> dict()
      const {
    return dict_;
  }

 private:
  raw_ptr<SharedDictionaryStorageInMemory> storage_;
  raw_ptr<const SharedDictionaryStorageInMemory::DictionaryInfo> dict_;
};

struct LastUsedTimeLess {
  bool operator()(const DictionaryReference& a,
                  const DictionaryReference& b) const noexcept {
    return a.dict()->last_used_time() < b.dict()->last_used_time();
  }
};

class EvictionCandidate {
 public:
  EvictionCandidate(raw_ptr<SharedDictionaryStorageInMemory> storage,
                    const url::SchemeHostPort& host,
                    const std::string& match,
                    const std::set<mojom::RequestDestination>& match_dest)
      : storage_(storage),
        host_(host),
        match_(match),
        match_dest_(match_dest) {}

  EvictionCandidate(const EvictionCandidate&) = default;
  EvictionCandidate& operator=(const EvictionCandidate&) = default;
  EvictionCandidate(EvictionCandidate&& other) = default;
  EvictionCandidate& operator=(EvictionCandidate&& other) = default;
  ~EvictionCandidate() = default;

  const url::SchemeHostPort& host() const { return host_; }
  raw_ptr<SharedDictionaryStorageInMemory> storage() const { return storage_; }
  const std::string& match() const { return match_; }
  const std::set<mojom::RequestDestination> match_dest() const {
    return match_dest_;
  }

 private:
  raw_ptr<SharedDictionaryStorageInMemory> storage_;
  url::SchemeHostPort host_;
  std::string match_;
  std::set<mojom::RequestDestination> match_dest_;
};

}  // namespace

SharedDictionaryManagerInMemory::SharedDictionaryManagerInMemory(
    uint64_t cache_max_size,
    uint64_t cache_max_count)
    : cache_max_size_(cache_max_size), cache_max_count_(cache_max_count) {}

SharedDictionaryManagerInMemory::~SharedDictionaryManagerInMemory() = default;

scoped_refptr<SharedDictionaryStorage>
SharedDictionaryManagerInMemory::CreateStorage(
    const net::SharedDictionaryIsolationKey& isolation_key,
    SharedDictionaryStorageEvictionReason previous_eviction_reason) {
  return base::MakeRefCounted<SharedDictionaryStorageInMemory>(
      weak_factory_.GetWeakPtr(), isolation_key,
      base::ScopedClosureRunner(
          base::BindOnce(&SharedDictionaryManager::OnStorageDeleted,
                         GetWeakPtr(), isolation_key)));
}

void SharedDictionaryManagerInMemory::SetCacheMaxSize(uint64_t cache_max_size) {
  cache_max_size_ = cache_max_size;
  MaybeRunCacheEviction();
}

void SharedDictionaryManagerInMemory::ClearData(
    base::Time start_time,
    base::Time end_time,
    base::RepeatingCallback<bool(const GURL&)> url_matcher,
    base::OnceClosure callback) {
  for (const auto& it : storages()) {
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it.second.get());
    base::RepeatingCallback<bool(const GURL&)> matcher = url_matcher;
    if (matcher && (matcher.Run(it.first.frame_origin().GetURL()) ||
                    matcher.Run(it.first.top_frame_site().GetURL()))) {
      matcher.Reset();
    }
    storage->ClearData(start_time, end_time, std::move(matcher));
  }
  std::move(callback).Run();
}

void SharedDictionaryManagerInMemory::ClearDataForIsolationKey(
    const net::SharedDictionaryIsolationKey& isolation_key,
    base::OnceClosure callback) {
  auto it = storages().find(isolation_key);
  if (it != storages().end()) {
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it->second.get());
    storage->ClearAllDictionaries();
  }
  std::move(callback).Run();
}

void SharedDictionaryManagerInMemory::MaybeRunCacheEvictionPerSite(
    const net::SchemefulSite& top_frame_site) {
  RunCacheEvictionImpl(top_frame_site, cache_max_size_ / 2, cache_max_size_ / 2,
                       cache_max_count_ / 2, cache_max_count_ / 2);
}

void SharedDictionaryManagerInMemory::MaybeRunCacheEviction() {
  RunCacheEvictionImpl(std::nullopt, cache_max_size_, cache_max_size_ * 0.9,
                       cache_max_count_, cache_max_count_ * 0.9);
}

void SharedDictionaryManagerInMemory::RunCacheEvictionImpl(
    std::optional<net::SchemefulSite> top_frame_site,
    uint64_t max_size,
    uint64_t size_low_watermark,
    uint64_t max_count,
    uint64_t count_low_watermark) {
  uint64_t total_size = 0u;
  size_t dictionary_count = 0u;
  for (const auto& it1 : storages()) {
    if (top_frame_site && it1.first.top_frame_site() != *top_frame_site) {
      continue;
    }
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it1.second.get());
    for (const auto& it2 : storage->GetDictionaryMap()) {
      dictionary_count += it2.second.size();
      for (const auto& it3 : it2.second) {
        total_size += it3.second.size();
      }
    }
  }

  if ((max_size == 0 || total_size <= max_size) &&
      dictionary_count <= max_count) {
    return;
  }

  std::vector<DictionaryReference> dictionaries;
  dictionaries.reserve(dictionary_count);
  for (auto& it1 : storages()) {
    if (top_frame_site && it1.first.top_frame_site() != *top_frame_site) {
      continue;
    }
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it1.second.get());
    for (auto& it2 : storage->GetDictionaryMap()) {
      for (auto& it3 : it2.second) {
        dictionaries.emplace_back(storage, &it3.second);
      }
    }
  }

  std::sort(dictionaries.begin(), dictionaries.end(), LastUsedTimeLess{});

  uint64_t to_be_removed_count = 0;
  if (dictionary_count > count_low_watermark) {
    to_be_removed_count = dictionary_count - count_low_watermark;
  }

  std::vector<EvictionCandidate> eviction_candidates;
  for (auto& dict_ref : dictionaries) {
    total_size -= dict_ref.dict()->size();
    eviction_candidates.emplace_back(
        dict_ref.storage(), url::SchemeHostPort(dict_ref.dict()->url()),
        dict_ref.dict()->match(), dict_ref.dict()->match_dest());
    if ((max_size == 0 || size_low_watermark >= total_size) &&
        eviction_candidates.size() >= to_be_removed_count) {
      break;
    }
  }
  dictionaries.clear();  // Unneeded, and may ref. about-to-be-deleted things.
  for (auto& candidate : eviction_candidates) {
    candidate.storage()->DeleteDictionary(candidate.host(), candidate.match(),
                                          candidate.match_dest());
  }
}

void SharedDictionaryManagerInMemory::GetUsageInfo(
    base::OnceCallback<void(const std::vector<net::SharedDictionaryUsageInfo>&)>
        callback) {
  std::vector<net::SharedDictionaryUsageInfo> result;
  for (auto& it : storages()) {
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it.second.get());
    net::SharedDictionaryUsageInfo usage;

    for (const auto& it1 : storage->GetDictionaryMap()) {
      for (const auto& it2 : it1.second) {
        usage.total_size_bytes += it2.second.size();
      }
    }
    if (usage.total_size_bytes != 0) {
      usage.isolation_key = it.first;
      result.emplace_back(usage);
    }
  }
  std::move(callback).Run(std::move(result));
}

void SharedDictionaryManagerInMemory::GetSharedDictionaryInfo(
    const net::SharedDictionaryIsolationKey& isolation_key,
    base::OnceCallback<
        void(std::vector<network::mojom::SharedDictionaryInfoPtr>)> callback) {
  std::vector<network::mojom::SharedDictionaryInfoPtr> dictionaries;

  const auto it = storages().find(isolation_key);
  if (it == storages().end()) {
    std::move(callback).Run(std::move(dictionaries));
    return;
  }
  SharedDictionaryStorageInMemory* storage =
      reinterpret_cast<SharedDictionaryStorageInMemory*>(it->second.get());

  std::vector<network::mojom::SharedDictionaryInfoPtr> dicts;
  for (const auto& it1 : storage->GetDictionaryMap()) {
    for (const auto& it2 : it1.second) {
      dictionaries.push_back(ToMojoSharedDictionaryInfo(it2.second));
    }
  }
  std::move(callback).Run(std::move(dictionaries));
}

void SharedDictionaryManagerInMemory::GetOriginsBetween(
    base::Time start_time,
    base::Time end_time,
    base::OnceCallback<void(const std::vector<url::Origin>&)> callback) {
  std::set<url::Origin> origins;
  for (const auto& it : storages()) {
    SharedDictionaryStorageInMemory* storage =
        reinterpret_cast<SharedDictionaryStorageInMemory*>(it.second.get());
    if (storage->HasDictionaryBetween(start_time, end_time)) {
      origins.insert(it.first.frame_origin());
    }
  }
  std::move(callback).Run(
      std::vector<url::Origin>(origins.begin(), origins.end()));
}

}  // namespace network