| 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_synchronous_entry.h" | 5 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/hash.h" | 12 #include "base/hash.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 15 #include "base/sha1.h" | 15 #include "base/sha1.h" |
| 16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/disk_cache/simple/simple_disk_format.h" | |
| 21 | 20 |
| 22 using base::ClosePlatformFile; | 21 using base::ClosePlatformFile; |
| 23 using base::GetPlatformFileInfo; | 22 using base::GetPlatformFileInfo; |
| 24 using base::PlatformFileError; | 23 using base::PlatformFileError; |
| 25 using base::PlatformFileInfo; | 24 using base::PlatformFileInfo; |
| 26 using base::PLATFORM_FILE_CREATE_ALWAYS; | 25 using base::PLATFORM_FILE_CREATE_ALWAYS; |
| 27 using base::PLATFORM_FILE_OK; | 26 using base::PLATFORM_FILE_OK; |
| 28 using base::PLATFORM_FILE_OPEN; | 27 using base::PLATFORM_FILE_OPEN; |
| 29 using base::PLATFORM_FILE_READ; | 28 using base::PLATFORM_FILE_READ; |
| 30 using base::PLATFORM_FILE_WRITE; | 29 using base::PLATFORM_FILE_WRITE; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 base::Bind(callback, sync_entry, | 94 base::Bind(callback, sync_entry, |
| 96 sync_entry ? net::OK : net::ERR_FAILED)); | 95 sync_entry ? net::OK : net::ERR_FAILED)); |
| 97 } | 96 } |
| 98 | 97 |
| 99 // static | 98 // static |
| 100 void SimpleSynchronousEntry::DoomEntry( | 99 void SimpleSynchronousEntry::DoomEntry( |
| 101 const FilePath& path, | 100 const FilePath& path, |
| 102 const std::string& key, | 101 const std::string& key, |
| 103 scoped_refptr<TaskRunner> callback_runner, | 102 scoped_refptr<TaskRunner> callback_runner, |
| 104 const net::CompletionCallback& callback) { | 103 const net::CompletionCallback& callback) { |
| 105 for (int i = 0; i < kIndexCount; ++i) { | 104 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 106 bool delete_result = | 105 bool delete_result = |
| 107 file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)), | 106 file_util::Delete(path.AppendASCII(GetFilenameForKeyAndIndex(key, i)), |
| 108 false); | 107 false); |
| 109 DCHECK(delete_result); | 108 DCHECK(delete_result); |
| 110 } | 109 } |
| 111 if (!callback.is_null()) | 110 if (!callback.is_null()) |
| 112 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); | 111 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); |
| 113 } | 112 } |
| 114 | 113 |
| 115 void SimpleSynchronousEntry::DoomAndClose() { | 114 void SimpleSynchronousEntry::DoomAndClose() { |
| 116 scoped_refptr<TaskRunner> callback_runner = callback_runner_; | 115 scoped_refptr<TaskRunner> callback_runner = callback_runner_; |
| 117 FilePath path = path_; | 116 FilePath path = path_; |
| 118 std::string key = key_; | 117 std::string key = key_; |
| 119 | 118 |
| 120 Close(); | 119 Close(); |
| 121 // |this| is now deleted. | 120 // |this| is now deleted. |
| 122 | 121 |
| 123 DoomEntry(path, key, callback_runner, net::CompletionCallback()); | 122 DoomEntry(path, key, callback_runner, net::CompletionCallback()); |
| 124 } | 123 } |
| 125 | 124 |
| 126 void SimpleSynchronousEntry::Close() { | 125 void SimpleSynchronousEntry::Close() { |
| 127 for (int i = 0; i < kIndexCount; ++i) { | 126 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 128 bool result = ClosePlatformFile(files_[i]); | 127 bool result = ClosePlatformFile(files_[i]); |
| 129 DCHECK(result); | 128 DCHECK(result); |
| 130 } | 129 } |
| 131 delete this; | 130 delete this; |
| 132 } | 131 } |
| 133 | 132 |
| 134 void SimpleSynchronousEntry::ReadData( | 133 void SimpleSynchronousEntry::ReadData( |
| 135 int index, | 134 int index, |
| 136 int offset, | 135 int offset, |
| 137 net::IOBuffer* buf, | 136 net::IOBuffer* buf, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 const std::string& key) : callback_runner_(callback_runner), | 200 const std::string& key) : callback_runner_(callback_runner), |
| 202 path_(path), | 201 path_(path), |
| 203 key_(key), | 202 key_(key), |
| 204 initialized_(false) { | 203 initialized_(false) { |
| 205 } | 204 } |
| 206 | 205 |
| 207 SimpleSynchronousEntry::~SimpleSynchronousEntry() { | 206 SimpleSynchronousEntry::~SimpleSynchronousEntry() { |
| 208 } | 207 } |
| 209 | 208 |
| 210 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { | 209 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { |
| 211 for (int i = 0; i < kIndexCount; ++i) { | 210 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 212 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); | 211 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); |
| 213 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; | 212 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; |
| 214 if (create) | 213 if (create) |
| 215 flags |= PLATFORM_FILE_CREATE_ALWAYS; | 214 flags |= PLATFORM_FILE_CREATE_ALWAYS; |
| 216 else | 215 else |
| 217 flags |= PLATFORM_FILE_OPEN; | 216 flags |= PLATFORM_FILE_OPEN; |
| 218 PlatformFileError error; | 217 PlatformFileError error; |
| 219 files_[i] = CreatePlatformFile(filename, flags, NULL, &error); | 218 files_[i] = CreatePlatformFile(filename, flags, NULL, &error); |
| 220 if (error != PLATFORM_FILE_OK) { | 219 if (error != PLATFORM_FILE_OK) { |
| 221 while (--i >= 0) { | 220 while (--i >= 0) { |
| 222 bool did_close = ClosePlatformFile(files_[i]); | 221 bool did_close = ClosePlatformFile(files_[i]); |
| 223 DCHECK(did_close); | 222 DCHECK(did_close); |
| 224 } | 223 } |
| 225 return false; | 224 return false; |
| 226 } | 225 } |
| 227 } | 226 } |
| 228 | 227 |
| 229 for (int i = 0; i < kIndexCount; ++i) { | 228 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 230 PlatformFileInfo file_info; | 229 PlatformFileInfo file_info; |
| 231 CHECK(GetPlatformFileInfo(files_[i], &file_info)); | 230 CHECK(GetPlatformFileInfo(files_[i], &file_info)); |
| 232 last_used_ = std::max(last_used_, file_info.last_accessed); | 231 last_used_ = std::max(last_used_, file_info.last_accessed); |
| 233 last_modified_ = std::max(last_modified_, file_info.last_modified); | 232 last_modified_ = std::max(last_modified_, file_info.last_modified); |
| 234 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); | 233 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); |
| 235 } | 234 } |
| 236 | 235 |
| 237 return true; | 236 return true; |
| 238 } | 237 } |
| 239 | 238 |
| 240 bool SimpleSynchronousEntry::InitializeForOpen() { | 239 bool SimpleSynchronousEntry::InitializeForOpen() { |
| 241 DCHECK(!initialized_); | 240 DCHECK(!initialized_); |
| 242 if (!OpenOrCreateFiles(false)) | 241 if (!OpenOrCreateFiles(false)) |
| 243 return false; | 242 return false; |
| 244 | 243 |
| 245 for (int i = 0; i < kIndexCount; ++i) { | 244 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 246 SimpleFileHeader header; | 245 SimpleFileHeader header; |
| 247 if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 246 if (ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
| 248 sizeof(header)) != sizeof(header)) { | 247 sizeof(header)) != sizeof(header)) { |
| 249 return false; | 248 return false; |
| 250 } | 249 } |
| 251 | 250 |
| 252 if (header.initial_magic_number != kSimpleInitialMagicNumber) { | 251 if (header.initial_magic_number != kSimpleInitialMagicNumber) { |
| 253 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we | 252 // TODO(gavinp): This seems very bad; for now we log at WARNING, but we |
| 254 // should give consideration to not saturating the log with these if that | 253 // should give consideration to not saturating the log with these if that |
| 255 // becomes a problem. | 254 // becomes a problem. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 return true; | 292 return true; |
| 294 } | 293 } |
| 295 | 294 |
| 296 bool SimpleSynchronousEntry::InitializeForCreate() { | 295 bool SimpleSynchronousEntry::InitializeForCreate() { |
| 297 DCHECK(!initialized_); | 296 DCHECK(!initialized_); |
| 298 if (!OpenOrCreateFiles(true)) { | 297 if (!OpenOrCreateFiles(true)) { |
| 299 LOG(ERROR) << "Could not create platform files for key " << key_; | 298 LOG(ERROR) << "Could not create platform files for key " << key_; |
| 300 return false; | 299 return false; |
| 301 } | 300 } |
| 302 | 301 |
| 303 for (int i = 0; i < kIndexCount; ++i) { | 302 for (int i = 0; i < kSimpleEntryFileCount; ++i) { |
| 304 SimpleFileHeader header; | 303 SimpleFileHeader header; |
| 305 header.initial_magic_number = kSimpleInitialMagicNumber; | 304 header.initial_magic_number = kSimpleInitialMagicNumber; |
| 306 header.version = kSimpleVersion; | 305 header.version = kSimpleVersion; |
| 307 | 306 |
| 308 header.key_length = key_.size(); | 307 header.key_length = key_.size(); |
| 309 header.key_hash = base::Hash(key_); | 308 header.key_hash = base::Hash(key_); |
| 310 | 309 |
| 311 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), | 310 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), |
| 312 sizeof(header)) != sizeof(header)) { | 311 sizeof(header)) != sizeof(header)) { |
| 313 // TODO(gavinp): Clean up created files. | 312 // TODO(gavinp): Clean up created files. |
| 314 LOG(WARNING) << "Could not write headers to new cache entry."; | 313 LOG(WARNING) << "Could not write headers to new cache entry."; |
| 315 return false; | 314 return false; |
| 316 } | 315 } |
| 317 | 316 |
| 318 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), | 317 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), |
| 319 key_.size()) != implicit_cast<int>(key_.size())) { | 318 key_.size()) != implicit_cast<int>(key_.size())) { |
| 320 // TODO(gavinp): Clean up created files. | 319 // TODO(gavinp): Clean up created files. |
| 321 LOG(WARNING) << "Could not write keys to new cache entry."; | 320 LOG(WARNING) << "Could not write keys to new cache entry."; |
| 322 return false; | 321 return false; |
| 323 } | 322 } |
| 324 } | 323 } |
| 325 | 324 |
| 326 initialized_ = true; | 325 initialized_ = true; |
| 327 return true; | 326 return true; |
| 328 } | 327 } |
| 329 | 328 |
| 330 } // namespace disk_cache | 329 } // namespace disk_cache |
| OLD | NEW |