1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H
16 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H
17 #include "ibase_strategy.h"
18 
19 #include <unordered_map>
20 #include <set>
21 #include <string>
22 
23 namespace OHOS {
24 namespace DevStandbyMgr {
25 struct ProxiedAppInfo {
26     std::string name_ {""};
27     int32_t uid_ {-1};
28     int32_t pid_ {-1};
29     uint8_t appExemptionFlag_ {0};
30 };
31 
32 struct ProxiedProcInfo {
33     std::string name_ {""};
34     int32_t uid_ {-1};
35     std::set<int32_t> pids_ {};
36     uint8_t appExemptionFlag_ {0};
37 };
38 
39 class RunningLockStrategy : public IBaseStrategy {
40 public:
41     /**
42      * @brief RunningLockStrategy HandleEvent by StandbyMessage.
43      */
44     void HandleEvent(const StandbyMessage& message) override;
45 
46     /**
47      * @brief RunningLockStrategy OnCreated.
48      *
49      * @return ERR_OK if OnCreated success, failed with other code.
50      */
51     ErrCode OnCreated() override;
52 
53     /**
54      * @brief RunningLockStrategy OnDestroy.
55      *
56      * @return ERR_OK if OnDestroy success, failed with other code.
57      */
58     ErrCode OnDestroy() override;
59     void ShellDump(const std::vector<std::string>& argsInStr, std::string& result) override;
60 protected:
61     // native processes will not be proxied, except in restriction list
62     virtual ErrCode InitNativeProcInfo();
63     // add exemption flag according to message
64     virtual void AddExemptionFlag(int32_t uid, const std::string& name, uint8_t flag);
65     // remove exemption flag according to message
66     virtual void RemoveExemptionFlag(int32_t uid, const std::string& name, uint8_t flag);
67     // proxy app and native process
68     virtual ErrCode ProxyAppAndProcess(bool isProxied);
69     // clear record of proxied app and process
70     virtual void ClearProxyRecord();
71 
72     void SetProxiedAppList(std::vector<std::pair<int32_t, int32_t>>& proxiedAppList,
73         const ProxiedProcInfo& info);
74     void ProxyRunningLockList(bool isProxied, const std::vector<std::pair<int32_t, int32_t>>& proxiedAppList);
75 private:
76     // update exemtion list when received exemtion list changed event
77     ErrCode UpdateExemptionList(const StandbyMessage& message);
78     // update resource config when received condition changed event
79     ErrCode UpdateResourceConfig(const StandbyMessage& message);
80     ErrCode StartProxy(const StandbyMessage& message);
81     ErrCode StartProxyInner();
82     ErrCode StopProxy(const StandbyMessage& message);
83     ErrCode StopProxyInner();
84     // add exemption to background task and work_scheduler task, unproxy running lock
85     ErrCode UpdateBgTaskAppStatus(const StandbyMessage& message);
86     void ResetProxyStatus(const StandbyMessage& message);
87 
88     // application in exemption list or with bgtask will not be proxied.
89     ErrCode InitProxiedAppInfo();
90     // get all apps, system apps defaultly not be restricted.
91     ErrCode GetAllAppInfos();
92     ErrCode GetAllRunningAppInfo();
93     ErrCode GetForegroundApplications();
94     // get running work scheduler task and add work_scheduler flag to relative apps.
95     ErrCode GetWorkSchedulerTask();
96     // get background task, including continuous task and transient, defaultly not be constricted.
97     ErrCode GetBackgroundTaskApp();
98     // get exemption app list from standby service
99     ErrCode GetExemptionConfig();
100 
101     // when process is created or died, update proxy app info
102     void HandleProcessStatusChanged(const StandbyMessage& message);
103 
104     void GetAndCreateAppInfo(uint32_t uid, uint32_t pid, const std::string& bundleName);
105     ErrCode GetExemptionConfigForApp(ProxiedProcInfo& appInfo, const std::string& bundleName);
106 
107     void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result);
108 protected:
109     bool isProxied_ {false};
110 private:
111     bool isIdleMaintence_ {false};
112     // proxyed app and native process info
113     std::unordered_map<std::string, ProxiedProcInfo> proxiedAppInfo_;
114 
115     std::unordered_map<std::int32_t, std::string> uidBundleNmeMap_;
116 };
117 }  // namespace DevStandbyMgr
118 }  // namespace OHOS
119 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H