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
  110
  111

content / public / test / browsing_data_remover_test_util.h [blame]

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

#ifndef CONTENT_PUBLIC_TEST_BROWSING_DATA_REMOVER_TEST_UTIL_H_
#define CONTENT_PUBLIC_TEST_BROWSING_DATA_REMOVER_TEST_UTIL_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/task/sequenced_task_runner.h"
#include "content/public/browser/browsing_data_remover.h"

namespace content {

// This class can be used to wait for a BrowsingDataRemover to complete
// operation. It is not suitable for repeated use.
class BrowsingDataRemoverCompletionObserver
    : public BrowsingDataRemover::Observer {
 public:
  explicit BrowsingDataRemoverCompletionObserver(BrowsingDataRemover* remover);

  BrowsingDataRemoverCompletionObserver(
      const BrowsingDataRemoverCompletionObserver&) = delete;
  BrowsingDataRemoverCompletionObserver& operator=(
      const BrowsingDataRemoverCompletionObserver&) = delete;

  ~BrowsingDataRemoverCompletionObserver() override;

  void BlockUntilCompletion();

  bool browsing_data_remover_done() { return browsing_data_remover_done_; }

  uint64_t failed_data_types() { return failed_data_types_; }

 protected:
  // BrowsingDataRemover::Observer:
  void OnBrowsingDataRemoverDone(uint64_t failed_data_types) override;

 private:
  void FlushForTestingComplete();
  void QuitRunLoopWhenTasksComplete();

  // Tracks when the Task Scheduler task flushing is done.
  bool flush_for_testing_complete_ = false;

  // Tracks when BrowsingDataRemover::Observer::OnBrowsingDataRemoverDone() is
  // called.
  bool browsing_data_remover_done_ = false;

  // Stores the |failed_data_types| mask passed into
  // OnBrowsingDataRemoverDone().
  uint64_t failed_data_types_ = 0;

  base::RunLoop run_loop_;
  base::ScopedObservation<BrowsingDataRemover, BrowsingDataRemover::Observer>
      observation_{this};
  scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
};

// The completion inhibitor can artificially delay completion of the browsing
// data removal process. It is used during testing to simulate scenarios in
// which the deletion stalls or takes a very long time.
//
// This class will detach itself from |remover| upon its destruction.
// If |remover| is destroyed during a test (e.g. in profile shutdown tests),
// users must call Reset() to detach it in advance.
class BrowsingDataRemoverCompletionInhibitor {
 public:
  explicit BrowsingDataRemoverCompletionInhibitor(BrowsingDataRemover* remover);

  BrowsingDataRemoverCompletionInhibitor(
      const BrowsingDataRemoverCompletionInhibitor&) = delete;
  BrowsingDataRemoverCompletionInhibitor& operator=(
      const BrowsingDataRemoverCompletionInhibitor&) = delete;

  virtual ~BrowsingDataRemoverCompletionInhibitor();

  void Reset();

  void BlockUntilNearCompletion();
  void ContinueToCompletion();

 protected:
  virtual void OnBrowsingDataRemoverWouldComplete(
      base::OnceClosure continue_to_completion);

 private:
  void FlushForTestingComplete();
  void QuitRunLoopWhenTasksComplete();

  // Tracks when the Task Scheduler task flushing is done.
  bool flush_for_testing_complete_ = false;

  // Tracks when OnBrowsingDataRemoverWouldComplete() is called.
  bool browsing_data_remover_would_complete_done_ = false;

  // Not owned by this class. If the pointer becomes invalid, the owner of
  // this class is responsible for calling Reset().
  raw_ptr<BrowsingDataRemover> remover_;

  std::unique_ptr<base::RunLoop> run_loop_;
  base::OnceClosure continue_to_completion_callback_;
  scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
};

}  // namespace content

#endif  // CONTENT_PUBLIC_TEST_BROWSING_DATA_REMOVER_TEST_UTIL_H_