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(); | |
| 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 self_ = NULL; |
|
felipeg
2013/04/12 15:52:19
should we DCHECK the sanity of self_ ?
something l
gavinp
2013/04/13 08:54:35
Done.
| |
| 105 WorkerPool::PostTask(FROM_HERE, | |
| 106 base::Bind(&SimpleSynchronousEntry::Close, | |
| 107 base::Unretained(synchronous_entry_)), | |
| 108 true); | |
| 109 } | |
| 110 // Entry::Close() is expected to release this entry. See disk_cache.h for | |
| 111 // details. | |
| 112 delete this; | |
| 113 } | 112 } |
| 114 | 113 |
| 115 std::string SimpleEntryImpl::GetKey() const { | 114 std::string SimpleEntryImpl::GetKey() const { |
| 116 DCHECK(io_thread_checker_.CalledOnValidThread()); | 115 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 117 return key_; | 116 return key_; |
| 118 } | 117 } |
| 119 | 118 |
| 120 Time SimpleEntryImpl::GetLastUsed() const { | 119 Time SimpleEntryImpl::GetLastUsed() const { |
| 121 DCHECK(io_thread_checker_.CalledOnValidThread()); | 120 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 122 return last_used_; | 121 return last_used_; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 143 // entry as read only. This might make calling SimpleSynchronousEntry::Close() | 142 // entry as read only. This might make calling SimpleSynchronousEntry::Close() |
| 144 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete). | 143 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete). |
| 145 if (synchronous_entry_in_use_by_worker_) { | 144 if (synchronous_entry_in_use_by_worker_) { |
| 146 NOTIMPLEMENTED(); | 145 NOTIMPLEMENTED(); |
| 147 CHECK(false); | 146 CHECK(false); |
| 148 } | 147 } |
| 149 synchronous_entry_in_use_by_worker_ = true; | 148 synchronous_entry_in_use_by_worker_ = true; |
| 150 index_->UseIfExists(key_); | 149 index_->UseIfExists(key_); |
| 151 SynchronousOperationCallback sync_operation_callback = | 150 SynchronousOperationCallback sync_operation_callback = |
| 152 base::Bind(&SimpleEntryImpl::EntryOperationComplete, | 151 base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| 153 index_, callback, weak_ptr_factory_.GetWeakPtr(), | 152 this, callback); |
| 154 synchronous_entry_); | |
| 155 WorkerPool::PostTask(FROM_HERE, | 153 WorkerPool::PostTask(FROM_HERE, |
| 156 base::Bind(&SimpleSynchronousEntry::ReadData, | 154 base::Bind(&SimpleSynchronousEntry::ReadData, |
| 157 base::Unretained(synchronous_entry_), | 155 base::Unretained(synchronous_entry_), |
| 158 index, offset, make_scoped_refptr(buf), | 156 index, offset, make_scoped_refptr(buf), |
| 159 buf_len, sync_operation_callback), | 157 buf_len, sync_operation_callback), |
| 160 true); | 158 true); |
| 161 return net::ERR_IO_PENDING; | 159 return net::ERR_IO_PENDING; |
| 162 } | 160 } |
| 163 | 161 |
| 164 int SimpleEntryImpl::WriteData(int index, | 162 int SimpleEntryImpl::WriteData(int index, |
| 165 int offset, | 163 int offset, |
| 166 net::IOBuffer* buf, | 164 net::IOBuffer* buf, |
| 167 int buf_len, | 165 int buf_len, |
| 168 const CompletionCallback& callback, | 166 const CompletionCallback& callback, |
| 169 bool truncate) { | 167 bool truncate) { |
| 170 DCHECK(io_thread_checker_.CalledOnValidThread()); | 168 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 171 if (synchronous_entry_in_use_by_worker_) { | 169 if (synchronous_entry_in_use_by_worker_) { |
| 172 NOTIMPLEMENTED(); | 170 NOTIMPLEMENTED(); |
| 173 CHECK(false); | 171 CHECK(false); |
| 174 } | 172 } |
| 175 synchronous_entry_in_use_by_worker_ = true; | 173 synchronous_entry_in_use_by_worker_ = true; |
| 176 index_->UseIfExists(key_); | 174 index_->UseIfExists(key_); |
| 177 SynchronousOperationCallback sync_operation_callback = | 175 SynchronousOperationCallback sync_operation_callback = |
| 178 base::Bind(&SimpleEntryImpl::EntryOperationComplete, | 176 base::Bind(&SimpleEntryImpl::EntryOperationComplete, |
| 179 index_, callback, weak_ptr_factory_.GetWeakPtr(), | 177 this, callback); |
| 180 synchronous_entry_); | |
| 181 WorkerPool::PostTask(FROM_HERE, | 178 WorkerPool::PostTask(FROM_HERE, |
| 182 base::Bind(&SimpleSynchronousEntry::WriteData, | 179 base::Bind(&SimpleSynchronousEntry::WriteData, |
| 183 base::Unretained(synchronous_entry_), | 180 base::Unretained(synchronous_entry_), |
| 184 index, offset, make_scoped_refptr(buf), | 181 index, offset, make_scoped_refptr(buf), |
| 185 buf_len, sync_operation_callback, truncate), | 182 buf_len, sync_operation_callback, truncate), |
| 186 true); | 183 true); |
| 187 return net::ERR_IO_PENDING; | 184 return net::ERR_IO_PENDING; |
| 188 } | 185 } |
| 189 | 186 |
| 190 int SimpleEntryImpl::ReadSparseData(int64 offset, | 187 int SimpleEntryImpl::ReadSparseData(int64 offset, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 NOTIMPLEMENTED(); | 226 NOTIMPLEMENTED(); |
| 230 } | 227 } |
| 231 | 228 |
| 232 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { | 229 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { |
| 233 DCHECK(io_thread_checker_.CalledOnValidThread()); | 230 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 234 // TODO(gavinp): Determine if the simple backend should support sparse data. | 231 // TODO(gavinp): Determine if the simple backend should support sparse data. |
| 235 NOTIMPLEMENTED(); | 232 NOTIMPLEMENTED(); |
| 236 return net::ERR_FAILED; | 233 return net::ERR_FAILED; |
| 237 } | 234 } |
| 238 | 235 |
| 239 SimpleEntryImpl::SimpleEntryImpl( | 236 SimpleEntryImpl::SimpleEntryImpl(WeakPtr<SimpleIndex> index, |
| 240 SimpleSynchronousEntry* synchronous_entry, | 237 const base::FilePath& path, |
| 241 WeakPtr<SimpleIndex> index) | 238 const std::string& key) |
| 242 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), | 239 : index_(index), |
| 243 path_(synchronous_entry->path()), | 240 path_(path), |
| 244 key_(synchronous_entry->key()), | 241 key_(key), |
| 245 synchronous_entry_(synchronous_entry), | 242 synchronous_entry_(NULL), |
| 246 synchronous_entry_in_use_by_worker_(false), | 243 synchronous_entry_in_use_by_worker_(false) { |
| 247 index_(index) { | |
| 248 DCHECK(synchronous_entry); | |
| 249 SetSynchronousData(); | |
| 250 } | 244 } |
| 251 | 245 |
| 252 SimpleEntryImpl::~SimpleEntryImpl() { | 246 SimpleEntryImpl::~SimpleEntryImpl() { |
| 253 DCHECK(io_thread_checker_.CalledOnValidThread()); | 247 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 248 if (synchronous_entry_) { | |
| 249 WorkerPool::PostTask(FROM_HERE, | |
| 250 base::Bind(&SimpleSynchronousEntry::Close, | |
| 251 base::Unretained(synchronous_entry_)), | |
| 252 true); | |
| 253 } | |
| 254 } | 254 } |
| 255 | 255 |
| 256 // static | |
| 257 void SimpleEntryImpl::CreationOperationComplete( | 256 void SimpleEntryImpl::CreationOperationComplete( |
| 258 WeakPtr<SimpleIndex> index, | |
| 259 const CompletionCallback& completion_callback, | 257 const CompletionCallback& completion_callback, |
| 260 const std::string& key, | |
| 261 Entry** out_entry, | |
| 262 SimpleSynchronousEntry* sync_entry) { | 258 SimpleSynchronousEntry* sync_entry) { |
| 259 DCHECK(io_thread_checker_.CalledOnValidThread()); | |
| 263 if (!sync_entry) { | 260 if (!sync_entry) { |
| 264 completion_callback.Run(net::ERR_FAILED); | 261 completion_callback.Run(net::ERR_FAILED); |
| 265 // If OpenEntry failed, we must remove it from our index. | 262 // If OpenEntry failed, we must remove it from our index. |
| 266 if (index) | 263 if (index_) |
| 267 index->Remove(key); | 264 index_->Remove(key_); |
| 268 return; | 265 return; |
| 269 } | 266 } |
| 270 if (index) | 267 Initialize(sync_entry); |
| 271 index->Insert(sync_entry->key()); | 268 if (index_) |
| 272 *out_entry = new SimpleEntryImpl(sync_entry, index); | 269 index_->Insert(key_); |
| 273 completion_callback.Run(net::OK); | 270 completion_callback.Run(net::OK); |
| 274 } | 271 } |
| 275 | 272 |
| 276 // static | |
| 277 void SimpleEntryImpl::EntryOperationComplete( | 273 void SimpleEntryImpl::EntryOperationComplete( |
| 278 base::WeakPtr<SimpleIndex> index, | |
| 279 const CompletionCallback& completion_callback, | 274 const CompletionCallback& completion_callback, |
| 280 base::WeakPtr<SimpleEntryImpl> entry, | |
| 281 SimpleSynchronousEntry* sync_entry, | |
| 282 int result) { | 275 int result) { |
| 283 DCHECK(sync_entry); | 276 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 277 DCHECK(synchronous_entry_); | |
| 278 DCHECK(synchronous_entry_in_use_by_worker_); | |
| 279 synchronous_entry_in_use_by_worker_ = false; | |
| 280 SetSynchronousData(); | |
| 284 if (index) { | 281 if (index) { |
| 285 if (result == net::OK) | 282 if (result == net::OK) |
| 286 index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize()); | 283 index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize()); |
| 287 else | 284 else |
| 288 index->Remove(sync_entry->key()); | 285 index->Remove(sync_entry->key()); |
| 289 } | 286 } |
| 287 completion_callback.Run(result); | |
| 288 } | |
| 290 | 289 |
| 291 if (entry) { | 290 void SimpleEntryImpl::Initialize( |
| 292 DCHECK(entry->synchronous_entry_in_use_by_worker_); | 291 SimpleSynchronousEntry* sync_entry) { |
| 293 entry->synchronous_entry_in_use_by_worker_ = false; | 292 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 294 entry->SetSynchronousData(); | 293 self_ = this; |
| 295 } else { | 294 synchronous_entry_ = sync_entry; |
| 296 // |entry| must have had Close() called while this operation was in flight. | 295 SetSynchronousData(); |
| 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 } | |
| 304 completion_callback.Run(result); | |
| 305 } | 296 } |
| 306 | 297 |
| 307 void SimpleEntryImpl::SetSynchronousData() { | 298 void SimpleEntryImpl::SetSynchronousData() { |
| 308 DCHECK(io_thread_checker_.CalledOnValidThread()); | 299 DCHECK(io_thread_checker_.CalledOnValidThread()); |
| 309 DCHECK(!synchronous_entry_in_use_by_worker_); | 300 DCHECK(!synchronous_entry_in_use_by_worker_); |
| 310 // TODO(felipeg): These copies to avoid data races are not optimal. While | 301 // 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 | 302 // 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 | 303 // in that structure. This also solves problems with last_used() on ext4 |
| 313 // filesystems not being accurate. | 304 // filesystems not being accurate. |
| 314 last_used_ = synchronous_entry_->last_used(); | 305 last_used_ = synchronous_entry_->last_used(); |
| 315 last_modified_ = synchronous_entry_->last_modified(); | 306 last_modified_ = synchronous_entry_->last_modified(); |
| 316 for (int i = 0; i < kSimpleEntryFileCount; ++i) | 307 for (int i = 0; i < kSimpleEntryFileCount; ++i) |
| 317 data_size_[i] = synchronous_entry_->data_size(i); | 308 data_size_[i] = synchronous_entry_->data_size(i); |
| 318 } | 309 } |
| 319 | 310 |
| 320 } // namespace disk_cache | 311 } // namespace disk_cache |
| OLD | NEW |