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>& remoteObj, const RunningLockInfo& runningLockInfo, 54 const UserIPCInfo &userIPCinfo, uint32_t timeOutMS = 0); 55 void UnLock(const sptr<IRemoteObject> remoteObj); 56 std::shared_ptr<RunningLockInner> CreateRunningLock(const sptr<IRemoteObject>& remoteObj, 57 const RunningLockInfo& runningLockInfo, const UserIPCInfo &userIPCinfo); 58 bool ReleaseLock(const sptr<IRemoteObject> remoteObj); 59 uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 60 uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 61 void SetWorkTriggerList(const sptr<IRemoteObject>& remoteObj, const WorkTriggerList& workTriggerList); 62 bool Init(); 63 bool ExistValidRunningLock(); 64 std::shared_ptr<RunningLockInner> GetRunningLockInner(const sptr<IRemoteObject>& remoteObj); 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>& remoteObj); 75 static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; 76 void CheckOverTime(); 77 void SetProximity(uint32_t status); 78 void DumpInfo(std::string& result); 79 void EnableMock(IRunningLockAction* mockAction); 80 private: 81 static constexpr const char * const LOCK_TAG_APP = "lock_app"; 82 static constexpr const char * const LOCK_TAG_DISPLAY = "lock_display"; 83 static constexpr const char * const LOCK_TAG_OTHER = "lock_other"; 84 85 void InitLocksTypeScreen(); 86 void InitLocksTypeBackground(); 87 void InitLocksTypeProximity(); 88 89 class SystemLock { 90 public: SystemLock(std::shared_ptr<IRunningLockAction> action,const char * const tag)91 SystemLock(std::shared_ptr<IRunningLockAction> action, const char * const tag) 92 : action_(action), tag_(tag), locking_(false) {}; 93 ~SystemLock() = default; 94 void Lock(); 95 void Unlock(); IsLocking()96 bool IsLocking() 97 { 98 return locking_; 99 }; EnableMock(std::shared_ptr<IRunningLockAction> & mock)100 void EnableMock(std::shared_ptr<IRunningLockAction>& mock) 101 { 102 locking_ = false; 103 action_ = mock; 104 } 105 private: 106 std::shared_ptr<IRunningLockAction> action_; 107 const std::string tag_; 108 bool locking_; 109 }; 110 111 class LockCounter { 112 public: LockCounter(RunningLockType type,std::function<void (bool)> activate)113 LockCounter(RunningLockType type, std::function<void(bool)> activate) 114 : type_(type), activate_(activate), counter_(0) {} 115 ~LockCounter() = default; 116 uint32_t Increase(const sptr<IRemoteObject>& remoteObj, 117 std::shared_ptr<RunningLockInner>& lockInner); 118 uint32_t Decrease(const sptr<IRemoteObject> remoteObj, 119 std::shared_ptr<RunningLockInner>& lockInner); 120 void Clear(); GetCount()121 uint32_t GetCount() 122 { 123 return counter_; 124 } GetType()125 RunningLockType GetType() 126 { 127 return type_; 128 } 129 private: 130 const RunningLockType type_; 131 std::shared_ptr<IRunningLockAction> action_; 132 std::function<void(bool)> activate_; 133 uint32_t counter_; 134 }; 135 136 class ProximityController { 137 public: 138 ProximityController(); 139 ~ProximityController(); 140 void Enable(); 141 void Disable(); IsEnabled()142 bool IsEnabled() 143 { 144 return enabled_; 145 } IsSupported()146 bool IsSupported() 147 { 148 return support_; 149 } 150 bool IsClose(); 151 void OnClose(); 152 void OnAway(); GetStatus()153 uint32_t GetStatus() 154 { 155 return status_; 156 } 157 void Clear(); 158 static void RecordSensorCallback(SensorEvent *event); 159 private: 160 static const int32_t PROXIMITY_CLOSE_SCALAR = 0; 161 static const int32_t PROXIMITY_AWAY_SCALAR = 5; 162 static const uint32_t SAMPLING_RATE = 100000000; 163 bool support_ {false}; 164 bool enabled_ {false}; 165 bool isClose {false}; 166 uint32_t status_ {0}; 167 SensorUser user_; 168 }; 169 170 class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { 171 public: 172 RunningLockDeathRecipient() = default; 173 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 174 virtual ~RunningLockDeathRecipient() = default; 175 }; 176 bool InitLocks(); 177 bool MatchProxyMap(const UserIPCInfo& userIPCinfo); 178 void SetRunningLockDisableFlag(std::shared_ptr<RunningLockInner>& lockInner, bool forceRefresh = false); 179 void LockReally(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 180 void UnLockReally(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 181 void ProxyRunningLockInner(bool proxyLock); 182 void RemoveAndPostUnlockTask(const sptr<IRemoteObject>& remoteObj, uint32_t timeOutMS = 0); 183 const wptr<PowerMgrService> pms_; 184 ProximityController proximityController_; 185 std::weak_ptr<PowermsEventHandler> handler_; 186 std::mutex mutex_; 187 RunningLockMap runningLocks_; 188 std::map<SystemLockType, std::shared_ptr<SystemLock>> systemLocks_; 189 std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_; 190 RunningLockProxyMap proxyMap_; 191 sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_; 192 std::shared_ptr<IRunningLockAction> runningLockAction_; 193 enum RunningLockChangedType { 194 NOTIFY_RUNNINGLOCK_ADD, 195 NOTIFY_RUNNINGLOCK_REMOVE, 196 NOTIFY_RUNNINGLOCK_WORKTRIGGER_CHANGED, 197 NOTIFY_RUNNINGLOCK_OVERTIME, 198 RUNNINGLOCK_CHANGED_BUTT 199 }; 200 const std::array<std::string, RUNNINGLOCK_CHANGED_BUTT> runninglockNotifyStr_ { 201 "DUBAI_TAG_RUNNINGLOCK_ADD", "DUBAI_TAG_RUNNINGLOCK_REMOVE", 202 "DUBAI_TAG_RUNNINGLOCK_WORKTRIGGER_CHANGED", "DUBAI_TAG_RUNNINGLOCK_OVERTIME" 203 }; 204 void NotifyRunningLockChanged(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner, 205 RunningLockChangedType changeType); 206 void SendCheckOverTimeMsg(int64_t delayTime); 207 void NotifyHiViewRunningLockInfo(const RunningLockInner& lockInner, RunningLockChangedType changeType) const; 208 void NotifyHiView(RunningLockChangedType changeType, const RunningLockInner& lockInner) const; 209 }; 210 } // namespace PowerMgr 211 } // namespace OHOS 212 #endif // POWERMGR_RUNNING_LOCK_MGR_H 213