| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <memory> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/callback.h" | 14 #include "base/callback.h" |
| 14 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
| 15 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 16 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/scoped_ptr.h" | |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
| 21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 24 #include "net/base/cache_type.h" | 24 #include "net/base/cache_type.h" |
| 25 #include "net/base/completion_callback.h" | 25 #include "net/base/completion_callback.h" |
| 26 #include "net/base/net_export.h" | 26 #include "net/base/net_export.h" |
| 27 | 27 |
| 28 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 INITIALIZE_METHOD_LOADED = 1, | 84 INITIALIZE_METHOD_LOADED = 1, |
| 85 INITIALIZE_METHOD_NEWCACHE = 2, | 85 INITIALIZE_METHOD_NEWCACHE = 2, |
| 86 INITIALIZE_METHOD_MAX = 3, | 86 INITIALIZE_METHOD_MAX = 3, |
| 87 }; | 87 }; |
| 88 | 88 |
| 89 typedef std::vector<uint64_t> HashList; | 89 typedef std::vector<uint64_t> HashList; |
| 90 | 90 |
| 91 SimpleIndex(const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, | 91 SimpleIndex(const scoped_refptr<base::SingleThreadTaskRunner>& io_thread, |
| 92 SimpleIndexDelegate* delegate, | 92 SimpleIndexDelegate* delegate, |
| 93 net::CacheType cache_type, | 93 net::CacheType cache_type, |
| 94 scoped_ptr<SimpleIndexFile> simple_index_file); | 94 std::unique_ptr<SimpleIndexFile> simple_index_file); |
| 95 | 95 |
| 96 virtual ~SimpleIndex(); | 96 virtual ~SimpleIndex(); |
| 97 | 97 |
| 98 void Initialize(base::Time cache_mtime); | 98 void Initialize(base::Time cache_mtime); |
| 99 | 99 |
| 100 void SetMaxSize(uint64_t max_bytes); | 100 void SetMaxSize(uint64_t max_bytes); |
| 101 uint64_t max_size() const { return max_size_; } | 101 uint64_t max_size() const { return max_size_; } |
| 102 | 102 |
| 103 void Insert(uint64_t entry_hash); | 103 void Insert(uint64_t entry_hash); |
| 104 void Remove(uint64_t entry_hash); | 104 void Remove(uint64_t entry_hash); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 123 const EntryMetadata& entry_metadata, | 123 const EntryMetadata& entry_metadata, |
| 124 EntrySet* entry_set); | 124 EntrySet* entry_set); |
| 125 | 125 |
| 126 // Executes the |callback| when the index is ready. Allows multiple callbacks. | 126 // Executes the |callback| when the index is ready. Allows multiple callbacks. |
| 127 int ExecuteWhenReady(const net::CompletionCallback& callback); | 127 int ExecuteWhenReady(const net::CompletionCallback& callback); |
| 128 | 128 |
| 129 // Returns entries from the index that have last accessed time matching the | 129 // Returns entries from the index that have last accessed time matching the |
| 130 // range between |initial_time| and |end_time| where open intervals are | 130 // range between |initial_time| and |end_time| where open intervals are |
| 131 // possible according to the definition given in |DoomEntriesBetween()| in the | 131 // possible according to the definition given in |DoomEntriesBetween()| in the |
| 132 // disk cache backend interface. | 132 // disk cache backend interface. |
| 133 scoped_ptr<HashList> GetEntriesBetween(const base::Time initial_time, | 133 std::unique_ptr<HashList> GetEntriesBetween(const base::Time initial_time, |
| 134 const base::Time end_time); | 134 const base::Time end_time); |
| 135 | 135 |
| 136 // Returns the list of all entries key hash. | 136 // Returns the list of all entries key hash. |
| 137 scoped_ptr<HashList> GetAllHashes(); | 137 std::unique_ptr<HashList> GetAllHashes(); |
| 138 | 138 |
| 139 // Returns number of indexed entries. | 139 // Returns number of indexed entries. |
| 140 int32_t GetEntryCount() const; | 140 int32_t GetEntryCount() const; |
| 141 | 141 |
| 142 // Returns the size of the entire cache in bytes. Can only be called after the | 142 // Returns the size of the entire cache in bytes. Can only be called after the |
| 143 // index has been initialized. | 143 // index has been initialized. |
| 144 uint64_t GetCacheSize() const; | 144 uint64_t GetCacheSize() const; |
| 145 | 145 |
| 146 // Returns whether the index has been initialized yet. | 146 // Returns whether the index has been initialized yet. |
| 147 bool initialized() const { return initialized_; } | 147 bool initialized() const { return initialized_; } |
| 148 | 148 |
| 149 IndexInitMethod init_method() const { return init_method_; } | 149 IndexInitMethod init_method() const { return init_method_; } |
| 150 | 150 |
| 151 private: | 151 private: |
| 152 friend class SimpleIndexTest; | 152 friend class SimpleIndexTest; |
| 153 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, IndexSizeCorrectOnMerge); | 153 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, IndexSizeCorrectOnMerge); |
| 154 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteQueued); | 154 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteQueued); |
| 155 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteExecuted); | 155 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWriteExecuted); |
| 156 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWritePostponed); | 156 FRIEND_TEST_ALL_PREFIXES(SimpleIndexTest, DiskWritePostponed); |
| 157 | 157 |
| 158 void StartEvictionIfNeeded(); | 158 void StartEvictionIfNeeded(); |
| 159 void EvictionDone(int result); | 159 void EvictionDone(int result); |
| 160 | 160 |
| 161 void PostponeWritingToDisk(); | 161 void PostponeWritingToDisk(); |
| 162 | 162 |
| 163 void UpdateEntryIteratorSize(EntrySet::iterator* it, int64_t entry_size); | 163 void UpdateEntryIteratorSize(EntrySet::iterator* it, int64_t entry_size); |
| 164 | 164 |
| 165 // Must run on IO Thread. | 165 // Must run on IO Thread. |
| 166 void MergeInitializingSet(scoped_ptr<SimpleIndexLoadResult> load_result); | 166 void MergeInitializingSet(std::unique_ptr<SimpleIndexLoadResult> load_result); |
| 167 | 167 |
| 168 #if defined(OS_ANDROID) | 168 #if defined(OS_ANDROID) |
| 169 void OnApplicationStateChange(base::android::ApplicationState state); | 169 void OnApplicationStateChange(base::android::ApplicationState state); |
| 170 | 170 |
| 171 scoped_ptr<base::android::ApplicationStatusListener> app_status_listener_; | 171 std::unique_ptr<base::android::ApplicationStatusListener> |
| 172 app_status_listener_; |
| 172 #endif | 173 #endif |
| 173 | 174 |
| 174 // The owner of |this| must ensure the |delegate_| outlives |this|. | 175 // The owner of |this| must ensure the |delegate_| outlives |this|. |
| 175 SimpleIndexDelegate* delegate_; | 176 SimpleIndexDelegate* delegate_; |
| 176 | 177 |
| 177 EntrySet entries_set_; | 178 EntrySet entries_set_; |
| 178 | 179 |
| 179 const net::CacheType cache_type_; | 180 const net::CacheType cache_type_; |
| 180 uint64_t cache_size_; // Total cache storage size in bytes. | 181 uint64_t cache_size_; // Total cache storage size in bytes. |
| 181 uint64_t max_size_; | 182 uint64_t max_size_; |
| 182 uint64_t high_watermark_; | 183 uint64_t high_watermark_; |
| 183 uint64_t low_watermark_; | 184 uint64_t low_watermark_; |
| 184 bool eviction_in_progress_; | 185 bool eviction_in_progress_; |
| 185 base::TimeTicks eviction_start_time_; | 186 base::TimeTicks eviction_start_time_; |
| 186 | 187 |
| 187 // This stores all the entry_hash of entries that are removed during | 188 // This stores all the entry_hash of entries that are removed during |
| 188 // initialization. | 189 // initialization. |
| 189 base::hash_set<uint64_t> removed_entries_; | 190 base::hash_set<uint64_t> removed_entries_; |
| 190 bool initialized_; | 191 bool initialized_; |
| 191 IndexInitMethod init_method_; | 192 IndexInitMethod init_method_; |
| 192 | 193 |
| 193 scoped_ptr<SimpleIndexFile> index_file_; | 194 std::unique_ptr<SimpleIndexFile> index_file_; |
| 194 | 195 |
| 195 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; | 196 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; |
| 196 | 197 |
| 197 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 198 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 198 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 199 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 199 base::ThreadChecker io_thread_checker_; | 200 base::ThreadChecker io_thread_checker_; |
| 200 | 201 |
| 201 // Timestamp of the last time we wrote the index to disk. | 202 // Timestamp of the last time we wrote the index to disk. |
| 202 // PostponeWritingToDisk() may give up postponing and allow the write if it | 203 // PostponeWritingToDisk() may give up postponing and allow the write if it |
| 203 // has been a while since last time we wrote. | 204 // has been a while since last time we wrote. |
| 204 base::TimeTicks last_write_to_disk_; | 205 base::TimeTicks last_write_to_disk_; |
| 205 | 206 |
| 206 base::OneShotTimer write_to_disk_timer_; | 207 base::OneShotTimer write_to_disk_timer_; |
| 207 base::Closure write_to_disk_cb_; | 208 base::Closure write_to_disk_cb_; |
| 208 | 209 |
| 209 typedef std::list<net::CompletionCallback> CallbackList; | 210 typedef std::list<net::CompletionCallback> CallbackList; |
| 210 CallbackList to_run_when_initialized_; | 211 CallbackList to_run_when_initialized_; |
| 211 | 212 |
| 212 // Set to true when the app is on the background. When the app is in the | 213 // Set to true when the app is on the background. When the app is in the |
| 213 // background we can write the index much more frequently, to insure fresh | 214 // background we can write the index much more frequently, to insure fresh |
| 214 // index on next startup. | 215 // index on next startup. |
| 215 bool app_on_background_; | 216 bool app_on_background_; |
| 216 }; | 217 }; |
| 217 | 218 |
| 218 } // namespace disk_cache | 219 } // namespace disk_cache |
| 219 | 220 |
| 220 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 221 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| OLD | NEW |