0

Transition PrefRegistry(Simple) to use std::string_view.

Bug: 348307659
Change-Id: I7366da5f8ec6df6e32e79ece1d695b1ba091684f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5643471
Commit-Queue: Jan Keitel <jkeitel@google.com>
Reviewed-by: Dominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1317293}
This commit is contained in:
Jan Keitel
2024-06-20 11:47:36 +00:00
committed by Chromium LUCI CQ
parent b982816940
commit 94450a66d8
6 changed files with 46 additions and 44 deletions

@ -13,7 +13,7 @@
using base::Value;
DefaultPrefStore::DefaultPrefStore() {}
DefaultPrefStore::DefaultPrefStore() = default;
bool DefaultPrefStore::GetValue(std::string_view key,
const Value** result) const {
@ -36,7 +36,7 @@ bool DefaultPrefStore::HasObservers() const {
return !observers_.empty();
}
void DefaultPrefStore::SetDefaultValue(const std::string& key, Value value) {
void DefaultPrefStore::SetDefaultValue(std::string_view key, Value value) {
DCHECK(!GetValue(key, nullptr));
prefs_.SetValue(key, std::move(value));
}
@ -59,4 +59,4 @@ DefaultPrefStore::const_iterator DefaultPrefStore::end() const {
return prefs_.end();
}
DefaultPrefStore::~DefaultPrefStore() {}
DefaultPrefStore::~DefaultPrefStore() = default;

@ -33,12 +33,12 @@ class COMPONENTS_PREFS_EXPORT DefaultPrefStore : public PrefStore {
void RemoveObserver(PrefStore::Observer* observer) override;
bool HasObservers() const override;
// Sets a |value| for |key|. Should only be called if a value has not been
// Sets a `value` for `key`. Should only be called if a value has not been
// set yet; otherwise call ReplaceDefaultValue().
void SetDefaultValue(const std::string& key, base::Value value);
void SetDefaultValue(std::string_view key, base::Value value);
// Replaces the the value for |key| with a new value. Should only be called
// if a value has alreday been set; otherwise call SetDefaultValue().
// Replaces the the value for `key` with a new value. Should only be called
// if a value has already been set; otherwise call SetDefaultValue().
void ReplaceDefaultValue(const std::string& key, base::Value value);
const_iterator begin() const;

@ -58,7 +58,7 @@ void PrefRegistry::SetDefaultForeignPrefValue(const std::string& path,
RegisterPreference(path, std::move(default_value), flags);
}
void PrefRegistry::RegisterPreference(const std::string& path,
void PrefRegistry::RegisterPreference(std::string_view path,
base::Value default_value,
uint32_t flags) {
base::Value::Type orig_type = default_value.type();
@ -67,12 +67,13 @@ void PrefRegistry::RegisterPreference(const std::string& path,
"invalid preference type: " << orig_type;
DCHECK(!defaults_->GetValue(path, nullptr))
<< "Trying to register a previously registered pref: " << path;
DCHECK(!base::Contains(registration_flags_, path))
DCHECK(!base::Contains(registration_flags_, std::string(path)))
<< "Trying to register a previously registered pref: " << path;
defaults_->SetDefaultValue(path, std::move(default_value));
if (flags != NO_REGISTRATION_FLAGS)
registration_flags_[path] = flags;
if (flags != NO_REGISTRATION_FLAGS) {
registration_flags_.insert_or_assign(std::string(path), flags);
}
OnPrefRegistered(path, flags);
}

@ -74,11 +74,11 @@ class COMPONENTS_PREFS_EXPORT PrefRegistry
// Changes the default value for a preference.
//
// |pref_name| must be a previously registered preference.
// `pref_name` must be a previously registered preference.
void SetDefaultPrefValue(const std::string& pref_name, base::Value value);
// Registers a pref owned by another service for use with the current service.
// The owning service must register that pref with the |PUBLIC| flag.
// The owning service must register that pref with the `PUBLIC` flag.
void RegisterForeignPref(const std::string& path);
// Sets the default value and flags of a previously-registered foreign pref
@ -96,8 +96,8 @@ class COMPONENTS_PREFS_EXPORT PrefRegistry
virtual ~PrefRegistry();
// Used by subclasses to register a default value and registration flags for
// a preference. |flags| is a bitmask of |PrefRegistrationFlags|.
void RegisterPreference(const std::string& path,
// a preference. `flags` is a bitmask of `PrefRegistrationFlags`.
void RegisterPreference(std::string_view path,
base::Value default_value,
uint32_t flags);

@ -4,6 +4,7 @@
#include "components/prefs/pref_registry_simple.h"
#include <string_view>
#include <utility>
#include "base/files/file_path.h"
@ -14,81 +15,81 @@
PrefRegistrySimple::PrefRegistrySimple() = default;
PrefRegistrySimple::~PrefRegistrySimple() = default;
void PrefRegistrySimple::RegisterBooleanPref(const std::string& path,
void PrefRegistrySimple::RegisterBooleanPref(std::string_view path,
bool default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(default_value), flags);
}
void PrefRegistrySimple::RegisterIntegerPref(const std::string& path,
void PrefRegistrySimple::RegisterIntegerPref(std::string_view path,
int default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(default_value), flags);
}
void PrefRegistrySimple::RegisterDoublePref(const std::string& path,
void PrefRegistrySimple::RegisterDoublePref(std::string_view path,
double default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(default_value), flags);
}
void PrefRegistrySimple::RegisterStringPref(const std::string& path,
const std::string& default_value,
void PrefRegistrySimple::RegisterStringPref(std::string_view path,
std::string_view default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(default_value), flags);
}
void PrefRegistrySimple::RegisterFilePathPref(
const std::string& path,
std::string_view path,
const base::FilePath& default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(default_value.AsUTF8Unsafe()), flags);
}
void PrefRegistrySimple::RegisterListPref(const std::string& path,
void PrefRegistrySimple::RegisterListPref(std::string_view path,
uint32_t flags) {
RegisterPreference(path, base::Value(base::Value::Type::LIST), flags);
}
void PrefRegistrySimple::RegisterListPref(const std::string& path,
void PrefRegistrySimple::RegisterListPref(std::string_view path,
base::Value::List default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(std::move(default_value)), flags);
}
void PrefRegistrySimple::RegisterDictionaryPref(const std::string& path,
void PrefRegistrySimple::RegisterDictionaryPref(std::string_view path,
uint32_t flags) {
RegisterPreference(path, base::Value(base::Value::Type::DICT), flags);
}
void PrefRegistrySimple::RegisterDictionaryPref(const std::string& path,
void PrefRegistrySimple::RegisterDictionaryPref(std::string_view path,
base::Value::Dict default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(std::move(default_value)), flags);
}
void PrefRegistrySimple::RegisterInt64Pref(const std::string& path,
void PrefRegistrySimple::RegisterInt64Pref(std::string_view path,
int64_t default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(base::NumberToString(default_value)),
flags);
}
void PrefRegistrySimple::RegisterUint64Pref(const std::string& path,
void PrefRegistrySimple::RegisterUint64Pref(std::string_view path,
uint64_t default_value,
uint32_t flags) {
RegisterPreference(path, base::Value(base::NumberToString(default_value)),
flags);
}
void PrefRegistrySimple::RegisterTimePref(const std::string& path,
void PrefRegistrySimple::RegisterTimePref(std::string_view path,
base::Time default_value,
uint32_t flags) {
RegisterInt64Pref(
path, default_value.ToDeltaSinceWindowsEpoch().InMicroseconds(), flags);
}
void PrefRegistrySimple::RegisterTimeDeltaPref(const std::string& path,
void PrefRegistrySimple::RegisterTimeDeltaPref(std::string_view path,
base::TimeDelta default_value,
uint32_t flags) {
RegisterInt64Pref(path, default_value.InMicroseconds(), flags);

@ -8,7 +8,7 @@
#include <stdint.h>
#include <memory>
#include <string>
#include <string_view>
#include "base/time/time.h"
#include "base/values.h"
@ -29,53 +29,53 @@ class COMPONENTS_PREFS_EXPORT PrefRegistrySimple : public PrefRegistry {
// For each of these registration methods, |flags| is an optional bitmask of
// PrefRegistrationFlags.
void RegisterBooleanPref(const std::string& path,
void RegisterBooleanPref(std::string_view path,
bool default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterIntegerPref(const std::string& path,
void RegisterIntegerPref(std::string_view path,
int default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterDoublePref(const std::string& path,
void RegisterDoublePref(std::string_view path,
double default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterStringPref(const std::string& path,
const std::string& default_value,
void RegisterStringPref(std::string_view path,
std::string_view default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterFilePathPref(const std::string& path,
void RegisterFilePathPref(std::string_view path,
const base::FilePath& default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterListPref(const std::string& path,
void RegisterListPref(std::string_view path,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterListPref(const std::string& path,
void RegisterListPref(std::string_view path,
base::Value::List default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterDictionaryPref(const std::string& path,
void RegisterDictionaryPref(std::string_view path,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterDictionaryPref(const std::string& path,
void RegisterDictionaryPref(std::string_view path,
base::Value::Dict default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterInt64Pref(const std::string& path,
void RegisterInt64Pref(std::string_view path,
int64_t default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterUint64Pref(const std::string& path,
void RegisterUint64Pref(std::string_view path,
uint64_t default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterTimePref(const std::string& path,
void RegisterTimePref(std::string_view path,
base::Time default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterTimeDeltaPref(const std::string& path,
void RegisterTimeDeltaPref(std::string_view path,
base::TimeDelta default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);