1 /* 2 * Copyright (c) 2021-2023 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 22 #include <iremote_object.h> 23 24 #include "actions/irunning_lock_action.h" 25 #include "running_lock_inner.h" 26 #include "running_lock_proxy.h" 27 #include "running_lock_token_stub.h" 28 #include "sensor_agent.h" 29 30 namespace OHOS { 31 namespace PowerMgr { 32 class PowerMgrService; 33 class PowerStateMachine; 34 using RunningLockMap = std::map<const sptr<IRemoteObject>, std::shared_ptr<RunningLockInner>>; 35 36 class RunningLockMgr { 37 public: 38 enum { 39 PROXIMITY_AWAY = 0, 40 PROXIMITY_CLOSE 41 }; RunningLockMgr(const wptr<PowerMgrService> & pms)42 explicit RunningLockMgr(const wptr<PowerMgrService>& pms) : pms_(pms) {} 43 ~RunningLockMgr(); 44 45 std::shared_ptr<RunningLockInner> CreateRunningLock(const sptr<IRemoteObject>& remoteObj, 46 const RunningLockParam& runningLockParam); 47 bool ReleaseLock(const sptr<IRemoteObject> remoteObj); 48 void Lock(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS = -1); 49 void UnLock(const sptr<IRemoteObject> remoteObj); 50 uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 51 uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 52 bool Init(); 53 bool ExistValidRunningLock(); 54 std::shared_ptr<RunningLockInner> GetRunningLockInner(const sptr<IRemoteObject>& remoteObj); GetRunningLockMap()55 const RunningLockMap& GetRunningLockMap() const 56 { 57 return runningLocks_; 58 } 59 bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); 60 void ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos); 61 void ResetRunningLocks(); 62 bool IsUsed(const sptr<IRemoteObject>& remoteObj); 63 static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; 64 void CheckOverTime(); 65 void SetProximity(uint32_t status); 66 void DumpInfo(std::string& result); 67 void EnableMock(IRunningLockAction* mockAction); 68 private: 69 static constexpr const char * const RUNNINGLOCK_TAG_BACKGROUND = "OHOS.RunningLock.Background"; 70 71 void InitLocksTypeScreen(); 72 void InitLocksTypeBackground(); 73 void InitLocksTypeProximity(); 74 void ProxyRunningLockInner(bool isProxied, pid_t pid, pid_t uid); 75 76 class SystemLock { 77 public: 78 SystemLock(std::shared_ptr<IRunningLockAction> action, RunningLockType type, const std::string& name); 79 ~SystemLock() = default; 80 void Lock(); 81 void Unlock(); IsLocking()82 bool IsLocking() 83 { 84 return locking_; 85 }; EnableMock(std::shared_ptr<IRunningLockAction> & mock)86 void EnableMock(std::shared_ptr<IRunningLockAction>& mock) 87 { 88 locking_ = false; 89 action_ = mock; 90 } 91 private: 92 std::shared_ptr<IRunningLockAction> action_; 93 RunningLockParam param_; 94 bool locking_; 95 }; 96 97 class LockCounter { 98 public: LockCounter(RunningLockType type,std::function<void (bool)> activate)99 LockCounter(RunningLockType type, std::function<void(bool)> activate) 100 : type_(type), activate_(activate), counter_(0) {} 101 ~LockCounter() = default; 102 uint32_t Increase(const sptr<IRemoteObject>& remoteObj, 103 std::shared_ptr<RunningLockInner>& lockInner); 104 uint32_t Decrease(const sptr<IRemoteObject> remoteObj, 105 std::shared_ptr<RunningLockInner>& lockInner); 106 void Clear(); GetCount()107 uint32_t GetCount() 108 { 109 return counter_; 110 } GetType()111 RunningLockType GetType() 112 { 113 return type_; 114 } 115 private: 116 const RunningLockType type_; 117 std::shared_ptr<IRunningLockAction> action_; 118 std::function<void(bool)> activate_; 119 uint32_t counter_; 120 }; 121 122 class ProximityController { 123 public: 124 ProximityController(); 125 ~ProximityController(); 126 void Enable(); 127 void Disable(); IsEnabled()128 bool IsEnabled() 129 { 130 return enabled_; 131 } IsSupported()132 bool IsSupported() 133 { 134 return support_; 135 } 136 bool IsClose(); 137 void OnClose(); 138 void OnAway(); GetStatus()139 uint32_t GetStatus() 140 { 141 return status_; 142 } 143 void Clear(); 144 static void RecordSensorCallback(SensorEvent *event); 145 private: 146 static const int32_t PROXIMITY_CLOSE_SCALAR = 0; 147 static const int32_t PROXIMITY_AWAY_SCALAR = 5; 148 static const uint32_t SAMPLING_RATE = 100000000; 149 bool support_ {false}; 150 bool enabled_ {false}; 151 bool isClose {false}; 152 uint32_t status_ {0}; 153 SensorUser user_; 154 }; 155 156 class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { 157 public: 158 RunningLockDeathRecipient() = default; 159 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 160 virtual ~RunningLockDeathRecipient() = default; 161 }; 162 bool InitLocks(); 163 void LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 164 void UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 165 void RemoveAndPostUnlockTask(const sptr<IRemoteObject>& remoteObj, int32_t timeOutMS = -1); 166 bool IsSceneRunningLockType(RunningLockType type); 167 const wptr<PowerMgrService> pms_; 168 ProximityController proximityController_; 169 std::mutex mutex_; 170 RunningLockMap runningLocks_; 171 std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_; 172 std::shared_ptr<RunningLockProxy> runninglockProxy_; 173 sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_; 174 std::shared_ptr<IRunningLockAction> runningLockAction_; 175 std::shared_ptr<SystemLock> backgroundLock_ = nullptr; 176 enum RunningLockChangedType { 177 NOTIFY_RUNNINGLOCK_ADD, 178 NOTIFY_RUNNINGLOCK_REMOVE, 179 NOTIFY_RUNNINGLOCK_OVERTIME, 180 RUNNINGLOCK_CHANGED_BUTT 181 }; 182 const std::array<std::string, RUNNINGLOCK_CHANGED_BUTT> runninglockNotifyStr_ { 183 "DUBAI_TAG_RUNNINGLOCK_ADD", "DUBAI_TAG_RUNNINGLOCK_REMOVE", "DUBAI_TAG_RUNNINGLOCK_OVERTIME" 184 }; 185 void NotifyRunningLockChanged(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner, 186 RunningLockChangedType changeType); 187 void SendCheckOverTimeMsg(int64_t delayTime); 188 void NotifyHiViewRunningLockInfo(const RunningLockInner& lockInner, RunningLockChangedType changeType) const; 189 void NotifyHiView(RunningLockChangedType changeType, const RunningLockInner& lockInner) const; 190 }; 191 } // namespace PowerMgr 192 } // namespace OHOS 193 #endif // POWERMGR_RUNNING_LOCK_MGR_H 194