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_SYNCHRONOUS_ENTRY_H_ | 5 #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 6 #define NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/platform_file.h" | 16 #include "base/platform_file.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "net/base/cache_type.h" | 18 #include "net/base/cache_type.h" |
| 19 #include "net/base/net_export.h" |
18 #include "net/disk_cache/simple/simple_entry_format.h" | 20 #include "net/disk_cache/simple/simple_entry_format.h" |
19 | 21 |
20 namespace net { | 22 namespace net { |
| 23 class GrowableIOBuffer; |
21 class IOBuffer; | 24 class IOBuffer; |
22 } | 25 } |
23 | 26 |
24 namespace disk_cache { | 27 namespace disk_cache { |
25 | 28 |
26 class SimpleSynchronousEntry; | 29 class SimpleSynchronousEntry; |
27 | 30 |
28 struct SimpleEntryStat { | 31 // This class handles the passing of data about the entry between |
29 SimpleEntryStat(); | 32 // SimpleEntryImplementation and SimpleSynchronousEntry and the computation of |
30 SimpleEntryStat(base::Time last_used_p, | 33 // file offsets based on the data size for all streams. |
31 base::Time last_modified_p, | 34 class NET_EXPORT_PRIVATE SimpleEntryStat { |
32 const int32 data_size_p[]); | 35 public: |
| 36 SimpleEntryStat(base::Time last_used, |
| 37 base::Time last_modified, |
| 38 const int32 data_size[]); |
33 | 39 |
34 base::Time last_used; | 40 int GetOffsetInFile(const std::string& key, |
35 base::Time last_modified; | 41 int offset, |
36 int32 data_size[kSimpleEntryFileCount]; | 42 int stream_index) const; |
| 43 int GetEOFOffsetInFile(const std::string& key, int stream_index) const; |
| 44 int GetLastEOFOffsetInFile(const std::string& key, int file_index) const; |
| 45 int GetFileSize(const std::string& key, int file_index) const; |
| 46 |
| 47 base::Time last_used() const { return last_used_; } |
| 48 base::Time last_modified() const { return last_modified_; } |
| 49 void set_last_used(base::Time last_used) { last_used_ = last_used; } |
| 50 void set_last_modified(base::Time last_modified) { |
| 51 last_modified_ = last_modified; |
| 52 } |
| 53 |
| 54 int32 data_size(int stream_index) const { return data_size_[stream_index]; } |
| 55 void set_data_size(int stream_index, int data_size) { |
| 56 data_size_[stream_index] = data_size; |
| 57 } |
| 58 |
| 59 private: |
| 60 base::Time last_used_; |
| 61 base::Time last_modified_; |
| 62 int32 data_size_[kSimpleEntryStreamCount]; |
37 }; | 63 }; |
38 | 64 |
39 struct SimpleEntryCreationResults { | 65 struct SimpleEntryCreationResults { |
40 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); | 66 explicit SimpleEntryCreationResults(SimpleEntryStat entry_stat); |
41 ~SimpleEntryCreationResults(); | 67 ~SimpleEntryCreationResults(); |
42 | 68 |
43 SimpleSynchronousEntry* sync_entry; | 69 SimpleSynchronousEntry* sync_entry; |
| 70 scoped_refptr<net::GrowableIOBuffer> stream_0_data; |
44 SimpleEntryStat entry_stat; | 71 SimpleEntryStat entry_stat; |
| 72 uint32 stream_0_crc32; |
45 int result; | 73 int result; |
46 }; | 74 }; |
47 | 75 |
48 // Worker thread interface to the very simple cache. This interface is not | 76 // Worker thread interface to the very simple cache. This interface is not |
49 // thread safe, and callers must ensure that it is only ever accessed from | 77 // thread safe, and callers must ensure that it is only ever accessed from |
50 // a single thread between synchronization points. | 78 // a single thread between synchronization points. |
51 class SimpleSynchronousEntry { | 79 class SimpleSynchronousEntry { |
52 public: | 80 public: |
53 struct CRCRecord { | 81 struct CRCRecord { |
54 CRCRecord(); | 82 CRCRecord(); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Like |DoomEntry()| above. Deletes all entries corresponding to the | 123 // Like |DoomEntry()| above. Deletes all entries corresponding to the |
96 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net | 124 // |key_hashes|. Succeeds only when all entries are deleted. Returns a net |
97 // error code. | 125 // error code. |
98 static int DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes, | 126 static int DoomEntrySet(scoped_ptr<std::vector<uint64> > key_hashes, |
99 const base::FilePath& path); | 127 const base::FilePath& path); |
100 | 128 |
101 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. | 129 // N.B. ReadData(), WriteData(), CheckEOFRecord() and Close() may block on IO. |
102 void ReadData(const EntryOperationData& in_entry_op, | 130 void ReadData(const EntryOperationData& in_entry_op, |
103 net::IOBuffer* out_buf, | 131 net::IOBuffer* out_buf, |
104 uint32* out_crc32, | 132 uint32* out_crc32, |
105 base::Time* out_last_used, | 133 SimpleEntryStat* entry_stat, |
106 int* out_result) const; | 134 int* out_result) const; |
107 void WriteData(const EntryOperationData& in_entry_op, | 135 void WriteData(const EntryOperationData& in_entry_op, |
108 net::IOBuffer* in_buf, | 136 net::IOBuffer* in_buf, |
109 SimpleEntryStat* out_entry_stat, | 137 SimpleEntryStat* out_entry_stat, |
110 int* out_result) const; | 138 int* out_result) const; |
111 void CheckEOFRecord(int index, | 139 void CheckEOFRecord(int index, |
112 int data_size, | 140 const SimpleEntryStat& entry_stat, |
113 uint32 expected_crc32, | 141 uint32 expected_crc32, |
114 int* out_result) const; | 142 int* out_result) const; |
115 | 143 |
116 // Close all streams, and add write EOF records to streams indicated by the | 144 // Close all streams, and add write EOF records to streams indicated by the |
117 // CRCRecord entries in |crc32s_to_write|. | 145 // CRCRecord entries in |crc32s_to_write|. |
118 void Close(const SimpleEntryStat& entry_stat, | 146 void Close(const SimpleEntryStat& entry_stat, |
119 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write); | 147 scoped_ptr<std::vector<CRCRecord> > crc32s_to_write, |
| 148 net::GrowableIOBuffer* stream_0_data); |
120 | 149 |
121 const base::FilePath& path() const { return path_; } | 150 const base::FilePath& path() const { return path_; } |
122 std::string key() const { return key_; } | 151 std::string key() const { return key_; } |
123 | 152 |
124 private: | 153 private: |
125 SimpleSynchronousEntry( | 154 SimpleSynchronousEntry( |
126 net::CacheType cache_type, | 155 net::CacheType cache_type, |
127 const base::FilePath& path, | 156 const base::FilePath& path, |
128 const std::string& key, | 157 const std::string& key, |
129 uint64 entry_hash); | 158 uint64 entry_hash); |
130 | 159 |
131 // Like Entry, the SimpleSynchronousEntry self releases when Close() is | 160 // Like Entry, the SimpleSynchronousEntry self releases when Close() is |
132 // called. | 161 // called. |
133 ~SimpleSynchronousEntry(); | 162 ~SimpleSynchronousEntry(); |
134 | 163 |
135 bool OpenOrCreateFiles(bool create, | 164 bool OpenOrCreateFiles(bool create, |
136 bool had_index, | 165 bool had_index, |
137 SimpleEntryStat* out_entry_stat); | 166 SimpleEntryStat* out_entry_stat); |
138 void CloseFiles(); | 167 void CloseFiles(); |
139 | 168 |
140 // Returns a net error, i.e. net::OK on success. |had_index| is passed | 169 // Returns a net error, i.e. net::OK on success. |had_index| is passed |
141 // from the main entry for metrics purposes, and is true if the index was | 170 // from the main entry for metrics purposes, and is true if the index was |
142 // initialized when the open operation began. | 171 // initialized when the open operation began. |
143 int InitializeForOpen(bool had_index, SimpleEntryStat* out_entry_stat); | 172 int InitializeForOpen(bool had_index, |
| 173 SimpleEntryStat* out_entry_stat, |
| 174 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
| 175 uint32* out_stream_0_crc32); |
144 | 176 |
145 // Returns a net error, including net::OK on success and net::FILE_EXISTS | 177 // Returns a net error, including net::OK on success and net::FILE_EXISTS |
146 // when the entry already exists. |had_index| is passed from the main entry | 178 // when the entry already exists. |had_index| is passed from the main entry |
147 // for metrics purposes, and is true if the index was initialized when the | 179 // for metrics purposes, and is true if the index was initialized when the |
148 // create operation began. | 180 // create operation began. |
149 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); | 181 int InitializeForCreate(bool had_index, SimpleEntryStat* out_entry_stat); |
150 | 182 |
| 183 // Allocates and fills a buffer with stream 0 data in |stream_0_data|, then |
| 184 // checks its crc32. |
| 185 int ReadAndValidateStream0( |
| 186 int total_data_size, |
| 187 SimpleEntryStat* out_entry_stat, |
| 188 scoped_refptr<net::GrowableIOBuffer>* stream_0_data, |
| 189 uint32* out_stream_0_crc32) const; |
| 190 |
| 191 int GetEOFRecordData(int index, |
| 192 const SimpleEntryStat& entry_stat, |
| 193 bool* out_has_crc32, |
| 194 uint32* out_crc32, |
| 195 int* out_data_size) const; |
151 void Doom() const; | 196 void Doom() const; |
152 | 197 |
153 static bool DeleteFilesForEntryHash(const base::FilePath& path, | 198 static bool DeleteFilesForEntryHash(const base::FilePath& path, |
154 uint64 entry_hash); | 199 uint64 entry_hash); |
155 | 200 |
156 const net::CacheType cache_type_; | 201 const net::CacheType cache_type_; |
157 const base::FilePath path_; | 202 const base::FilePath path_; |
158 const uint64 entry_hash_; | 203 const uint64 entry_hash_; |
159 std::string key_; | 204 std::string key_; |
160 | 205 |
161 bool have_open_files_; | 206 bool have_open_files_; |
162 bool initialized_; | 207 bool initialized_; |
163 | 208 |
164 base::PlatformFile files_[kSimpleEntryFileCount]; | 209 base::PlatformFile files_[kSimpleEntryFileCount]; |
165 }; | 210 }; |
166 | 211 |
167 } // namespace disk_cache | 212 } // namespace disk_cache |
168 | 213 |
169 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ | 214 #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_SYNCHRONOUS_ENTRY_H_ |
OLD | NEW |