0

Make more PrefService methods take std::string_view.

Bug: 349741884
Change-Id: Iebff3b8d1d61374ba1ce1708306e8d7db136995a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5679782
Reviewed-by: Dominic Battré <battre@chromium.org>
Commit-Queue: Dominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1325511}
This commit is contained in:
Jan Keitel
2024-07-10 15:54:59 +00:00
committed by Chromium LUCI CQ
parent d0ce4dfc91
commit 0a58e9061f
3 changed files with 58 additions and 53 deletions

@ -27,11 +27,17 @@ const char kApplicationLocale[] = "intl.app_locale";
// A mock provider that allows us to capture pref observer changes.
class MockPrefService : public TestingPrefServiceSimple {
public:
MockPrefService() {}
~MockPrefService() override {}
MockPrefService() = default;
~MockPrefService() override = default;
MOCK_METHOD2(AddPrefObserver, void(const std::string&, PrefObserver*));
MOCK_METHOD2(RemovePrefObserver, void(const std::string&, PrefObserver*));
MOCK_METHOD(void,
AddPrefObserver,
(const std::string&, PrefObserver*),
(override));
MOCK_METHOD(void,
RemovePrefObserver,
(const std::string&, PrefObserver*),
(override));
};
// Due to overloads, base::DoNothing() cannot be passed directly to
@ -44,8 +50,8 @@ base::RepeatingClosure DoNothingClosure() {
class PrefChangeRegistrarTest : public testing::Test {
public:
PrefChangeRegistrarTest() {}
~PrefChangeRegistrarTest() override {}
PrefChangeRegistrarTest() = default;
~PrefChangeRegistrarTest() override = default;
protected:
void SetUp() override;

@ -359,8 +359,7 @@ const base::Value::List& PrefService::GetList(std::string_view path) const {
return value.GetList();
}
const base::Value* PrefService::GetUserPrefValue(
const std::string& path) const {
const base::Value* PrefService::GetUserPrefValue(std::string_view path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const Preference* pref = FindPreference(path);
@ -417,7 +416,7 @@ PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
return pref_registry_.get();
}
void PrefService::ClearPref(const std::string& path) {
void PrefService::ClearPref(std::string_view path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const Preference* pref = FindPreference(path);
@ -429,7 +428,7 @@ void PrefService::ClearPref(const std::string& path) {
user_pref_store_->RemoveValue(path, GetWriteFlags(pref));
}
void PrefService::ClearPrefsWithPrefixSilently(const std::string& prefix) {
void PrefService::ClearPrefsWithPrefixSilently(std::string_view prefix) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->RemoveValuesByPrefixSilently(prefix);
}
@ -455,55 +454,55 @@ base::android::ScopedJavaLocalRef<jobject> PrefService::GetJavaObject() {
}
#endif
void PrefService::Set(const std::string& path, const base::Value& value) {
void PrefService::Set(std::string_view path, const base::Value& value) {
SetUserPrefValue(path, value.Clone());
}
void PrefService::SetBoolean(const std::string& path, bool value) {
void PrefService::SetBoolean(std::string_view path, bool value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetInteger(const std::string& path, int value) {
void PrefService::SetInteger(std::string_view path, int value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetDouble(const std::string& path, double value) {
void PrefService::SetDouble(std::string_view path, double value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetString(const std::string& path, std::string_view value) {
void PrefService::SetString(std::string_view path, std::string_view value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetDict(const std::string& path, base::Value::Dict dict) {
void PrefService::SetDict(std::string_view path, base::Value::Dict dict) {
SetUserPrefValue(path, base::Value(std::move(dict)));
}
void PrefService::SetList(const std::string& path, base::Value::List list) {
void PrefService::SetList(std::string_view path, base::Value::List list) {
SetUserPrefValue(path, base::Value(std::move(list)));
}
void PrefService::SetFilePath(const std::string& path,
void PrefService::SetFilePath(std::string_view path,
const base::FilePath& value) {
SetUserPrefValue(path, base::FilePathToValue(value));
}
void PrefService::SetInt64(const std::string& path, int64_t value) {
void PrefService::SetInt64(std::string_view path, int64_t value) {
SetUserPrefValue(path, base::Int64ToValue(value));
}
int64_t PrefService::GetInt64(const std::string& path) const {
int64_t PrefService::GetInt64(std::string_view path) const {
const base::Value& value = GetValue(path);
std::optional<int64_t> integer = base::ValueToInt64(value);
DCHECK(integer);
return integer.value_or(0);
}
void PrefService::SetUint64(const std::string& path, uint64_t value) {
void PrefService::SetUint64(std::string_view path, uint64_t value) {
SetUserPrefValue(path, base::Value(base::NumberToString(value)));
}
uint64_t PrefService::GetUint64(const std::string& path) const {
uint64_t PrefService::GetUint64(std::string_view path) const {
const base::Value& value = GetValue(path);
if (!value.is_string())
return 0;
@ -513,29 +512,29 @@ uint64_t PrefService::GetUint64(const std::string& path) const {
return result;
}
void PrefService::SetTime(const std::string& path, base::Time value) {
void PrefService::SetTime(std::string_view path, base::Time value) {
SetUserPrefValue(path, base::TimeToValue(value));
}
base::Time PrefService::GetTime(const std::string& path) const {
base::Time PrefService::GetTime(std::string_view path) const {
const base::Value& value = GetValue(path);
std::optional<base::Time> time = base::ValueToTime(value);
DCHECK(time);
return time.value_or(base::Time());
}
void PrefService::SetTimeDelta(const std::string& path, base::TimeDelta value) {
void PrefService::SetTimeDelta(std::string_view path, base::TimeDelta value) {
SetUserPrefValue(path, base::TimeDeltaToValue(value));
}
base::TimeDelta PrefService::GetTimeDelta(const std::string& path) const {
base::TimeDelta PrefService::GetTimeDelta(std::string_view path) const {
const base::Value& value = GetValue(path);
std::optional<base::TimeDelta> time_delta = base::ValueToTimeDelta(value);
DCHECK(time_delta);
return time_delta.value_or(base::TimeDelta());
}
base::Value* PrefService::GetMutableUserPref(const std::string& path,
base::Value* PrefService::GetMutableUserPref(std::string_view path,
base::Value::Type type) {
CHECK(type == base::Value::Type::DICT || type == base::Value::Type::LIST);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
@ -581,7 +580,7 @@ void PrefService::ReportUserPrefChanged(
GetWriteFlags(FindPreference(key)));
}
void PrefService::SetUserPrefValue(const std::string& path,
void PrefService::SetUserPrefValue(std::string_view path,
base::Value new_value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
@ -710,7 +709,7 @@ const base::Value* PrefService::GetPreferenceValue(
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
void PrefService::SetStandaloneBrowserPref(const std::string& path,
void PrefService::SetStandaloneBrowserPref(std::string_view path,
const base::Value& value) {
if (!standalone_browser_pref_store_) {
LOG(WARNING) << "Failure to set value of " << path
@ -721,7 +720,7 @@ void PrefService::SetStandaloneBrowserPref(const std::string& path,
path, value.Clone(), WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
void PrefService::RemoveStandaloneBrowserPref(const std::string& path) {
void PrefService::RemoveStandaloneBrowserPref(std::string_view path) {
if (!standalone_browser_pref_store_) {
LOG(WARNING) << "Failure to remove value of " << path
<< " in standalone browser store";

@ -276,10 +276,10 @@ class COMPONENTS_PREFS_EXPORT PrefService {
const base::Value::List& GetList(std::string_view path) const;
// Removes a user pref and restores the pref to its default value.
void ClearPref(const std::string& path);
void ClearPref(std::string_view path);
// Removes user prefs that start with |prefix|.
void ClearPrefsWithPrefixSilently(const std::string& prefix);
void ClearPrefsWithPrefixSilently(std::string_view prefix);
// If the path is valid (i.e., registered), update the pref value in the user
// prefs.
@ -288,38 +288,38 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// SetDict()/SetList(), but to modify the value of a dictionary or list use
// either ScopedDictPrefUpdate or ScopedListPrefUpdate from
// scoped_user_pref_update.h.
void Set(const std::string& path, const base::Value& value);
void SetBoolean(const std::string& path, bool value);
void SetInteger(const std::string& path, int value);
void SetDouble(const std::string& path, double value);
void SetString(const std::string& path, std::string_view value);
void SetDict(const std::string& path, base::Value::Dict dict);
void SetList(const std::string& path, base::Value::List list);
void SetFilePath(const std::string& path, const base::FilePath& value);
void Set(std::string_view path, const base::Value& value);
void SetBoolean(std::string_view path, bool value);
void SetInteger(std::string_view path, int value);
void SetDouble(std::string_view path, double value);
void SetString(std::string_view path, std::string_view value);
void SetDict(std::string_view path, base::Value::Dict dict);
void SetList(std::string_view path, base::Value::List list);
void SetFilePath(std::string_view path, const base::FilePath& value);
// Int64 helper methods that actually store the given value as a string.
// Note that if obtaining the named value via GetDictionary or GetList, the
// Value type will be Type::STRING.
void SetInt64(const std::string& path, int64_t value);
int64_t GetInt64(const std::string& path) const;
void SetInt64(std::string_view path, int64_t value);
int64_t GetInt64(std::string_view path) const;
// As above, but for unsigned values.
void SetUint64(const std::string& path, uint64_t value);
uint64_t GetUint64(const std::string& path) const;
void SetUint64(std::string_view path, uint64_t value);
uint64_t GetUint64(std::string_view path) const;
// Time helper methods that actually store the given value as a string, which
// represents the number of microseconds elapsed (absolute for TimeDelta and
// relative to Windows epoch for Time variants). Note that if obtaining the
// named value via GetDictionary or GetList, the Value type will be
// Type::STRING.
void SetTime(const std::string& path, base::Time value);
base::Time GetTime(const std::string& path) const;
void SetTimeDelta(const std::string& path, base::TimeDelta value);
base::TimeDelta GetTimeDelta(const std::string& path) const;
void SetTime(std::string_view path, base::Time value);
base::Time GetTime(std::string_view path) const;
void SetTimeDelta(std::string_view path, base::TimeDelta value);
base::TimeDelta GetTimeDelta(std::string_view path) const;
// Returns the value of the given preference, from the user pref store. If
// the preference is not set in the user pref store, returns NULL.
const base::Value* GetUserPrefValue(const std::string& path) const;
const base::Value* GetUserPrefValue(std::string_view path) const;
// Changes the default value for a preference.
//
@ -411,10 +411,10 @@ class COMPONENTS_PREFS_EXPORT PrefService {
#if BUILDFLAG(IS_CHROMEOS_ASH)
// Write extension-controlled prefs from Lacros in ash.
void SetStandaloneBrowserPref(const std::string& path,
void SetStandaloneBrowserPref(std::string_view path,
const base::Value& value);
// Clear extension-controlled prefs from Lacros in ash.
void RemoveStandaloneBrowserPref(const std::string& path);
void RemoveStandaloneBrowserPref(std::string_view path);
// Clear all prefs in standalone_browser_pref_store_. Use it when rolling back
// to Ash (i.e. disabling Lacros).
@ -505,7 +505,7 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// Sets the value for this pref path in the user pref store and informs the
// PrefNotifier of the change.
void SetUserPrefValue(const std::string& path, base::Value new_value);
void SetUserPrefValue(std::string_view path, base::Value new_value);
// Load preferences from storage, attempting to diagnose and handle errors.
// This should only be called from the constructor.
@ -523,7 +523,7 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// |type| may only be Values::Type::DICT or Values::Type::LIST and
// |path| must point to a registered preference of type |type|.
// Ownership of the returned value remains at the user pref store.
base::Value* GetMutableUserPref(const std::string& path,
base::Value* GetMutableUserPref(std::string_view path,
base::Value::Type type);
// GetPreferenceValue is the equivalent of FindPreference(path)->GetValue(),