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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#ifndef CHROME_BROWSER_ASH_POLICY_REPORTING_INSTALL_EVENT_LOG_H_
#define CHROME_BROWSER_ASH_POLICY_REPORTING_INSTALL_EVENT_LOG_H_
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/files/file_path.h"
#include "base/logging.h"
#include "chrome/browser/ash/policy/reporting/single_install_event_log.h"
namespace policy {
// An event log for app installs. The app refers to extension or ARC++ app. The
// log entries for each app are kept in a separate round-robin buffer. The log
// can be stored on disk and serialized for upload to a server. Log entries are
// pruned after upload has completed. Uses a sequence checker in
// |AppInstallEventLogManager| to ensure that methods are called from the
// right thread. |T| specifies the event type, and |C| specifies the event log
// class for single app.
template <typename T, typename C>
class InstallEventLog {
public:
// Restores the event log from |file_name|. If there is an error parsing the
// file, as many log entries as possible are restored.
explicit InstallEventLog(const base::FilePath& file_name);
~InstallEventLog();
// The current total number of log entries across apps.
int total_size() { return total_size_; }
// The current maximum number of log entries for a single app.
int max_size() { return max_size_; }
// Add a log entry for |id|. If the buffer for that app is
// full, the oldest entry is removed.
void Add(const std::string& id, const T& event);
// Stores the event log to the file name provided to the constructor. If the
// event log has not changed since it was last stored to disk (or initially
// loaded from disk), does nothing.
void Store();
// Clears log entries that were previously serialized.
void ClearSerialized();
static constexpr int64_t kLogFileVersion = 3;
static constexpr ssize_t kMaxLogs = 1024;
protected:
// The round-robin log event buffers for individual apps.
std::map<std::string, std::unique_ptr<C>> logs_;
const base::FilePath file_name_;
// The current total number of log entries, across all apps.
int total_size_ = 0;
// The current maximum number of log entries for a single app.
int max_size_ = 0;
// Whether the event log changed since it was last stored to disk (or
// initially loaded from disk).
bool dirty_ = false;
};
// Implementation details below here.
template <typename T, typename C>
constexpr int64_t InstallEventLog<T, C>::kLogFileVersion;
template <typename T, typename C>
constexpr ssize_t InstallEventLog<T, C>::kMaxLogs;
template <typename T, typename C>
InstallEventLog<T, C>::InstallEventLog(const base::FilePath& file_name)
: file_name_(file_name) {
base::File file(file_name_, base::File::FLAG_OPEN | base::File::FLAG_READ);
if (!file.IsValid())
return;
int64_t version;
if (!file.ReadAtCurrentPosAndCheck(
base::as_writable_bytes(base::span_from_ref(version)))) {
LOG(WARNING) << "Corrupted install log.";
return;
}
if (version != kLogFileVersion) {
LOG(WARNING) << "Log file version mismatch.";
return;
}
ssize_t entries;
if (!file.ReadAtCurrentPosAndCheck(
base::as_writable_bytes(base::span_from_ref(entries)))) {
LOG(WARNING) << "Corrupted install log.";
return;
}
for (int i = 0; i < std::min(entries, kMaxLogs); ++i) {
std::unique_ptr<C> log;
const bool file_ok = C::Load(&file, &log);
const bool log_ok =
log && !log->id().empty() && logs_.find(log->id()) == logs_.end();
if (!file_ok || !log_ok) {
LOG(WARNING) << "Corrupted install log.";
}
if (log_ok) {
total_size_ += log->size();
max_size_ = std::max(max_size_, log->size());
logs_[log->id()] = std::move(log);
}
if (!file_ok) {
return;
}
}
if (entries >= kMaxLogs) {
LOG(WARNING) << "Corrupted install log.";
}
}
template <typename T, typename C>
InstallEventLog<T, C>::~InstallEventLog() = default;
template <typename T, typename C>
void InstallEventLog<T, C>::Add(const std::string& extension_id,
const T& event) {
if (logs_.size() == kMaxLogs && logs_.find(extension_id) == logs_.end()) {
LOG(WARNING) << "Install log overflow.";
return;
}
auto& log = logs_[extension_id];
if (!log)
log = std::make_unique<C>(extension_id);
total_size_ -= log->size();
log->Add(event);
total_size_ += log->size();
max_size_ = std::max(max_size_, log->size());
dirty_ = true;
}
template <typename T, typename C>
void InstallEventLog<T, C>::Store() {
if (!dirty_) {
return;
}
base::File file(file_name_,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
if (!file.IsValid()) {
LOG(WARNING) << "Unable to store install log.";
return;
}
if (!file.WriteAtCurrentPosAndCheck(
base::byte_span_from_ref(kLogFileVersion))) {
LOG(WARNING) << "Unable to store install log.";
return;
}
ssize_t entries = logs_.size();
if (!file.WriteAtCurrentPosAndCheck(base::byte_span_from_ref(entries))) {
LOG(WARNING) << "Unable to store install log.";
return;
}
for (const auto& log : logs_) {
if (!log.second->Store(&file)) {
LOG(WARNING) << "Unable to store install log.";
return;
}
}
dirty_ = false;
}
template <typename T, typename C>
void InstallEventLog<T, C>::ClearSerialized() {
int total_size = 0;
max_size_ = 0;
auto log = logs_.begin();
while (log != logs_.end()) {
log->second->ClearSerialized();
if (log->second->empty()) {
log = logs_.erase(log);
} else {
total_size += log->second->size();
max_size_ = std::max(max_size_, log->second->size());
++log;
}
}
if (total_size != total_size_) {
total_size_ = total_size;
dirty_ = true;
}
}
} // namespace policy
#endif // CHROME_BROWSER_ASH_POLICY_REPORTING_INSTALL_EVENT_LOG_H_
|