| 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_FILE_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/pickle.h" | 19 #include "base/pickle.h" |
| 19 #include "net/base/cache_type.h" | 20 #include "net/base/cache_type.h" |
| 20 #include "net/base/net_export.h" | 21 #include "net/base/net_export.h" |
| 21 #include "net/disk_cache/simple/simple_index.h" | 22 #include "net/disk_cache/simple/simple_index.h" |
| 22 | 23 |
| 23 namespace base { | 24 namespace base { |
| 24 class SingleThreadTaskRunner; | 25 class SingleThreadTaskRunner; |
| 25 class TaskRunner; | 26 class TaskRunner; |
| 26 } | 27 } |
| 27 | 28 |
| 28 namespace disk_cache { | 29 namespace disk_cache { |
| 29 | 30 |
| 30 const uint64 kSimpleIndexMagicNumber = UINT64_C(0x656e74657220796f); | 31 const uint64_t kSimpleIndexMagicNumber = UINT64_C(0x656e74657220796f); |
| 31 | 32 |
| 32 struct NET_EXPORT_PRIVATE SimpleIndexLoadResult { | 33 struct NET_EXPORT_PRIVATE SimpleIndexLoadResult { |
| 33 SimpleIndexLoadResult(); | 34 SimpleIndexLoadResult(); |
| 34 ~SimpleIndexLoadResult(); | 35 ~SimpleIndexLoadResult(); |
| 35 void Reset(); | 36 void Reset(); |
| 36 | 37 |
| 37 bool did_load; | 38 bool did_load; |
| 38 SimpleIndex::EntrySet entries; | 39 SimpleIndex::EntrySet entries; |
| 39 bool flush_required; | 40 bool flush_required; |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 // Simple Index File format is a pickle serialized data of IndexMetadata and | 43 // Simple Index File format is a pickle serialized data of IndexMetadata and |
| 43 // EntryMetadata objects. The file format is as follows: one instance of | 44 // EntryMetadata objects. The file format is as follows: one instance of |
| 44 // serialized |IndexMetadata| followed serialized |EntryMetadata| entries | 45 // serialized |IndexMetadata| followed serialized |EntryMetadata| entries |
| 45 // repeated |number_of_entries| amount of times. To know more about the format, | 46 // repeated |number_of_entries| amount of times. To know more about the format, |
| 46 // see SimpleIndexFile::Serialize() and SeeSimpleIndexFile::LoadFromDisk() | 47 // see SimpleIndexFile::Serialize() and SeeSimpleIndexFile::LoadFromDisk() |
| 47 // methods. | 48 // methods. |
| 48 // | 49 // |
| 49 // The non-static methods must run on the IO thread. All the real | 50 // The non-static methods must run on the IO thread. All the real |
| 50 // work is done in the static methods, which are run on the cache thread | 51 // work is done in the static methods, which are run on the cache thread |
| 51 // or in worker threads. Synchronization between methods is the | 52 // or in worker threads. Synchronization between methods is the |
| 52 // responsibility of the caller. | 53 // responsibility of the caller. |
| 53 class NET_EXPORT_PRIVATE SimpleIndexFile { | 54 class NET_EXPORT_PRIVATE SimpleIndexFile { |
| 54 public: | 55 public: |
| 55 class NET_EXPORT_PRIVATE IndexMetadata { | 56 class NET_EXPORT_PRIVATE IndexMetadata { |
| 56 public: | 57 public: |
| 57 IndexMetadata(); | 58 IndexMetadata(); |
| 58 IndexMetadata(uint64 number_of_entries, uint64 cache_size); | 59 IndexMetadata(uint64_t number_of_entries, uint64_t cache_size); |
| 59 | 60 |
| 60 void Serialize(base::Pickle* pickle) const; | 61 void Serialize(base::Pickle* pickle) const; |
| 61 bool Deserialize(base::PickleIterator* it); | 62 bool Deserialize(base::PickleIterator* it); |
| 62 | 63 |
| 63 bool CheckIndexMetadata(); | 64 bool CheckIndexMetadata(); |
| 64 | 65 |
| 65 uint64 GetNumberOfEntries() { return number_of_entries_; } | 66 uint64_t GetNumberOfEntries() { return number_of_entries_; } |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Basics); | 69 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Basics); |
| 69 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Serialize); | 70 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Serialize); |
| 70 | 71 |
| 71 uint64 magic_number_; | 72 uint64_t magic_number_; |
| 72 uint32 version_; | 73 uint32_t version_; |
| 73 uint64 number_of_entries_; | 74 uint64_t number_of_entries_; |
| 74 uint64 cache_size_; // Total cache storage size in bytes. | 75 uint64_t cache_size_; // Total cache storage size in bytes. |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 SimpleIndexFile( | 78 SimpleIndexFile( |
| 78 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, | 79 const scoped_refptr<base::SingleThreadTaskRunner>& cache_thread, |
| 79 const scoped_refptr<base::TaskRunner>& worker_pool, | 80 const scoped_refptr<base::TaskRunner>& worker_pool, |
| 80 net::CacheType cache_type, | 81 net::CacheType cache_type, |
| 81 const base::FilePath& cache_directory); | 82 const base::FilePath& cache_directory); |
| 82 virtual ~SimpleIndexFile(); | 83 virtual ~SimpleIndexFile(); |
| 83 | 84 |
| 84 // Get index entries based on current disk context. | 85 // Get index entries based on current disk context. |
| 85 virtual void LoadIndexEntries(base::Time cache_last_modified, | 86 virtual void LoadIndexEntries(base::Time cache_last_modified, |
| 86 const base::Closure& callback, | 87 const base::Closure& callback, |
| 87 SimpleIndexLoadResult* out_result); | 88 SimpleIndexLoadResult* out_result); |
| 88 | 89 |
| 89 // Write the specified set of entries to disk. | 90 // Write the specified set of entries to disk. |
| 90 virtual void WriteToDisk(const SimpleIndex::EntrySet& entry_set, | 91 virtual void WriteToDisk(const SimpleIndex::EntrySet& entry_set, |
| 91 uint64 cache_size, | 92 uint64_t cache_size, |
| 92 const base::TimeTicks& start, | 93 const base::TimeTicks& start, |
| 93 bool app_on_background, | 94 bool app_on_background, |
| 94 const base::Closure& callback); | 95 const base::Closure& callback); |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 friend class WrappedSimpleIndexFile; | 98 friend class WrappedSimpleIndexFile; |
| 98 | 99 |
| 99 // Used for cache directory traversal. | 100 // Used for cache directory traversal. |
| 100 typedef base::Callback<void (const base::FilePath&)> EntryFileCallback; | 101 typedef base::Callback<void (const base::FilePath&)> EntryFileCallback; |
| 101 | 102 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 SimpleIndexLoadResult* out_result); | 163 SimpleIndexLoadResult* out_result); |
| 163 | 164 |
| 164 // Determines if an index file is stale relative to the time of last | 165 // Determines if an index file is stale relative to the time of last |
| 165 // modification of the cache directory. Obsolete, used only for a histogram to | 166 // modification of the cache directory. Obsolete, used only for a histogram to |
| 166 // compare with the new method. | 167 // compare with the new method. |
| 167 // TODO(pasko): remove this method after getting enough data. | 168 // TODO(pasko): remove this method after getting enough data. |
| 168 static bool LegacyIsIndexFileStale(base::Time cache_last_modified, | 169 static bool LegacyIsIndexFileStale(base::Time cache_last_modified, |
| 169 const base::FilePath& index_file_path); | 170 const base::FilePath& index_file_path); |
| 170 | 171 |
| 171 struct PickleHeader : public base::Pickle::Header { | 172 struct PickleHeader : public base::Pickle::Header { |
| 172 uint32 crc; | 173 uint32_t crc; |
| 173 }; | 174 }; |
| 174 | 175 |
| 175 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; | 176 const scoped_refptr<base::SingleThreadTaskRunner> cache_thread_; |
| 176 const scoped_refptr<base::TaskRunner> worker_pool_; | 177 const scoped_refptr<base::TaskRunner> worker_pool_; |
| 177 const net::CacheType cache_type_; | 178 const net::CacheType cache_type_; |
| 178 const base::FilePath cache_directory_; | 179 const base::FilePath cache_directory_; |
| 179 const base::FilePath index_file_; | 180 const base::FilePath index_file_; |
| 180 const base::FilePath temp_index_file_; | 181 const base::FilePath temp_index_file_; |
| 181 | 182 |
| 182 static const char kIndexDirectory[]; | 183 static const char kIndexDirectory[]; |
| 183 static const char kIndexFileName[]; | 184 static const char kIndexFileName[]; |
| 184 static const char kTempIndexFileName[]; | 185 static const char kTempIndexFileName[]; |
| 185 | 186 |
| 186 DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile); | 187 DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile); |
| 187 }; | 188 }; |
| 188 | 189 |
| 189 | 190 |
| 190 } // namespace disk_cache | 191 } // namespace disk_cache |
| 191 | 192 |
| 192 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ | 193 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ |
| OLD | NEW |