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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "extensions/common/extension_api.h"
using value_store::ValueStore;
namespace extensions {
namespace {
// Resources there are a quota for.
enum class Resource {
kQuotaBytes,
kQuotaBytesPerItem,
kMaxItems,
};
// Allocates a setting in a record of total and per-setting usage.
void Allocate(
const std::string& key,
const base::Value& value,
size_t* used_total,
std::map<std::string, size_t>* used_per_setting) {
// Calculate the setting size based on its JSON serialization size.
// TODO(kalman): Does this work with different encodings?
// TODO(kalman): This is duplicating work that the leveldb delegate
// implementation is about to do, and it would be nice to avoid this.
std::string value_as_json;
base::JSONWriter::Write(value, &value_as_json);
size_t new_size = key.size() + value_as_json.size();
size_t existing_size = (*used_per_setting)[key];
*used_total += (new_size - existing_size);
(*used_per_setting)[key] = new_size;
}
ValueStore::Status QuotaExceededError(Resource resource) {
const char* name = nullptr;
switch (resource) {
case Resource::kQuotaBytes:
name = "Resource::kQuotaBytes";
break;
case Resource::kQuotaBytesPerItem:
name = "Resource::kQuotaBytesPerItem";
break;
case Resource::kMaxItems:
name = "Resource::kMaxItems";
break;
}
CHECK(name);
return ValueStore::Status(ValueStore::QUOTA_EXCEEDED,
base::StringPrintf("%s quota exceeded", name));
}
} // namespace
SettingsStorageQuotaEnforcer::SettingsStorageQuotaEnforcer(
const Limits& limits,
std::unique_ptr<ValueStore> delegate)
: limits_(limits),
delegate_(std::move(delegate)),
used_total_(0),
usage_calculated_(false) {}
SettingsStorageQuotaEnforcer::~SettingsStorageQuotaEnforcer() = default;
size_t SettingsStorageQuotaEnforcer::GetBytesInUse(const std::string& key) {
LazyCalculateUsage();
auto maybe_used = used_per_setting_.find(key);
return maybe_used == used_per_setting_.end() ? 0u : maybe_used->second;
}
size_t SettingsStorageQuotaEnforcer::GetBytesInUse(
const std::vector<std::string>& keys) {
size_t used = 0;
for (const std::string& key : keys)
used += GetBytesInUse(key);
return used;
}
size_t SettingsStorageQuotaEnforcer::GetBytesInUse() {
// All ValueStore implementations rely on GetBytesInUse being
// implemented here.
LazyCalculateUsage();
return used_total_;
}
ValueStore::ReadResult SettingsStorageQuotaEnforcer::GetKeys() {
return HandleResult(delegate_->GetKeys());
}
ValueStore::ReadResult SettingsStorageQuotaEnforcer::Get(
const std::string& key) {
return HandleResult(delegate_->Get(key));
}
ValueStore::ReadResult SettingsStorageQuotaEnforcer::Get(
const std::vector<std::string>& keys) {
return HandleResult(delegate_->Get(keys));
}
ValueStore::ReadResult SettingsStorageQuotaEnforcer::Get() {
return HandleResult(delegate_->Get());
}
ValueStore::WriteResult SettingsStorageQuotaEnforcer::Set(
WriteOptions options, const std::string& key, const base::Value& value) {
LazyCalculateUsage();
size_t new_used_total = used_total_;
std::map<std::string, size_t> new_used_per_setting = used_per_setting_;
Allocate(key, value, &new_used_total, &new_used_per_setting);
if (!(options & IGNORE_QUOTA)) {
if (new_used_total > limits_.quota_bytes)
return WriteResult(QuotaExceededError(Resource::kQuotaBytes));
if (new_used_per_setting[key] > limits_.quota_bytes_per_item)
return WriteResult(QuotaExceededError(Resource::kQuotaBytesPerItem));
if (new_used_per_setting.size() > limits_.max_items)
return WriteResult(QuotaExceededError(Resource::kMaxItems));
}
WriteResult result = HandleResult(delegate_->Set(options, key, value));
if (!result.status().ok())
return result;
if (usage_calculated_) {
used_total_ = new_used_total;
used_per_setting_.swap(new_used_per_setting);
}
return result;
}
ValueStore::WriteResult SettingsStorageQuotaEnforcer::Set(
WriteOptions options,
const base::Value::Dict& values) {
LazyCalculateUsage();
size_t new_used_total = used_total_;
std::map<std::string, size_t> new_used_per_setting = used_per_setting_;
for (const auto [key, value] : values) {
Allocate(key, value, &new_used_total, &new_used_per_setting);
if (!(options & IGNORE_QUOTA) &&
new_used_per_setting[key] > limits_.quota_bytes_per_item) {
return WriteResult(QuotaExceededError(Resource::kQuotaBytesPerItem));
}
}
if (!(options & IGNORE_QUOTA)) {
if (new_used_total > limits_.quota_bytes)
return WriteResult(QuotaExceededError(Resource::kQuotaBytes));
if (new_used_per_setting.size() > limits_.max_items)
return WriteResult(QuotaExceededError(Resource::kMaxItems));
}
WriteResult result = HandleResult(delegate_->Set(options, values));
if (!result.status().ok())
return result;
if (usage_calculated_) {
used_total_ = new_used_total;
used_per_setting_ = new_used_per_setting;
}
return result;
}
ValueStore::WriteResult SettingsStorageQuotaEnforcer::Remove(
const std::string& key) {
LazyCalculateUsage();
WriteResult result = HandleResult(delegate_->Remove(key));
if (!result.status().ok())
return result;
Free(key);
return result;
}
ValueStore::WriteResult SettingsStorageQuotaEnforcer::Remove(
const std::vector<std::string>& keys) {
WriteResult result = HandleResult(delegate_->Remove(keys));
if (!result.status().ok())
return result;
for (const std::string& key : keys)
Free(key);
return result;
}
ValueStore::WriteResult SettingsStorageQuotaEnforcer::Clear() {
LazyCalculateUsage();
WriteResult result = HandleResult(delegate_->Clear());
if (!result.status().ok())
return result;
used_per_setting_.clear();
used_total_ = 0u;
return result;
}
template <class T>
T SettingsStorageQuotaEnforcer::HandleResult(T result) {
if (result.status().restore_status != RESTORE_NONE) {
// Restoration means that an unknown amount, possibly all, of the data was
// lost from the database. Reset our counters - they will be lazily
// recalculated if/when needed.
used_per_setting_.clear();
used_total_ = 0u;
usage_calculated_ = false;
}
return result;
}
void SettingsStorageQuotaEnforcer::LazyCalculateUsage() {
if (usage_calculated_)
return;
DCHECK_EQ(0u, used_total_);
DCHECK(used_per_setting_.empty());
ReadResult maybe_settings = HandleResult(delegate_->Get());
if (!maybe_settings.status().ok()) {
LOG(WARNING) << "Failed to get settings for quota:"
<< maybe_settings.status().message;
return;
}
for (const auto [key, value] : maybe_settings.settings()) {
Allocate(key, value, &used_total_, &used_per_setting_);
}
usage_calculated_ = true;
}
void SettingsStorageQuotaEnforcer::Free(const std::string& key) {
if (!usage_calculated_)
return;
auto it = used_per_setting_.find(key);
if (it == used_per_setting_.end())
return;
DCHECK_GE(used_total_, it->second);
used_total_ -= it->second;
used_per_setting_.erase(it);
}
} // namespace extensions
|