1 // Copyright 2018 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 COMPONENTS_OFFLINE_PAGES_CORE_MODEL_PERSISTENT_PAGE_CONSISTENCY_CHECK_TASK_H_
6 #define COMPONENTS_OFFLINE_PAGES_CORE_MODEL_PERSISTENT_PAGE_CONSISTENCY_CHECK_TASK_H_
7 
8 #include <vector>
9 
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "components/offline_pages/core/offline_page_archive_publisher.h"
13 #include "components/offline_pages/core/offline_store_types.h"
14 #include "components/offline_pages/task/task.h"
15 
16 namespace offline_pages {
17 
18 class ArchiveManager;
19 class OfflinePageMetadataStore;
20 
21 // This task is responsible for checking consistency of persistent pages, mark
22 // the expired ones with the file missing time and recover the previously
23 // missing entries back to normal.
24 class PersistentPageConsistencyCheckTask : public Task {
25  public:
26   using PersistentPageConsistencyCheckCallback = base::OnceCallback<void(
27       bool success,
28       const std::vector<PublishedArchiveId>& ids_of_deleted_pages)>;
29 
30   struct CheckResult {
31     CheckResult();
32     CheckResult(SyncOperationResult result,
33                 const std::vector<PublishedArchiveId>& ids_of_deleted_pages);
34     CheckResult(const CheckResult& other);
35     CheckResult& operator=(const CheckResult& other);
36     ~CheckResult();
37 
38     SyncOperationResult result;
39     std::vector<PublishedArchiveId> ids_of_deleted_pages;
40   };
41 
42   PersistentPageConsistencyCheckTask(
43       OfflinePageMetadataStore* store,
44       ArchiveManager* archive_manager,
45       base::Time check_time,
46       PersistentPageConsistencyCheckCallback callback);
47   ~PersistentPageConsistencyCheckTask() override;
48 
49  private:
50   // Task implementation:
51   void Run() override;
52 
53   void OnPersistentPageConsistencyCheckDone(CheckResult result);
54 
55   // The store containing the offline pages. Not owned.
56   OfflinePageMetadataStore* store_;
57   // The archive manager storing archive directories. Not owned.
58   ArchiveManager* archive_manager_;
59   base::Time check_time_;
60   // The callback for the task.
61   PersistentPageConsistencyCheckCallback callback_;
62 
63   base::WeakPtrFactory<PersistentPageConsistencyCheckTask> weak_ptr_factory_{
64       this};
65   DISALLOW_COPY_AND_ASSIGN(PersistentPageConsistencyCheckTask);
66 };
67 
68 }  // namespace offline_pages
69 
70 #endif  // COMPONENTS_OFFLINE_PAGES_CORE_MODEL_PERSISTENT_PAGE_CONSISTENCY_CHECK_TASK_H_
71