1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef WATCHER_MANAGER_KITS_H 17 #define WATCHER_MANAGER_KITS_H 18 #include <functional> 19 #include <map> 20 #include <mutex> 21 #include <thread> 22 23 #include "iwatcher.h" 24 #include "iwatcher_manager.h" 25 #include "singleton.h" 26 #include "init_param.h" 27 #include "watcher.h" 28 #include "watcher_utils.h" 29 30 namespace OHOS { 31 namespace init_param { 32 class WatcherManagerKits final : public DelayedRefSingleton<WatcherManagerKits> { 33 DECLARE_DELAYED_REF_SINGLETON(WatcherManagerKits); 34 public: 35 DISALLOW_COPY_AND_MOVE(WatcherManagerKits); 36 37 static WatcherManagerKits &GetInstance(void); 38 int32_t AddWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context); 39 int32_t DelWatcher(const std::string &keyPrefix, ParameterChangePtr callback, void *context); 40 void ReAddWatcher(void); 41 42 #ifndef STARTUP_INIT_TEST 43 private: 44 #endif 45 class ParameterChangeListener { 46 public: ParameterChangeListener(ParameterChangePtr callback,void * context)47 ParameterChangeListener(ParameterChangePtr callback, void *context) 48 : callback_(callback), context_(context) {} 49 ~ParameterChangeListener(void) = default; 50 IsEqual(ParameterChangePtr callback,const void * context)51 bool IsEqual(ParameterChangePtr callback, const void *context) const 52 { 53 return (callback == callback_ && context == context_); 54 } 55 void OnParameterChange(const std::string &name, const std::string &value); CheckValueChange(const std::string & value)56 bool CheckValueChange(const std::string &value) 57 { 58 bool ret = (value_ == value); 59 value_ = value; 60 return ret; 61 } 62 private: 63 std::string value_ {}; 64 ParameterChangePtr callback_ { nullptr }; 65 void *context_ { nullptr }; 66 }; 67 68 class ParamWatcher { 69 public: ParamWatcher(const std::string & key)70 explicit ParamWatcher(const std::string &key) : keyPrefix_(key) {} ~ParamWatcher(void)71 ~ParamWatcher(void) 72 { 73 parameterChangeListeners.clear(); 74 }; 75 void OnParameterChange(const std::string &name, const std::string &value); 76 int AddParameterListener(ParameterChangePtr callback, void *context); 77 int DelParameterListener(ParameterChangePtr callback, void *context); 78 private: 79 ParameterChangeListener *GetParameterListener(uint32_t *idx); 80 void RemoveParameterListener(uint32_t idx); 81 std::string keyPrefix_ {}; 82 std::mutex mutex_; 83 uint32_t listenerId_ { 0 }; 84 std::map<uint32_t, std::shared_ptr<ParameterChangeListener>> parameterChangeListeners; 85 }; 86 87 class RemoteWatcher final : public Watcher { 88 public: RemoteWatcher(WatcherManagerKits * watcherManager)89 explicit RemoteWatcher(WatcherManagerKits *watcherManager) : watcherManager_(watcherManager) {} ~RemoteWatcher(void)90 ~RemoteWatcher(void) override {} 91 92 void OnParameterChange(const std::string &prefix, const std::string &name, const std::string &value) final; 93 private: 94 WatcherManagerKits *watcherManager_ = { nullptr }; 95 }; 96 97 using ParamWatcherKitPtr = std::shared_ptr<WatcherManagerKits::ParamWatcher>; 98 // For death event procession 99 class DeathRecipient final : public IRemoteObject::DeathRecipient { 100 public: 101 DeathRecipient(void) = default; 102 ~DeathRecipient(void) final = default; 103 DISALLOW_COPY_AND_MOVE(DeathRecipient); 104 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 105 }; GetDeathRecipient(void)106 sptr<IRemoteObject::DeathRecipient> GetDeathRecipient(void) 107 { 108 return deathRecipient_; 109 } 110 111 uint32_t GetRemoteWatcher(void); 112 ParamWatcher *GetParamWatcher(const std::string &keyPrefix); 113 void ResetService(const wptr<IRemoteObject> &remote); 114 sptr<IWatcherManager> GetService(void); 115 std::mutex lock_; 116 sptr<IWatcherManager> watcherManager_ {}; 117 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {}; 118 119 std::mutex mutex_; 120 uint32_t remoteWatcherId_ = { 0 }; 121 sptr<Watcher> remoteWatcher_ = { nullptr }; 122 std::map<std::string, ParamWatcherKitPtr> watchers_; 123 std::atomic<bool> stop_ { false }; 124 std::thread *threadForReWatch_ { nullptr }; 125 }; 126 } // namespace init_param 127 } // namespace OHOS 128 #endif // WATCHER_MANAGER_KITS_H 129