summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/model/cleanup_thumbnails_task.cc
blob: dcb00a9d63c8e4d708956d2041c5da3483d2d4fd (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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/cleanup_thumbnails_task.h"

#include "base/metrics/histogram_macros.h"
#include "components/offline_pages/core/offline_page_metadata_store_sql.h"
#include "components/offline_pages/core/offline_store_utils.h"
#include "sql/connection.h"
#include "sql/statement.h"
#include "sql/transaction.h"

namespace offline_pages {

namespace {
typedef base::OnceCallback<void(CleanupThumbnailsTask::Result)> ResultCallback;

CleanupThumbnailsTask::Result CleanupThumbnailsSync(base::Time now,
                                                    sql::Connection* db) {
  if (!db)
    return CleanupThumbnailsTask::Result();
  static const char kSql[] =
      "DELETE FROM page_thumbnails "
      "WHERE offline_id IN ("
      "  SELECT pt.offline_id from page_thumbnails pt"
      "  LEFT OUTER JOIN offlinepages_v1 op"
      "  ON pt.offline_id = op.offline_id "
      "  WHERE op.offline_id IS NULL "
      "  AND pt.expiration < ?"
      ")";
  sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
  statement.BindInt64(0, store_utils::ToDatabaseTime(now));
  if (!statement.Run())
    return CleanupThumbnailsTask::Result();

  return CleanupThumbnailsTask::Result{true, db->GetLastChangeCount()};
}

}  // namespace

CleanupThumbnailsTask::CleanupThumbnailsTask(
    OfflinePageMetadataStoreSQL* store,
    base::Time now,
    CleanupThumbnailsCallback complete_callback)
    : store_(store),
      now_(now),
      complete_callback_(std::move(complete_callback)),
      weak_ptr_factory_(this) {}

CleanupThumbnailsTask::~CleanupThumbnailsTask() = default;

void CleanupThumbnailsTask::Run() {
  store_->Execute(base::BindOnce(CleanupThumbnailsSync, now_),
                  base::BindOnce(&CleanupThumbnailsTask::Complete,
                                 weak_ptr_factory_.GetWeakPtr()));
}

void CleanupThumbnailsTask::Complete(Result result) {
  TaskComplete();
  UMA_HISTOGRAM_COUNTS_1000("OfflinePages.CleanupThumbnails.Count",
                            result.removed_thumbnails);
  std::move(complete_callback_).Run(result.success);
}

}  // namespace offline_pages