• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/simple/post_doom_waiter.h"
6 
7 #include "base/check_op.h"
8 #include "base/functional/bind.h"
9 #include "base/functional/callback.h"
10 #include "net/disk_cache/simple/simple_histogram_macros.h"
11 
12 namespace disk_cache {
13 
14 SimplePostDoomWaiter::SimplePostDoomWaiter() = default;
15 
SimplePostDoomWaiter(base::OnceClosure to_run_post_doom)16 SimplePostDoomWaiter::SimplePostDoomWaiter(base::OnceClosure to_run_post_doom)
17     : run_post_doom(std::move(to_run_post_doom)) {}
18 
19 SimplePostDoomWaiter::SimplePostDoomWaiter(SimplePostDoomWaiter&& other) =
20     default;
21 SimplePostDoomWaiter& SimplePostDoomWaiter::operator=(
22     SimplePostDoomWaiter&& other) = default;
23 SimplePostDoomWaiter::~SimplePostDoomWaiter() = default;
24 
SimplePostDoomWaiterTable(net::CacheType cache_type)25 SimplePostDoomWaiterTable::SimplePostDoomWaiterTable(net::CacheType cache_type)
26     : cache_type_(cache_type) {}
27 SimplePostDoomWaiterTable::~SimplePostDoomWaiterTable() = default;
28 
OnDoomStart(uint64_t entry_hash)29 void SimplePostDoomWaiterTable::OnDoomStart(uint64_t entry_hash) {
30   DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash));
31   entries_pending_doom_.insert(
32       std::make_pair(entry_hash, std::vector<SimplePostDoomWaiter>()));
33 }
34 
OnDoomComplete(uint64_t entry_hash)35 void SimplePostDoomWaiterTable::OnDoomComplete(uint64_t entry_hash) {
36   DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash));
37   auto it = entries_pending_doom_.find(entry_hash);
38   std::vector<SimplePostDoomWaiter> to_handle_waiters;
39   to_handle_waiters.swap(it->second);
40   entries_pending_doom_.erase(it);
41 
42   for (SimplePostDoomWaiter& post_doom : to_handle_waiters) {
43     std::move(post_doom.run_post_doom).Run();
44   }
45 }
46 
Find(uint64_t entry_hash)47 std::vector<SimplePostDoomWaiter>* SimplePostDoomWaiterTable::Find(
48     uint64_t entry_hash) {
49   auto doom_it = entries_pending_doom_.find(entry_hash);
50   if (doom_it != entries_pending_doom_.end())
51     return &doom_it->second;
52   else
53     return nullptr;
54 }
55 
56 }  // namespace disk_cache
57