File: delete_page_task.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,138 bytes parent folder | download | duplicates (11)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_OFFLINE_PAGES_CORE_MODEL_DELETE_PAGE_TASK_H_
#define COMPONENTS_OFFLINE_PAGES_CORE_MODEL_DELETE_PAGE_TASK_H_

#include <memory>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/offline_pages/core/offline_page_metadata_store.h"
#include "components/offline_pages/core/offline_page_model.h"
#include "components/offline_pages/core/offline_page_types.h"
#include "components/offline_pages/task/task.h"

namespace sql {
class Database;
}  // namespace sql

namespace offline_pages {

class OfflinePageMetadataStore;

// Task that deletes pages from the metadata store. It takes the store and
// archive manager for deleting entries from database and file system. Also the
// task needs to be constructed with a DeleteFunction, which defines which pages
// are going to be deleted.
// The caller needs to provide a callback which takes a vector of pages that are
// deleted, along with a DeletePageResult.
// The tasks have to be created by using the static CreateTask* methods.
class DeletePageTask : public Task {
 public:
  struct DeletePageTaskResult;
  using DeletePageTaskCallback =
      base::OnceCallback<void(DeletePageResult,
                              const std::vector<OfflinePageItem>&)>;

  static std::unique_ptr<DeletePageTask> CreateTaskWithCriteria(
      OfflinePageMetadataStore* store,
      const PageCriteria& criteria,
      DeletePageTask::DeletePageTaskCallback callback);

  // Creates a task to delete pages which satisfy |predicate|.
  static std::unique_ptr<DeletePageTask>
  CreateTaskMatchingUrlPredicateForCachedPages(
      OfflinePageMetadataStore* store,
      DeletePageTask::DeletePageTaskCallback callback,
      const UrlPredicate& predicate);

  // Creates a task to delete old pages that have the same url and namespace
  // with |page| to make the number of pages with same url less than the limit
  // defined with the namespace that this |page| belongs to.
  // Returns nullptr if there's no page limit per url of the page's namespace.
  static std::unique_ptr<DeletePageTask> CreateTaskDeletingForPageLimit(
      OfflinePageMetadataStore* store,
      DeletePageTask::DeletePageTaskCallback callback,
      const OfflinePageItem& page);

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

  ~DeletePageTask() override;

  // Deletes a single page from the database. This function reads
  // from the database and should be called from within an
  // |SqlStoreBase::Execute()| call.
  static bool DeletePageFromDbSync(int64_t offline_id, sql::Database* db);
  // Deletes all pages with matching offline_ids from the database. Returns
  // false and aborts if a page could not be deleted. This function reads
  // from the database and should be called from within an
  // |SqlStoreBase::Execute()| call.
  static bool DeletePagesFromDbSync(const std::vector<int64_t>& offline_ids,
                                    sql::Database* db);

 private:
  using DeleteFunction =
      base::OnceCallback<DeletePageTaskResult(sql::Database*)>;

  // Task implementation.
  void Run() override;

  // Making the constructor private, in order to use static methods to create
  // tasks.
  DeletePageTask(OfflinePageMetadataStore* store,
                 DeleteFunction func,
                 DeletePageTaskCallback callback);

  void OnDeletePageDone(DeletePageTaskResult result);
  void OnDeleteArchiveFilesDone(
      std::unique_ptr<OfflinePagesUpdateResult> result,
      bool delete_files_result);
  void InformDeletePageDone(DeletePageResult result);

  // The store to delete pages from. Not owned.
  raw_ptr<OfflinePageMetadataStore> store_;
  // The function which will delete pages.
  DeleteFunction func_;
  DeletePageTaskCallback callback_;

  base::WeakPtrFactory<DeletePageTask> weak_ptr_factory_{this};
};

}  // namespace offline_pages

#endif  // COMPONENTS_OFFLINE_PAGES_CORE_MODEL_DELETE_PAGE_TASK_H_