Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ | |
| 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/hash_tables.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/pickle.h" | |
| 17 #include "base/port.h" | |
| 18 #include "net/disk_cache/simple/simple_index.h" | |
| 19 | |
| 20 namespace base { | |
| 21 class TaskRunner; | |
| 22 } | |
| 23 | |
| 24 namespace disk_cache { | |
| 25 | |
| 26 const uint64 kSimpleIndexMagicNumber = GG_UINT64_C(0x656e74657220796f); | |
| 27 | |
| 28 // Simple Index File format is a pickle serialized data of IndexMetadata and | |
| 29 // EntryMetadata objects. The file format is as follows: one instance of | |
| 30 // serialized |IndexMetadata| followed serialized |EntryMetadata| entries | |
| 31 // repeated |number_of_entries| amount of times. To know more about the format, | |
| 32 // see SimpleIndexFile::Serialize() and SeeSimpleIndexFile::LoadFromDisk() | |
| 33 // methods. | |
| 34 class SimpleIndexFile { | |
| 35 public: | |
| 36 class IndexMetadata { | |
| 37 public: | |
| 38 IndexMetadata(); | |
| 39 IndexMetadata(uint64 number_of_entries, uint64 cache_size); | |
| 40 | |
| 41 void Serialize(Pickle* pickle) const; | |
| 42 bool Deserialize(PickleIterator* it); | |
| 43 | |
| 44 bool CheckIndexMetadata(); | |
| 45 | |
| 46 uint64 GetNumberOfEntries() { return number_of_entries_; } | |
| 47 | |
| 48 private: | |
| 49 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Basics); | |
| 50 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest, Serialize); | |
| 51 | |
| 52 uint64 magic_number_; | |
| 53 uint32 version_; | |
| 54 uint64 number_of_entries_; | |
| 55 uint64 cache_size_; // Total cache storage size in bytes. | |
| 56 }; | |
| 57 | |
| 58 // Load the index file from disk, deserializing it and returning the | |
| 59 // corresponding EntrySet in a scoped_ptr<>, if successful. | |
| 60 // Uppon failure, the scoped_ptr<> will contain NULL. | |
| 61 static scoped_ptr<EntrySet> LoadFromDisk( | |
| 62 const base::FilePath& index_filename); | |
| 63 | |
| 64 // Returns a scoped_ptr for a newly allocated Pickle containing the serialized | |
| 65 // data to be written to a file. | |
| 66 static scoped_ptr<Pickle> Serialize( | |
| 67 const SimpleIndexFile::IndexMetadata& index_metadata, | |
| 68 const EntrySet& entries); | |
| 69 | |
| 70 // Write the serialized data from |pickle| into the index file. | |
| 71 static void WriteToDisk(const base::FilePath& index_filename, | |
| 72 const Pickle& pickle); | |
| 73 | |
| 74 private: | |
| 75 FRIEND_TEST_ALL_PREFIXES(SimpleIndexFileTest, Serialize); | |
|
gavinp
2013/04/15 19:27:46
Blank line here.
felipeg
2013/04/15 20:05:04
Done.
| |
| 76 // Deserialize() is separate from LoadFromDisk() for easier testing. | |
| 77 static scoped_ptr<EntrySet> Deserialize(const char* data, | |
| 78 int data_len); | |
| 79 | |
| 80 struct PickleHeader : public Pickle::Header { | |
| 81 uint32 crc; | |
| 82 }; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile); | |
| 85 }; | |
| 86 | |
| 87 | |
| 88 } // namespace disk_cache | |
| 89 | |
| 90 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_ | |
| OLD | NEW |