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