File: delete_page_task.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (220 lines) | stat: -rw-r--r-- 8,075 bytes parent folder | download | duplicates (9)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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.

#include "components/offline_pages/core/model/delete_page_task.h"

#include <iterator>
#include <string>
#include <utility>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "components/offline_pages/core/model/get_pages_task.h"
#include "components/offline_pages/core/model/offline_page_model_utils.h"
#include "components/offline_pages/core/offline_page_client_policy.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 "sql/database.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace offline_pages {

struct DeletePageTask::DeletePageTaskResult {
  DeletePageTaskResult() = default;
  DeletePageTaskResult(DeletePageResult result,
                       const std::vector<OfflinePageItem>& deleted_pages)
      : result(result), deleted_pages(deleted_pages) {}
  DeletePageTaskResult(DeletePageTaskResult&& other) = default;
  DeletePageTaskResult(const DeletePageTaskResult& other) = delete;
  ~DeletePageTaskResult() = default;

  DeletePageResult result;
  std::vector<OfflinePageItem> deleted_pages;
};
using DeletePageTaskResult = DeletePageTask::DeletePageTaskResult;

namespace {

// Deletes pages. This will return a DeletePageTaskResult which contains the
// deleted pages (which are successfully deleted from the disk and the store)
// and a DeletePageResult. For each page to be deleted, the deletion will delete
// the archive file first, then database entry, in order to avoid the potential
// issue of leaving archive files behind (and they may be imported later).
// Since the database entry will only be deleted while the associated archive
// file is deleted successfully, there will be no such issue.
DeletePageTaskResult DeletePagesSync(
    sql::Database* db,
    std::vector<OfflinePageItem> pages_to_delete) {
  std::vector<OfflinePageItem> deleted_pages;

  // If there's no page to delete, return an empty list with SUCCESS.
  if (pages_to_delete.size() == 0)
    return {DeletePageResult::SUCCESS, deleted_pages};

  bool any_archive_deleted = false;
  for (auto& item : pages_to_delete) {
    if (base::DeleteFile(item.file_path)) {
      any_archive_deleted = true;
      if (DeletePageTask::DeletePageFromDbSync(item.offline_id, db))
        deleted_pages.push_back(std::move(item));
    }
  }
  // If there're no files deleted, return DEVICE_FAILURE.
  if (!any_archive_deleted)
    return {DeletePageResult::DEVICE_FAILURE, std::move(deleted_pages)};

  return {DeletePageResult::SUCCESS, std::move(deleted_pages)};
}

DeletePageTaskResult DeletePagesWithCriteria(
    const PageCriteria& criteria,
    sql::Database* db) {
  // If you create a transaction but dont Commit() it is automatically
  // rolled back by its destructor when it falls out of scope.
  sql::Transaction transaction(db);
  if (!transaction.Begin())
    return {DeletePageResult::STORE_FAILURE, {}};

  GetPagesTask::ReadResult read_result =
      GetPagesTask::ReadPagesWithCriteriaSync(criteria, db);
  if (!read_result.success)
    return {DeletePageResult::STORE_FAILURE, {}};

  DeletePageTaskResult result =
      DeletePagesSync(db, std::move(read_result.pages));

  if (!transaction.Commit())
    return {DeletePageResult::STORE_FAILURE, {}};
  return result;
}

// Deletes all but |limit| pages that match |criteria|, in the order specified
// by |criteria|.
DeletePageTaskResult DeletePagesForPageLimit(
    const PageCriteria& criteria,
    size_t limit,
    sql::Database* db) {
  // If the namespace can have unlimited pages per url, just return success. In
  // practice, we shouldn't hit this condition.
  if (limit == kUnlimitedPages) {
    DLOG(ERROR) << "DeletePagesForPageLimit called with unlimited limit";
    return {DeletePageResult::SUCCESS, {}};
  }

  // If you create a transaction but dont Commit() it is automatically
  // rolled back by its destructor when it falls out of scope.
  sql::Transaction transaction(db);
  if (!transaction.Begin())
    return {DeletePageResult::STORE_FAILURE, {}};

  GetPagesTask::ReadResult read_result =
      GetPagesTask::ReadPagesWithCriteriaSync(criteria, db);
  if (!read_result.success)
    return {DeletePageResult::STORE_FAILURE, {}};

  if (read_result.pages.size() > limit)
    read_result.pages.resize(read_result.pages.size() - limit);
  else
    read_result.pages.clear();

  DeletePageTaskResult result =
      DeletePagesSync(db, std::move(read_result.pages));

  if (!transaction.Commit())
    return DeletePageTaskResult(DeletePageResult::STORE_FAILURE, {});
  return result;
}

}  // namespace

