| 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_entry_impl.h" | 5 #include "net/disk_cache/simple/simple_entry_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 typedef disk_cache::SimpleSynchronousEntry::SynchronousOperationCallback | 23 typedef disk_cache::SimpleSynchronousEntry::SynchronousOperationCallback |
| 24 SynchronousOperationCallback; | 24 SynchronousOperationCallback; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 namespace disk_cache { | 28 namespace disk_cache { |
| 29 | 29 |
| 30 using base::FilePath; | 30 using base::FilePath; |
| 31 using base::MessageLoopProxy; | 31 using base::MessageLoopProxy; |
| 32 using base::Time; | 32 using base::Time; |
| 33 using base::WeakPtr; | |
| 34 using base::WorkerPool; | 33 using base::WorkerPool; |
| 35 | 34 |
| 36 // static | 35 // static |
| 37 int SimpleEntryImpl::OpenEntry(WeakPtr<SimpleIndex> index, | 36 int SimpleEntryImpl::OpenEntry(scoped_refptr<SimpleIndex> index, |
| 38 const FilePath& path, | 37 const FilePath& path, |
| 39 const std::string& key, | 38 const std::string& key, |
| 40 Entry** entry, | 39 Entry** entry, |
| 41 const CompletionCallback& callback) { | 40 const CompletionCallback& callback) { |
| 42 // TODO(gavinp): More closely unify the last_used_ in the | 41 // TODO(gavinp): More closely unify the last_used_ in the |
| 43 // SimpleSynchronousEntry and the SimpleIndex. | 42 // SimpleSynchronousEntry and the SimpleIndex. |
| 44 if (!index || index->UseIfExists(key)) { | 43 if (!index || index->UseIfExists(key)) { |
| 44 scoped_refptr<SimpleEntryImpl> new_entry = |
| 45 new SimpleEntryImpl(index, path, key); |
| 45 SynchronousCreationCallback sync_creation_callback = | 46 SynchronousCreationCallback sync_creation_callback = |
| 46 base::Bind(&SimpleEntryImpl::CreationOperationComplete, | 47 base::Bind(&SimpleEntryImpl::CreationOperationComplete, |
| 47 index, callback, key, entry); | 48 new_entry, entry, callback); |
| 48 WorkerPool::PostTask(FROM_HERE, | 49 WorkerPool::PostTask(FROM_HERE, |
| 49 base::Bind(&SimpleSynchronousEntry::OpenEntry, path, | 50 base::Bind(&SimpleSynchronousEntry::OpenEntry, path, |
| 50 key, MessageLoopProxy::current(), | 51 key, MessageLoopProxy::current(), |
| 51 sync_creation_callback), | 52 sync_creation_callback), |
| 52 true); | 53 true); |
| 53 return net::ERR_IO_PENDING; | 54 return net::ERR_IO_PENDING; |
| 54 } | 55 } |
| 55 return net::ERR_FAILED; | 56 return net::ERR_FAILED; |
| 56 } | 57 } |
| 57 | 58 |
| 58 // static | 59 // static |
| 59 int SimpleEntryImpl::CreateEntry(WeakPtr<SimpleIndex> index, | 60 int SimpleEntryImpl::CreateEntry(scoped_refptr<SimpleIndex> index, |
| 60 const FilePath& path, | 61 const FilePath& path, |
| 61 const std::string& key, | 62 const std::string& key, |
| 62 Entry** entry, | 63 Entry** entry, |
| 63 const CompletionCallback& callback) { | 64 const CompletionCallback& callback) { |
| 65 scoped_refptr<SimpleEntryImpl> new_entry = |
| 66 new SimpleEntryImpl(index, path, key); |
| 64 SynchronousCreationCallback sync_creation_callback = | 67 SynchronousCreationCallback sync_creation_callback = |
| 65 base::Bind(&SimpleEntryImpl::CreationOperationComplete, | 68 base::Bind(&SimpleEntryImpl::CreationOperationComplete, |
| 66 index, callback, key, entry); | 69 new_entry, entry, callback); |
| 67 WorkerPool::PostTask(FROM_HERE, | 70 WorkerPool::PostTask(FROM_HERE, |
| 68 base::Bind(&SimpleSynchronousEntry::CreateEntry, path, | 71 base::Bind(&SimpleSynchronousEntry::CreateEntry, path, |
| 69 key, MessageLoopProxy::current(), | 72 key, MessageLoopProxy::current(), |
| 70 sync_creation_callback), | 73 sync_creation_callback), |
| 71 true); | 74 true); |
| 72 return net::ERR_IO_PENDING; | 75 return net::ERR_IO_PENDING; |
| 73 } | 76 } |
| 74 | 77 |
| 75 // static | 78 // static |
| 76 int SimpleEntryImpl::DoomEntry(WeakPtr<SimpleIndex> index, | 79 int SimpleEntryImpl::DoomEntry(scoped_refptr<SimpleIndex> index, |
| 77 const FilePath& path, | 80 const FilePath& path, |
| 78 const std::string& key, | 81 const std::string& key, |
| 79 const CompletionCallback& callback) { | 82 const CompletionCallback& callback) { |
| 80 if (index) | 83 if (index) |
| 81 index->Remove(key); | 84 index->Remove(key); |
| 82 WorkerPool::PostTask(FROM_HERE, | 85 WorkerPool::PostTask(FROM_HERE, |
| 83 base::Bind(&SimpleSynchronousEntry::DoomEntry, path, key, | 86 base::Bind(&SimpleSynchronousEntry::DoomEntry, path, key, |
| 84 MessageLoopProxy::current(), callback), | 87 MessageLoopProxy::current(), callback), |
| 85 true); | 88 true); |
| 86 return net::ERR_IO_PENDING; | 89 return net::ERR_IO_PENDING; |
| 87 } | 90 } |
| 88 | 91 |
| 89 void SimpleEntryImpl::Doom() { | 92 void SimpleEntryImpl::Doom() { |
| 90 DCHECK(io_thread_checker_.CalledOnValidThread()); | 93 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 94 DCHECK(synchronous_entry_); |
| 91 #if defined(OS_POSIX) | 95 #if defined(OS_POSIX) |
| 92 // This call to static SimpleEntryImpl::DoomEntry() will just erase the | 96 // This call to static SimpleEntryImpl::DoomEntry() will just erase the |
| 93 // underlying files. On POSIX, this is fine; the files are still open on the | 97 // underlying files. On POSIX, this is fine; the files are still open on the |
| 94 // SimpleSynchronousEntry, and operations can even happen on them. The files | 98 // SimpleSynchronousEntry, and operations can even happen on them. The files |
| 95 // will be removed from the filesystem when they are closed. | 99 // will be removed from the filesystem when they are closed. |
| 96 DoomEntry(index_, path_, key_, CompletionCallback()); | 100 DoomEntry(index_, path_, key_, CompletionCallback()); |
| 97 #else | 101 #else |
| 98 NOTIMPLEMENTED(); | 102 NOTIMPLEMENTED(); |
| 99 #endif | 103 #endif |
| 100 } | 104 } |
| 101 | 105 |
| 102 void SimpleEntryImpl::Close() { | 106 void SimpleEntryImpl::Close() { |
| 103 DCHECK(io_thread_checker_.CalledOnValidThread()); | 107 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 104 if (!synchronous_entry_in_use_by_worker_) { | 108 bool in_use = synchronous_entry_in_use_by_worker_; |
| 105 WorkerPool::PostTask(FROM_HERE, | 109 DCHECK((in_use && !HasOneRef()) || (!in_use && HasOneRef())); |
| 106 base::Bind(&SimpleSynchronousEntry::Close, | 110 Release(); // Balanced in CreationOperationCompleted(). |
| 107 base::Unretained(synchronous_entry_)), | 111 // At most one worker can be operating on our |synchronous_entry_| at a time, |
| 108 true); | 112 // and so if we were in use when we cleared |self_|, we should expect a single |
| 109 } | 113 // reference from that operation now. |
| 110 // Entry::Close() is expected to release this entry. See disk_cache.h for | 114 if (in_use) |
| 111 // details. | 115 DCHECK(HasOneRef()); |
| 112 delete this; | |
| 113 } | 116 } |
| 114 | 117 |
| 115 std::string SimpleEntryImpl::GetKey() const { | 118 std::string SimpleEntryImpl::GetKey() const { |
| 116 DCHECK(io_thread_checker_.CalledOnValidThread()); | 119 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 117 return key_; | 120 return key_; |
| 118 } | 121 } |
| 119 | 122 |
| 120 Time SimpleEntryImpl::GetLastUsed() const { | 123 Time SimpleEntryImpl::GetLastUsed() const { |
| 121 DCHECK(io_thread_checker_.CalledOnValidThread()); | 124 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 122 return last_used_; | 125 return last_used_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 144 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete). | 147 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete). |
| 145 if (synchronous_entry_in_use_by_worker_) { | 148 if (synchronous_entry_in_use_by_worker_) { |
| 146 NOTIMPLEMENTED(); | 149 NOTIMPLEMENTED(); |
| 147 CHECK(false); | 150 CHECK(false); |
| 148 } | 151 } |
| 149 synchronous_entry_in_use_by_worker_ = true; | 152 synchronous_entry_in_use_by_worker_ = true; |
| 150 if (index_) | 153 if (index_) |
| 151 index_->UseIfExists(key_); | 154 index_->UseIfExists(key_); |
| 152 SynchronousOperationCallback sync_operation_callback = | 155 SynchronousOperationCallback sync_operation_callback = |
| 153 base::Bind(&SimpleEntryImpl::EntryOperationComplete, | 156 base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| 154 index_, callback, weak_ptr_factory_.GetWeakPtr(), | 157 this, callback); |
| 155 synchronous_entry_); | |
| 156 WorkerPool::PostTask(FROM_HERE, | 158 WorkerPool::PostTask(FROM_HERE, |
| 157 base::Bind(&SimpleSynchronousEntry::ReadData, | 159 base::Bind(&SimpleSynchronousEntry::ReadData, |
| 158 base::Unretained(synchronous_entry_), | 160 base::Unretained(synchronous_entry_), |
| 159 index, offset, make_scoped_refptr(buf), | 161 index, offset, make_scoped_refptr(buf), |
| 160 buf_len, sync_operation_callback), | 162 buf_len, sync_operation_callback), |
| 161 true); | 163 true); |
| 162 return net::ERR_IO_PENDING; | 164 return net::ERR_IO_PENDING; |
| 163 } | 165 } |
| 164 | 166 |
| 165 int SimpleEntryImpl::WriteData(int index, | 167 int SimpleEntryImpl::WriteData(int index, |
| 166 int offset, | 168 int offset, |
| 167 net::IOBuffer* buf, | 169 net::IOBuffer* buf, |
| 168 int buf_len, | 170 int buf_len, |
| 169 const CompletionCallback& callback, | 171 const CompletionCallback& callback, |
| 170 bool truncate) { | 172 bool truncate) { |
| 171 DCHECK(io_thread_checker_.CalledOnValidThread()); | 173 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 172 if (synchronous_entry_in_use_by_worker_) { | 174 if (synchronous_entry_in_use_by_worker_) { |
| 173 NOTIMPLEMENTED(); | 175 NOTIMPLEMENTED(); |
| 174 CHECK(false); | 176 CHECK(false); |
| 175 } | 177 } |
| 176 synchronous_entry_in_use_by_worker_ = true; | 178 synchronous_entry_in_use_by_worker_ = true; |
| 177 if (index_) | 179 if (index_) |
| 178 index_->UseIfExists(key_); | 180 index_->UseIfExists(key_); |
| 179 SynchronousOperationCallback sync_operation_callback = | 181 SynchronousOperationCallback sync_operation_callback = |
| 180 base::Bind(&SimpleEntryImpl::EntryOperationComplete, | 182 base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| 181 index_, callback, weak_ptr_factory_.GetWeakPtr(), | 183 this, callback); |
| 182 synchronous_entry_); | |
| 183 WorkerPool::PostTask(FROM_HERE, | 184 WorkerPool::PostTask(FROM_HERE, |
| 184 base::Bind(&SimpleSynchronousEntry::WriteData, | 185 base::Bind(&SimpleSynchronousEntry::WriteData, |
| 185 base::Unretained(synchronous_entry_), | 186 base::Unretained(synchronous_entry_), |
| 186 index, offset, make_scoped_refptr(buf), | 187 index, offset, make_scoped_refptr(buf), |
| 187 buf_len, sync_operation_callback, truncate), | 188 buf_len, sync_operation_callback, truncate), |
| 188 true); | 189 true); |
| 189 return net::ERR_IO_PENDING; | 190 return net::ERR_IO_PENDING; |
| 190 } | 191 } |
| 191 | 192 |
| 192 int SimpleEntryImpl::ReadSparseData(int64 offset, | 193 int SimpleEntryImpl::ReadSparseData(int64 offset, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 NOTIMPLEMENTED(); | 232 NOTIMPLEMENTED(); |
| 232 } | 233 } |
| 233 | 234 |
| 234 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 235 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
| 235 DCHECK(io_thread_checker_.CalledOnValidThread()); | 236 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 236 // TODO(gavinp): Determine if the simple backend should support sparse data. | 237 // TODO(gavinp): Determine if the simple backend should support sparse data. |
| 237 NOTIMPLEMENTED(); | 238 NOTIMPLEMENTED(); |
| 238 return net::ERR_FAILED; | 239 return net::ERR_FAILED; |
| 239 } | 240 } |
| 240 | 241 |
| 241 SimpleEntryImpl::SimpleEntryImpl( | 242 SimpleEntryImpl::SimpleEntryImpl(scoped_refptr<SimpleIndex> index, |
| 242 SimpleSynchronousEntry* synchronous_entry, | 243 const base::FilePath& path, |
| 243 WeakPtr<SimpleIndex> index) | 244 const std::string& key) |
| 244 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 245 : index_(index), |
| 245 path_(synchronous_entry->path()), | 246 path_(path), |
| 246 key_(synchronous_entry->key()), | 247 key_(key), |
| 247 synchronous_entry_(synchronous_entry), | 248 synchronous_entry_(NULL), |
| 248 synchronous_entry_in_use_by_worker_(false), | 249 synchronous_entry_in_use_by_worker_(false) { |
| 249 index_(index) { | |
| 250 DCHECK(synchronous_entry); | |
| 251 SetSynchronousData(); | |
| 252 } | 250 } |
| 253 | 251 |
| 254 SimpleEntryImpl::~SimpleEntryImpl() { | 252 SimpleEntryImpl::~SimpleEntryImpl() { |
| 255 DCHECK(io_thread_checker_.CalledOnValidThread()); | 253 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 254 if (synchronous_entry_) { |
| 255 WorkerPool::PostTask(FROM_HERE, |
| 256 base::Bind(&SimpleSynchronousEntry::Close, |
| 257 base::Unretained(synchronous_entry_)), |
| 258 true); |
| 259 } |
| 256 } | 260 } |
| 257 | 261 |
| 258 // static | |
| 259 void SimpleEntryImpl::CreationOperationComplete( | 262 void SimpleEntryImpl::CreationOperationComplete( |
| 260 WeakPtr<SimpleIndex> index, | 263 Entry** out_entry, |
| 261 const CompletionCallback& completion_callback, | 264 const CompletionCallback& completion_callback, |
| 262 const std::string& key, | |
| 263 Entry** out_entry, | |
| 264 SimpleSynchronousEntry* sync_entry) { | 265 SimpleSynchronousEntry* sync_entry) { |
| 266 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 265 if (!sync_entry) { | 267 if (!sync_entry) { |
| 266 completion_callback.Run(net::ERR_FAILED); | 268 completion_callback.Run(net::ERR_FAILED); |
| 267 // If OpenEntry failed, we must remove it from our index. | 269 // If OpenEntry failed, we must remove it from our index. |
| 268 if (index) | 270 if (index_) |
| 269 index->Remove(key); | 271 index_->Remove(key_); |
| 272 // The reference held by the Callback calling us will go out of scope and |
| 273 // delete |this| on leaving this scope. |
| 270 return; | 274 return; |
| 271 } | 275 } |
| 272 if (index) | 276 // Adding a reference to self will keep |this| alive after the scope of our |
| 273 index->Insert(sync_entry->key()); | 277 // Callback calling us is destroyed. |
| 274 *out_entry = new SimpleEntryImpl(sync_entry, index); | 278 AddRef(); // Balanced in Close(). |
| 279 synchronous_entry_ = sync_entry; |
| 280 SetSynchronousData(); |
| 281 if (index_) |
| 282 index_->Insert(key_); |
| 283 *out_entry = this; |
| 275 completion_callback.Run(net::OK); | 284 completion_callback.Run(net::OK); |
| 276 } | 285 } |
| 277 | 286 |
| 278 // static | |
| 279 void SimpleEntryImpl::EntryOperationComplete( | 287 void SimpleEntryImpl::EntryOperationComplete( |
| 280 base::WeakPtr<SimpleIndex> index, | |
| 281 const CompletionCallback& completion_callback, | 288 const CompletionCallback& completion_callback, |
| 282 base::WeakPtr<SimpleEntryImpl> entry, | |
| 283 SimpleSynchronousEntry* sync_entry, | |
| 284 int result) { | 289 int result) { |
| 285 DCHECK(sync_entry); | 290 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 286 if (index) { | 291 DCHECK(synchronous_entry_); |
| 287 if (result >= 0) | 292 DCHECK(synchronous_entry_in_use_by_worker_); |
| 288 index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize()); | 293 synchronous_entry_in_use_by_worker_ = false; |
| 289 else | 294 SetSynchronousData(); |
| 290 index->Remove(sync_entry->key()); | 295 if (index_) { |
| 291 } | 296 if (result >= 0) { |
| 292 | 297 index_->UpdateEntrySize(synchronous_entry_->key(), |
| 293 if (entry) { | 298 synchronous_entry_->GetFileSize()); |
| 294 DCHECK(entry->synchronous_entry_in_use_by_worker_); | 299 } else { |
| 295 entry->synchronous_entry_in_use_by_worker_ = false; | 300 index_->Remove(synchronous_entry_->key()); |
| 296 entry->SetSynchronousData(); | 301 } |
| 297 } else { | |
| 298 // |entry| must have had Close() called while this operation was in flight. | |
| 299 // Since the simple cache now only supports one pending entry operation in | |
| 300 // flight at a time, it's safe to now call Close() on |sync_entry|. | |
| 301 WorkerPool::PostTask(FROM_HERE, | |
| 302 base::Bind(&SimpleSynchronousEntry::Close, | |
| 303 base::Unretained(sync_entry)), | |
| 304 true); | |
| 305 } | 302 } |
| 306 completion_callback.Run(result); | 303 completion_callback.Run(result); |
| 307 } | 304 } |
| 308 | 305 |
| 309 void SimpleEntryImpl::SetSynchronousData() { | 306 void SimpleEntryImpl::SetSynchronousData() { |
| 310 DCHECK(io_thread_checker_.CalledOnValidThread()); | 307 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 311 DCHECK(!synchronous_entry_in_use_by_worker_); | 308 DCHECK(!synchronous_entry_in_use_by_worker_); |
| 312 // TODO(felipeg): These copies to avoid data races are not optimal. While | 309 // TODO(felipeg): These copies to avoid data races are not optimal. While |
| 313 // adding an IO thread index (for fast misses etc...), we can store this data | 310 // adding an IO thread index (for fast misses etc...), we can store this data |
| 314 // in that structure. This also solves problems with last_used() on ext4 | 311 // in that structure. This also solves problems with last_used() on ext4 |
| 315 // filesystems not being accurate. | 312 // filesystems not being accurate. |
| 316 last_used_ = synchronous_entry_->last_used(); | 313 last_used_ = synchronous_entry_->last_used(); |
| 317 last_modified_ = synchronous_entry_->last_modified(); | 314 last_modified_ = synchronous_entry_->last_modified(); |
| 318 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 315 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
| 319 data_size_[i] = synchronous_entry_->data_size(i); | 316 data_size_[i] = synchronous_entry_->data_size(i); |
| 320 } | 317 } |
| 321 | 318 |
| 322 } // namespace disk_cache | 319 } // namespace disk_cache |
| OLD | NEW |