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 #include "net/disk_cache/simple/simple_index_file.h" | 5 #include "net/disk_cache/simple/simple_index_file.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 using base::File; | 26 using base::File; |
27 | 27 |
28 namespace disk_cache { | 28 namespace disk_cache { |
29 namespace { | 29 namespace { |
30 | 30 |
31 const int kEntryFilesHashLength = 16; | 31 const int kEntryFilesHashLength = 16; |
32 const int kEntryFilesSuffixLength = 2; | 32 const int kEntryFilesSuffixLength = 2; |
33 | 33 |
34 const uint64 kMaxEntiresInIndex = 100000000; | 34 const uint64 kMaxEntiresInIndex = 100000000; |
35 | 35 |
36 uint32 CalculatePickleCRC(const Pickle& pickle) { | 36 uint32 CalculatePickleCRC(const base::Pickle& pickle) { |
37 return crc32(crc32(0, Z_NULL, 0), | 37 return crc32(crc32(0, Z_NULL, 0), |
38 reinterpret_cast<const Bytef*>(pickle.payload()), | 38 reinterpret_cast<const Bytef*>(pickle.payload()), |
39 pickle.payload_size()); | 39 pickle.payload_size()); |
40 } | 40 } |
41 | 41 |
42 // Used in histograms. Please only add new values at the end. | 42 // Used in histograms. Please only add new values at the end. |
43 enum IndexFileState { | 43 enum IndexFileState { |
44 INDEX_STATE_CORRUPT = 0, | 44 INDEX_STATE_CORRUPT = 0, |
45 INDEX_STATE_STALE = 1, | 45 INDEX_STATE_STALE = 1, |
46 INDEX_STATE_FRESH = 2, | 46 INDEX_STATE_FRESH = 2, |
(...skipping 14 matching lines...) Expand all Loading... |
61 INITIALIZE_METHOD_MAX = 3, | 61 INITIALIZE_METHOD_MAX = 3, |
62 }; | 62 }; |
63 | 63 |
64 void UmaRecordIndexInitMethod(IndexInitMethod method, | 64 void UmaRecordIndexInitMethod(IndexInitMethod method, |
65 net::CacheType cache_type) { | 65 net::CacheType cache_type) { |
66 SIMPLE_CACHE_UMA(ENUMERATION, | 66 SIMPLE_CACHE_UMA(ENUMERATION, |
67 "IndexInitializeMethod", cache_type, | 67 "IndexInitializeMethod", cache_type, |
68 method, INITIALIZE_METHOD_MAX); | 68 method, INITIALIZE_METHOD_MAX); |
69 } | 69 } |
70 | 70 |
71 bool WritePickleFile(Pickle* pickle, const base::FilePath& file_name) { | 71 bool WritePickleFile(base::Pickle* pickle, const base::FilePath& file_name) { |
72 File file( | 72 File file( |
73 file_name, | 73 file_name, |
74 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_SHARE_DELETE); | 74 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_SHARE_DELETE); |
75 if (!file.IsValid()) | 75 if (!file.IsValid()) |
76 return false; | 76 return false; |
77 | 77 |
78 int bytes_written = | 78 int bytes_written = |
79 file.Write(0, static_cast<const char*>(pickle->data()), pickle->size()); | 79 file.Write(0, static_cast<const char*>(pickle->data()), pickle->size()); |
80 if (bytes_written != implicit_cast<int>(pickle->size())) { | 80 if (bytes_written != implicit_cast<int>(pickle->size())) { |
81 simple_util::SimpleCacheDeleteFile(file_name); | 81 simple_util::SimpleCacheDeleteFile(file_name); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 number_of_entries_(0), | 159 number_of_entries_(0), |
160 cache_size_(0) {} | 160 cache_size_(0) {} |
161 | 161 |
162 SimpleIndexFile::IndexMetadata::IndexMetadata( | 162 SimpleIndexFile::IndexMetadata::IndexMetadata( |
163 uint64 number_of_entries, uint64 cache_size) | 163 uint64 number_of_entries, uint64 cache_size) |
164 : magic_number_(kSimpleIndexMagicNumber), | 164 : magic_number_(kSimpleIndexMagicNumber), |
165 version_(kSimpleVersion), | 165 version_(kSimpleVersion), |
166 number_of_entries_(number_of_entries), | 166 number_of_entries_(number_of_entries), |
167 cache_size_(cache_size) {} | 167 cache_size_(cache_size) {} |
168 | 168 |
169 void SimpleIndexFile::IndexMetadata::Serialize(Pickle* pickle) const { | 169 void SimpleIndexFile::IndexMetadata::Serialize(base::Pickle* pickle) const { |
170 DCHECK(pickle); | 170 DCHECK(pickle); |
171 pickle->WriteUInt64(magic_number_); | 171 pickle->WriteUInt64(magic_number_); |
172 pickle->WriteUInt32(version_); | 172 pickle->WriteUInt32(version_); |
173 pickle->WriteUInt64(number_of_entries_); | 173 pickle->WriteUInt64(number_of_entries_); |
174 pickle->WriteUInt64(cache_size_); | 174 pickle->WriteUInt64(cache_size_); |
175 } | 175 } |
176 | 176 |
177 // static | 177 // static |
178 bool SimpleIndexFile::SerializeFinalData(base::Time cache_modified, | 178 bool SimpleIndexFile::SerializeFinalData(base::Time cache_modified, |
179 Pickle* pickle) { | 179 base::Pickle* pickle) { |
180 if (!pickle->WriteInt64(cache_modified.ToInternalValue())) | 180 if (!pickle->WriteInt64(cache_modified.ToInternalValue())) |
181 return false; | 181 return false; |
182 SimpleIndexFile::PickleHeader* header_p = pickle->headerT<PickleHeader>(); | 182 SimpleIndexFile::PickleHeader* header_p = pickle->headerT<PickleHeader>(); |
183 header_p->crc = CalculatePickleCRC(*pickle); | 183 header_p->crc = CalculatePickleCRC(*pickle); |
184 return true; | 184 return true; |
185 } | 185 } |
186 | 186 |
187 bool SimpleIndexFile::IndexMetadata::Deserialize(PickleIterator* it) { | 187 bool SimpleIndexFile::IndexMetadata::Deserialize(base::PickleIterator* it) { |
188 DCHECK(it); | 188 DCHECK(it); |
189 return it->ReadUInt64(&magic_number_) && | 189 return it->ReadUInt64(&magic_number_) && |
190 it->ReadUInt32(&version_) && | 190 it->ReadUInt32(&version_) && |
191 it->ReadUInt64(&number_of_entries_)&& | 191 it->ReadUInt64(&number_of_entries_)&& |
192 it->ReadUInt64(&cache_size_); | 192 it->ReadUInt64(&cache_size_); |
193 } | 193 } |
194 | 194 |
195 void SimpleIndexFile::SyncWriteToDisk(net::CacheType cache_type, | 195 void SimpleIndexFile::SyncWriteToDisk(net::CacheType cache_type, |
196 const base::FilePath& cache_directory, | 196 const base::FilePath& cache_directory, |
197 const base::FilePath& index_filename, | 197 const base::FilePath& index_filename, |
198 const base::FilePath& temp_index_filename, | 198 const base::FilePath& temp_index_filename, |
199 scoped_ptr<Pickle> pickle, | 199 scoped_ptr<base::Pickle> pickle, |
200 const base::TimeTicks& start_time, | 200 const base::TimeTicks& start_time, |
201 bool app_on_background) { | 201 bool app_on_background) { |
202 DCHECK_EQ(index_filename.DirName().value(), | 202 DCHECK_EQ(index_filename.DirName().value(), |
203 temp_index_filename.DirName().value()); | 203 temp_index_filename.DirName().value()); |
204 base::FilePath index_file_directory = temp_index_filename.DirName(); | 204 base::FilePath index_file_directory = temp_index_filename.DirName(); |
205 if (!base::DirectoryExists(index_file_directory) && | 205 if (!base::DirectoryExists(index_file_directory) && |
206 !base::CreateDirectory(index_file_directory)) { | 206 !base::CreateDirectory(index_file_directory)) { |
207 LOG(ERROR) << "Could not create a directory to hold the index file"; | 207 LOG(ERROR) << "Could not create a directory to hold the index file"; |
208 return; | 208 return; |
209 } | 209 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 index_file_, out_result); | 274 index_file_, out_result); |
275 worker_pool_->PostTaskAndReply(FROM_HERE, task, callback); | 275 worker_pool_->PostTaskAndReply(FROM_HERE, task, callback); |
276 } | 276 } |
277 | 277 |
278 void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set, | 278 void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set, |
279 uint64 cache_size, | 279 uint64 cache_size, |
280 const base::TimeTicks& start, | 280 const base::TimeTicks& start, |
281 bool app_on_background, | 281 bool app_on_background, |
282 const base::Closure& callback) { | 282 const base::Closure& callback) { |
283 IndexMetadata index_metadata(entry_set.size(), cache_size); | 283 IndexMetadata index_metadata(entry_set.size(), cache_size); |
284 scoped_ptr<Pickle> pickle = Serialize(index_metadata, entry_set); | 284 scoped_ptr<base::Pickle> pickle = Serialize(index_metadata, entry_set); |
285 base::Closure task = | 285 base::Closure task = |
286 base::Bind(&SimpleIndexFile::SyncWriteToDisk, | 286 base::Bind(&SimpleIndexFile::SyncWriteToDisk, |
287 cache_type_, cache_directory_, index_file_, temp_index_file_, | 287 cache_type_, cache_directory_, index_file_, temp_index_file_, |
288 base::Passed(&pickle), start, app_on_background); | 288 base::Passed(&pickle), start, app_on_background); |
289 if (callback.is_null()) | 289 if (callback.is_null()) |
290 cache_thread_->PostTask(FROM_HERE, task); | 290 cache_thread_->PostTask(FROM_HERE, task); |
291 else | 291 else |
292 cache_thread_->PostTaskAndReply(FROM_HERE, task, callback); | 292 cache_thread_->PostTaskAndReply(FROM_HERE, task, callback); |
293 } | 293 } |
294 | 294 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 reinterpret_cast<const char*>(index_file_map.data()), | 362 reinterpret_cast<const char*>(index_file_map.data()), |
363 index_file_map.length(), | 363 index_file_map.length(), |
364 out_last_cache_seen_by_index, | 364 out_last_cache_seen_by_index, |
365 out_result); | 365 out_result); |
366 | 366 |
367 if (!out_result->did_load) | 367 if (!out_result->did_load) |
368 simple_util::SimpleCacheDeleteFile(index_filename); | 368 simple_util::SimpleCacheDeleteFile(index_filename); |
369 } | 369 } |
370 | 370 |
371 // static | 371 // static |
372 scoped_ptr<Pickle> SimpleIndexFile::Serialize( | 372 scoped_ptr<base::Pickle> SimpleIndexFile::Serialize( |
373 const SimpleIndexFile::IndexMetadata& index_metadata, | 373 const SimpleIndexFile::IndexMetadata& index_metadata, |
374 const SimpleIndex::EntrySet& entries) { | 374 const SimpleIndex::EntrySet& entries) { |
375 scoped_ptr<Pickle> pickle(new Pickle(sizeof(SimpleIndexFile::PickleHeader))); | 375 scoped_ptr<base::Pickle> pickle( |
| 376 new base::Pickle(sizeof(SimpleIndexFile::PickleHeader))); |
376 | 377 |
377 index_metadata.Serialize(pickle.get()); | 378 index_metadata.Serialize(pickle.get()); |
378 for (SimpleIndex::EntrySet::const_iterator it = entries.begin(); | 379 for (SimpleIndex::EntrySet::const_iterator it = entries.begin(); |
379 it != entries.end(); ++it) { | 380 it != entries.end(); ++it) { |
380 pickle->WriteUInt64(it->first); | 381 pickle->WriteUInt64(it->first); |
381 it->second.Serialize(pickle.get()); | 382 it->second.Serialize(pickle.get()); |
382 } | 383 } |
383 return pickle.Pass(); | 384 return pickle.Pass(); |
384 } | 385 } |
385 | 386 |
386 // static | 387 // static |
387 void SimpleIndexFile::Deserialize(const char* data, int data_len, | 388 void SimpleIndexFile::Deserialize(const char* data, int data_len, |
388 base::Time* out_cache_last_modified, | 389 base::Time* out_cache_last_modified, |
389 SimpleIndexLoadResult* out_result) { | 390 SimpleIndexLoadResult* out_result) { |
390 DCHECK(data); | 391 DCHECK(data); |
391 | 392 |
392 out_result->Reset(); | 393 out_result->Reset(); |
393 SimpleIndex::EntrySet* entries = &out_result->entries; | 394 SimpleIndex::EntrySet* entries = &out_result->entries; |
394 | 395 |
395 Pickle pickle(data, data_len); | 396 base::Pickle pickle(data, data_len); |
396 if (!pickle.data()) { | 397 if (!pickle.data()) { |
397 LOG(WARNING) << "Corrupt Simple Index File."; | 398 LOG(WARNING) << "Corrupt Simple Index File."; |
398 return; | 399 return; |
399 } | 400 } |
400 | 401 |
401 PickleIterator pickle_it(pickle); | 402 base::PickleIterator pickle_it(pickle); |
402 SimpleIndexFile::PickleHeader* header_p = | 403 SimpleIndexFile::PickleHeader* header_p = |
403 pickle.headerT<SimpleIndexFile::PickleHeader>(); | 404 pickle.headerT<SimpleIndexFile::PickleHeader>(); |
404 const uint32 crc_read = header_p->crc; | 405 const uint32 crc_read = header_p->crc; |
405 const uint32 crc_calculated = CalculatePickleCRC(pickle); | 406 const uint32 crc_calculated = CalculatePickleCRC(pickle); |
406 | 407 |
407 if (crc_read != crc_calculated) { | 408 if (crc_read != crc_calculated) { |
408 LOG(WARNING) << "Invalid CRC in Simple Index file."; | 409 LOG(WARNING) << "Invalid CRC in Simple Index file."; |
409 return; | 410 return; |
410 } | 411 } |
411 | 412 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 bool SimpleIndexFile::LegacyIsIndexFileStale( | 474 bool SimpleIndexFile::LegacyIsIndexFileStale( |
474 base::Time cache_last_modified, | 475 base::Time cache_last_modified, |
475 const base::FilePath& index_file_path) { | 476 const base::FilePath& index_file_path) { |
476 base::Time index_mtime; | 477 base::Time index_mtime; |
477 if (!simple_util::GetMTime(index_file_path, &index_mtime)) | 478 if (!simple_util::GetMTime(index_file_path, &index_mtime)) |
478 return true; | 479 return true; |
479 return index_mtime < cache_last_modified; | 480 return index_mtime < cache_last_modified; |
480 } | 481 } |
481 | 482 |
482 } // namespace disk_cache | 483 } // namespace disk_cache |
OLD | NEW |