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
|
// 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 "chrome/browser/extensions/api/storage/sync_value_store_cache.h"
#include <stddef.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/browser/extensions/api/storage/sync_storage_backend.h"
#include "chrome/browser/sync/glue/sync_start_util.h"
#include "components/value_store/value_store_factory.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/api/storage/backend_task_runner.h"
#include "extensions/common/api/storage.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
using content::BrowserThread;
namespace extensions {
namespace {
// Returns the quota limit for sync storage, taken from the schema in
// extensions/common/api/storage.json.
SettingsStorageQuotaEnforcer::Limits GetSyncQuotaLimits() {
SettingsStorageQuotaEnforcer::Limits limits = {
static_cast<size_t>(api::storage::sync::QUOTA_BYTES),
static_cast<size_t>(api::storage::sync::QUOTA_BYTES_PER_ITEM),
static_cast<size_t>(api::storage::sync::MAX_ITEMS)};
return limits;
}
} // namespace
SyncValueStoreCache::SyncValueStoreCache(
scoped_refptr<value_store::ValueStoreFactory> factory,
SettingsChangedCallback observer,
const base::FilePath& profile_path)
: initialized_(false) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// This post is safe since the destructor can only be invoked from the
// same message loop, and any potential post of a deletion task must come
// after the constructor returns.
GetBackendTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&SyncValueStoreCache::InitOnBackend,
base::Unretained(this), std::move(factory),
GetSequenceBoundSettingsChangedCallback(
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(observer)),
profile_path));
}
SyncValueStoreCache::~SyncValueStoreCache() {
DCHECK(IsOnBackendSequence());
}
base::WeakPtr<SyncValueStoreCache> SyncValueStoreCache::AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
syncer::SyncableService* SyncValueStoreCache::GetSyncableService(
syncer::DataType type) {
DCHECK(IsOnBackendSequence());
DCHECK(initialized_);
switch (type) {
case syncer::APP_SETTINGS:
return app_backend_.get();
case syncer::EXTENSION_SETTINGS:
return extension_backend_.get();
default:
NOTREACHED();
}
}
void SyncValueStoreCache::RunWithValueStoreForExtension(
StorageCallback callback,
scoped_refptr<const Extension> extension) {
DCHECK(IsOnBackendSequence());
DCHECK(initialized_);
SyncStorageBackend* backend =
extension->is_app() ? app_backend_.get() : extension_backend_.get();
std::move(callback).Run(backend->GetStorage(extension->id()));
}
void SyncValueStoreCache::DeleteStorageSoon(const ExtensionId& extension_id) {
DCHECK(IsOnBackendSequence());
app_backend_->DeleteStorage(extension_id);
extension_backend_->DeleteStorage(extension_id);
}
void SyncValueStoreCache::InitOnBackend(
scoped_refptr<value_store::ValueStoreFactory> factory,
SequenceBoundSettingsChangedCallback observer,
const base::FilePath& profile_path) {
DCHECK(IsOnBackendSequence());
DCHECK(!initialized_);
app_backend_ = std::make_unique<SyncStorageBackend>(
factory, GetSyncQuotaLimits(), observer, syncer::APP_SETTINGS,
sync_start_util::GetFlareForSyncableService(profile_path));
extension_backend_ = std::make_unique<SyncStorageBackend>(
std::move(factory), GetSyncQuotaLimits(), std::move(observer),
syncer::EXTENSION_SETTINGS,
sync_start_util::GetFlareForSyncableService(profile_path));
initialized_ = true;
}
} // namespace extensions
|