| 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/hash_tables.h" | 14 #include "base/hash_tables.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/timer.h" |
| 20 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 21 | 22 |
| 22 class Pickle; | 23 class Pickle; |
| 23 class PickleIterator; | 24 class PickleIterator; |
| 24 | 25 |
| 25 namespace base { | 26 namespace base { |
| 26 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace disk_cache { | 30 namespace disk_cache { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 | 98 |
| 98 // TODO(felipeg): This way we are storing the hash_key twice, as the | 99 // TODO(felipeg): This way we are storing the hash_key twice, as the |
| 99 // hash_map::key and as a member of EntryMetadata. We could save space if we | 100 // hash_map::key and as a member of EntryMetadata. We could save space if we |
| 100 // use a hash_set. | 101 // use a hash_set. |
| 101 typedef base::hash_map<uint64, EntryMetadata> EntrySet; | 102 typedef base::hash_map<uint64, EntryMetadata> EntrySet; |
| 102 | 103 |
| 103 static void InsertInEntrySet(const EntryMetadata& entry_metadata, | 104 static void InsertInEntrySet(const EntryMetadata& entry_metadata, |
| 104 EntrySet* entry_set); | 105 EntrySet* entry_set); |
| 105 | 106 |
| 106 private: | 107 private: |
| 107 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback; | 108 typedef base::Callback<void(scoped_ptr<EntrySet>, bool force_index_flush)> |
| 109 IndexCompletionCallback; |
| 110 |
| 111 void PostponeWritingToDisk(); |
| 108 | 112 |
| 109 static void LoadFromDisk( | 113 static void LoadFromDisk( |
| 110 const base::FilePath& index_filename, | 114 const base::FilePath& index_filename, |
| 111 base::SingleThreadTaskRunner* io_thread, | 115 base::SingleThreadTaskRunner* io_thread, |
| 112 const IndexCompletionCallback& completion_callback); | 116 const IndexCompletionCallback& completion_callback); |
| 113 | 117 |
| 114 // Enumerates all entries' files on disk and regenerates the index. | 118 // Enumerates all entries' files on disk and regenerates the index. |
| 115 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk( | 119 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk( |
| 116 const base::FilePath& index_filename); | 120 const base::FilePath& index_filename); |
| 117 | 121 |
| 118 static void WriteToDiskInternal(const base::FilePath& index_filename, | 122 static void WriteToDiskInternal(const base::FilePath& index_filename, |
| 119 scoped_ptr<Pickle> pickle); | 123 scoped_ptr<Pickle> pickle); |
| 120 | 124 |
| 121 // Must run on IO Thread. | 125 // Must run on IO Thread. |
| 122 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); | 126 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, |
| 127 bool force_index_flush); |
| 123 | 128 |
| 124 EntrySet entries_set_; | 129 EntrySet entries_set_; |
| 125 uint64 cache_size_; // Total cache storage size in bytes. | 130 uint64 cache_size_; // Total cache storage size in bytes. |
| 126 | 131 |
| 127 // This stores all the hash_key of entries that are removed during | 132 // This stores all the hash_key of entries that are removed during |
| 128 // initialization. | 133 // initialization. |
| 129 base::hash_set<uint64> removed_entries_; | 134 base::hash_set<uint64> removed_entries_; |
| 130 bool initialized_; | 135 bool initialized_; |
| 131 | 136 |
| 132 base::FilePath index_filename_; | 137 base::FilePath index_filename_; |
| 133 | 138 |
| 134 scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; | 139 scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; |
| 135 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; | 140 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; |
| 136 | 141 |
| 137 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 142 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 138 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 143 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 139 base::ThreadChecker io_thread_checker_; | 144 base::ThreadChecker io_thread_checker_; |
| 145 |
| 146 // Timestamp of the last time we wrote the index to disk. |
| 147 // PostponeWritingToDisk() may give up postponing and allow the write if it |
| 148 // has been a while since last time we wrote. |
| 149 base::Time last_write_to_disk_; |
| 150 base::OneShotTimer<SimpleIndex> write_to_disk_timer_; |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 } // namespace disk_cache | 153 } // namespace disk_cache |
| 143 | 154 |
| 144 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 155 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| OLD | NEW |