监听prefs变化主要用components\prefs\pref_change_registrar.h
class PrefService;
// Automatically manages the registration of one or more pref change observers
// with a PrefStore. Functions much like NotificationRegistrar, but specifically
// manages observers of preference changes. When the Registrar is destroyed,
// all registered observers are automatically unregistered with the PrefStore.
class COMPONENTS_PREFS_EXPORT PrefChangeRegistrar final : public PrefObserver {
public:
// You can register this type of callback if you need to know the
// path of the preference that is changing.
using NamedChangeCallback = base::RepeatingCallback<void(const std::string&)>;
PrefChangeRegistrar();
PrefChangeRegistrar(const PrefChangeRegistrar&) = delete;
PrefChangeRegistrar& operator=(const PrefChangeRegistrar&) = delete;
~PrefChangeRegistrar();
// Must be called before adding or removing observers. Can be called more
// than once as long as the value of |service| doesn't change.
void Init(PrefService* service);
// Adds a pref observer for the specified pref |path| and |obs| observer
// object. All registered observers will be automatically unregistered
// when the registrar's destructor is called.
//
// The second version binds a callback that will receive the path of
// the preference that is changing as its parameter.
//
// Only one observer may be registered per path.
void Add(const std::string& path, const base::RepeatingClosure& obs);
void Add(const std::string& path, const NamedChangeCallback& obs);
// Removes the pref observer registered for |path|.
void Remove(const std::string& path);
// Removes all observers that have been previously added with a call to Add.
void RemoveAll();
// Returns true if no pref observers are registered.
bool IsEmpty() const;
// Check whether |pref| is in the set of preferences being observed.
bool IsObserved(const std::string& pref);
// Check whether any of the observed preferences has the managed bit set.
bool IsManaged();
// Return the PrefService for this registrar.
PrefService* prefs();
const PrefService* prefs() const;
private:
// PrefObserver:
void OnPreferenceChanged(PrefService* service,
const std::string& pref_name) override;
static void InvokeUnnamedCallback(const base::RepeatingClosure& callback,
const std::string& pref_name);
using ObserverMap = std::map<std::string, NamedChangeCallback>;
ObserverMap observers_;
raw_ptr<PrefService> service_;
};
可以参考proximity_auth_profile_pref_manager.h实现
示例代码:
1、在头文件声明
ash\components\proximity_auth\proximity_auth_profile_pref_manager.h
// Listens to pref changes so they can be synced to the local state.
PrefChangeRegistrar registrar_;
2、ash\components\proximity_auth\proximity_auth_profile_pref_manager.cc里面添加监听
ProximityAuthProfilePrefManager::ProximityAuthProfilePrefManager(
PrefService* pref_service,
ash::multidevice_setup::MultiDeviceSetupClient* multidevice_setup_client)
: pref_service_(pref_service),
multidevice_setup_client_(multidevice_setup_client) {
}
ProximityAuthProfilePrefManager::~ProximityAuthProfilePrefManager() {
registrar_.RemoveAll(); //移除监听
}
void ProximityAuthProfilePrefManager::StartSyncingToLocalState(
PrefService* local_state,
const AccountId& account_id) {
auto on_pref_changed_callback = base::BindRepeating(
&ProximityAuthProfilePrefManager::SyncPrefsToLocalState,
weak_ptr_factory_.GetWeakPtr());
registrar_.Init(pref_service_); //初始化
//添加特定prefs监听
registrar_.Add(ash::multidevice_setup::kSmartLockAllowedPrefName,
on_pref_changed_callback);
registrar_.Add(ash::multidevice_setup::kSmartLockEnabledDeprecatedPrefName,
on_pref_changed_callback);
registrar_.Add(proximity_auth::prefs::kProximityAuthIsChromeOSLoginEnabled,
on_pref_changed_callback);
registrar_.Add(ash::multidevice_setup::kSmartLockSigninAllowedPrefName,
on_pref_changed_callback);
SyncPrefsToLocalState();
}
void ProximityAuthProfilePrefManager::SyncPrefsToLocalState() {
//变化之后to something
}
Chromium 监听prefs变化(c++)
于 2024-05-22 13:02:53 首次发布