0

Use std::string_view in PrefRegistry.

Bug: 349741884
Change-Id: I495cc538641abe57a93b586d467d9d0049c23b67
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5678719
Reviewed-by: Dominic Battré <battre@chromium.org>
Commit-Queue: Dominic Battré <battre@chromium.org>
Auto-Submit: Jan Keitel <jkeitel@google.com>
Cr-Commit-Position: refs/heads/main@{#1326193}
This commit is contained in:
Jan Keitel
2024-07-11 16:19:39 +00:00
committed by Chromium LUCI CQ
parent cb6e41f4b4
commit b0955c2061
6 changed files with 13 additions and 16 deletions

@@ -4,7 +4,6 @@
#include "components/prefs/default_pref_store.h" #include "components/prefs/default_pref_store.h"
#include <string>
#include <string_view> #include <string_view>
#include <utility> #include <utility>
@@ -41,8 +40,7 @@ void DefaultPrefStore::SetDefaultValue(std::string_view key, Value value) {
prefs_.SetValue(key, std::move(value)); prefs_.SetValue(key, std::move(value));
} }
void DefaultPrefStore::ReplaceDefaultValue(const std::string& key, void DefaultPrefStore::ReplaceDefaultValue(std::string_view key, Value value) {
Value value) {
DCHECK(GetValue(key, nullptr)); DCHECK(GetValue(key, nullptr));
bool notify = prefs_.SetValue(key, std::move(value)); bool notify = prefs_.SetValue(key, std::move(value));
if (notify) { if (notify) {

@@ -6,7 +6,6 @@
#define COMPONENTS_PREFS_DEFAULT_PREF_STORE_H_ #define COMPONENTS_PREFS_DEFAULT_PREF_STORE_H_
#include <memory> #include <memory>
#include <string>
#include <string_view> #include <string_view>
#include "base/observer_list.h" #include "base/observer_list.h"
@@ -39,7 +38,7 @@ class COMPONENTS_PREFS_EXPORT DefaultPrefStore : public PrefStore {
// Replaces the the value for `key` with a new value. Should only be called // Replaces the the value for `key` with a new value. Should only be called
// if a value has already been set; otherwise call SetDefaultValue(). // if a value has already been set; otherwise call SetDefaultValue().
void ReplaceDefaultValue(const std::string& key, base::Value value); void ReplaceDefaultValue(std::string_view key, base::Value value);
const_iterator begin() const; const_iterator begin() const;
const_iterator end() const; const_iterator end() const;

@@ -21,8 +21,7 @@ PrefRegistry::PrefRegistry()
PrefRegistry::~PrefRegistry() { PrefRegistry::~PrefRegistry() {
} }
uint32_t PrefRegistry::GetRegistrationFlags( uint32_t PrefRegistry::GetRegistrationFlags(std::string_view pref_name) const {
const std::string& pref_name) const {
const auto& it = registration_flags_.find(pref_name); const auto& it = registration_flags_.find(pref_name);
return it != registration_flags_.end() ? it->second : NO_REGISTRATION_FLAGS; return it != registration_flags_.end() ? it->second : NO_REGISTRATION_FLAGS;
} }
@@ -39,7 +38,7 @@ PrefRegistry::const_iterator PrefRegistry::end() const {
return defaults_->end(); return defaults_->end();
} }
void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name, void PrefRegistry::SetDefaultPrefValue(std::string_view pref_name,
base::Value value) { base::Value value) {
const base::Value* current_value = nullptr; const base::Value* current_value = nullptr;
DCHECK(defaults_->GetValue(pref_name, &current_value)) DCHECK(defaults_->GetValue(pref_name, &current_value))

@@ -14,6 +14,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "components/prefs/pref_value_map.h" #include "components/prefs/pref_value_map.h"
#include "components/prefs/prefs_export.h" #include "components/prefs/prefs_export.h"
#include "components/prefs/transparent_unordered_string_map.h"
namespace base { namespace base {
class Value; class Value;
@@ -52,8 +53,8 @@ class COMPONENTS_PREFS_EXPORT PrefRegistry
// Registering a pref as public allows other services to access it. // Registering a pref as public allows other services to access it.
static constexpr PrefRegistrationFlags PUBLIC = 1 << 9; static constexpr PrefRegistrationFlags PUBLIC = 1 << 9;
typedef PrefValueMap::const_iterator const_iterator; using const_iterator = PrefValueMap::const_iterator;
typedef std::unordered_map<std::string, uint32_t> PrefRegistrationFlagsMap; using PrefRegistrationFlagsMap = TransparentUnorderedStringMap<uint32_t>;
PrefRegistry(); PrefRegistry();
@@ -62,7 +63,7 @@ class COMPONENTS_PREFS_EXPORT PrefRegistry
// Retrieve the set of registration flags for the given preference. The return // Retrieve the set of registration flags for the given preference. The return
// value is a bitmask of PrefRegistrationFlags. // value is a bitmask of PrefRegistrationFlags.
uint32_t GetRegistrationFlags(const std::string& pref_name) const; uint32_t GetRegistrationFlags(std::string_view pref_name) const;
// Gets the registered defaults. // Gets the registered defaults.
scoped_refptr<PrefStore> defaults(); scoped_refptr<PrefStore> defaults();
@@ -74,7 +75,7 @@ class COMPONENTS_PREFS_EXPORT PrefRegistry
// Changes the default value for a preference. // 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); void SetDefaultPrefValue(std::string_view pref_name, base::Value value);
protected: protected:
friend class base::RefCounted<PrefRegistry>; friend class base::RefCounted<PrefRegistry>;

@@ -383,14 +383,14 @@ const base::Value* PrefService::GetUserPrefValue(std::string_view path) const {
return value; return value;
} }
void PrefService::SetDefaultPrefValue(const std::string& path, void PrefService::SetDefaultPrefValue(std::string_view path,
base::Value value) { base::Value value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_registry_->SetDefaultPrefValue(path, std::move(value)); pref_registry_->SetDefaultPrefValue(path, std::move(value));
} }
const base::Value* PrefService::GetDefaultPrefValue( const base::Value* PrefService::GetDefaultPrefValue(
const std::string& path) const { std::string_view path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Lookup the preference in the default store. // Lookup the preference in the default store.
const base::Value* value = nullptr; const base::Value* value = nullptr;

@@ -325,12 +325,12 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// //
// Will cause a pref change notification to be fired if this causes // Will cause a pref change notification to be fired if this causes
// the effective value to change. // the effective value to change.
void SetDefaultPrefValue(const std::string& path, base::Value value); void SetDefaultPrefValue(std::string_view path, base::Value value);
// Returns the default value of the given preference. |path| must point to a // Returns the default value of the given preference. |path| must point to a
// registered preference. In that case, will never return nullptr, so callers // registered preference. In that case, will never return nullptr, so callers
// do not need to check this. // do not need to check this.
const base::Value* GetDefaultPrefValue(const std::string& path) const; const base::Value* GetDefaultPrefValue(std::string_view path) const;
// Returns true if a value has been set for the specified path. // Returns true if a value has been set for the specified path.
// NOTE: this is NOT the same as FindPreference. In particular // NOTE: this is NOT the same as FindPreference. In particular