1 // Copyright 2019 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.
4 
5 #ifndef CHROME_BROWSER_CHROMEOS_PRINTING_HISTORY_PRINT_JOB_HISTORY_CLEANER_H_
6 #define CHROME_BROWSER_CHROMEOS_PRINTING_HISTORY_PRINT_JOB_HISTORY_CLEANER_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/chromeos/printing/history/print_job_database.h"
11 
12 class PrefService;
13 
14 namespace base {
15 class Clock;
16 }  // namespace base
17 
18 namespace chromeos {
19 
20 class PrintJobHistoryCleaner {
21  public:
22   // The default amount of time the metadata of completed print job is stored on
23   // the device.
24   static constexpr int kDefaultPrintJobHistoryExpirationPeriodDays = 90;
25 
26   PrintJobHistoryCleaner(PrintJobDatabase* print_job_database,
27                          PrefService* pref_service);
28   ~PrintJobHistoryCleaner();
29 
30   // Removes expired print jobs from the database.
31   // The expiration period is controlled by
32   // |prefs::kPrintJobHistoryExpirationPeriod| pref.
33   // |callback| is called after all expired print jobs are removed from the
34   // database.
35   void CleanUp(base::OnceClosure callback);
36 
37   void SetClockForTesting(const base::Clock* clock);
38 
39  private:
40   void OnPrefServiceInitialized(base::OnceClosure callback, bool success);
41   void OnPrintJobsRetrieved(
42       base::OnceClosure callback,
43       bool success,
44       std::vector<printing::proto::PrintJobInfo> print_job_infos);
45   void OnPrintJobsDeleted(base::OnceClosure callback, bool success);
46 
47   // This object is owned by PrintJobHistoryService and outlives
48   // PrintJobHistoryCleaner.
49   PrintJobDatabase* print_job_database_;
50 
51   PrefService* pref_service_;
52 
53   // Points to the base::DefaultClock by default.
54   const base::Clock* clock_;
55 
56   // Stores the completion time of the oldest print job in the database or the
57   // time of last cleanup.
58   // This is used to determine whether we need to run real cleanup or not.
59   base::Time oldest_print_job_completion_time_;
60 
61   base::WeakPtrFactory<PrintJobHistoryCleaner> weak_ptr_factory_{this};
62 
63   DISALLOW_COPY_AND_ASSIGN(PrintJobHistoryCleaner);
64 };
65 
66 }  // namespace chromeos
67 
68 #endif  // CHROME_BROWSER_CHROMEOS_PRINTING_HISTORY_PRINT_JOB_HISTORY_CLEANER_H_
69