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