Delete unmasked credit cards when clearing data.
[chromium-blink-merge.git] / net / disk_cache / blockfile / in_flight_backend_io.cc
blob5b3b90430390fa82e83b2b2b1be44b2a0e091e22
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/blockfile/in_flight_backend_io.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "base/single_thread_task_runner.h"
13 #include "net/base/net_errors.h"
14 #include "net/disk_cache/blockfile/backend_impl.h"
15 #include "net/disk_cache/blockfile/entry_impl.h"
16 #include "net/disk_cache/blockfile/histogram_macros.h"
18 // Provide a BackendImpl object to macros from histogram_macros.h.
19 #define CACHE_UMA_BACKEND_IMPL_OBJ backend_
21 namespace disk_cache {
23 BackendIO::BackendIO(InFlightIO* controller, BackendImpl* backend,
24 const net::CompletionCallback& callback)
25 : BackgroundIO(controller),
26 backend_(backend),
27 callback_(callback),
28 operation_(OP_NONE),
29 entry_ptr_(NULL),
30 iterator_(NULL),
31 entry_(NULL),
32 index_(0),
33 offset_(0),
34 buf_len_(0),
35 truncate_(false),
36 offset64_(0),
37 start_(NULL) {
38 start_time_ = base::TimeTicks::Now();
41 // Runs on the background thread.
42 void BackendIO::ExecuteOperation() {
43 if (IsEntryOperation())
44 return ExecuteEntryOperation();
46 ExecuteBackendOperation();
49 // Runs on the background thread.
50 void BackendIO::OnIOComplete(int result) {
51 DCHECK(IsEntryOperation());
52 DCHECK_NE(result, net::ERR_IO_PENDING);
53 result_ = result;
54 NotifyController();
57 // Runs on the primary thread.
58 void BackendIO::OnDone(bool cancel) {
59 if (IsEntryOperation()) {
60 CACHE_UMA(TIMES, "TotalIOTime", 0, ElapsedTime());
63 if (!ReturnsEntry())
64 return;
66 if (result() == net::OK) {
67 static_cast<EntryImpl*>(*entry_ptr_)->OnEntryCreated(backend_);
68 if (cancel) {
69 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is
70 // fixed.
71 tracked_objects::ScopedTracker tracking_profile(
72 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 BackendIO::OnDone"));
74 (*entry_ptr_)->Close();
79 bool BackendIO::IsEntryOperation() {
80 return operation_ > OP_MAX_BACKEND;
83 // Runs on the background thread.
84 void BackendIO::ReferenceEntry() {
85 entry_->AddRef();
88 void BackendIO::Init() {
89 operation_ = OP_INIT;
92 void BackendIO::OpenEntry(const std::string& key, Entry** entry) {
93 operation_ = OP_OPEN;
94 key_ = key;
95 entry_ptr_ = entry;
98 void BackendIO::CreateEntry(const std::string& key, Entry** entry) {
99 operation_ = OP_CREATE;
100 key_ = key;
101 entry_ptr_ = entry;
104 void BackendIO::DoomEntry(const std::string& key) {
105 operation_ = OP_DOOM;
106 key_ = key;
109 void BackendIO::DoomAllEntries() {
110 operation_ = OP_DOOM_ALL;
113 void BackendIO::DoomEntriesBetween(const base::Time initial_time,
114 const base::Time end_time) {
115 operation_ = OP_DOOM_BETWEEN;
116 initial_time_ = initial_time;
117 end_time_ = end_time;
120 void BackendIO::DoomEntriesSince(const base::Time initial_time) {
121 operation_ = OP_DOOM_SINCE;
122 initial_time_ = initial_time;
125 void BackendIO::OpenNextEntry(Rankings::Iterator* iterator,
126 Entry** next_entry) {
127 operation_ = OP_OPEN_NEXT;
128 iterator_ = iterator;
129 entry_ptr_ = next_entry;
132 void BackendIO::EndEnumeration(scoped_ptr<Rankings::Iterator> iterator) {
133 operation_ = OP_END_ENUMERATION;
134 scoped_iterator_ = iterator.Pass();
137 void BackendIO::OnExternalCacheHit(const std::string& key) {
138 operation_ = OP_ON_EXTERNAL_CACHE_HIT;
139 key_ = key;
142 void BackendIO::CloseEntryImpl(EntryImpl* entry) {
143 operation_ = OP_CLOSE_ENTRY;
144 entry_ = entry;
147 void BackendIO::DoomEntryImpl(EntryImpl* entry) {
148 operation_ = OP_DOOM_ENTRY;
149 entry_ = entry;
152 void BackendIO::FlushQueue() {
153 operation_ = OP_FLUSH_QUEUE;
156 void BackendIO::RunTask(const base::Closure& task) {
157 operation_ = OP_RUN_TASK;
158 task_ = task;
161 void BackendIO::ReadData(EntryImpl* entry, int index, int offset,
162 net::IOBuffer* buf, int buf_len) {
163 operation_ = OP_READ;
164 entry_ = entry;
165 index_ = index;
166 offset_ = offset;
167 buf_ = buf;
168 buf_len_ = buf_len;
171 void BackendIO::WriteData(EntryImpl* entry, int index, int offset,
172 net::IOBuffer* buf, int buf_len, bool truncate) {
173 operation_ = OP_WRITE;
174 entry_ = entry;
175 index_ = index;
176 offset_ = offset;
177 buf_ = buf;
178 buf_len_ = buf_len;
179 truncate_ = truncate;
182 void BackendIO::ReadSparseData(EntryImpl* entry, int64 offset,
183 net::IOBuffer* buf, int buf_len) {
184 operation_ = OP_READ_SPARSE;
185 entry_ = entry;
186 offset64_ = offset;
187 buf_ = buf;
188 buf_len_ = buf_len;
191 void BackendIO::WriteSparseData(EntryImpl* entry, int64 offset,
192 net::IOBuffer* buf, int buf_len) {
193 operation_ = OP_WRITE_SPARSE;
194 entry_ = entry;
195 offset64_ = offset;
196 buf_ = buf;
197 buf_len_ = buf_len;
200 void BackendIO::GetAvailableRange(EntryImpl* entry, int64 offset, int len,
201 int64* start) {
202 operation_ = OP_GET_RANGE;
203 entry_ = entry;
204 offset64_ = offset;
205 buf_len_ = len;
206 start_ = start;
209 void BackendIO::CancelSparseIO(EntryImpl* entry) {
210 operation_ = OP_CANCEL_IO;
211 entry_ = entry;
214 void BackendIO::ReadyForSparseIO(EntryImpl* entry) {
215 operation_ = OP_IS_READY;
216 entry_ = entry;
219 BackendIO::~BackendIO() {}
221 bool BackendIO::ReturnsEntry() {
222 return operation_ == OP_OPEN || operation_ == OP_CREATE ||
223 operation_ == OP_OPEN_NEXT;
226 base::TimeDelta BackendIO::ElapsedTime() const {
227 return base::TimeTicks::Now() - start_time_;
230 // Runs on the background thread.
231 void BackendIO::ExecuteBackendOperation() {
232 switch (operation_) {
233 case OP_INIT:
234 result_ = backend_->SyncInit();
235 break;
236 case OP_OPEN:
237 result_ = backend_->SyncOpenEntry(key_, entry_ptr_);
238 break;
239 case OP_CREATE:
240 result_ = backend_->SyncCreateEntry(key_, entry_ptr_);
241 break;
242 case OP_DOOM:
243 result_ = backend_->SyncDoomEntry(key_);
244 break;
245 case OP_DOOM_ALL:
246 result_ = backend_->SyncDoomAllEntries();
247 break;
248 case OP_DOOM_BETWEEN:
249 result_ = backend_->SyncDoomEntriesBetween(initial_time_, end_time_);
250 break;
251 case OP_DOOM_SINCE:
252 result_ = backend_->SyncDoomEntriesSince(initial_time_);
253 break;
254 case OP_OPEN_NEXT:
255 result_ = backend_->SyncOpenNextEntry(iterator_, entry_ptr_);
256 break;
257 case OP_END_ENUMERATION:
258 backend_->SyncEndEnumeration(scoped_iterator_.Pass());
259 result_ = net::OK;
260 break;
261 case OP_ON_EXTERNAL_CACHE_HIT:
262 backend_->SyncOnExternalCacheHit(key_);
263 result_ = net::OK;
264 break;
265 case OP_CLOSE_ENTRY:
266 entry_->Release();
267 result_ = net::OK;
268 break;
269 case OP_DOOM_ENTRY:
270 entry_->DoomImpl();
271 result_ = net::OK;
272 break;
273 case OP_FLUSH_QUEUE:
274 result_ = net::OK;
275 break;
276 case OP_RUN_TASK:
277 task_.Run();
278 result_ = net::OK;
279 break;
280 default:
281 NOTREACHED() << "Invalid Operation";
282 result_ = net::ERR_UNEXPECTED;
284 DCHECK_NE(net::ERR_IO_PENDING, result_);
285 NotifyController();
288 // Runs on the background thread.
289 void BackendIO::ExecuteEntryOperation() {
290 switch (operation_) {
291 case OP_READ:
292 result_ =
293 entry_->ReadDataImpl(index_, offset_, buf_.get(), buf_len_,
294 base::Bind(&BackendIO::OnIOComplete, this));
295 break;
296 case OP_WRITE:
297 result_ =
298 entry_->WriteDataImpl(index_, offset_, buf_.get(), buf_len_,
299 base::Bind(&BackendIO::OnIOComplete, this),
300 truncate_);
301 break;
302 case OP_READ_SPARSE:
303 result_ = entry_->ReadSparseDataImpl(
304 offset64_, buf_.get(), buf_len_,
305 base::Bind(&BackendIO::OnIOComplete, this));
306 break;
307 case OP_WRITE_SPARSE:
308 result_ = entry_->WriteSparseDataImpl(
309 offset64_, buf_.get(), buf_len_,
310 base::Bind(&BackendIO::OnIOComplete, this));
311 break;
312 case OP_GET_RANGE:
313 result_ = entry_->GetAvailableRangeImpl(offset64_, buf_len_, start_);
314 break;
315 case OP_CANCEL_IO:
316 entry_->CancelSparseIOImpl();
317 result_ = net::OK;
318 break;
319 case OP_IS_READY:
320 result_ = entry_->ReadyForSparseIOImpl(
321 base::Bind(&BackendIO::OnIOComplete, this));
322 break;
323 default:
324 NOTREACHED() << "Invalid Operation";
325 result_ = net::ERR_UNEXPECTED;
327 buf_ = NULL;
328 if (result_ != net::ERR_IO_PENDING)
329 NotifyController();
332 InFlightBackendIO::InFlightBackendIO(
333 BackendImpl* backend,
334 const scoped_refptr<base::SingleThreadTaskRunner>& background_thread)
335 : backend_(backend),
336 background_thread_(background_thread),
337 ptr_factory_(this) {
340 InFlightBackendIO::~InFlightBackendIO() {
343 void InFlightBackendIO::Init(const net::CompletionCallback& callback) {
344 // TODO(vadimt): Remove wrapping the callback with
345 // ScopedTracker::TrackCallback() once crbug.com/422516 is fixed.
346 scoped_refptr<BackendIO> operation(new BackendIO(
347 this, backend_,
348 tracked_objects::ScopedTracker::TrackCallback(
349 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::Init"),
350 callback)));
351 operation->Init();
352 PostOperation(operation.get());
355 void InFlightBackendIO::OpenEntry(const std::string& key, Entry** entry,
356 const net::CompletionCallback& callback) {
357 scoped_refptr<BackendIO> operation(new BackendIO(
358 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
359 FROM_HERE_WITH_EXPLICIT_FUNCTION(
360 "422516 InFlightBackendIO::OpenEntry"),
361 callback)));
362 operation->OpenEntry(key, entry);
363 PostOperation(operation.get());
366 void InFlightBackendIO::CreateEntry(const std::string& key, Entry** entry,
367 const net::CompletionCallback& callback) {
368 scoped_refptr<BackendIO> operation(new BackendIO(
369 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
370 FROM_HERE_WITH_EXPLICIT_FUNCTION(
371 "422516 InFlightBackendIO::CreateEntry"),
372 callback)));
373 operation->CreateEntry(key, entry);
374 PostOperation(operation.get());
377 void InFlightBackendIO::DoomEntry(const std::string& key,
378 const net::CompletionCallback& callback) {
379 scoped_refptr<BackendIO> operation(new BackendIO(
380 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
381 FROM_HERE_WITH_EXPLICIT_FUNCTION(
382 "422516 InFlightBackendIO::DoomEntry"),
383 callback)));
384 operation->DoomEntry(key);
385 PostOperation(operation.get());
388 void InFlightBackendIO::DoomAllEntries(
389 const net::CompletionCallback& callback) {
390 scoped_refptr<BackendIO> operation(new BackendIO(
391 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
392 FROM_HERE_WITH_EXPLICIT_FUNCTION(
393 "422516 InFlightBackendIO::DoomAllEntries"),
394 callback)));
395 operation->DoomAllEntries();
396 PostOperation(operation.get());
399 void InFlightBackendIO::DoomEntriesBetween(const base::Time initial_time,
400 const base::Time end_time,
401 const net::CompletionCallback& callback) {
402 scoped_refptr<BackendIO> operation(new BackendIO(
403 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
404 FROM_HERE_WITH_EXPLICIT_FUNCTION(
405 "422516 InFlightBackendIO::DoomEntriesBetween"),
406 callback)));
407 operation->DoomEntriesBetween(initial_time, end_time);
408 PostOperation(operation.get());
411 void InFlightBackendIO::DoomEntriesSince(
412 const base::Time initial_time, const net::CompletionCallback& callback) {
413 scoped_refptr<BackendIO> operation(new BackendIO(
414 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
415 FROM_HERE_WITH_EXPLICIT_FUNCTION(
416 "422516 InFlightBackendIO::DoomEntriesSince"),
417 callback)));
418 operation->DoomEntriesSince(initial_time);
419 PostOperation(operation.get());
422 void InFlightBackendIO::OpenNextEntry(Rankings::Iterator* iterator,
423 Entry** next_entry,
424 const net::CompletionCallback& callback) {
425 scoped_refptr<BackendIO> operation(new BackendIO(
426 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
427 FROM_HERE_WITH_EXPLICIT_FUNCTION(
428 "422516 InFlightBackendIO::OpenNextEntry"),
429 callback)));
430 operation->OpenNextEntry(iterator, next_entry);
431 PostOperation(operation.get());
434 void InFlightBackendIO::EndEnumeration(
435 scoped_ptr<Rankings::Iterator> iterator) {
436 scoped_refptr<BackendIO> operation(
437 new BackendIO(this, backend_, net::CompletionCallback()));
438 operation->EndEnumeration(iterator.Pass());
439 PostOperation(operation.get());
442 void InFlightBackendIO::OnExternalCacheHit(const std::string& key) {
443 scoped_refptr<BackendIO> operation(
444 new BackendIO(this, backend_, net::CompletionCallback()));
445 operation->OnExternalCacheHit(key);
446 PostOperation(operation.get());
449 void InFlightBackendIO::CloseEntryImpl(EntryImpl* entry) {
450 scoped_refptr<BackendIO> operation(
451 new BackendIO(this, backend_, net::CompletionCallback()));
452 operation->CloseEntryImpl(entry);
453 PostOperation(operation.get());
456 void InFlightBackendIO::DoomEntryImpl(EntryImpl* entry) {
457 scoped_refptr<BackendIO> operation(
458 new BackendIO(this, backend_, net::CompletionCallback()));
459 operation->DoomEntryImpl(entry);
460 PostOperation(operation.get());
463 void InFlightBackendIO::FlushQueue(const net::CompletionCallback& callback) {
464 scoped_refptr<BackendIO> operation(new BackendIO(
465 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
466 FROM_HERE_WITH_EXPLICIT_FUNCTION(
467 "422516 InFlightBackendIO::FlushQueue"),
468 callback)));
469 operation->FlushQueue();
470 PostOperation(operation.get());
473 void InFlightBackendIO::RunTask(
474 const base::Closure& task, const net::CompletionCallback& callback) {
475 scoped_refptr<BackendIO> operation(new BackendIO(
476 this, backend_,
477 tracked_objects::ScopedTracker::TrackCallback(
478 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 InFlightBackendIO::RunTask"),
479 callback)));
480 operation->RunTask(task);
481 PostOperation(operation.get());
484 void InFlightBackendIO::ReadData(EntryImpl* entry, int index, int offset,
485 net::IOBuffer* buf, int buf_len,
486 const net::CompletionCallback& callback) {
487 scoped_refptr<BackendIO> operation(new BackendIO(
488 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
489 FROM_HERE_WITH_EXPLICIT_FUNCTION(
490 "422516 InFlightBackendIO::ReadData"),
491 callback)));
492 operation->ReadData(entry, index, offset, buf, buf_len);
493 PostOperation(operation.get());
496 void InFlightBackendIO::WriteData(EntryImpl* entry, int index, int offset,
497 net::IOBuffer* buf, int buf_len,
498 bool truncate,
499 const net::CompletionCallback& callback) {
500 scoped_refptr<BackendIO> operation(new BackendIO(
501 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
502 FROM_HERE_WITH_EXPLICIT_FUNCTION(
503 "422516 InFlightBackendIO::WriteData"),
504 callback)));
505 operation->WriteData(entry, index, offset, buf, buf_len, truncate);
506 PostOperation(operation.get());
509 void InFlightBackendIO::ReadSparseData(
510 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len,
511 const net::CompletionCallback& callback) {
512 scoped_refptr<BackendIO> operation(new BackendIO(
513 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
514 FROM_HERE_WITH_EXPLICIT_FUNCTION(
515 "422516 InFlightBackendIO::ReadSparseData"),
516 callback)));
517 operation->ReadSparseData(entry, offset, buf, buf_len);
518 PostOperation(operation.get());
521 void InFlightBackendIO::WriteSparseData(
522 EntryImpl* entry, int64 offset, net::IOBuffer* buf, int buf_len,
523 const net::CompletionCallback& callback) {
524 scoped_refptr<BackendIO> operation(new BackendIO(
525 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
526 FROM_HERE_WITH_EXPLICIT_FUNCTION(
527 "422516 InFlightBackendIO::WriteSparseData"),
528 callback)));
529 operation->WriteSparseData(entry, offset, buf, buf_len);
530 PostOperation(operation.get());
533 void InFlightBackendIO::GetAvailableRange(
534 EntryImpl* entry, int64 offset, int len, int64* start,
535 const net::CompletionCallback& callback) {
536 scoped_refptr<BackendIO> operation(new BackendIO(
537 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
538 FROM_HERE_WITH_EXPLICIT_FUNCTION(
539 "422516 InFlightBackendIO::GetAvailableRange"),
540 callback)));
541 operation->GetAvailableRange(entry, offset, len, start);
542 PostOperation(operation.get());
545 void InFlightBackendIO::CancelSparseIO(EntryImpl* entry) {
546 scoped_refptr<BackendIO> operation(
547 new BackendIO(this, backend_, net::CompletionCallback()));
548 operation->CancelSparseIO(entry);
549 PostOperation(operation.get());
552 void InFlightBackendIO::ReadyForSparseIO(
553 EntryImpl* entry, const net::CompletionCallback& callback) {
554 scoped_refptr<BackendIO> operation(new BackendIO(
555 this, backend_, tracked_objects::ScopedTracker::TrackCallback(
556 FROM_HERE_WITH_EXPLICIT_FUNCTION(
557 "422516 InFlightBackendIO::CancelSparseIO"),
558 callback)));
559 operation->ReadyForSparseIO(entry);
560 PostOperation(operation.get());
563 void InFlightBackendIO::WaitForPendingIO() {
564 InFlightIO::WaitForPendingIO();
567 void InFlightBackendIO::OnOperationComplete(BackgroundIO* operation,
568 bool cancel) {
569 BackendIO* op = static_cast<BackendIO*>(operation);
570 op->OnDone(cancel);
572 if (!op->callback().is_null() && (!cancel || op->IsEntryOperation())) {
573 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
574 tracked_objects::ScopedTracker tracking_profile(
575 FROM_HERE_WITH_EXPLICIT_FUNCTION(
576 "422516 InFlightBackendIO::OnOperationComplete"));
578 op->callback().Run(op->result());
582 void InFlightBackendIO::PostOperation(BackendIO* operation) {
583 background_thread_->PostTask(
584 FROM_HERE, base::Bind(&BackendIO::ExecuteOperation, operation));
585 OnOperationPosted(operation);
588 base::WeakPtr<InFlightBackendIO> InFlightBackendIO::GetWeakPtr() {
589 return ptr_factory_.GetWeakPtr();
592 } // namespace