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 "proximity_controller_base.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: 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, const std::string& name = ""); 48 bool UpdateWorkSource(const sptr<IRemoteObject>& remoteObj, 49 const std::map<int32_t, std::string>& workSources); 50 bool Lock(const sptr<IRemoteObject>& remoteObj); 51 bool UnLock(const sptr<IRemoteObject> remoteObj, const std::string& name = ""); 52 void WriteHiSysEvent(std::shared_ptr<RunningLockInner>& lockInner); 53 void RegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 54 void UnRegisterRunningLockCallback(const sptr<IPowerRunninglockCallback>& callback); 55 void QueryRunningLockLists(std::map<std::string, RunningLockInfo>& runningLockLists); 56 uint32_t GetRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 57 uint32_t GetValidRunningLockNum(RunningLockType type = RunningLockType::RUNNINGLOCK_BUTT); 58 bool Init(); 59 bool ExistValidRunningLock(); 60 std::shared_ptr<RunningLockInner> GetRunningLockInner(const sptr<IRemoteObject>& remoteObj); 61 std::shared_ptr<RunningLockInner> GetRunningLockInnerByName(const std::string& name); GetRunningLockMap()62 const RunningLockMap& GetRunningLockMap() const 63 { 64 return runningLocks_; 65 } 66 static void NotifyRunningLockChanged(const RunningLockParam& lockInnerParam, 67 const std::string &tag, const std::string &logTag); 68 bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid); 69 void ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos); 70 void LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 71 void UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner); 72 void ResetRunningLocks(); 73 bool IsUsed(const sptr<IRemoteObject>& remoteObj); 74 static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000; 75 #ifdef HAS_SENSORS_SENSOR_PART 76 void SetProximity(uint32_t status); 77 bool IsProximityClose(); 78 #endif 79 void DumpInfo(std::string& result); 80 void EnableMock(IRunningLockAction* mockAction); 81 bool IsExistAudioStream(pid_t uid); 82 private: 83 84 void AsyncWakeup(); 85 void InitLocksTypeScreen(); 86 void InitLocksTypeBackground(); 87 void InitLocksTypeCoordination(); 88 #ifdef HAS_SENSORS_SENSOR_PART 89 void InitLocksTypeProximity(); 90 bool InitProximityController(); 91 void HandleProximityCloseEvent(); 92 void HandleProximityAwayEvent(); 93 #endif 94 95 class LockCounter { 96 public: LockCounter(RunningLockType type,std::function<int32_t (bool,RunningLockParam)> activate)97 LockCounter(RunningLockType type, std::function<int32_t(bool, RunningLockParam)> activate) 98 : type_(type), activate_(activate), counter_(0) {} 99 ~LockCounter() = default; 100 int32_t Increase(const RunningLockParam& lockInnerParam); 101 int32_t Decrease(const RunningLockParam& lockInnerParam); 102 void Clear(); GetCount()103 uint32_t GetCount() 104 { 105 return counter_; 106 } GetType()107 RunningLockType GetType() 108 { 109 return type_; 110 } 111 private: 112 const RunningLockType type_; 113 std::shared_ptr<IRunningLockAction> action_; 114 std::function<int32_t(bool, RunningLockParam)> activate_; 115 uint32_t counter_; 116 }; 117 118 #ifdef HAS_SENSORS_SENSOR_PART 119 class ProximityController : public ProximityControllerBase { 120 public: 121 explicit ProximityController(const std::string& name = "RunningLock", SensorCallbackFunc ProximityControllerBase(name,callback)122 callback = &ProximityController::RecordSensorCallback) : ProximityControllerBase(name, callback) {} ~ProximityController()123 ~ProximityController() override {} 124 virtual void OnClose() override; 125 virtual void OnAway() override; 126 static void RecordSensorCallback(SensorEvent *event); 127 }; 128 #endif 129 130 class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient { 131 public: 132 RunningLockDeathRecipient() = default; 133 virtual void OnRemoteDied(const wptr<IRemoteObject>& remote); 134 virtual ~RunningLockDeathRecipient() = default; 135 }; 136 bool InitLocks(); 137 static bool IsSceneRunningLockType(RunningLockType type); 138 static bool NeedNotify(RunningLockType type); 139 static uint64_t TransformLockid(const sptr<IRemoteObject>& remoteObj); 140 bool IsValidType(RunningLockType type); 141 void PreprocessBeforeAwake(); 142 RunningLockInfo FillAppRunningLockInfo(const RunningLockParam& info); 143 void UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill); 144 145 const wptr<PowerMgrService> pms_; 146 std::mutex mutex_; 147 std::mutex screenLockListsMutex_; 148 RunningLockMap runningLocks_; 149 std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_; 150 std::shared_ptr<IProximityController> proximityController_ {nullptr}; 151 std::shared_ptr<RunningLockProxy> runninglockProxy_; 152 sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_; 153 std::shared_ptr<IRunningLockAction> runningLockAction_; 154 std::map<std::string, RunningLockInfo> unSceneLockLists_; 155 }; 156 } // namespace PowerMgr 157 } // namespace OHOS 158 #endif // POWERMGR_RUNNING_LOCK_MGR_H 159