Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(524)

Side by Side Diff: net/disk_cache/simple/simple_entry_impl.cc

Issue 13880016: Make SimpleEntryImpl ref counted. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove last vestigal weak ptr stuff Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 index->Remove(key);
81 index->Remove(key);
82 WorkerPool::PostTask(FROM_HERE, 84 WorkerPool::PostTask(FROM_HERE,
83 base::Bind(&SimpleSynchronousEntry::DoomEntry, path, key, 85 base::Bind(&SimpleSynchronousEntry::DoomEntry, path, key,
84 MessageLoopProxy::current(), callback), 86 MessageLoopProxy::current(), callback),
85 true); 87 true);
86 return net::ERR_IO_PENDING; 88 return net::ERR_IO_PENDING;
87 } 89 }
88 90
89 void SimpleEntryImpl::Doom() { 91 void SimpleEntryImpl::Doom() {
90 DCHECK(io_thread_checker_.CalledOnValidThread()); 92 DCHECK(io_thread_checker_.CalledOnValidThread());
93 DCHECK(synchronous_entry_);
91 #if defined(OS_POSIX) 94 #if defined(OS_POSIX)
92 // This call to static SimpleEntryImpl::DoomEntry() will just erase the 95 // 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 96 // 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 97 // SimpleSynchronousEntry, and operations can even happen on them. The files
95 // will be removed from the filesystem when they are closed. 98 // will be removed from the filesystem when they are closed.
96 DoomEntry(index_, path_, key_, CompletionCallback()); 99 DoomEntry(index_, path_, key_, CompletionCallback());
97 #else 100 #else
98 NOTIMPLEMENTED(); 101 NOTIMPLEMENTED();
99 #endif 102 #endif
100 } 103 }
101 104
102 void SimpleEntryImpl::Close() { 105 void SimpleEntryImpl::Close() {
103 DCHECK(io_thread_checker_.CalledOnValidThread()); 106 DCHECK(io_thread_checker_.CalledOnValidThread());
104 if (!synchronous_entry_in_use_by_worker_) { 107 bool in_use = synchronous_entry_in_use_by_worker_;
105 WorkerPool::PostTask(FROM_HERE, 108 DCHECK((in_use && !HasOneRef()) || (!in_use && HasOneRef()));
Philippe 2013/04/17 10:19:22 It is probably ok now but this DCHECK() might be a
gavinp 2013/04/17 10:56:28 Good point. I've added a comment noting that this
106 base::Bind(&SimpleSynchronousEntry::Close, 109 Release(); // Balanced in CreationOperationCompleted().
107 base::Unretained(synchronous_entry_)), 110 // At most one worker can be operating on our |synchronous_entry_| at a time,
108 true); 111 // and so if we were in use when we cleared |self_|, we should expect a single
109 } 112 // reference from that operation now.
110 // Entry::Close() is expected to release this entry. See disk_cache.h for 113 if (in_use)
111 // details. 114 DCHECK(HasOneRef());
112 delete this;
113 } 115 }
114 116
115 std::string SimpleEntryImpl::GetKey() const { 117 std::string SimpleEntryImpl::GetKey() const {
116 DCHECK(io_thread_checker_.CalledOnValidThread()); 118 DCHECK(io_thread_checker_.CalledOnValidThread());
117 return key_; 119 return key_;
118 } 120 }
119 121
120 Time SimpleEntryImpl::GetLastUsed() const { 122 Time SimpleEntryImpl::GetLastUsed() const {
121 DCHECK(io_thread_checker_.CalledOnValidThread()); 123 DCHECK(io_thread_checker_.CalledOnValidThread());
122 return last_used_; 124 return last_used_;
(...skipping 17 matching lines...) Expand all
140 DCHECK(io_thread_checker_.CalledOnValidThread()); 142 DCHECK(io_thread_checker_.CalledOnValidThread());
141 // TODO(gavinp): Add support for overlapping reads. The net::HttpCache does 143 // TODO(gavinp): Add support for overlapping reads. The net::HttpCache does
142 // make overlapping read requests when multiple transactions access the same 144 // make overlapping read requests when multiple transactions access the same
143 // entry as read only. This might make calling SimpleSynchronousEntry::Close() 145 // entry as read only. This might make calling SimpleSynchronousEntry::Close()
144 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete). 146 // correctly more tricky (see SimpleEntryImpl::EntryOperationComplete).
145 if (synchronous_entry_in_use_by_worker_) { 147 if (synchronous_entry_in_use_by_worker_) {
146 NOTIMPLEMENTED(); 148 NOTIMPLEMENTED();
147 CHECK(false); 149 CHECK(false);
148 } 150 }
149 synchronous_entry_in_use_by_worker_ = true; 151 synchronous_entry_in_use_by_worker_ = true;
150 if (index_) 152 index_->UseIfExists(key_);
151 index_->UseIfExists(key_);
152 SynchronousOperationCallback sync_operation_callback = 153 SynchronousOperationCallback sync_operation_callback =
153 base::Bind(&SimpleEntryImpl::EntryOperationComplete, 154 base::Bind(&SimpleEntryImpl::EntryOperationComplete,
154 index_, callback, weak_ptr_factory_.GetWeakPtr(), 155 this, callback);
155 synchronous_entry_);
156 WorkerPool::PostTask(FROM_HERE, 156 WorkerPool::PostTask(FROM_HERE,
157 base::Bind(&SimpleSynchronousEntry::ReadData, 157 base::Bind(&SimpleSynchronousEntry::ReadData,
158 base::Unretained(synchronous_entry_), 158 base::Unretained(synchronous_entry_),
159 index, offset, make_scoped_refptr(buf), 159 index, offset, make_scoped_refptr(buf),
160 buf_len, sync_operation_callback), 160 buf_len, sync_operation_callback),
161 true); 161 true);
162 return net::ERR_IO_PENDING; 162 return net::ERR_IO_PENDING;
163 } 163 }
164 164
165 int SimpleEntryImpl::WriteData(int index, 165 int SimpleEntryImpl::WriteData(int index,
166 int offset, 166 int offset,
167 net::IOBuffer* buf, 167 net::IOBuffer* buf,
168 int buf_len, 168 int buf_len,
169 const CompletionCallback& callback, 169 const CompletionCallback& callback,
170 bool truncate) { 170 bool truncate) {
171 DCHECK(io_thread_checker_.CalledOnValidThread()); 171 DCHECK(io_thread_checker_.CalledOnValidThread());
172 if (synchronous_entry_in_use_by_worker_) { 172 if (synchronous_entry_in_use_by_worker_) {
173 NOTIMPLEMENTED(); 173 NOTIMPLEMENTED();
174 CHECK(false); 174 CHECK(false);
175 } 175 }
176 synchronous_entry_in_use_by_worker_ = true; 176 synchronous_entry_in_use_by_worker_ = true;
177 if (index_) 177 index_->UseIfExists(key_);
178 index_->UseIfExists(key_);
179 SynchronousOperationCallback sync_operation_callback = 178 SynchronousOperationCallback sync_operation_callback =
180 base::Bind(&SimpleEntryImpl::EntryOperationComplete, 179 base::Bind(&SimpleEntryImpl::EntryOperationComplete,
181 index_, callback, weak_ptr_factory_.GetWeakPtr(), 180 this, callback);
182 synchronous_entry_);
183 WorkerPool::PostTask(FROM_HERE, 181 WorkerPool::PostTask(FROM_HERE,
184 base::Bind(&SimpleSynchronousEntry::WriteData, 182 base::Bind(&SimpleSynchronousEntry::WriteData,
185 base::Unretained(synchronous_entry_), 183 base::Unretained(synchronous_entry_),
186 index, offset, make_scoped_refptr(buf), 184 index, offset, make_scoped_refptr(buf),
187 buf_len, sync_operation_callback, truncate), 185 buf_len, sync_operation_callback, truncate),
188 true); 186 true);
189 return net::ERR_IO_PENDING; 187 return net::ERR_IO_PENDING;
190 } 188 }
191 189
192 int SimpleEntryImpl::ReadSparseData(int64 offset, 190 int SimpleEntryImpl::ReadSparseData(int64 offset,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 NOTIMPLEMENTED(); 229 NOTIMPLEMENTED();
232 } 230 }
233 231
234 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { 232 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) {
235 DCHECK(io_thread_checker_.CalledOnValidThread()); 233 DCHECK(io_thread_checker_.CalledOnValidThread());
236 // TODO(gavinp): Determine if the simple backend should support sparse data. 234 // TODO(gavinp): Determine if the simple backend should support sparse data.
237 NOTIMPLEMENTED(); 235 NOTIMPLEMENTED();
238 return net::ERR_FAILED; 236 return net::ERR_FAILED;
239 } 237 }
240 238
241 SimpleEntryImpl::SimpleEntryImpl( 239 SimpleEntryImpl::SimpleEntryImpl(scoped_refptr<SimpleIndex> index,
242 SimpleSynchronousEntry* synchronous_entry, 240 const base::FilePath& path,
243 WeakPtr<SimpleIndex> index) 241 const std::string& key)
244 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), 242 : index_(index),
245 path_(synchronous_entry->path()), 243 path_(path),
246 key_(synchronous_entry->key()), 244 key_(key),
247 synchronous_entry_(synchronous_entry), 245 synchronous_entry_(NULL),
248 synchronous_entry_in_use_by_worker_(false), 246 synchronous_entry_in_use_by_worker_(false) {
249 index_(index) {
250 DCHECK(synchronous_entry);
251 SetSynchronousData();
252 } 247 }
253 248
254 SimpleEntryImpl::~SimpleEntryImpl() { 249 SimpleEntryImpl::~SimpleEntryImpl() {
255 DCHECK(io_thread_checker_.CalledOnValidThread()); 250 DCHECK(io_thread_checker_.CalledOnValidThread());
251 if (synchronous_entry_) {
252 WorkerPool::PostTask(FROM_HERE,
253 base::Bind(&SimpleSynchronousEntry::Close,
254 base::Unretained(synchronous_entry_)),
255 true);
256 }
256 } 257 }
257 258
258 // static
259 void SimpleEntryImpl::CreationOperationComplete( 259 void SimpleEntryImpl::CreationOperationComplete(
260 WeakPtr<SimpleIndex> index, 260 Entry** out_entry,
261 const CompletionCallback& completion_callback, 261 const CompletionCallback& completion_callback,
262 const std::string& key,
263 Entry** out_entry,
264 SimpleSynchronousEntry* sync_entry) { 262 SimpleSynchronousEntry* sync_entry) {
263 DCHECK(io_thread_checker_.CalledOnValidThread());
265 if (!sync_entry) { 264 if (!sync_entry) {
266 completion_callback.Run(net::ERR_FAILED); 265 completion_callback.Run(net::ERR_FAILED);
267 // If OpenEntry failed, we must remove it from our index. 266 // If OpenEntry failed, we must remove it from our index.
268 if (index) 267 index_->Remove(key_);
269 index->Remove(key); 268 // The reference held by the Callback calling us will go out of scope and
269 // delete |this| on leaving this scope.
270 return; 270 return;
271 } 271 }
272 if (index) 272 // Adding a reference to self will keep |this| alive after the scope of our
273 index->Insert(sync_entry->key()); 273 // Callback calling us is destroyed.
274 *out_entry = new SimpleEntryImpl(sync_entry, index); 274 AddRef(); // Balanced in Close().
275 synchronous_entry_ = sync_entry;
276 SetSynchronousData();
277 index_->Insert(key_);
278 *out_entry = this;
275 completion_callback.Run(net::OK); 279 completion_callback.Run(net::OK);
276 } 280 }
277 281
278 // static
279 void SimpleEntryImpl::EntryOperationComplete( 282 void SimpleEntryImpl::EntryOperationComplete(
280 base::WeakPtr<SimpleIndex> index,
281 const CompletionCallback& completion_callback, 283 const CompletionCallback& completion_callback,
282 base::WeakPtr<SimpleEntryImpl> entry,
283 SimpleSynchronousEntry* sync_entry,
284 int result) { 284 int result) {
285 DCHECK(sync_entry); 285 DCHECK(io_thread_checker_.CalledOnValidThread());
286 if (index) { 286 DCHECK(synchronous_entry_);
287 if (result >= 0) 287 DCHECK(synchronous_entry_in_use_by_worker_);
288 index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize()); 288 synchronous_entry_in_use_by_worker_ = false;
289 else 289 SetSynchronousData();
290 index->Remove(sync_entry->key()); 290 if (result >= 0) {
291 } 291 index_->UpdateEntrySize(synchronous_entry_->key(),
292 292 synchronous_entry_->GetFileSize());
293 if (entry) {
294 DCHECK(entry->synchronous_entry_in_use_by_worker_);
295 entry->synchronous_entry_in_use_by_worker_ = false;
296 entry->SetSynchronousData();
297 } else { 293 } else {
298 // |entry| must have had Close() called while this operation was in flight. 294 index_->Remove(synchronous_entry_->key());
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 } 295 }
306 completion_callback.Run(result); 296 completion_callback.Run(result);
307 } 297 }
308 298
309 void SimpleEntryImpl::SetSynchronousData() { 299 void SimpleEntryImpl::SetSynchronousData() {
310 DCHECK(io_thread_checker_.CalledOnValidThread()); 300 DCHECK(io_thread_checker_.CalledOnValidThread());
311 DCHECK(!synchronous_entry_in_use_by_worker_); 301 DCHECK(!synchronous_entry_in_use_by_worker_);
312 // TODO(felipeg): These copies to avoid data races are not optimal. While 302 // 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 303 // 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 304 // in that structure. This also solves problems with last_used() on ext4
315 // filesystems not being accurate. 305 // filesystems not being accurate.
316 last_used_ = synchronous_entry_->last_used(); 306 last_used_ = synchronous_entry_->last_used();
317 last_modified_ = synchronous_entry_->last_modified(); 307 last_modified_ = synchronous_entry_->last_modified();
318 for (int i = 0; i < kSimpleEntryFileCount; ++i) 308 for (int i = 0; i < kSimpleEntryFileCount; ++i)
319 data_size_[i] = synchronous_entry_->data_size(i); 309 data_size_[i] = synchronous_entry_->data_size(i);
320 } 310 }
321 311
322 } // namespace disk_cache 312 } // namespace disk_cache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698