1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109

content / browser / browsing_data / conditional_cache_deletion_helper.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/browsing_data/conditional_cache_deletion_helper.h"

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/sequence_checker.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/browser/browser_thread.h"

namespace {

bool EntryPredicateFromURLsAndTime(
    const base::RepeatingCallback<bool(const GURL&)>& url_predicate,
    const base::RepeatingCallback<std::string(const std::string&)>&
        get_url_from_key,
    base::Time begin_time,
    base::Time end_time,
    const disk_cache::Entry* entry) {
  std::string url = entry->GetKey();
  if (!get_url_from_key.is_null())
    url = get_url_from_key.Run(url);
  return (entry->GetLastUsed() >= begin_time &&
          entry->GetLastUsed() < end_time && !url.empty() &&
          url_predicate.Run(GURL(url)));
}

}  // namespace

namespace content {

ConditionalCacheDeletionHelper::ConditionalCacheDeletionHelper(
    disk_cache::Backend* cache,
    base::RepeatingCallback<bool(const disk_cache::Entry*)> condition)
    : cache_(cache),
      condition_(std::move(condition)),
      previous_entry_(nullptr) {
}

// static
base::RepeatingCallback<bool(const disk_cache::Entry*)>
ConditionalCacheDeletionHelper::CreateURLAndTimeCondition(
    base::RepeatingCallback<bool(const GURL&)> url_predicate,
    base::RepeatingCallback<std::string(const std::string&)> get_url_from_key,
    base::Time begin_time,
    base::Time end_time) {
  return base::BindRepeating(&EntryPredicateFromURLsAndTime,
                             std::move(url_predicate),
                             std::move(get_url_from_key),
                             begin_time.is_null() ? base::Time() : begin_time,
                             end_time.is_null() ? base::Time::Max() : end_time);
}

int ConditionalCacheDeletionHelper::DeleteAndDestroySelfWhenFinished(
    net::CompletionOnceCallback completion_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  completion_callback_ = std::move(completion_callback);
  iterator_ = cache_->CreateIterator();

  // Any status other than OK (since no entry), IO_PENDING, or FAILED would
  // work here.
  IterateOverEntries(
      disk_cache::EntryResult::MakeError(net::ERR_CACHE_OPEN_FAILURE));

  // DeleteAndDestroySelfWhenFinished() itself is always async since
  // |completion_callback| is always posted and never run directly.
  return net::ERR_IO_PENDING;
}

ConditionalCacheDeletionHelper::~ConditionalCacheDeletionHelper() {}

void ConditionalCacheDeletionHelper::IterateOverEntries(
    disk_cache::EntryResult result) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  while (result.net_error() != net::ERR_IO_PENDING) {
    // If the entry obtained in the previous iteration matches the condition,
    // mark it for deletion. The iterator is already one step forward, so it
    // won't be invalidated. Always close the previous entry so it does not
    // leak.
    if (previous_entry_) {
      if (condition_.Run(previous_entry_.get()))
        previous_entry_->Doom();
      previous_entry_.ExtractAsDangling()->Close();
    }

    if (result.net_error() == net::ERR_FAILED) {
      // The iteration finished successfully or we can no longer iterate
      // (e.g. the cache was destroyed). We cannot distinguish between the two,
      // but we know that there is nothing more that we can do, so we return OK.
      DCHECK(completion_callback_);
      base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
          FROM_HERE, base::BindOnce(std::move(completion_callback_), net::OK));
      base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
                                                                    this);
      return;
    }

    previous_entry_ = result.ReleaseEntry();
    result = iterator_->OpenNextEntry(
        base::BindOnce(&ConditionalCacheDeletionHelper::IterateOverEntries,
                       base::Unretained(this)));
  }
}

}  // namespace content