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