summaryrefslogtreecommitdiff
path: root/chromium/components/offline_pages/core/prefetch/finalize_dismissed_url_suggestion_task.cc
blob: 77a07866857423bea522e337abac7ea8da801a0c (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
67
68
69
70
// Copyright 2017 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/prefetch/finalize_dismissed_url_suggestion_task.h"

#include <memory>

#include "base/bind.h"
#include "components/offline_pages/core/client_id.h"
#include "components/offline_pages/core/prefetch/store/prefetch_store.h"
#include "sql/connection.h"
#include "sql/statement.h"

namespace offline_pages {

namespace {

bool DeletePageByClientIdIfNotDownloadedSync(const ClientId& client_id,
                                             sql::Connection* db) {
  if (!db)
    return false;

  static const std::array<PrefetchItemState, 6>& finalizable_states =
      FinalizeDismissedUrlSuggestionTask::kFinalizableStates;

  static const char kSql[] =
      "UPDATE prefetch_items SET state = ?, error_code = ?"
      " WHERE client_id = ? AND client_namespace = ? "
      " AND state IN (?, ?, ?, ?, ?, ?)";

  sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql));
  statement.BindInt(0, static_cast<int>(PrefetchItemState::FINISHED));
  statement.BindInt(
      1, static_cast<int>(PrefetchItemErrorCode::SUGGESTION_INVALIDATED));
  statement.BindString(2, client_id.id);
  statement.BindString(3, client_id.name_space);
  for (size_t i = 0; i < finalizable_states.size(); ++i)
    statement.BindInt(4 + i, static_cast<int>(finalizable_states[i]));
  return statement.Run();
}
}  // namespace

// static
constexpr std::array<PrefetchItemState, 6>
    FinalizeDismissedUrlSuggestionTask::kFinalizableStates;

FinalizeDismissedUrlSuggestionTask::FinalizeDismissedUrlSuggestionTask(
    PrefetchStore* prefetch_store,
    const ClientId& client_id)
    : prefetch_store_(prefetch_store),
      client_id_(client_id),
      weak_ptr_factory_(this) {
  DCHECK(prefetch_store_);
}

FinalizeDismissedUrlSuggestionTask::~FinalizeDismissedUrlSuggestionTask() {}

void FinalizeDismissedUrlSuggestionTask::Run() {
  prefetch_store_->Execute<bool>(
      base::BindOnce(&DeletePageByClientIdIfNotDownloadedSync, client_id_),
      base::BindOnce(&FinalizeDismissedUrlSuggestionTask::OnComplete,
                     weak_ptr_factory_.GetWeakPtr()));
}

void FinalizeDismissedUrlSuggestionTask::OnComplete(bool result) {
  TaskComplete();
}

}  // namespace offline_pages