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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
// Copyright 2021 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/session_storage_manager.h"
#include "base/no_destructor.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/api/storage.h"
using base::trace_event::EstimateMemoryUsage;
namespace extensions {
namespace {
class SessionStorageManagerFactory : public BrowserContextKeyedServiceFactory {
public:
SessionStorageManagerFactory();
SessionStorageManagerFactory(const SessionStorageManagerFactory&) = delete;
SessionStorageManagerFactory& operator=(const SessionStorageManagerFactory&) =
delete;
~SessionStorageManagerFactory() override = default;
SessionStorageManager* GetForBrowserContext(
content::BrowserContext* browser_context);
private:
// BrowserContextKeyedServiceFactory:
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* browser_context) const override;
std::unique_ptr<KeyedService> BuildServiceInstanceForBrowserContext(
content::BrowserContext* browser_context) const override;
};
SessionStorageManagerFactory::SessionStorageManagerFactory()
: BrowserContextKeyedServiceFactory(
"SessionStorageManager",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(ExtensionRegistryFactory::GetInstance());
}
SessionStorageManager* SessionStorageManagerFactory::GetForBrowserContext(
content::BrowserContext* browser_context) {
return static_cast<SessionStorageManager*>(
GetServiceForBrowserContext(browser_context, /*create=*/true));
}
content::BrowserContext* SessionStorageManagerFactory::GetBrowserContextToUse(
content::BrowserContext* browser_context) const {
// Share storage between incognito and on-the-record profiles by using the
// original context of an incognito window.
return ExtensionsBrowserClient::Get()->GetContextRedirectedToOriginal(
browser_context);
}
std::unique_ptr<KeyedService>
SessionStorageManagerFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* browser_context) const {
return std::make_unique<SessionStorageManager>(
api::storage::session::QUOTA_BYTES, browser_context);
}
} // namespace
// Implementation of SessionValue.
SessionStorageManager::SessionValue::SessionValue(base::Value value,
size_t size)
: value(std::move(value)), size(size) {}
// Implementation of ValueChange.
SessionStorageManager::ValueChange::ValueChange(
std::string key,
std::optional<base::Value> old_value,
base::Value* new_value)
: key(key), old_value(std::move(old_value)), new_value(new_value) {}
SessionStorageManager::ValueChange::~ValueChange() = default;
SessionStorageManager::ValueChange::ValueChange(ValueChange&& other) = default;
// Implementation of ExtensionStorage.
SessionStorageManager::ExtensionStorage::ExtensionStorage(size_t quota_bytes)
: quota_bytes_(quota_bytes) {}
SessionStorageManager::ExtensionStorage::~ExtensionStorage() = default;
size_t SessionStorageManager::ExtensionStorage::CalculateUsage(
std::map<std::string, base::Value> input_values,
std::map<std::string, std::unique_ptr<SessionValue>>& session_values)
const {
size_t updated_used_total = used_total_;
for (auto& input_it : input_values) {
size_t input_size = EstimateMemoryUsage(input_it.first) +
EstimateMemoryUsage(input_it.second);
updated_used_total += input_size;
// Remove session value size of existent key from total used bytes.
auto existent_value_it = values_.find(input_it.first);
if (existent_value_it != values_.end()) {
// `updated_used_total` is guaranteed to be at least as large as any
// individual value that's already in the map.
DCHECK_GE(updated_used_total, existent_value_it->second->size);
updated_used_total -= existent_value_it->second->size;
}
// Add input to the session values map.
session_values.emplace(
std::move(input_it.first),
std::make_unique<SessionValue>(std::move(input_it.second), input_size));
}
if (updated_used_total >= quota_bytes_) {
session_values.clear();
return quota_bytes_;
}
return updated_used_total;
}
std::vector<std::string> SessionStorageManager::ExtensionStorage::GetKeys()
const {
std::vector<std::string> keys;
for (auto& value : values_) {
keys.push_back(value.first);
}
return keys;
}
std::map<std::string, const base::Value*>
SessionStorageManager::ExtensionStorage::Get(
const std::vector<std::string>& keys) const {
std::map<std::string, const base::Value*> values;
for (auto& key : keys) {
auto value_it = values_.find(key);
if (value_it != values_.end())
values.emplace(key, &value_it->second->value);
}
return values;
}
std::map<std::string, const base::Value*>
SessionStorageManager::ExtensionStorage::GetAll() const {
std::map<std::string, const base::Value*> values;
for (auto& value : values_) {
values.emplace(value.first, &value.second->value);
}
return values;
}
bool SessionStorageManager::ExtensionStorage::Set(
std::map<std::string, base::Value> input_values,
std::vector<ValueChange>& changes,
std::string* error) {
std::map<std::string, std::unique_ptr<SessionValue>> session_values;
size_t updated_used_total =
CalculateUsage(std::move(input_values), session_values);
if (updated_used_total == quota_bytes_) {
*error = "Session storage quota bytes exceeded. Values were not stored.";
return false;
}
// Insert values in storage map and update total bytes.
for (auto& session_value : session_values) {
// Do nothing if key's existent value is the same as the new value.
auto& existent_value = values_[session_value.first];
if (existent_value && existent_value->value == session_value.second->value)
continue;
// Add the change to the changes list.
ValueChange change(session_value.first,
existent_value ? std::optional<base::Value>(
std::move(existent_value->value))
: std::nullopt,
&session_value.second->value);
changes.push_back(std::move(change));
existent_value = std::move(session_value.second);
}
used_total_ = updated_used_total;
return true;
}
void SessionStorageManager::ExtensionStorage::Remove(
const std::vector<std::string>& keys,
std::vector<ValueChange>& changes) {
for (auto& key : keys) {
auto value_it = values_.find(key);
if (value_it == values_.end())
continue;
// Add the change to the changes list.
ValueChange change(
key, std::optional<base::Value>(std::move(value_it->second->value)),
nullptr);
changes.push_back(std::move(change));
used_total_ -= value_it->second->size;
values_.erase(value_it);
}
}
void SessionStorageManager::ExtensionStorage::Clear(
std::vector<ValueChange>& changes) {
for (auto& value : values_) {
ValueChange change(
value.first, std::optional<base::Value>(std::move(value.second->value)),
nullptr);
changes.push_back(std::move(change));
}
Clear();
}
void SessionStorageManager::ExtensionStorage::Clear() {
used_total_ = 0;
values_.clear();
}
size_t SessionStorageManager::ExtensionStorage::GetBytesInUse(
const std::vector<std::string>& keys) const {
size_t total = 0;
for (const auto& key : keys) {
auto value_it = values_.find(key);
if (value_it != values_.end())
total += value_it->second->size;
}
return total;
}
size_t SessionStorageManager::ExtensionStorage::GetTotalBytesInUse() const {
return used_total_;
}
// Implementation of SessionStorageManager.
SessionStorageManager::SessionStorageManager(
size_t quota_bytes_per_extension,
content::BrowserContext* browser_context)
: quota_bytes_per_extension_(quota_bytes_per_extension) {
extension_registry_observation_.Observe(
ExtensionRegistry::Get(browser_context));
}
SessionStorageManager::~SessionStorageManager() = default;
// static
SessionStorageManager* SessionStorageManager::GetForBrowserContext(
content::BrowserContext* browser_context) {
return static_cast<SessionStorageManagerFactory*>(GetFactory())
->GetForBrowserContext(browser_context);
}
// static
BrowserContextKeyedServiceFactory* SessionStorageManager::GetFactory() {
static base::NoDestructor<SessionStorageManagerFactory> g_factory;
return g_factory.get();
}
std::vector<std::string> SessionStorageManager::GetKeys(
const ExtensionId& extension_id) const {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it == extensions_storage_.end()) {
return std::vector<std::string>();
}
return storage_it->second->GetKeys();
}
const base::Value* SessionStorageManager::Get(const ExtensionId& extension_id,
const std::string& key) const {
std::map<std::string, const base::Value*> values =
Get(extension_id, std::vector<std::string>(1, key));
if (values.empty())
return nullptr;
// Only a single value should be returned.
DCHECK_EQ(1u, values.size());
return values.begin()->second;
}
std::map<std::string, const base::Value*> SessionStorageManager::Get(
const ExtensionId& extension_id,
const std::vector<std::string>& keys) const {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it == extensions_storage_.end()) {
return std::map<std::string, const base::Value*>();
}
return storage_it->second->Get(keys);
}
std::map<std::string, const base::Value*> SessionStorageManager::GetAll(
const ExtensionId& extension_id) const {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it == extensions_storage_.end()) {
return std::map<std::string, const base::Value*>();
}
return storage_it->second->GetAll();
}
bool SessionStorageManager::Set(const ExtensionId& extension_id,
std::map<std::string, base::Value> input_values,
std::vector<ValueChange>& changes,
std::string* error) {
auto& storage = extensions_storage_[extension_id];
// Initialize the extension storage, if it doesn't already exist.
if (!storage)
storage = std::make_unique<ExtensionStorage>(quota_bytes_per_extension_);
return storage->Set(std::move(input_values), changes, error);
}
void SessionStorageManager::Remove(const ExtensionId& extension_id,
const std::vector<std::string>& keys,
std::vector<ValueChange>& changes) {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it != extensions_storage_.end())
storage_it->second->Remove(keys, changes);
}
void SessionStorageManager::Remove(const ExtensionId& extension_id,
const std::string& key,
std::vector<ValueChange>& changes) {
Remove(extension_id, std::vector<std::string>(1, key), changes);
}
void SessionStorageManager::Clear(const ExtensionId& extension_id,
std::vector<ValueChange>& changes) {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it != extensions_storage_.end())
storage_it->second->Clear(changes);
}
void SessionStorageManager::Clear(const ExtensionId& extension_id) {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it != extensions_storage_.end())
storage_it->second->Clear();
}
size_t SessionStorageManager::GetBytesInUse(const ExtensionId& extension_id,
const std::string& key) const {
return GetBytesInUse(extension_id, std::vector<std::string>(1, key));
}
size_t SessionStorageManager::GetBytesInUse(
const ExtensionId& extension_id,
const std::vector<std::string>& keys) const {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it != extensions_storage_.end())
return storage_it->second->GetBytesInUse(keys);
return 0;
}
size_t SessionStorageManager::GetTotalBytesInUse(
const ExtensionId& extension_id) const {
auto storage_it = extensions_storage_.find(extension_id);
if (storage_it != extensions_storage_.end())
return storage_it->second->GetTotalBytesInUse();
return 0;
}
void SessionStorageManager::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) {
Clear(extension->id());
}
} // namespace extensions
|