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

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: fix windows build 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
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/disk_cache/simple/simple_index.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
11 #include "base/logging.h"
11 #include "base/message_loop_proxy.h" 12 #include "base/message_loop_proxy.h"
12 #include "base/threading/worker_pool.h" 13 #include "base/threading/worker_pool.h"
13 #include "net/base/io_buffer.h" 14 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h" 15 #include "net/base/net_errors.h"
15 #include "net/disk_cache/simple/simple_index.h" 16 #include "net/disk_cache/simple/simple_index.h"
16 #include "net/disk_cache/simple/simple_synchronous_entry.h" 17 #include "net/disk_cache/simple/simple_synchronous_entry.h"
17 18
18 namespace { 19 namespace {
19 20
20 typedef disk_cache::Entry::CompletionCallback CompletionCallback; 21 typedef disk_cache::Entry::CompletionCallback CompletionCallback;
21 typedef disk_cache::SimpleSynchronousEntry::SynchronousCreationCallback 22 typedef disk_cache::SimpleSynchronousEntry::SynchronousCreationCallback
22 SynchronousCreationCallback; 23 SynchronousCreationCallback;
23 typedef disk_cache::SimpleSynchronousEntry::SynchronousOperationCallback 24 typedef disk_cache::SimpleSynchronousEntry::SynchronousOperationCallback
24 SynchronousOperationCallback; 25 SynchronousOperationCallback;
25 26
26 } // namespace 27 } // namespace
27 28
28 namespace disk_cache { 29 namespace disk_cache {
29 30
30 using base::FilePath; 31 using base::FilePath;
31 using base::MessageLoopProxy; 32 using base::MessageLoopProxy;
32 using base::Time; 33 using base::Time;
33 using base::WeakPtr;
34 using base::WorkerPool; 34 using base::WorkerPool;
35 35
36 // static 36 // static
37 int SimpleEntryImpl::OpenEntry(WeakPtr<SimpleIndex> index, 37 int SimpleEntryImpl::OpenEntry(const scoped_refptr<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);
45 SynchronousCreationCallback sync_creation_callback = 47 SynchronousCreationCallback sync_creation_callback =
46 base::Bind(&SimpleEntryImpl::CreationOperationComplete, 48 base::Bind(&SimpleEntryImpl::CreationOperationComplete,
47 index, callback, key, entry); 49 new_entry, entry, callback);
48 WorkerPool::PostTask(FROM_HERE, 50 WorkerPool::PostTask(FROM_HERE,
49 base::Bind(&SimpleSynchronousEntry::OpenEntry, path, 51 base::Bind(&SimpleSynchronousEntry::OpenEntry, path,
50 key, MessageLoopProxy::current(), 52 key, MessageLoopProxy::current(),
51 sync_creation_callback), 53 sync_creation_callback),
52 true); 54 true);
53 return net::ERR_IO_PENDING; 55 return net::ERR_IO_PENDING;
54 } 56 }
55 return net::ERR_FAILED; 57 return net::ERR_FAILED;
56 } 58 }
57 59
58 // static 60 // static
59 int SimpleEntryImpl::CreateEntry(WeakPtr<SimpleIndex> index, 61 int SimpleEntryImpl::CreateEntry(const scoped_refptr<SimpleIndex>& index,
60 const FilePath& path, 62 const FilePath& path,
61 const std::string& key, 63 const std::string& key,
62 Entry** entry, 64 Entry** entry,
63 const CompletionCallback& callback) { 65 const CompletionCallback& callback) {
66 scoped_refptr<SimpleEntryImpl> new_entry =
67 new SimpleEntryImpl(index, path, key);
64 SynchronousCreationCallback sync_creation_callback = 68 SynchronousCreationCallback sync_creation_callback =
65 base::Bind(&SimpleEntryImpl::CreationOperationComplete, 69 base::Bind(&SimpleEntryImpl::CreationOperationComplete,
66 index, callback, key, entry); 70 new_entry, entry, callback);
67 WorkerPool::PostTask(FROM_HERE, 71 WorkerPool::PostTask(FROM_HERE,
68 base::Bind(&SimpleSynchronousEntry::CreateEntry, path, 72 base::Bind(&SimpleSynchronousEntry::CreateEntry, path,
69 key, MessageLoopProxy::current(), 73 key, MessageLoopProxy::current(),
70 sync_creation_callback), 74 sync_creation_callback),
71 true); 75 true);
72 return net::ERR_IO_PENDING; 76 return net::ERR_IO_PENDING;
73 } 77 }
74 78
75 // static 79 // static
76 int SimpleEntryImpl::DoomEntry(WeakPtr<SimpleIndex> index, 80 int SimpleEntryImpl::DoomEntry(const scoped_refptr<SimpleIndex>& index,
77 const FilePath& path, 81 const FilePath& path,
78 const std::string& key, 82 const std::string& key,
79 const CompletionCallback& callback) { 83 const CompletionCallback& callback) {
80 if (index) 84 index->Remove(key);
81 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_;
felipeg 2013/04/17 11:48:25 can you add a comment explaining why you need to s
105 WorkerPool::PostTask(FROM_HERE, 109 // These HasOneRef DCHECKs may cause flakiness since extra refs do exist
106 base::Bind(&SimpleSynchronousEntry::Close, 110 // ephemerally during copies etc..., if they are on balance causing pain
107 base::Unretained(synchronous_entry_)), 111 // we should consider removing them.
108 true); 112 DCHECK((in_use && !HasOneRef()) || (!in_use && HasOneRef()));
109 } 113 Release(); // Balanced in CreationOperationCompleted().
110 // Entry::Close() is expected to release this entry. See disk_cache.h for 114 // At most one worker can be operating on our |synchronous_entry_| at a time,
111 // details. 115 // and so if we were in use when we cleared |self_|, we should expect a single
112 delete this; 116 // reference from that operation now.
117 if (in_use)
118 DCHECK(HasOneRef());
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 17 matching lines...) Expand all
140 DCHECK(io_thread_checker_.CalledOnValidThread()); 146 DCHECK(io_thread_checker_.CalledOnValidThread());
141 // TODO(gavinp): Add support for overlapping reads. The net::HttpCache does 147 // TODO(gavinp): Add support for overlapping reads. The net::HttpCache does
142 // make overlapping read requests when multiple transactions access the same 148 // make overlapping read requests when multiple transactions access the same
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 if (index_) 156 index_->UseIfExists(key_);
151 index_->UseIfExists(key_);
152 SynchronousOperationCallback sync_operation_callback = 157 SynchronousOperationCallback sync_operation_callback =
153 base::Bind(&SimpleEntryImpl::EntryOperationComplete, 158 base::Bind(&SimpleEntryImpl::EntryOperationComplete,
154 index_, callback, weak_ptr_factory_.GetWeakPtr(), 159 this, callback);
155 synchronous_entry_);
156 WorkerPool::PostTask(FROM_HERE, 160 WorkerPool::PostTask(FROM_HERE,
157 base::Bind(&SimpleSynchronousEntry::ReadData, 161 base::Bind(&SimpleSynchronousEntry::ReadData,
158 base::Unretained(synchronous_entry_), 162 base::Unretained(synchronous_entry_),
159 index, offset, make_scoped_refptr(buf), 163 index, offset, make_scoped_refptr(buf),
160 buf_len, sync_operation_callback), 164 buf_len, sync_operation_callback),
161 true); 165 true);
162 return net::ERR_IO_PENDING; 166 return net::ERR_IO_PENDING;
163 } 167 }
164 168
165 int SimpleEntryImpl::WriteData(int index, 169 int SimpleEntryImpl::WriteData(int index,
166 int offset, 170 int offset,
167 net::IOBuffer* buf, 171 net::IOBuffer* buf,
168 int buf_len, 172 int buf_len,
169 const CompletionCallback& callback, 173 const CompletionCallback& callback,
170 bool truncate) { 174 bool truncate) {
171 DCHECK(io_thread_checker_.CalledOnValidThread()); 175 DCHECK(io_thread_checker_.CalledOnValidThread());
172 if (synchronous_entry_in_use_by_worker_) { 176 if (synchronous_entry_in_use_by_worker_) {
173 NOTIMPLEMENTED(); 177 NOTIMPLEMENTED();
174 CHECK(false); 178 CHECK(false);
175 } 179 }
176 synchronous_entry_in_use_by_worker_ = true; 180 synchronous_entry_in_use_by_worker_ = true;
177 if (index_) 181 index_->UseIfExists(key_);
178 index_->UseIfExists(key_);
179 SynchronousOperationCallback sync_operation_callback = 182 SynchronousOperationCallback sync_operation_callback =
180 base::Bind(&SimpleEntryImpl::EntryOperationComplete, 183 base::Bind(&SimpleEntryImpl::EntryOperationComplete,
181 index_, callback, weak_ptr_factory_.GetWeakPtr(), 184 this, callback);
182 synchronous_entry_);
183 WorkerPool::PostTask(FROM_HERE, 185 WorkerPool::PostTask(FROM_HERE,
184 base::Bind(&SimpleSynchronousEntry::WriteData, 186 base::Bind(&SimpleSynchronousEntry::WriteData,
185 base::Unretained(synchronous_entry_), 187 base::Unretained(synchronous_entry_),
186 index, offset, make_scoped_refptr(buf), 188 index, offset, make_scoped_refptr(buf),
187 buf_len, sync_operation_callback, truncate), 189 buf_len, sync_operation_callback, truncate),
188 true); 190 true);
189 return net::ERR_IO_PENDING; 191 return net::ERR_IO_PENDING;
190 } 192 }
191 193
192 int SimpleEntryImpl::ReadSparseData(int64 offset, 194 int SimpleEntryImpl::ReadSparseData(int64 offset,
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 NOTIMPLEMENTED(); 233 NOTIMPLEMENTED();
232 } 234 }
233 235
234 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) { 236 int SimpleEntryImpl::ReadyForSparseIO(const CompletionCallback& callback) {
235 DCHECK(io_thread_checker_.CalledOnValidThread()); 237 DCHECK(io_thread_checker_.CalledOnValidThread());
236 // TODO(gavinp): Determine if the simple backend should support sparse data. 238 // TODO(gavinp): Determine if the simple backend should support sparse data.
237 NOTIMPLEMENTED(); 239 NOTIMPLEMENTED();
238 return net::ERR_FAILED; 240 return net::ERR_FAILED;
239 } 241 }
240 242
241 SimpleEntryImpl::SimpleEntryImpl( 243 SimpleEntryImpl::SimpleEntryImpl(const scoped_refptr<SimpleIndex>& index,
242 SimpleSynchronousEntry* synchronous_entry, 244 const base::FilePath& path,
243 WeakPtr<SimpleIndex> index) 245 const std::string& key)
244 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), 246 : index_(index),
245 path_(synchronous_entry->path()), 247 path_(path),
246 key_(synchronous_entry->key()), 248 key_(key),
247 synchronous_entry_(synchronous_entry), 249 synchronous_entry_(NULL),
248 synchronous_entry_in_use_by_worker_(false), 250 synchronous_entry_in_use_by_worker_(false) {
249 index_(index) {
250 DCHECK(synchronous_entry);
251 SetSynchronousData();
252 } 251 }
253 252
254 SimpleEntryImpl::~SimpleEntryImpl() { 253 SimpleEntryImpl::~SimpleEntryImpl() {
255 DCHECK(io_thread_checker_.CalledOnValidThread()); 254 DCHECK(io_thread_checker_.CalledOnValidThread());
255 if (synchronous_entry_) {
256 WorkerPool::PostTask(FROM_HERE,
gavinp 2013/04/17 11:02:21 Wait, worker pool can't post to worker pool.
Philippe 2013/04/17 12:04:46 I don't think you will be able to use a RefCounted
257 base::Bind(&SimpleSynchronousEntry::Close,
258 base::Unretained(synchronous_entry_)),
259 true);
260 }
256 } 261 }
257 262
258 // static
259 void SimpleEntryImpl::CreationOperationComplete( 263 void SimpleEntryImpl::CreationOperationComplete(
260 WeakPtr<SimpleIndex> index, 264 Entry** out_entry,
261 const CompletionCallback& completion_callback, 265 const CompletionCallback& completion_callback,
262 const std::string& key,
263 Entry** out_entry,
264 SimpleSynchronousEntry* sync_entry) { 266 SimpleSynchronousEntry* sync_entry) {
267 DCHECK(io_thread_checker_.CalledOnValidThread());
265 if (!sync_entry) { 268 if (!sync_entry) {
266 completion_callback.Run(net::ERR_FAILED); 269 completion_callback.Run(net::ERR_FAILED);
267 // If OpenEntry failed, we must remove it from our index. 270 // If OpenEntry failed, we must remove it from our index.
268 if (index) 271 index_->Remove(key_);
269 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 index_->Insert(key_);
282 *out_entry = this;
275 completion_callback.Run(net::OK); 283 completion_callback.Run(net::OK);
276 } 284 }
277 285
278 // static
279 void SimpleEntryImpl::EntryOperationComplete( 286 void SimpleEntryImpl::EntryOperationComplete(
280 base::WeakPtr<SimpleIndex> index,
281 const CompletionCallback& completion_callback, 287 const CompletionCallback& completion_callback,
282 base::WeakPtr<SimpleEntryImpl> entry,
283 SimpleSynchronousEntry* sync_entry,
284 int result) { 288 int result) {
285 DCHECK(sync_entry); 289 DCHECK(io_thread_checker_.CalledOnValidThread());
286 if (index) { 290 DCHECK(synchronous_entry_);
287 if (result >= 0) 291 DCHECK(synchronous_entry_in_use_by_worker_);
288 index->UpdateEntrySize(sync_entry->key(), sync_entry->GetFileSize()); 292 synchronous_entry_in_use_by_worker_ = false;
289 else 293 SetSynchronousData();
290 index->Remove(sync_entry->key()); 294 if (result >= 0) {
291 } 295 index_->UpdateEntrySize(synchronous_entry_->key(),
292 296 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 { 297 } else {
298 // |entry| must have had Close() called while this operation was in flight. 298 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 } 299 }
306 completion_callback.Run(result); 300 completion_callback.Run(result);
307 } 301 }
308 302
309 void SimpleEntryImpl::SetSynchronousData() { 303 void SimpleEntryImpl::SetSynchronousData() {
310 DCHECK(io_thread_checker_.CalledOnValidThread()); 304 DCHECK(io_thread_checker_.CalledOnValidThread());
311 DCHECK(!synchronous_entry_in_use_by_worker_); 305 DCHECK(!synchronous_entry_in_use_by_worker_);
312 // TODO(felipeg): These copies to avoid data races are not optimal. While 306 // 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 307 // 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 308 // in that structure. This also solves problems with last_used() on ext4
315 // filesystems not being accurate. 309 // filesystems not being accurate.
316 last_used_ = synchronous_entry_->last_used(); 310 last_used_ = synchronous_entry_->last_used();
317 last_modified_ = synchronous_entry_->last_modified(); 311 last_modified_ = synchronous_entry_->last_modified();
318 for (int i = 0; i < kSimpleEntryFileCount; ++i) 312 for (int i = 0; i < kSimpleEntryFileCount; ++i)
319 data_size_[i] = synchronous_entry_->data_size(i); 313 data_size_[i] = synchronous_entry_->data_size(i);
320 } 314 }
321 315
322 } // namespace disk_cache 316 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_entry_impl.h ('k') | net/disk_cache/simple/simple_index.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698