Use std::string_view in PrefNotifier.
Bug: 349741884 Change-Id: I380e8969080f906af941aa539be305ea886fecf3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5666790 Commit-Queue: Jan Keitel <jkeitel@google.com> Reviewed-by: Dominic Battré <battre@chromium.org> Reviewed-by: Marc Treib <treib@chromium.org> Auto-Submit: Jan Keitel <jkeitel@google.com> Cr-Commit-Position: refs/heads/main@{#1324161}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
f79520c87f
commit
6a0cc39bfa
chrome/browser
components/prefs
@ -59,10 +59,11 @@ void FontPrefChangeNotifier::RemoveRegistrar(Registrar* registrar) {
|
||||
}
|
||||
|
||||
void FontPrefChangeNotifier::OnPreferenceChanged(PrefService* pref_service,
|
||||
const std::string& pref_name) {
|
||||
std::string_view pref_name) {
|
||||
if (base::StartsWith(pref_name, pref_names_util::kWebKitFontPrefPrefix,
|
||||
base::CompareCase::SENSITIVE)) {
|
||||
const std::string pref_name_string(pref_name);
|
||||
for (auto& reg : registrars_)
|
||||
reg.callback_.Run(pref_name);
|
||||
reg.callback_.Run(pref_name_string);
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ class FontPrefChangeNotifier : public PrefObserver, public KeyedService {
|
||||
|
||||
// PrefObserver implementation.
|
||||
void OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref_name) override;
|
||||
std::string_view pref_name) override;
|
||||
|
||||
raw_ptr<PrefService> pref_service_; // Non-owning.
|
||||
|
||||
|
@ -74,14 +74,17 @@ bool PrefChangeRegistrar::IsEmpty() const {
|
||||
return observers_.empty();
|
||||
}
|
||||
|
||||
bool PrefChangeRegistrar::IsObserved(const std::string& pref) {
|
||||
bool PrefChangeRegistrar::IsObserved(std::string_view pref) {
|
||||
return observers_.find(pref) != observers_.end();
|
||||
}
|
||||
|
||||
void PrefChangeRegistrar::OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref) {
|
||||
if (IsObserved(pref))
|
||||
observers_[pref].Run(pref);
|
||||
std::string_view pref) {
|
||||
if (auto it = observers_.find(pref); it != observers_.end()) {
|
||||
// TODO: crbug.com/349741884 - Consider changing the callback to accept a
|
||||
// string_view.
|
||||
it->second.Run(std::string(pref));
|
||||
}
|
||||
}
|
||||
|
||||
void PrefChangeRegistrar::InvokeUnnamedCallback(
|
||||
|
@ -5,8 +5,10 @@
|
||||
#ifndef COMPONENTS_PREFS_PREF_CHANGE_REGISTRAR_H_
|
||||
#define COMPONENTS_PREFS_PREF_CHANGE_REGISTRAR_H_
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "base/functional/callback.h"
|
||||
#include "base/memory/raw_ptr.h"
|
||||
@ -60,7 +62,7 @@ class COMPONENTS_PREFS_EXPORT PrefChangeRegistrar final : public PrefObserver {
|
||||
bool IsEmpty() const;
|
||||
|
||||
// Check whether |pref| is in the set of preferences being observed.
|
||||
bool IsObserved(const std::string& pref);
|
||||
bool IsObserved(std::string_view pref);
|
||||
|
||||
// Return the PrefService for this registrar.
|
||||
PrefService* prefs();
|
||||
@ -69,12 +71,12 @@ class COMPONENTS_PREFS_EXPORT PrefChangeRegistrar final : public PrefObserver {
|
||||
private:
|
||||
// PrefObserver:
|
||||
void OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref_name) override;
|
||||
std::string_view pref_name) override;
|
||||
|
||||
static void InvokeUnnamedCallback(const base::RepeatingClosure& callback,
|
||||
const std::string& pref_name);
|
||||
|
||||
using ObserverMap = std::map<std::string, NamedChangeCallback>;
|
||||
using ObserverMap = std::map<std::string, NamedChangeCallback, std::less<>>;
|
||||
|
||||
ObserverMap observers_;
|
||||
raw_ptr<PrefService, AcrossTasksDanglingUntriaged> service_;
|
||||
|
@ -60,10 +60,10 @@ void PrefMemberBase::MoveToSequence(
|
||||
}
|
||||
|
||||
void PrefMemberBase::OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref_name) {
|
||||
std::string_view pref_name) {
|
||||
VerifyValuePrefName();
|
||||
UpdateValueFromPref((!setting_value_ && !observer_.is_null())
|
||||
? base::BindOnce(observer_, pref_name)
|
||||
? base::BindOnce(observer_, std::string(pref_name))
|
||||
: base::OnceClosure());
|
||||
}
|
||||
|
||||
|
@ -111,9 +111,9 @@ class COMPONENTS_PREFS_EXPORT PrefMemberBase : public PrefObserver {
|
||||
|
||||
void MoveToSequence(scoped_refptr<base::SequencedTaskRunner> task_runner);
|
||||
|
||||
// PrefObserver
|
||||
// PrefObserver:
|
||||
void OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref_name) override;
|
||||
std::string_view pref_name) override;
|
||||
|
||||
void VerifyValuePrefName() const {
|
||||
DCHECK(!pref_name_.empty());
|
||||
|
@ -5,7 +5,7 @@
|
||||
#ifndef COMPONENTS_PREFS_PREF_NOTIFIER_H_
|
||||
#define COMPONENTS_PREFS_PREF_NOTIFIER_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// Delegate interface used by PrefValueStore to notify its owner about changes
|
||||
// to the preference values.
|
||||
@ -17,7 +17,7 @@ class PrefNotifier {
|
||||
|
||||
// Sends out a change notification for the preference identified by
|
||||
// |pref_name|.
|
||||
virtual void OnPreferenceChanged(const std::string& pref_name) = 0;
|
||||
virtual void OnPreferenceChanged(std::string_view pref_name) = 0;
|
||||
|
||||
// Broadcasts the intialization completed notification.
|
||||
virtual void OnInitializationCompleted(bool succeeded) = 0;
|
||||
|
@ -63,16 +63,16 @@ PrefNotifierImpl::~PrefNotifierImpl() {
|
||||
init_observers_.clear();
|
||||
}
|
||||
|
||||
void PrefNotifierImpl::AddPrefObserver(const std::string& path,
|
||||
void PrefNotifierImpl::AddPrefObserver(std::string_view path,
|
||||
PrefObserver* obs) {
|
||||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
|
||||
|
||||
// Add the pref observer. ObserverList hits a DCHECK if it already is
|
||||
// in the list.
|
||||
pref_observers_[path].AddObserver(obs);
|
||||
pref_observers_[std::string(path)].AddObserver(obs);
|
||||
}
|
||||
|
||||
void PrefNotifierImpl::RemovePrefObserver(const std::string& path,
|
||||
void PrefNotifierImpl::RemovePrefObserver(std::string_view path,
|
||||
PrefObserver* obs) {
|
||||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
|
||||
|
||||
@ -99,7 +99,7 @@ void PrefNotifierImpl::AddInitObserver(base::OnceCallback<void(bool)> obs) {
|
||||
init_observers_.push_back(std::move(obs));
|
||||
}
|
||||
|
||||
void PrefNotifierImpl::OnPreferenceChanged(const std::string& path) {
|
||||
void PrefNotifierImpl::OnPreferenceChanged(std::string_view path) {
|
||||
FireObservers(path);
|
||||
}
|
||||
|
||||
@ -116,7 +116,7 @@ void PrefNotifierImpl::OnInitializationCompleted(bool succeeded) {
|
||||
std::move(observer).Run(succeeded);
|
||||
}
|
||||
|
||||
void PrefNotifierImpl::FireObservers(const std::string& path) {
|
||||
void PrefNotifierImpl::FireObservers(std::string_view path) {
|
||||
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
|
||||
|
||||
// Only send notifications for registered preferences.
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
@ -18,6 +19,7 @@
|
||||
#include "components/prefs/pref_notifier.h"
|
||||
#include "components/prefs/pref_observer.h"
|
||||
#include "components/prefs/prefs_export.h"
|
||||
#include "components/prefs/transparent_unordered_string_map.h"
|
||||
|
||||
class PrefService;
|
||||
|
||||
@ -34,8 +36,8 @@ class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier {
|
||||
|
||||
// If the pref at the given path changes, we call the observer's
|
||||
// OnPreferenceChanged method.
|
||||
void AddPrefObserver(const std::string& path, PrefObserver* observer);
|
||||
void RemovePrefObserver(const std::string& path, PrefObserver* observer);
|
||||
void AddPrefObserver(std::string_view path, PrefObserver* observer);
|
||||
void RemovePrefObserver(std::string_view path, PrefObserver* observer);
|
||||
|
||||
// These observers are called for any pref changes.
|
||||
//
|
||||
@ -52,7 +54,7 @@ class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier {
|
||||
void SetPrefService(PrefService* pref_service);
|
||||
|
||||
// PrefNotifier overrides.
|
||||
void OnPreferenceChanged(const std::string& pref_name) override;
|
||||
void OnPreferenceChanged(std::string_view pref_name) override;
|
||||
|
||||
protected:
|
||||
// PrefNotifier overrides.
|
||||
@ -61,17 +63,16 @@ class COMPONENTS_PREFS_EXPORT PrefNotifierImpl : public PrefNotifier {
|
||||
// A map from pref names to a list of observers. Observers get fired in the
|
||||
// order they are added. These should only be accessed externally for unit
|
||||
// testing.
|
||||
typedef base::ObserverList<PrefObserver>::Unchecked PrefObserverList;
|
||||
typedef std::unordered_map<std::string, PrefObserverList> PrefObserverMap;
|
||||
|
||||
typedef std::list<base::OnceCallback<void(bool)>> PrefInitObserverList;
|
||||
using PrefObserverList = base::ObserverList<PrefObserver>::Unchecked;
|
||||
using PrefObserverMap = TransparentUnorderedStringMap<PrefObserverList>;
|
||||
using PrefInitObserverList = std::list<base::OnceCallback<void(bool)>>;
|
||||
|
||||
const PrefObserverMap* pref_observers() const { return &pref_observers_; }
|
||||
|
||||
private:
|
||||
// For the given pref_name, fire any observer of the pref. Virtual so it can
|
||||
// be mocked for unit testing.
|
||||
virtual void FireObservers(const std::string& path);
|
||||
virtual void FireObservers(std::string_view path);
|
||||
|
||||
// Weak reference; the notifier is owned by the PrefService.
|
||||
raw_ptr<PrefService> pref_service_;
|
||||
|
@ -51,7 +51,7 @@ class MockPrefNotifier : public PrefNotifierImpl {
|
||||
: PrefNotifierImpl(pref_service) {}
|
||||
~MockPrefNotifier() override {}
|
||||
|
||||
MOCK_METHOD(void, FireObservers, (const std::string& path), (override));
|
||||
MOCK_METHOD(void, FireObservers, (std::string_view path), (override));
|
||||
|
||||
size_t CountObserver(const std::string& path, PrefObserver* obs) {
|
||||
auto observer_iterator = pref_observers()->find(path);
|
||||
@ -74,10 +74,10 @@ class MockPrefNotifier : public PrefNotifierImpl {
|
||||
|
||||
class PrefObserverMock : public PrefObserver {
|
||||
public:
|
||||
PrefObserverMock() {}
|
||||
virtual ~PrefObserverMock() {}
|
||||
|
||||
MOCK_METHOD2(OnPreferenceChanged, void(PrefService*, const std::string&));
|
||||
MOCK_METHOD(void,
|
||||
OnPreferenceChanged,
|
||||
(PrefService*, std::string_view),
|
||||
(override));
|
||||
};
|
||||
|
||||
// Test fixture class.
|
||||
|
@ -5,7 +5,7 @@
|
||||
#ifndef COMPONENTS_PREFS_PREF_OBSERVER_H_
|
||||
#define COMPONENTS_PREFS_PREF_OBSERVER_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
class PrefService;
|
||||
|
||||
@ -15,7 +15,7 @@ class PrefService;
|
||||
class PrefObserver {
|
||||
public:
|
||||
virtual void OnPreferenceChanged(PrefService* service,
|
||||
const std::string& pref_name) = 0;
|
||||
std::string_view pref_name) = 0;
|
||||
};
|
||||
|
||||
#endif // COMPONENTS_PREFS_PREF_OBSERVER_H_
|
||||
|
@ -40,9 +40,7 @@ void PrefValueStore::PrefStoreKeeper::Initialize(
|
||||
}
|
||||
|
||||
void PrefValueStore::PrefStoreKeeper::OnPrefValueChanged(std::string_view key) {
|
||||
// TODO: crbug.com/349741884 - Pass `std::string_view` once the interface is
|
||||
// changed.
|
||||
pref_value_store_->OnPrefValueChanged(type_, std::string(key));
|
||||
pref_value_store_->OnPrefValueChanged(type_, key);
|
||||
}
|
||||
|
||||
void PrefValueStore::PrefStoreKeeper::OnInitializationCompleted(
|
||||
@ -138,7 +136,7 @@ bool PrefValueStore::GetRecommendedValue(const std::string& name,
|
||||
}
|
||||
|
||||
void PrefValueStore::NotifyPrefChanged(
|
||||
const std::string& path,
|
||||
std::string_view path,
|
||||
PrefValueStore::PrefStoreType new_store) {
|
||||
DCHECK(new_store != INVALID_STORE);
|
||||
// A notification is sent when the pref value in any store changes. If this
|
||||
@ -280,7 +278,7 @@ bool PrefValueStore::GetValueFromStoreWithType(
|
||||
}
|
||||
|
||||
void PrefValueStore::OnPrefValueChanged(PrefValueStore::PrefStoreType type,
|
||||
const std::string& key) {
|
||||
std::string_view key) {
|
||||
NotifyPrefChanged(key, type);
|
||||
}
|
||||
|
||||
|
@ -236,11 +236,11 @@ class COMPONENTS_PREFS_EXPORT PrefValueStore {
|
||||
// the user-visible pref value has changed. Triggers the change notification
|
||||
// if the effective value of the preference has changed, or if the store
|
||||
// controlling the pref has changed.
|
||||
void NotifyPrefChanged(const std::string& path, PrefStoreType new_store);
|
||||
void NotifyPrefChanged(std::string_view, PrefStoreType new_store);
|
||||
|
||||
// Called from the PrefStoreKeeper implementation when a pref value for |key|
|
||||
// changed in the pref store for |type|.
|
||||
void OnPrefValueChanged(PrefStoreType type, const std::string& key);
|
||||
void OnPrefValueChanged(PrefStoreType type, std::string_view key);
|
||||
|
||||
// Handle the event that the store for |type| has completed initialization.
|
||||
void OnInitializationCompleted(PrefStoreType type, bool succeeded);
|
||||
|
@ -23,7 +23,7 @@ namespace {
|
||||
// Allows to capture pref notifications through gmock.
|
||||
class MockPrefNotifier : public PrefNotifier {
|
||||
public:
|
||||
MOCK_METHOD(void, OnPreferenceChanged, (const std::string&), (override));
|
||||
MOCK_METHOD(void, OnPreferenceChanged, (std::string_view), (override));
|
||||
MOCK_METHOD(void, OnInitializationCompleted, (bool), (override));
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user