Chromium 监听prefs变化(c++)

监听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
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值