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