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 #ifndef WATCHER_MANAGER_H_ 16 #define WATCHER_MANAGER_H_ 17 #include <atomic> 18 #include <iostream> 19 #include <map> 20 #include <mutex> 21 #include <thread> 22 #include <vector> 23 24 #include "iremote_stub.h" 25 #include "iwatcher.h" 26 #include "list.h" 27 #include "message_parcel.h" 28 #include "param_utils.h" 29 #include "parcel.h" 30 #include "system_ability.h" 31 #include "watcher_manager_stub.h" 32 33 namespace OHOS { 34 namespace init_param { 35 class WatcherManager : public SystemAbility, public WatcherManagerStub { 36 public: 37 DECLARE_SYSTEM_ABILITY(WatcherManager); 38 DISALLOW_COPY_AND_MOVE(WatcherManager); 39 explicit WatcherManager(int32_t systemAbilityId, bool runOnCreate = true) SystemAbility(systemAbilityId,runOnCreate)40 : SystemAbility(systemAbilityId, runOnCreate) 41 { 42 } 43 ~WatcherManager() override; 44 45 class ParamWatcher; 46 class WatcherGroup; 47 using ParamWatcherPtr = std::shared_ptr<WatcherManager::ParamWatcher>; 48 using WatcherGroupPtr = std::shared_ptr<WatcherManager::WatcherGroup>; 49 50 // For death event procession 51 class DeathRecipient final : public IRemoteObject::DeathRecipient { 52 public: DeathRecipient(WatcherManager * manager)53 explicit DeathRecipient(WatcherManager *manager) : manager_(manager) {} 54 ~DeathRecipient() final = default; 55 DISALLOW_COPY_AND_MOVE(DeathRecipient); 56 void OnRemoteDied(const wptr<IRemoteObject> &remote) final; 57 private: 58 WatcherManager *manager_; 59 }; GetDeathRecipient()60 sptr<IRemoteObject::DeathRecipient> GetDeathRecipient() 61 { 62 return deathRecipient_; 63 } 64 65 class ParamWatcher { 66 public: ParamWatcher(uint32_t watcherId,const sptr<IWatcher> & watcher,const WatcherGroupPtr & group)67 ParamWatcher(uint32_t watcherId, const sptr<IWatcher> &watcher, const WatcherGroupPtr &group) 68 : watcherId_(watcherId), watcher_(watcher), group_(group) 69 { 70 ListInit(&groupNode_); 71 } 72 ~ParamWatcher() = default; 73 GetWatcherId()74 uint32_t GetWatcherId() 75 { 76 return watcherId_; 77 } GetWatcherGroup()78 WatcherGroupPtr GetWatcherGroup() 79 { 80 return group_; 81 } GetGroupNode()82 ListNode *GetGroupNode() 83 { 84 return &groupNode_; 85 } GetRemoteWatcher()86 sptr<IWatcher> GetRemoteWatcher() 87 { 88 return watcher_; 89 } ProcessParameterChange(const std::string & name,const std::string & value)90 void ProcessParameterChange(const std::string &name, const std::string &value) 91 { 92 #ifndef STARTUP_INIT_TEST 93 watcher_->OnParamerterChange(name, value); 94 #endif 95 } 96 private: 97 ListNode groupNode_; 98 uint32_t watcherId_ = { 0 }; 99 sptr<IWatcher> watcher_; 100 WatcherGroupPtr group_ { nullptr }; 101 }; 102 103 class WatcherGroup { 104 public: WatcherGroup(uint32_t groupId,const std::string & key)105 WatcherGroup(uint32_t groupId, const std::string &key) : groupId_(groupId), keyPrefix_(key) 106 { 107 ListInit(&watchers_); 108 } 109 ~WatcherGroup() = default; 110 void AddWatcher(const ParamWatcherPtr &watcher); 111 void ProcessParameterChange(const std::string &name, const std::string &value); 112 GetKeyPrefix()113 const std::string GetKeyPrefix() const 114 { 115 return keyPrefix_; 116 } Emptry()117 bool Emptry() const 118 { 119 return ListEmpty(watchers_); 120 } GetGroupId()121 uint32_t GetGroupId() const 122 { 123 return groupId_; 124 } GetWatchers()125 ListNode *GetWatchers() 126 { 127 return &watchers_; 128 } 129 private: 130 uint32_t groupId_; 131 std::string keyPrefix_ { }; 132 ListNode watchers_; 133 }; 134 135 uint32_t AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher) override; 136 int32_t DelWatcher(const std::string &keyPrefix, uint32_t watcherId) override; 137 int32_t DelWatcher(WatcherGroupPtr group, ParamWatcherPtr watcher); 138 ParamWatcherPtr GetWatcher(uint32_t watcherId); 139 ParamWatcherPtr GetWatcher(const wptr<IRemoteObject> &remote); 140 141 #ifndef STARTUP_INIT_TEST 142 protected: 143 #endif 144 void OnStart() override; 145 void OnStop() override; 146 #ifndef STARTUP_INIT_TEST 147 private: 148 #endif 149 void ProcessWatcherMessage(const std::vector<char> &buffer, uint32_t dataSize); 150 WatcherGroupPtr GetWatcherGroup(uint32_t groupId); 151 WatcherGroupPtr GetWatcherGroup(const std::string &keyPrefix); 152 void DelWatcherGroup(WatcherGroupPtr group); 153 void AddParamWatcher(const std::string &keyPrefix, WatcherGroupPtr group, ParamWatcherPtr watcher); 154 void DelParamWatcher(ParamWatcherPtr watcher); 155 void RunLoop(); 156 void StartLoop(); 157 void StopLoop(); 158 void SendLocalChange(const std::string &keyPrefix, ParamWatcherPtr watcher); 159 int SendMessage(WatcherGroupPtr group, int type); 160 int GetServerFd(bool retry); 161 int GetWatcherId(uint32_t &watcherId); 162 int GetGroupId(uint32_t &groupId); 163 private: 164 std::atomic<uint32_t> watcherId_ { 0 }; 165 std::atomic<uint32_t> groupId_ { 0 }; 166 std::mutex mutex_; 167 std::mutex watcherMutex_; 168 int serverFd_ { -1 }; 169 std::thread *pRecvThread_ { nullptr }; 170 std::atomic<bool> stop_ { false }; 171 std::map<std::string, uint32_t> groupMap_ {}; 172 std::map<uint32_t, WatcherGroupPtr> watcherGroups_ {}; 173 std::map<uint32_t, ParamWatcherPtr> watchers_ {}; 174 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {}; 175 }; 176 } // namespace init_param 177 } // namespace OHOS 178 #endif // WATCHER_MANAGER_H_