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