• 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 #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:
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,
71         const std::string &tag, const std::string &logTag);
72     bool ProxyRunningLock(bool isProxied, pid_t pid, pid_t uid);
73     void ProxyRunningLocks(bool isProxied, const std::vector<std::pair<pid_t, pid_t>>& processInfos);
74     void LockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
75     void UnlockInnerByProxy(const sptr<IRemoteObject>& remoteObj, std::shared_ptr<RunningLockInner>& lockInner);
76     void ResetRunningLocks();
77     bool IsUsed(const sptr<IRemoteObject>& remoteObj);
78     static constexpr uint32_t CHECK_TIMEOUT_INTERVAL_MS = 60 * 1000;
79 #ifdef HAS_SENSORS_SENSOR_PART
80     void SetProximity(uint32_t status);
81     bool IsProximityClose();
82 #endif
83     void DumpInfo(std::string& result);
84     void EnableMock(IRunningLockAction* mockAction);
85 private:
86 
87     void AsyncWakeup();
88     void InitLocksTypeScreen();
89     void InitLocksTypeBackground();
90 #ifdef HAS_SENSORS_SENSOR_PART
91     void InitLocksTypeProximity();
92 #endif
93     void InitLocksTypeCoordination();
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         void OnClose();
125         void OnAway();
126         static void RecordSensorCallback(SensorEvent *event);
127     };
128     ProximityController proximityController_;
129 #endif
130 
131     class RunningLockDeathRecipient : public IRemoteObject::DeathRecipient {
132     public:
133         RunningLockDeathRecipient() = default;
134         virtual void OnRemoteDied(const wptr<IRemoteObject>& remote);
135         virtual ~RunningLockDeathRecipient() = default;
136     };
137     bool InitLocks();
138     static bool IsSceneRunningLockType(RunningLockType type);
139     static bool NeedNotify(RunningLockType type);
140     static uint64_t TransformLockid(const sptr<IRemoteObject>& remoteObj);
141     bool IsValidType(RunningLockType type);
142     void PreprocessBeforeAwake();
143 #ifdef HAS_SENSORS_SENSOR_PART
144     void ProximityLockOn();
145 #endif
146     RunningLockInfo FillAppRunningLockInfo(const RunningLockParam& info);
147     void UpdateUnSceneLockLists(RunningLockParam& singleLockParam, bool fill);
148 
149     const wptr<PowerMgrService> pms_;
150     std::mutex mutex_;
151     std::mutex screenLockListsMutex_;
152     RunningLockMap runningLocks_;
153     std::map<RunningLockType, std::shared_ptr<LockCounter>> lockCounters_;
154     std::shared_ptr<RunningLockProxy> runninglockProxy_;
155     sptr<IRemoteObject::DeathRecipient> runningLockDeathRecipient_;
156     std::shared_ptr<IRunningLockAction> runningLockAction_;
157     std::map<std::string, RunningLockInfo> unSceneLockLists_;
158 };
159 } // namespace PowerMgr
160 } // namespace OHOS
161 #endif // POWERMGR_RUNNING_LOCK_MGR_H
162