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 "running_lock_info.h" 29 #include "ipower_runninglock_callback.h" 30 #ifdef HAS_SENSORS_SENSOR_PART 31 #include "sensor_agent.h" 32 #endif 33 34 namespace OHOS { 35 namespace PowerMgr { 36 class PowerMgrService; 37 class PowerStateMachine; 38 using RunningLockMap = std::map<const sptr<IRemoteObject>, std::shared_ptr<RunningLockInner>>; 39 40 class RunningLockMgr { 41 public: 42 enum { 43 PROXIMITY_AWAY = 0, 44 PROXIMITY_CLOSE 45 }; RunningLockMgr(const wptr<PowerMgrService> & pms)46 explicit RunningLockMgr(const wptr<PowerMgrService>& pms) : pms_(pms) {} 47 ~RunningLockMgr(); 48 49 std::shared_ptr<RunningLockInner> CreateRunningLock(const sptr<IRemoteObject>& remoteObj, 50 const RunningLockParam& runningLockParam); 51 bool ReleaseLock(const sptr<IRemoteObject> remoteObj, const std::string& name = ""); 52 bool UpdateWorkSource(const sptr<IRemoteObject>& remoteObj, 53 const std::map<int32_t, std::string>& workSources); 54 bool Lock(const sptr<IRemoteObject>& remoteObj); 55 bool UnLock(const sptr<IRemoteObject> remoteObj, const std::string& name = ""); 56 void WriteHiSysEvent(std::shared_ptr<RunningLockInner>& lockInner); 57 void RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 58 void UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 59 void QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists); 60 uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 61 uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 62 bool Init(); 63 bool ExistValidRunningLock(); 64 std::shared_ptr<RunningLockInner> GetRunningLockInner(const sptr<IRemoteObject>& remoteObj); 65 std::shared_ptr<RunningLockInner> GetRunningLockInnerByName(const std::string& name); GetRunningLockMap()66 const RunningLockMap& GetRunningLockMap() const 67 { 68 return runningLocks_; 69 } 70 static void NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, const std::string &tag); 71 bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); 72 void ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos); 73 void LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 74 void UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 75 void ResetRunningLocks(); 76 bool IsUsed(const sptr<IRemoteObject>& remoteObj); 77 static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; 78 void SetProximity(uint32_t status); 79 bool IsProximityClose(); 80 void DumpInfo(std::string& result); 81 void EnableMock(IRunningLockAction* mockAction); 82 private: 83 84 void AsyncWakeup(); 85 void InitLocksTypeScreen(); 86 void InitLocksTypeBackground(); 87 void InitLocksTypeProximity(); 88 void InitLocksTypeCoordination(); 89 90 class LockCounter { 91 public: LockCounter(RunningLockType type,std::function<int32_t (bool,RunningLockParam)> activate)92 LockCounter(RunningLockType type, std::function<int32_t(bool, RunningLockParam)> activate) 93 : type_(type), activate_(activate), counter_(0) {} 94 ~LockCounter() = default; 95 int32_t Increase(const RunningLockParam& lockInnerParam); 96 int32_t Decrease(const RunningLockParam& lockInnerParam); 97 void Clear(); GetCount()98 uint32_t GetCount() 99 { 100 return counter_; 101 } GetType()102 RunningLockType GetType() 103 { 104 return type_; 105 } 106 private: 107 const RunningLockType type_; 108 std::shared_ptr<IRunningLockAction> action_; 109 std::function<int32_t(bool, RunningLockParam)> activate_; 110 uint32_t counter_; 111 }; 112 113 #ifdef HAS_SENSORS_SENSOR_PART 114 class ProximityController { 115 public: 116 ProximityController(); 117 ~ProximityController(); 118 void Enable(); 119 void Disable(); IsEnabled()120 bool IsEnabled() 121 { 122 return enabled_; 123 } IsSupported()124 bool IsSupported() 125 { 126 return support_; 127 } 128 bool IsClose(); 129 void OnClose(); 130 void OnAway(); GetStatus()131 uint32_t GetStatus() 132 { 133 return status_; 134 } 135 void Clear(); 136 static void RecordSensorCallback(SensorEvent *event); 137 private: 138 static const int32_t PROXIMITY_CLOSE_SCALAR = 0; 139 static const int32_t PROXIMITY_AWAY_SCALAR = 5; 140 static const uint32_t SAMPLING_RATE = 100000000; 141 bool support_ {false}; 142 bool enabled_ {false}; 143 bool isClose_ {false}; 144 uint32_t status_ {0}; 145 SensorUser user_; 146 }; 147 ProximityController proximityController_; 148 #endif 149 150 class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { 151 public: 152 RunningLockDeathRecipient() = default; 153 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 154 virtual ~RunningLockDeathRecipient() = default; 155 }; 156 bool InitLocks(); 157 static bool IsSceneRunningLockType(RunningLockType type); 158 static bool NeedNotify(RunningLockType type); 159 static uint64_t TransformLockid(const sptr<IRemoteObject>& remoteObj); 160 bool IsValidType(RunningLockType type); 161 void PreprocessBeforeAwake(); 162 void ProximityLockOn(); 163 RunningLockInfo FillAppRunningLockInfo(const RunningLockParam& info); 164 void UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill); 165 166 const wptr<PowerMgrService> pms_; 167 std::mutex mutex_; 168 std::mutex screenLockListsMutex_; 169 RunningLockMap runningLocks_; 170 std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_; 171 std::shared_ptr<RunningLockProxy> runninglockProxy_; 172 sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_; 173 std::shared_ptr<IRunningLockAction> runningLockAction_; 174 std::map<std::string, RunningLockInfo> unSceneLockLists_; 175 }; 176 } // namespace PowerMgr 177 } // namespace OHOS 178 #endif // POWERMGR_RUNNING_LOCK_MGR_H 179