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() const65     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 InitLocksTypeScreen();
84     void InitLocksTypeBackground();
85     void InitLocksTypeProximity();
86     void InitLocksTypeCoordination();
87 
88     class LockCounter {
89     public:
LockCounter(RunningLockType type, std::function<int32_t(bool, RunningLockParam)> activate)90         LockCounter(RunningLockType type, std::function<int32_t(bool, RunningLockParam)> activate)
91             : type_(type), activate_(activate), counter_(0) {}
92         ~LockCounter() = default;
93         int32_t Increase(const RunningLockParam& lockInnerParam);
94         int32_t Decrease(const RunningLockParam& lockInnerParam);
95         void Clear();
GetCount()96         uint32_t GetCount()
97         {
98             return counter_;
99         }
GetType()100         RunningLockType GetType()
101         {
102             return type_;
103         }
104     private:
105         const RunningLockType type_;
106         std::shared_ptr<IRunningLockAction> action_;
107         std::function<int32_t(bool, RunningLockParam)> activate_;
108         uint32_t counter_;
109     };
110 
111 #ifdef HAS_SENSORS_SENSOR_PART
112     class ProximityController {
113     public:
114         ProximityController();
115         ~ProximityController();
116         void Enable();
117         void Disable();
IsEnabled()118         bool IsEnabled()
119         {
120             return enabled_;
121         }
IsSupported()122         bool IsSupported()
123         {
124             return support_;
125         }
126         bool IsClose();
127         void OnClose();
128         void OnAway();
GetStatus()129         uint32_t GetStatus()
130         {
131             return status_;
132         }
133         void Clear();
134         static void RecordSensorCallback(SensorEvent *event);
135     private:
136         static const int32_t PROXIMITY_CLOSE_SCALAR = 0;
137         static const int32_t PROXIMITY_AWAY_SCALAR = 5;
138         static const uint32_t SAMPLING_RATE =  100000000;
139         bool support_ {false};
140         bool enabled_ {false};
141         bool isClose_ {false};
142         uint32_t status_ {0};
143         SensorUser user_;
144     };
145     ProximityController proximityController_;
146 #endif
147 
148     class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient {
149     public:
150         RunningLockDeathRecipient() = default;
151         virtual void OnRemoteDied(const wptr<IRemoteObject>& remote);
152         virtual ~RunningLockDeathRecipient() = default;
153     };
154     bool InitLocks();
155     static bool IsSceneRunningLockType(RunningLockType type);
156     static bool NeedNotify(RunningLockType type);
157     static uint64_t TransformLockid(const sptr<IRemoteObject>& remoteObj);
158     bool IsValidType(RunningLockType type);
159     void PreprocessBeforeAwake();
160     void ProximityLockOn();
161     RunningLockInfo FillAppRunningLockInfo(const RunningLockParam& info);
162     void UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill);
163 
164     const wptr<PowerMgrService> pms_;
165     std::mutex mutex_;
166     std::mutex screenLockListsMutex_;
167     RunningLockMap runningLocks_;
168     std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_;
169     std::shared_ptr<RunningLockProxy> runninglockProxy_;
170     sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_;
171     std::shared_ptr<IRunningLockAction> runningLockAction_;
172     std::map<std::string, RunningLockInfo> unSceneLockLists_;
173 };
174 } // namespace PowerMgr
175 } // namespace OHOS
176 #endif // POWERMGR_RUNNING_LOCK_MGR_H
177