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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// Copyright 2018 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
#include "components/leveldb_proto/internal/shared_proto_database_client.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "base/check_op.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "components/leveldb_proto/internal/proto_leveldb_wrapper.h"
#include "components/leveldb_proto/internal/shared_proto_database.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/shared_proto_database_client_list.h"
namespace leveldb_proto {
namespace {
const ProtoDbType* g_obsolete_client_list_for_testing = nullptr;
// Holds the db wrapper alive and callback is called at destruction. This class
// is used to post multiple update tasks on |db_wrapper| and keep the instance
// alive till all the callbacks are returned.
class ObsoleteClientsDbHolder
: public base::RefCounted<ObsoleteClientsDbHolder> {
public:
ObsoleteClientsDbHolder(std::unique_ptr<ProtoLevelDBWrapper> db_wrapper,
Callbacks::UpdateCallback callback)
: success_(true),
owned_db_wrapper_(std::move(db_wrapper)),
callback_(std::move(callback)) {}
void set_success(bool success) { success_ &= success; }
private:
friend class base::RefCounted<ObsoleteClientsDbHolder>;
~ObsoleteClientsDbHolder() { std::move(callback_).Run(success_); }
bool success_;
std::unique_ptr<ProtoLevelDBWrapper> owned_db_wrapper_;
Callbacks::UpdateCallback callback_;
};
PhysicalKey MakePhysicalKey(const KeyPrefix& prefix, const LogicalKey& key) {
return PhysicalKey(base::StrCat({prefix.value(), key.value()}));
}
} // namespace
// static
bool SharedProtoDatabaseClient::HasPrefix(const PhysicalKey& key,
const KeyPrefix& prefix) {
return base::StartsWith(key.value(), prefix.value(),
base::CompareCase::SENSITIVE);
}
// static
std::optional<LogicalKey> SharedProtoDatabaseClient::StripPrefix(
const PhysicalKey& key,
const KeyPrefix& prefix) {
if (!HasPrefix(key, prefix))
return std::nullopt;
return LogicalKey(key.value().substr(prefix.value().length()));
}
// static
std::unique_ptr<KeyVector> SharedProtoDatabaseClient::PrefixStrings(
std::unique_ptr<KeyVector> strings,
const KeyPrefix& prefix) {
for (auto& str : *strings)
str.assign(MakePhysicalKey(prefix, LogicalKey(str)).value());
return strings;
}
// static
bool SharedProtoDatabaseClient::KeyFilterStripPrefix(
const KeyFilter& key_filter,
const KeyPrefix& prefix,
const PhysicalKey& key) {
if (key_filter.is_null())
return true;
std::optional<LogicalKey> stripped = StripPrefix(key, prefix);
if (!stripped)
return false;
return key_filter.Run(stripped->value());
}
// static
bool SharedProtoDatabaseClient::KeyStringFilterStripPrefix(
const KeyFilter& key_filter,
const KeyPrefix& prefix,
const std::string& key) {
return KeyFilterStripPrefix(key_filter, prefix, PhysicalKey(key));
}
// static
Enums::KeyIteratorAction
SharedProtoDatabaseClient::KeyIteratorControllerStripPrefix(
const KeyIteratorController& controller,
const KeyPrefix& prefix,
const PhysicalKey& key) {
DCHECK(!controller.is_null());
std::optional<LogicalKey> stripped = StripPrefix(key, prefix);
if (!stripped)
return Enums::kSkipAndStop;
return controller.Run(stripped->value());
}
// static
Enums::KeyIteratorAction
SharedProtoDatabaseClient::KeyStringIteratorControllerStripPrefix(
const KeyIteratorController& controller,
const KeyPrefix& prefix,
const std::string& key) {
return KeyIteratorControllerStripPrefix(controller, prefix, PhysicalKey(key));
}
// static
void SharedProtoDatabaseClient::GetSharedDatabaseInitStatusAsync(
const std::string& client_db_id,
const scoped_refptr<SharedProtoDatabase>& shared_db,
Callbacks::InitStatusCallback callback) {
shared_db->GetDatabaseInitStatusAsync(client_db_id, std::move(callback));
}
// static
void SharedProtoDatabaseClient::UpdateClientMetadataAsync(
const scoped_refptr<SharedProtoDatabase>& shared_db,
const std::string& client_db_id,
SharedDBMetadataProto::MigrationStatus migration_status,
ClientCorruptCallback callback) {
shared_db->UpdateClientMetadataAsync(client_db_id, migration_status,
std::move(callback));
}
// static
void SharedProtoDatabaseClient::DestroyObsoleteSharedProtoDatabaseClients(
std::unique_ptr<ProtoLevelDBWrapper> db_wrapper,
Callbacks::UpdateCallback callback) {
ProtoLevelDBWrapper* db_wrapper_ptr = db_wrapper.get();
scoped_refptr<ObsoleteClientsDbHolder> db_holder =
new ObsoleteClientsDbHolder(std::move(db_wrapper), std::move(callback));
const ProtoDbType* list = g_obsolete_client_list_for_testing
? g_obsolete_client_list_for_testing
: kObsoleteSharedProtoDbTypeClients;
for (size_t i = 0; list[i] != ProtoDbType::LAST; ++i) {
// Callback keeps a ref pointer to db_holder alive till the changes are
// done. |db_holder| will be destroyed once all the RemoveKeys() calls
// return.
Callbacks::UpdateCallback callback_wrapper =
base::BindOnce([](scoped_refptr<ObsoleteClientsDbHolder> db_holder,
bool success) { db_holder->set_success(success); },
db_holder);
// Remove all type prefixes for the client.
// TODO(ssid): Support cleanup of namespaces for clients. This code assumes
// the prefix contains the client namespace at the beginning.
db_wrapper_ptr->RemoveKeys(
base::BindRepeating([](const std::string& key) { return true; }),
SharedProtoDatabaseClient::PrefixForDatabase(list[i]).value(),
std::move(callback_wrapper));
}
}
// static
void SharedProtoDatabaseClient::SetObsoleteClientListForTesting(
const ProtoDbType* list) {
g_obsolete_client_list_for_testing = list;
}
// static
KeyPrefix SharedProtoDatabaseClient::PrefixForDatabase(ProtoDbType db_type) {
return KeyPrefix(base::StringPrintf("%d_", static_cast<int>(db_type)));
}
SharedProtoDatabaseClient::SharedProtoDatabaseClient(
std::unique_ptr<ProtoLevelDBWrapper> db_wrapper,
ProtoDbType db_type,
const scoped_refptr<SharedProtoDatabase>& parent_db)
: UniqueProtoDatabase(std::move(db_wrapper)),
prefix_(PrefixForDatabase(db_type)),
parent_db_(parent_db) {
SetMetricsId(SharedProtoDatabaseClientList::ProtoDbTypeToString(db_type));
DETACH_FROM_SEQUENCE(sequence_checker_);
}
SharedProtoDatabaseClient::~SharedProtoDatabaseClient() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
void SharedProtoDatabaseClient::Init(const std::string& client_uma_name,
Callbacks::InitStatusCallback callback) {
// Should never be called from from the selector, and init is not necessary.
NOTREACHED();
}
void SharedProtoDatabaseClient::InitWithDatabase(
LevelDB* database,
const base::FilePath& database_dir,
const leveldb_env::Options& options,
bool destroy_on_corruption,
Callbacks::InitStatusCallback callback) {
NOTREACHED();
}
void SharedProtoDatabaseClient::UpdateEntries(
std::unique_ptr<KeyValueVector> entries_to_save,
std::unique_ptr<KeyVector> keys_to_remove,
Callbacks::UpdateCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::UpdateEntries(
PrefixKeyEntryVector(std::move(entries_to_save), prefix_),
PrefixStrings(std::move(keys_to_remove), prefix_), std::move(callback));
}
void SharedProtoDatabaseClient::UpdateEntriesWithRemoveFilter(
std::unique_ptr<KeyValueVector> entries_to_save,
const KeyFilter& delete_key_filter,
Callbacks::UpdateCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UpdateEntriesWithRemoveFilter(std::move(entries_to_save), delete_key_filter,
std::string(), std::move(callback));
}
void SharedProtoDatabaseClient::UpdateEntriesWithRemoveFilter(
std::unique_ptr<KeyValueVector> entries_to_save,
const KeyFilter& delete_key_filter,
const std::string& target_prefix,
Callbacks::UpdateCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::UpdateEntriesWithRemoveFilter(
PrefixKeyEntryVector(std::move(entries_to_save), prefix_),
base::BindRepeating(&KeyStringFilterStripPrefix, delete_key_filter,
prefix_),
MakePhysicalKey(prefix_, LogicalKey(target_prefix)).value(),
std::move(callback));
}
void SharedProtoDatabaseClient::LoadEntries(Callbacks::LoadCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LoadEntriesWithFilter(KeyFilter(), std::move(callback));
}
void SharedProtoDatabaseClient::LoadEntriesWithFilter(
const KeyFilter& filter,
Callbacks::LoadCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LoadEntriesWithFilter(filter, leveldb::ReadOptions(), std::string(),
std::move(callback));
}
void SharedProtoDatabaseClient::LoadEntriesWithFilter(
const KeyFilter& filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
Callbacks::LoadCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::LoadEntriesWithFilter(
base::BindRepeating(&KeyStringFilterStripPrefix, filter, prefix_),
options, MakePhysicalKey(prefix_, LogicalKey(target_prefix)).value(),
std::move(callback));
}
void SharedProtoDatabaseClient::LoadKeys(Callbacks::LoadKeysCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
LoadKeys(std::string(), std::move(callback));
}
void SharedProtoDatabaseClient::LoadKeys(const std::string& target_prefix,
Callbacks::LoadKeysCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::LoadKeys(
MakePhysicalKey(prefix_, LogicalKey(target_prefix)).value(),
base::BindOnce(&SharedProtoDatabaseClient::StripPrefixLoadKeysCallback,
std::move(callback), prefix_));
}
void SharedProtoDatabaseClient::LoadKeysAndEntries(
Callbacks::LoadKeysAndEntriesCallback callback) {
LoadKeysAndEntriesWithFilter(KeyFilter(), std::move(callback));
}
void SharedProtoDatabaseClient::LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
Callbacks::LoadKeysAndEntriesCallback callback) {
LoadKeysAndEntriesWithFilter(filter, leveldb::ReadOptions(), std::string(),
std::move(callback));
}
void SharedProtoDatabaseClient::LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
Callbacks::LoadKeysAndEntriesCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::LoadKeysAndEntriesWithFilter(
base::BindRepeating(&KeyStringFilterStripPrefix, filter, prefix_),
options, MakePhysicalKey(prefix_, LogicalKey(target_prefix)).value(),
base::BindOnce(
&SharedProtoDatabaseClient::StripPrefixLoadKeysAndEntriesCallback,
std::move(callback), prefix_));
}
void SharedProtoDatabaseClient::LoadKeysAndEntriesInRange(
const std::string& start,
const std::string& end,
Callbacks::LoadKeysAndEntriesCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::LoadKeysAndEntriesInRange(
MakePhysicalKey(prefix_, LogicalKey(start)).value(),
MakePhysicalKey(prefix_, LogicalKey(end)).value(),
base::BindOnce(
&SharedProtoDatabaseClient::StripPrefixLoadKeysAndEntriesCallback,
std::move(callback), prefix_));
}
void SharedProtoDatabaseClient::LoadKeysAndEntriesWhile(
const std::string& start,
const leveldb_proto::KeyIteratorController& controller,
Callbacks::LoadKeysAndEntriesCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::LoadKeysAndEntriesWhile(
MakePhysicalKey(prefix_, LogicalKey(start)).value(),
base::BindRepeating(&KeyStringIteratorControllerStripPrefix, controller,
prefix_),
base::BindOnce(
&SharedProtoDatabaseClient::StripPrefixLoadKeysAndEntriesCallback,
std::move(callback), prefix_));
}
void SharedProtoDatabaseClient::GetEntry(const std::string& key,
Callbacks::GetCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UniqueProtoDatabase::GetEntry(
MakePhysicalKey(prefix_, LogicalKey(key)).value(), std::move(callback));
}
void SharedProtoDatabaseClient::Destroy(Callbacks::DestroyCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UpdateEntriesWithRemoveFilter(
std::make_unique<KeyValueVector>(),
base::BindRepeating([](const std::string& key) { return true; }),
base::BindOnce([](Callbacks::DestroyCallback callback,
bool success) { std::move(callback).Run(success); },
std::move(callback)));
}
void SharedProtoDatabaseClient::UpdateClientInitMetadata(
SharedDBMetadataProto::MigrationStatus migration_status) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
migration_status_ = migration_status;
// Tell the SharedProtoDatabase that we've seen the corruption state so it's
// safe to update its records for this client.
UpdateClientMetadataAsync(parent_db_, client_db_id(), migration_status_,
base::BindOnce([](bool success) {
// TODO(thildebr): Should we do anything special
// here? If the shared DB can't update the
// client's corruption counter to match its own,
// then the client will think it's corrupt on the
// next Init as well.
}));
}
// static
void SharedProtoDatabaseClient::StripPrefixLoadKeysCallback(
Callbacks::LoadKeysCallback callback,
const KeyPrefix& prefix,
bool success,
std::unique_ptr<leveldb_proto::KeyVector> keys) {
auto stripped_keys = std::make_unique<leveldb_proto::KeyVector>();
for (auto& key : *keys) {
std::optional<LogicalKey> stripped = StripPrefix(PhysicalKey(key), prefix);
if (!stripped)
continue;
stripped_keys->emplace_back(stripped->value());
}
std::move(callback).Run(success, std::move(stripped_keys));
}
// static
void SharedProtoDatabaseClient::StripPrefixLoadKeysAndEntriesCallback(
Callbacks::LoadKeysAndEntriesCallback callback,
const KeyPrefix& prefix,
bool success,
std::unique_ptr<KeyValueMap> keys_entries) {
auto stripped_keys_map = std::make_unique<KeyValueMap>();
for (auto& key_entry : *keys_entries) {
std::optional<LogicalKey> stripped_key =
StripPrefix(PhysicalKey(key_entry.first), prefix);
if (!stripped_key)
continue;
stripped_keys_map->insert(
std::make_pair(stripped_key->value(), key_entry.second));
}
std::move(callback).Run(success, std::move(stripped_keys_map));
}
// static
std::unique_ptr<KeyValueVector> SharedProtoDatabaseClient::PrefixKeyEntryVector(
std::unique_ptr<KeyValueVector> kev,
const KeyPrefix& prefix) {
for (auto& key_entry_pair : *kev) {
key_entry_pair.first =
MakePhysicalKey(prefix, LogicalKey(key_entry_pair.first)).value();
}
return kev;
}
} // namespace leveldb_proto
|