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