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 // Simple Index File format is a pickle serialized data of IndexMetadata and
32 // EntryMetadata objects. The file format is as follows: one instance of
33 // serialized |IndexMetadata| followed serialized |EntryMetadata| entries
34 // repeated |number_of_entries| amount of times. To know more about the format,
35 // see SimpleIndexFile::Serialize() and SeeSimpleIndexFile::LoadFromDisk()
38 // The non-static methods must run on the IO thread. All the real
39 // work is done in the static methods, which are run on the cache thread
40 // or in worker threads. Synchronization between methods is the
41 // responsibility of the caller.
42 class NET_EXPORT_PRIVATE SimpleIndexFile
{
44 class NET_EXPORT_PRIVATE IndexMetadata
{
47 IndexMetadata(uint64 number_of_entries
, uint64 cache_size
);
49 void Serialize(Pickle
* pickle
) const;
50 bool Deserialize(PickleIterator
* it
);
52 bool CheckIndexMetadata();
54 uint64
GetNumberOfEntries() { return number_of_entries_
; }
57 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest
, Basics
);
58 FRIEND_TEST_ALL_PREFIXES(IndexMetadataTest
, Serialize
);
62 uint64 number_of_entries_
;
63 uint64 cache_size_
; // Total cache storage size in bytes.
66 typedef base::Callback
<void(
67 scoped_ptr
<SimpleIndex::EntrySet
>, bool force_index_flush
)>
68 IndexCompletionCallback
;
70 SimpleIndexFile(base::SingleThreadTaskRunner
* cache_thread
,
71 base::TaskRunner
* worker_pool
,
72 const base::FilePath
& index_file_directory
);
73 virtual ~SimpleIndexFile();
75 // Get index entries based on current disk context.
76 virtual void LoadIndexEntries(
77 scoped_refptr
<base::SingleThreadTaskRunner
> response_thread
,
78 const SimpleIndexFile::IndexCompletionCallback
& completion_callback
);
80 // Write the specified set of entries to disk.
81 virtual void WriteToDisk(const SimpleIndex::EntrySet
& entry_set
,
83 const base::TimeTicks
& start
,
84 bool app_on_background
);
86 // Doom the entries specified in |entry_hashes|, calling |reply_callback|
87 // with the result on the current thread when done.
88 virtual void DoomEntrySet(scoped_ptr
<std::vector
<uint64
> > entry_hashes
,
89 const base::Callback
<void(int)>& reply_callback
);
92 friend class WrappedSimpleIndexFile
;
94 // Synchronous (IO performing) implementation of LoadIndexEntries.
95 static void SyncLoadIndexEntries(
96 const base::FilePath
& index_file_path
,
97 scoped_refptr
<base::SingleThreadTaskRunner
> response_thread
,
98 const SimpleIndexFile::IndexCompletionCallback
& completion_callback
);
100 // Load the index file from disk returning an EntrySet. Upon failure, returns
102 static scoped_ptr
<SimpleIndex::EntrySet
> SyncLoadFromDisk(
103 const base::FilePath
& index_filename
);
105 // Returns a scoped_ptr for a newly allocated Pickle containing the serialized
106 // data to be written to a file.
107 static scoped_ptr
<Pickle
> Serialize(
108 const SimpleIndexFile::IndexMetadata
& index_metadata
,
109 const SimpleIndex::EntrySet
& entries
);
111 // Given the contents of an index file |data| of length |data_len|, returns
112 // the corresponding EntrySet. Returns NULL on error.
113 static scoped_ptr
<SimpleIndex::EntrySet
> Deserialize(const char* data
,
116 // Scan the index directory for entries, returning an EntrySet of all entries
118 static scoped_ptr
<SimpleIndex::EntrySet
> SyncRestoreFromDisk(
119 const base::FilePath
& index_file_path
);
121 // Determines if an index file is stale relative to the cache directory.
122 static bool IsIndexFileStale(const base::FilePath
& index_filename
);
124 struct PickleHeader
: public Pickle::Header
{
128 const scoped_refptr
<base::SingleThreadTaskRunner
> cache_thread_
;
129 const scoped_refptr
<base::TaskRunner
> worker_pool_
;
130 const base::FilePath index_file_path_
;
132 static const char kIndexFileName
[];
134 DISALLOW_COPY_AND_ASSIGN(SimpleIndexFile
);
138 } // namespace disk_cache
140 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_INDEX_FILE_H_