File: model_impl.cc

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 (160 lines) | stat: -rw-r--r-- 4,948 bytes parent folder | download | duplicates (5)
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
// 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/download/internal/background_service/model_impl.h"

#include <map>
#include <memory>

#include "base/functional/bind.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "components/download/internal/background_service/entry.h"
#include "components/download/internal/background_service/model_stats.h"

namespace download {

ModelImpl::ModelImpl(std::unique_ptr<Store> store)
    : client_(nullptr), store_(std::move(store)) {
  DCHECK(store_);
}

ModelImpl::~ModelImpl() = default;

void ModelImpl::Initialize(Client* client) {
  DCHECK(!client_);
  client_ = client;
  DCHECK(client_);

  DCHECK(!store_->IsInitialized());
  store_->Initialize(base::BindOnce(&ModelImpl::OnInitializedFinished,
                                    weak_ptr_factory_.GetWeakPtr()));
}

void ModelImpl::HardRecover() {
  entries_.clear();

  store_->HardRecover(base::BindOnce(&ModelImpl::OnHardRecoverFinished,
                                     weak_ptr_factory_.GetWeakPtr()));
}

void ModelImpl::Add(const Entry& entry) {
  DCHECK(store_->IsInitialized());
  DCHECK(entries_.find(entry.guid) == entries_.end());

  entries_.emplace(entry.guid, std::make_unique<Entry>(entry));

  store_->Update(entry, base::BindOnce(&ModelImpl::OnAddFinished,
                                       weak_ptr_factory_.GetWeakPtr(),
                                       entry.client, entry.guid));
}

void ModelImpl::Update(const Entry& entry) {
  DCHECK(store_->IsInitialized());
  DCHECK(entries_.find(entry.guid) != entries_.end());

  *entries_[entry.guid] = entry;

  store_->Update(entry, base::BindOnce(&ModelImpl::OnUpdateFinished,
                                       weak_ptr_factory_.GetWeakPtr(),
                                       entry.client, entry.guid));
}

void ModelImpl::Remove(const std::string& guid) {
  DCHECK(store_->IsInitialized());

  const auto& it = entries_.find(guid);
  CHECK(it != entries_.end());

  // Pull out a separate guid and a DownloadClient so that when we destroy the
  // entry we don't destroy the std::string that is backing the guid.
  std::string standalone_guid = guid;
  DownloadClient client = it->second->client;
  entries_.erase(it);
  store_->Remove(standalone_guid, base::BindOnce(&ModelImpl::OnRemoveFinished,
                                                 weak_ptr_factory_.GetWeakPtr(),
                                                 client, standalone_guid));
}

Entry* ModelImpl::Get(const std::string& guid) {
  const auto& it = entries_.find(guid);
  return it == entries_.end() ? nullptr : it->second.get();
}

Model::EntryList ModelImpl::PeekEntries() {
  EntryList entries;
  for (const auto& it : entries_)
    entries.push_back(it.second.get());

  return entries;
}

void ModelImpl::OnInitializedFinished(
    bool success,
    std::unique_ptr<std::vector<Entry>> entries) {
  stats::LogModelOperationResult(stats::ModelAction::kInitialize, success);

  if (!success) {
    client_->OnModelReady(false);
    return;
  }

  std::map<Entry::State, uint32_t> entries_count;
  for (const auto& entry : *entries) {
    entries_count[entry.state]++;
    entries_.emplace(entry.guid, std::make_unique<Entry>(entry));
  }

  stats::LogEntries(entries_count);
  client_->OnModelReady(true);
}

void ModelImpl::OnHardRecoverFinished(bool success) {
  client_->OnModelHardRecoverComplete(success);
}

void ModelImpl::OnAddFinished(DownloadClient client,
                              const std::string& guid,
                              bool success) {
  stats::LogModelOperationResult(stats::ModelAction::kAdd, success);

  // Don't notify the Client if the entry was already removed.
  auto it = entries_.find(guid);
  if (it == entries_.end())
    return;

  // Remove the entry from the map if the add failed.
  if (!success) {
    entries_.erase(it);
  }

  client_->OnItemAdded(success, client, guid);
}

void ModelImpl::OnUpdateFinished(DownloadClient client,
                                 const std::string& guid,
                                 bool success) {
  stats::LogModelOperationResult(stats::ModelAction::kUpdate, success);

  // Don't notify the Client if the entry was already removed.
  if (entries_.find(guid) == entries_.end())
    return;

  client_->OnItemUpdated(success, client, guid);
}

void ModelImpl::OnRemoveFinished(DownloadClient client,
                                 const std::string& guid,
                                 bool success) {
  stats::LogModelOperationResult(stats::ModelAction::kRemove, success);

  DCHECK(entries_.find(guid) == entries_.end());
  client_->OnItemRemoved(success, client, guid);
}

size_t ModelImpl::EstimateMemoryUsage() const {
  // Only track in-memory cache size.
  return base::trace_event::EstimateMemoryUsage(entries_);
}

}  // namespace download