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 #ifndef NET_DISK_CACHE_SIMPLE_POST_DOOM_WAITER_H_ 6 #define NET_DISK_CACHE_SIMPLE_POST_DOOM_WAITER_H_ 7 8 #include <stdint.h> 9 10 #include <unordered_map> 11 #include <vector> 12 13 #include "base/functional/callback.h" 14 #include "base/memory/ref_counted.h" 15 #include "net/base/cache_type.h" 16 17 namespace disk_cache { 18 19 struct SimplePostDoomWaiter { 20 SimplePostDoomWaiter(); 21 explicit SimplePostDoomWaiter(base::OnceClosure to_run_post_doom); 22 SimplePostDoomWaiter(SimplePostDoomWaiter&& other); 23 ~SimplePostDoomWaiter(); 24 SimplePostDoomWaiter& operator=(SimplePostDoomWaiter&& other); 25 26 base::OnceClosure run_post_doom; 27 }; 28 29 // See |SimpleBackendImpl::post_doom_waiting_| for the description. This is 30 // refcounted since sometimes this needs to survive backend destruction to 31 // complete some per-entry operations. 32 class SimplePostDoomWaiterTable 33 : public base::RefCounted<SimplePostDoomWaiterTable> { 34 friend class base::RefCounted<SimplePostDoomWaiterTable>; 35 36 public: 37 explicit SimplePostDoomWaiterTable(net::CacheType cache_type); 38 39 SimplePostDoomWaiterTable(const SimplePostDoomWaiterTable&) = delete; 40 SimplePostDoomWaiterTable& operator=(const SimplePostDoomWaiterTable&) = 41 delete; 42 43 // The entry for |entry_hash| is being doomed; the backend will not attempt 44 // to run new operations for this |entry_hash| until the Doom is completed. 45 void OnDoomStart(uint64_t entry_hash); 46 47 // The entry for |entry_hash| has been successfully doomed, we can now allow 48 // operations on this entry, and we can run any operations enqueued while the 49 // doom completed. 50 void OnDoomComplete(uint64_t entry_hash); 51 52 // Returns nullptr if not found. 53 std::vector<SimplePostDoomWaiter>* Find(uint64_t entry_hash); 54 Has(uint64_t entry_hash)55 bool Has(uint64_t entry_hash) { 56 return entries_pending_doom_.find(entry_hash) != 57 entries_pending_doom_.end(); 58 } 59 60 private: 61 ~SimplePostDoomWaiterTable(); 62 63 net::CacheType cache_type_; 64 std::unordered_map<uint64_t, std::vector<SimplePostDoomWaiter>> 65 entries_pending_doom_; 66 }; 67 68 } // namespace disk_cache 69 70 #endif // NET_DISK_CACHE_SIMPLE_POST_DOOM_WAITER_H_ 71