| 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/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 18 #include "base/time.h" | 19 #include "base/time.h" |
| 19 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
| 20 | 21 |
| 21 class Pickle; | 22 class Pickle; |
| 22 class PickleIterator; | 23 class PickleIterator; |
| 23 | 24 |
| 24 namespace base { | 25 namespace base { |
| 25 class TaskRunner; | 26 class SingleThreadTaskRunner; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace disk_cache { | 29 namespace disk_cache { |
| 29 | 30 |
| 30 class NET_EXPORT_PRIVATE EntryMetadata { | 31 class NET_EXPORT_PRIVATE EntryMetadata { |
| 31 public: | 32 public: |
| 32 EntryMetadata(); | 33 EntryMetadata(); |
| 33 EntryMetadata(uint64 hash_key, | 34 EntryMetadata(uint64 hash_key, |
| 34 base::Time last_used_time, | 35 base::Time last_used_time, |
| 35 uint64 entry_size); | 36 uint64 entry_size); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 60 // If you want to make calculations/comparisons, you should use the | 61 // If you want to make calculations/comparisons, you should use the |
| 61 // base::Time() class. Use the GetLastUsedTime() method above. | 62 // base::Time() class. Use the GetLastUsedTime() method above. |
| 62 // TODO(felipeg): Use Time() here. | 63 // TODO(felipeg): Use Time() here. |
| 63 int64 last_used_time_; | 64 int64 last_used_time_; |
| 64 | 65 |
| 65 uint64 entry_size_; // Storage size in bytes. | 66 uint64 entry_size_; // Storage size in bytes. |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 // This class is not Thread-safe. | 69 // This class is not Thread-safe. |
| 69 class NET_EXPORT_PRIVATE SimpleIndex | 70 class NET_EXPORT_PRIVATE SimpleIndex |
| 70 : public base::RefCountedThreadSafe<SimpleIndex> { | 71 : public base::SupportsWeakPtr<SimpleIndex> { |
| 71 friend class base::RefCountedThreadSafe<SimpleIndex>; | |
| 72 public: | 72 public: |
| 73 SimpleIndex( | 73 SimpleIndex( |
| 74 const scoped_refptr<base::TaskRunner>& cache_thread, | 74 base::SingleThreadTaskRunner* cache_thread, |
| 75 const scoped_refptr<base::TaskRunner>& io_thread, | 75 base::SingleThreadTaskRunner* io_thread, |
| 76 const base::FilePath& path); | 76 const base::FilePath& path); |
| 77 | 77 |
| 78 virtual ~SimpleIndex(); |
| 79 |
| 78 void Initialize(); | 80 void Initialize(); |
| 79 | 81 |
| 80 void Insert(const std::string& key); | 82 void Insert(const std::string& key); |
| 81 void Remove(const std::string& key); | 83 void Remove(const std::string& key); |
| 82 | 84 |
| 83 bool Has(const std::string& key) const; | 85 bool Has(const std::string& key) const; |
| 84 | 86 |
| 85 // Update the last used time of the entry with the given key and return true | 87 // Update the last used time of the entry with the given key and return true |
| 86 // iff the entry exist in the index. | 88 // iff the entry exist in the index. |
| 87 bool UseIfExists(const std::string& key); | 89 bool UseIfExists(const std::string& key); |
| 88 | 90 |
| 89 void WriteToDisk(); | 91 void WriteToDisk(); |
| 90 | 92 |
| 91 // Update the size (in bytes) of an entry, in the metadata stored in the | 93 // Update the size (in bytes) of an entry, in the metadata stored in the |
| 92 // index. This should be the total disk-file size including all streams of the | 94 // index. This should be the total disk-file size including all streams of the |
| 93 // entry. | 95 // entry. |
| 94 bool UpdateEntrySize(const std::string& key, uint64 entry_size); | 96 bool UpdateEntrySize(const std::string& key, uint64 entry_size); |
| 95 | 97 |
| 96 // TODO(felipeg): This way we are storing the hash_key twice, as the | 98 // TODO(felipeg): This way we are storing the hash_key twice, as the |
| 97 // hash_map::key and as a member of EntryMetadata. We could save space if we | 99 // hash_map::key and as a member of EntryMetadata. We could save space if we |
| 98 // use a hash_set. | 100 // use a hash_set. |
| 99 typedef base::hash_map<uint64, EntryMetadata> EntrySet; | 101 typedef base::hash_map<uint64, EntryMetadata> EntrySet; |
| 100 | 102 |
| 101 static void InsertInEntrySet(const EntryMetadata& entry_metadata, | 103 static void InsertInEntrySet(const EntryMetadata& entry_metadata, |
| 102 EntrySet* entry_set); | 104 EntrySet* entry_set); |
| 103 | 105 |
| 104 private: | 106 private: |
| 105 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback; | 107 typedef base::Callback<void(scoped_ptr<EntrySet>)> IndexCompletionCallback; |
| 106 | 108 |
| 107 virtual ~SimpleIndex(); | |
| 108 | |
| 109 static void LoadFromDisk( | 109 static void LoadFromDisk( |
| 110 const base::FilePath& index_filename, | 110 const base::FilePath& index_filename, |
| 111 const scoped_refptr<base::TaskRunner>& io_thread, | 111 base::SingleThreadTaskRunner* io_thread, |
| 112 const IndexCompletionCallback& completion_callback); | 112 const IndexCompletionCallback& completion_callback); |
| 113 | 113 |
| 114 // Enumerates all entries' files on disk and regenerates the index. | 114 // Enumerates all entries' files on disk and regenerates the index. |
| 115 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk( | 115 static scoped_ptr<SimpleIndex::EntrySet> RestoreFromDisk( |
| 116 const base::FilePath& index_filename); | 116 const base::FilePath& index_filename); |
| 117 | 117 |
| 118 static void WriteToDiskInternal(const base::FilePath& index_filename, | 118 static void WriteToDiskInternal(const base::FilePath& index_filename, |
| 119 scoped_ptr<Pickle> pickle); | 119 scoped_ptr<Pickle> pickle); |
| 120 | 120 |
| 121 // Must run on IO Thread. | 121 // Must run on IO Thread. |
| 122 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); | 122 void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries); |
| 123 | 123 |
| 124 EntrySet entries_set_; | 124 EntrySet entries_set_; |
| 125 uint64 cache_size_; // Total cache storage size in bytes. | 125 uint64 cache_size_; // Total cache storage size in bytes. |
| 126 | 126 |
| 127 // This stores all the hash_key of entries that are removed during | 127 // This stores all the hash_key of entries that are removed during |
| 128 // initialization. | 128 // initialization. |
| 129 base::hash_set<uint64> removed_entries_; | 129 base::hash_set<uint64> removed_entries_; |
| 130 bool initialized_; | 130 bool initialized_; |
| 131 | 131 |
| 132 base::FilePath index_filename_; | 132 base::FilePath index_filename_; |
| 133 | 133 |
| 134 scoped_refptr<base::TaskRunner> cache_thread_; | 134 scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; |
| 135 scoped_refptr<base::TaskRunner> io_thread_; | 135 scoped_refptr<base::SingleThreadTaskRunner> io_thread_; |
| 136 | 136 |
| 137 // All nonstatic SimpleEntryImpl methods should always be called on the IO | 137 // All nonstatic SimpleEntryImpl methods should always be called on the IO |
| 138 // thread, in all cases. |io_thread_checker_| documents and enforces this. | 138 // thread, in all cases. |io_thread_checker_| documents and enforces this. |
| 139 base::ThreadChecker io_thread_checker_; | 139 base::ThreadChecker io_thread_checker_; |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 } // namespace disk_cache | 142 } // namespace disk_cache |
| 143 | 143 |
| 144 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ | 144 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_H_ |
| OLD | NEW |