// static
std::unique_ptr<DeletePageTask> DeletePageTask::CreateTaskWithCriteria(
    OfflinePageMetadataStore* store,
    const PageCriteria& criteria,
    DeletePageTask::DeletePageTaskCallback callback) {
  return std::unique_ptr<DeletePageTask>(new DeletePageTask(
      store, base::BindOnce(&DeletePagesWithCriteria, criteria),
      std::move(callback)));
}

// static
std::unique_ptr<DeletePageTask>
DeletePageTask::CreateTaskMatchingUrlPredicateForCachedPages(
    OfflinePageMetadataStore* store,
    DeletePageTask::DeletePageTaskCallback callback,
    const UrlPredicate& predicate) {
  PageCriteria criteria;
  criteria.lifetime_type = LifetimeType::TEMPORARY;
  criteria.additional_criteria = base::BindRepeating(
      [](const UrlPredicate& predicate, const OfflinePageItem& item) {
        return predicate.Run(item.url);
      },
      predicate);
  return CreateTaskWithCriteria(store, criteria, std::move(callback));
}

// static
std::unique_ptr<DeletePageTask> DeletePageTask::CreateTaskDeletingForPageLimit(
    OfflinePageMetadataStore* store,
    DeletePageTask::DeletePageTaskCallback callback,
    const OfflinePageItem& page) {
  std::string name_space = page.client_id.name_space;
  size_t limit = GetPolicy(name_space).pages_allowed_per_url;
  PageCriteria criteria;
  criteria.url = page.url;
  criteria.client_namespaces = std::vector<std::string>{name_space};
  // Sorting is important here. DeletePagesForPageLimit will delete the results
  // in order, leaving only the last |limit| pages.
  criteria.result_order = PageCriteria::kAscendingAccessTime;
  return base::WrapUnique(new DeletePageTask(
      store, base::BindOnce(&DeletePagesForPageLimit, criteria, limit),
      std::move(callback)));
}

DeletePageTask::DeletePageTask(OfflinePageMetadataStore* store,
                               DeleteFunction func,
                               DeletePageTaskCallback callback)
    : store_(store), func_(std::move(func)), callback_(std::move(callback)) {
  DCHECK(store_);
  DCHECK(!callback_.is_null());
}

DeletePageTask::~DeletePageTask() = default;

void DeletePageTask::Run() {
  store_->Execute(std::move(func_),
                  base::BindOnce(&DeletePageTask::OnDeletePageDone,
                                 weak_ptr_factory_.GetWeakPtr()),
                  DeletePageTaskResult(DeletePageResult::STORE_FAILURE, {}));
}

void DeletePageTask::OnDeletePageDone(DeletePageTaskResult result) {
  std::move(callback_).Run(result.result, std::move(result.deleted_pages));
  TaskComplete();
}

// static
bool DeletePageTask::DeletePageFromDbSync(int64_t offline_id,
                                          sql::Database* db) {
  static const char kSql[] = "DELETE FROM offlinepages_v1 WHERE offline_id=?";
  sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
  statement.BindInt64(0, offline_id);
  return statement.Run();
}

bool DeletePageTask::DeletePagesFromDbSync(
    const std::vector<int64_t>& offline_ids,
    sql::Database* db) {
  for (const auto& offline_id : offline_ids) {
    if (!DeletePageTask::DeletePageFromDbSync(offline_id, db))
      return false;
  }
  return true;
}

}  // namespace offline_pages