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_SYNCHRONOUS_ENTRY_H_
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/platform_file.h"
16 #include "base/time/time.h"
17 #include "net/disk_cache/simple/simple_entry_format.h"
23 namespace disk_cache
{
25 class SimpleSynchronousEntry
;
27 struct SimpleEntryStat
{
29 SimpleEntryStat(base::Time last_used_p
,
30 base::Time last_modified_p
,
31 const int32 data_size_p
[]);
34 base::Time last_modified
;
35 int32 data_size
[kSimpleEntryFileCount
];
38 struct SimpleEntryCreationResults
{
39 SimpleEntryCreationResults(SimpleEntryStat entry_stat
);
40 ~SimpleEntryCreationResults();
42 SimpleSynchronousEntry
* sync_entry
;
43 SimpleEntryStat entry_stat
;
47 // Worker thread interface to the very simple cache. This interface is not
48 // thread safe, and callers must ensure that it is only ever accessed from
49 // a single thread between synchronization points.
50 class SimpleSynchronousEntry
{
54 CRCRecord(int index_p
, bool has_crc32_p
, uint32 data_crc32_p
);
61 struct EntryOperationData
{
62 EntryOperationData(int index_p
, int offset_p
, int buf_len_p
);
63 EntryOperationData(int index_p
,
74 static void OpenEntry(const base::FilePath
& path
,
77 SimpleEntryCreationResults
* out_results
);
79 static void CreateEntry(const base::FilePath
& path
,
80 const std::string
& key
,
83 SimpleEntryCreationResults
* out_results
);
85 // Deletes an entry without first Opening it. Does not check if there is
86 // already an Entry object in memory holding the open files. Be careful! This
87 // is meant to be used by the Backend::DoomEntry() call. |callback| will be
88 // run by |callback_runner|.
89 static void DoomEntry(const base::FilePath
& path
,
90 const std::string
& key
,
94 // Like |DoomEntry()| above. Deletes all entries corresponding to the
95 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net
97 static int DoomEntrySet(scoped_ptr
<std::vector
<uint64
> > key_hashes
,
98 const base::FilePath
& path
);
100 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO.
101 void ReadData(const EntryOperationData
& in_entry_op
,
102 net::IOBuffer
* out_buf
,
104 base::Time
* out_last_used
,
105 int* out_result
) const;
106 void WriteData(const EntryOperationData
& in_entry_op
,
107 net::IOBuffer
* in_buf
,
108 SimpleEntryStat
* out_entry_stat
,
109 int* out_result
) const;
110 void CheckEOFRecord(int index
,
112 uint32 expected_crc32
,
113 int* out_result
) const;
115 // Close all streams, and add write EOF records to streams indicated by the
116 // CRCRecord entries in |crc32s_to_write|.
117 void Close(const SimpleEntryStat
& entry_stat
,
118 scoped_ptr
<std::vector
<CRCRecord
> > crc32s_to_write
);
120 const base::FilePath
& path() const { return path_
; }
121 std::string
key() const { return key_
; }
124 SimpleSynchronousEntry(
125 const base::FilePath
& path
,
126 const std::string
& key
,
129 // Like Entry, the SimpleSynchronousEntry self releases when Close() is
131 ~SimpleSynchronousEntry();
133 bool OpenOrCreateFiles(bool create
,
135 SimpleEntryStat
* out_entry_stat
);
138 // Returns a net error, i.e. net::OK on success. |had_index| is passed
139 // from the main entry for metrics purposes, and is true if the index was
140 // initialized when the open operation began.
141 int InitializeForOpen(bool had_index
, SimpleEntryStat
* out_entry_stat
);
143 // Returns a net error, including net::OK on success and net::FILE_EXISTS
144 // when the entry already exists. |had_index| is passed from the main entry
145 // for metrics purposes, and is true if the index was initialized when the
146 // create operation began.
147 int InitializeForCreate(bool had_index
, SimpleEntryStat
* out_entry_stat
);
151 static bool DeleteFilesForEntryHash(const base::FilePath
& path
,
154 const base::FilePath path_
;
155 const uint64 entry_hash_
;
158 bool have_open_files_
;
161 base::PlatformFile files_
[kSimpleEntryFileCount
];
164 } // namespace disk_cache
166 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_