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.
5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/files/file_path.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/logging.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/pickle.h"
18 #include "base/port.h"
19 #include "net/base/net_export.h"
20 #include "net/disk_cache/simple/simple_index.h"
23 class SingleThreadTaskRunner
;
27 namespace disk_cache
{
29 const uint64 kSimpleIndexMagicNumber
= GG_UINT64_C(0x656e74657220796f);
31 struct NET_EXPORT_PRIVATE SimpleIndexLoadResult
{
32 SimpleIndexLoadResult();
33 ~SimpleIndexLoadResult();
37 SimpleIndex::EntrySet entries
;
41 // Simple Index File format is a pickle serialized data of IndexMetadata and
42 // EntryMetadata objects. The file format is as follows: one instance of
43 // serialized |IndexMetadata| followed serialized |EntryMetadata| entries
44 // repeated |number_of_entries| amount of times. To know more about the format,
45 // see SimpleIndexFile::Serialize() and SeeSimpleIndexFile::LoadFromDisk()
48 // The non-static methods must run on the IO thread. All the real
49 // work is done in the static methods, which are run on the cache thread
50 // or in worker threads. Synchronization between methods is the
51 // responsibility of the caller.
52 class NET_EXPORT_PRIVATE SimpleIndexFile
{
54 class NET_EXPORT_PRIVATE IndexMetadata
{
57 IndexMetadata(uint64 number_of_entries
, uint64 cache_size
);
59 void Serialize(Pickle
* pickle
) const;
60 bool Deserialize(PickleIterator
* it
);
62 bool CheckIndexMetadata();
64 uint64
GetNumberOfEntries() { return number_of_entries_
; }
67 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest
, Basics
);
68 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest
, Serialize
);
72 uint64 number_of_entries_
;
73 uint64 cache_size_
; // Total cache storage size in bytes.
76 SimpleIndexFile(base::SingleThreadTaskRunner
* cache_thread
,
77 base::TaskRunner
* worker_pool
,
78 const base::FilePath
& cache_directory
);
79 virtual ~SimpleIndexFile();
81 // Get index entries based on current disk context.
82 virtual void LoadIndexEntries(base::Time cache_last_modified
,
83 const base::Closure
& callback
,
84 SimpleIndexLoadResult
* out_result
);
86 // Write the specified set of entries to disk.
87 virtual void WriteToDisk(const SimpleIndex::EntrySet
& entry_set
,
89 const base::TimeTicks
& start
,
90 bool app_on_background
);
92 // Doom the entries specified in |entry_hashes|, calling |reply_callback|
93 // with the result on the current thread when done.
94 virtual void DoomEntrySet(scoped_ptr
<std::vector
<uint64
> > entry_hashes
,
95 const base::Callback
<void(int)>& reply_callback
);
98 friend class WrappedSimpleIndexFile
;
100 // When loading the entries from disk, add this many extra hash buckets to
101 // prevent reallocation on the IO thread when merging in new live entries.
102 static const int kExtraSizeForMerge
= 512;
104 // Synchronous (IO performing) implementation of LoadIndexEntries.
105 static void SyncLoadIndexEntries(base::Time cache_last_modified
,
106 const base::FilePath
& cache_directory
,
107 const base::FilePath
& index_file_path
,
108 SimpleIndexLoadResult
* out_result
);
110 // Load the index file from disk returning an EntrySet. Upon failure, returns
112 static void SyncLoadFromDisk(const base::FilePath
& index_filename
,
113 SimpleIndexLoadResult
* out_result
);
115 // Returns a scoped_ptr for a newly allocated Pickle containing the serialized
116 // data to be written to a file.
117 static scoped_ptr
<Pickle
> Serialize(
118 const SimpleIndexFile::IndexMetadata
& index_metadata
,
119 const SimpleIndex::EntrySet
& entries
);
121 // Given the contents of an index file |data| of length |data_len|, returns
122 // the corresponding EntrySet. Returns NULL on error.
123 static void Deserialize(const char* data
, int data_len
,
124 SimpleIndexLoadResult
* out_result
);
126 // Scan the index directory for entries, returning an EntrySet of all entries
128 static void SyncRestoreFromDisk(const base::FilePath
& cache_directory
,
129 const base::FilePath
& index_file_path
,
130 SimpleIndexLoadResult
* out_result
);
132 // Determines if an index file is stale relative to the time of last
133 // modification of the cache directory.
134 static bool IsIndexFileStale(base::Time cache_last_modified
,
135 const base::FilePath
& index_file_path
);
137 struct PickleHeader
: public Pickle::Header
{
141 const scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
142 const scoped_refptr
<base::TaskRunner
> worker_pool_
;
143 const base::FilePath cache_directory_
;
144 const base::FilePath index_file_
;
145 const base::FilePath temp_index_file_
;
147 static const char kIndexFileName
[];
148 static const char kTempIndexFileName
[];
150 DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile
);
154 } // namespace disk_cache
156 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_