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 POWERMGR_RUNNING_LOCK_MGR_H 17 #define POWERMGR_RUNNING_LOCK_MGR_H 18 19 #include <array> 20 #include <map> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include <iremote_object.h> 25 26 #include "actions/irunning_lock_action.h" 27 #include "running_lock_inner.h" 28 #include "running_lock_token_stub.h" 29 #include "sensor_agent.h" 30 31 namespace OHOS { 32 namespace PowerMgr { 33 class PowerMgrService; 34 class PowermsEventHandler; 35 class PowerStateMachine; 36 using RunningLockMap = std::map<const sptr<IRemoteObject>, std::shared_ptr<RunningLockInner>>; 37 38 class RunningLockMgr { 39 public: 40 enum class SystemLockType : uint32_t { 41 SYSTEM_LOCK_APP = 0, 42 SYSTEM_LOCK_DISPLAY = 1, 43 SYSTEM_LOCK_OTHER 44 }; 45 enum { 46 PROXIMITY_AWAY = 0, 47 PROXIMITY_CLOSE 48 }; 49 using RunningLockProxyMap = std::unordered_map<pid_t, std::unordered_set<pid_t>>; RunningLockMgr(const wptr<PowerMgrService> & pms)50 explicit RunningLockMgr(const wptr<PowerMgrService>& pms) : pms_(pms) {} 51 ~RunningLockMgr(); 52 53 void Lock(const sptr<IRemoteObject>& token, const RunningLockInfo& runningLockInfo, 54 const UserIPCInfo &userIPCinfo, uint32_t timeOutMS = 0); 55 void UnLock(const sptr<IRemoteObject> token); 56 std::shared_ptr<RunningLockInner> CreateRunningLock(const sptr<IRemoteObject>& token, 57 const RunningLockInfo& runningLockInfo, const UserIPCInfo &userIPCinfo); 58 void ReleaseLock(const sptr<IRemoteObject> token); 59 uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 60 uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 61 void SetWorkTriggerList(const sptr<IRemoteObject>& token, const WorkTriggerList& workTriggerList); 62 bool Init(); 63 bool ExistValidRunningLock(); 64 std::shared_ptr<RunningLockInner> GetRunningLockInner(const sptr<IRemoteObject>& token); GetRunningLockMap()65 const RunningLockMap& GetRunningLockMap() const 66 { 67 return runningLocks_; 68 } GetRunningLockProxyMap()69 const RunningLockProxyMap& GetRunningLockProxyMap() const 70 { 71 return proxyMap_; 72 } 73 void ProxyRunningLock(bool proxyLock, pid_t uid, pid_t pid); 74 bool IsUsed(const sptr<IRemoteObject>& token); 75 static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; 76 static constexpr uint32_t MAX_DUMP_NUM = 10; 77 void CheckOverTime(); 78 void SetProximity(uint32_t status); 79 void DumpInfo(std::string& result); 80 void EnableMock(IRunningLockAction* mockAction); 81 private: 82 static constexpr const char * const LOCK_TAG_APP = "lock_app"; 83 static constexpr const char * const LOCK_TAG_DISPLAY = "lock_display"; 84 static constexpr const char * const LOCK_TAG_OTHER = "lock_other"; 85 86 void InitLocksTypeScreen(); 87 void InitLocksTypeBackground(); 88 void InitLocksTypeProximity(); 89 90 class SystemLock { 91 public: SystemLock(std::shared_ptr<IRunningLockAction> action,const char * const tag)92 SystemLock(std::shared_ptr<IRunningLockAction> action, const char * const tag) 93 : action_(action), tag_(tag), locking_(false) {}; 94 ~SystemLock() = default; 95 void Lock(); 96 void Unlock(); IsLocking()97 bool IsLocking() 98 { 99 return locking_; 100 }; EnableMock(std::shared_ptr<IRunningLockAction> & mock)101 void EnableMock(std::shared_ptr<IRunningLockAction>& mock) 102 { 103 locking_ = false; 104 action_ = mock; 105 } 106 private: 107 std::shared_ptr<IRunningLockAction> action_; 108 const std::string tag_; 109 bool locking_; 110 }; 111 112 class LockCounter { 113 public: LockCounter(RunningLockType type,std::function<void (bool)> activate)114 LockCounter(RunningLockType type, std::function<void(bool)> activate) 115 : type_(type), activate_(activate), counter_(0) {} 116 ~LockCounter() = default; 117 uint32_t Increase(); 118 uint32_t Decrease(); 119 void Clear(); GetCount()120 uint32_t GetCount() 121 { 122 return counter_; 123 } GetType()124 RunningLockType GetType() 125 { 126 return type_; 127 } 128 private: 129 const RunningLockType type_; 130 std::shared_ptr<IRunningLockAction> action_; 131 std::function<void(bool)> activate_; 132 uint32_t counter_; 133 }; 134 135 class ProximityController { 136 public: 137 ProximityController(); 138 ~ProximityController(); 139 void Enable(); 140 void Disable(); IsEnabled()141 bool IsEnabled() 142 { 143 return enabled_; 144 } IsSupported()145 bool IsSupported() 146 { 147 return support_; 148 } 149 bool IsClose(); 150 void OnClose(); 151 void OnAway(); GetStatus()152 uint32_t GetStatus() 153 { 154 return status_; 155 } 156 void Clear(); 157 static void RecordSensorCallback(SensorEvent *event); 158 private: 159 static const int PROXIMITY_CLOSE_SCALAR = 0; 160 static const int PROXIMITY_AWAY_SCALAR = 5; 161 static const uint32_t SAMPLING_RATE = 100000000; 162 bool support_ {false}; 163 bool enabled_ {false}; 164 bool isClose {false}; 165 uint32_t status_ {0}; 166 SensorUser user_; 167 }; 168 169 class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { 170 public: 171 RunningLockDeathRecipient() = default; 172 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 173 virtual ~RunningLockDeathRecipient() = default; 174 }; 175 bool InitLocks(); 176 bool MatchProxyMap(const UserIPCInfo& userIPCinfo); 177 void SetRunningLockDisableFlag(std::shared_ptr<RunningLockInner>& lockInner, bool forceRefresh = false); 178 void LockReally(const sptr<IRemoteObject>& token, std::shared_ptr<RunningLockInner>& lockInner); 179 void UnLockReally(const sptr<IRemoteObject>& token, std::shared_ptr<RunningLockInner>& lockInner); 180 void ProxyRunningLockInner(bool proxyLock); 181 void RemoveAndPostUnlockTask(const sptr<IRemoteObject>& token, uint32_t timeOutMS = 0); 182 const wptr<PowerMgrService> pms_; 183 ProximityController proximityController_; 184 std::weak_ptr<PowermsEventHandler> handler_; 185 std::mutex mutex_; 186 RunningLockMap runningLocks_; 187 std::map<SystemLockType, std::shared_ptr<SystemLock>> systemLocks_; 188 std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_; 189 RunningLockProxyMap proxyMap_; 190 sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_; 191 std::shared_ptr<IRunningLockAction> runningLockAction_; 192 enum RunningLockChangedType { 193 NOTIFY_RUNNINGLOCK_ADD, 194 NOTIFY_RUNNINGLOCK_REMOVE, 195 NOTIFY_RUNNINGLOCK_WORKTRIGGER_CHANGED, 196 NOTIFY_RUNNINGLOCK_OVERTIME, 197 RUNNINGLOCK_CHANGED_BUTT 198 }; 199 const std::array<std::string, RUNNINGLOCK_CHANGED_BUTT> runninglockNotifyStr_ { 200 "DUBAI_TAG_RUNNINGLOCK_ADD", "DUBAI_TAG_RUNNINGLOCK_REMOVE", 201 "DUBAI_TAG_RUNNINGLOCK_WORKTRIGGER_CHANGED", "DUBAI_TAG_RUNNINGLOCK_OVERTIME" 202 }; 203 void NotifyRunningLockChanged(const sptr<IRemoteObject>& token, std::shared_ptr<RunningLockInner>& lockInner, 204 RunningLockChangedType changeType); 205 void SendCheckOverTimeMsg(int64_t delayTime); 206 void NotifyHiViewRunningLockInfo(const std::string& tokenstr, const RunningLockInner& lockInner, 207 RunningLockChangedType changeType) const; 208 void NotifyHiView(RunningLockChangedType changeType, const std::string& msg) const; 209 }; 210 } // namespace PowerMgr 211 } // namespace OHOS 212 #endif // POWERMGR_RUNNING_LOCK_MGR_H 213