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 DEVICE_STANDBY_EXT_BASE_NETWPRK_STRATEGY_H
16 #define DEVICE_STANDBY_EXT_BASE_NETWPRK_STRATEGY_H
17 
18 #include "ibase_strategy.h"
19 
20 namespace OHOS {
21 namespace DevStandbyMgr {
22 struct NetLimtedAppInfo {
23     std::string name_ {""};
24     uint8_t appExemptionFlag_ {0};
25 };
26 
27 class BaseNetworkStrategy : public IBaseStrategy {
28 public:
29     /**
30      * @brief BaseNetworkStrategy HandleEvent by StandbyMessage.
31      */
32     void HandleEvent(const StandbyMessage& message) override;
33 
34     /**
35      * @brief BaseNetworkStrategy OnCreated.
36      *
37      * @return ERR_OK if OnCreated success, failed with other code.
38      */
39     ErrCode OnCreated() override;
40 
41     /**
42      * @brief BaseNetworkStrategy OnDestroy.
43      *
44      * @return ERR_OK if OnDestroy success, failed with other code.
45      */
46     ErrCode OnDestroy() override;
47     void ShellDump(const std::vector<std::string>& argsInStr, std::string& result) override;
48 
49     /**
50      * @brief set net limit status.
51      *
52      * @return NetPolicy handle ret.
53      */
54     virtual int32_t HandleDeviceIdlePolicy(bool enableFirewall);
55 protected:
56     /**
57      * @brief stop power save mode, clean all exemption uid.
58      */
59     void ResetFirewallAllowList();
60 
61     /**
62      * @brief get all apps, system apps defaultly not be restricted.
63      */
64     virtual void SetFirewallAllowedList(const std::vector<uint32_t>& uids, bool isAdded) = 0;
65 
66     /**
67      * @brief set net limited mode, if netLimited is true, start net limited mode, else stop net limited mode.
68      */
69     ErrCode SetFirewallStatus(bool enableFirewall);
70 
71     /**
72      * @brief update exemption list when received exemption list changed event.
73      */
74     virtual ErrCode UpdateExemptionList(const StandbyMessage& message);
75 
76     /**
77      * @brief update resource config when received message of day night switch or sleep stat change.
78      */
79     virtual ErrCode UpdateFirewallAllowList();
80 
81     /**
82      * @brief start net limited mode when has recerved reletive event.
83      */
84     virtual ErrCode EnableNetworkFirewall(const StandbyMessage& message);
85 
86     /**
87      * @brief stop net limited mode when has recerved reletive event.
88      */
89     virtual ErrCode DisableNetworkFirewall(const StandbyMessage& message);
90 
91     /**
92      * @brief update status of app with background task.
93      */
94     virtual ErrCode UpdateBgTaskAppStatus(const StandbyMessage& message);
95 
96     /**
97      * @brief handle process creted or died.
98      */
99     virtual void HandleProcessStatusChanged(const StandbyMessage& message);
100     /**
101      * @brief application in exemption list or with bgtask will not be proxied.
102      */
103     virtual ErrCode InitNetLimitedAppInfo();
104 
105     /**
106      * @brief update allow uid and send to  Firewall.
107      */
108     virtual void SetNetAllowApps(bool isAllow);
109 
110 protected:
111     void ResetFirewallStatus(const StandbyMessage& message);
112 
113     ErrCode EnableNetworkFirewallInner();
114     ErrCode DisableNetworkFirewallInner();
115     ErrCode GetAllRunningAppInfo();
116 
117     ErrCode GetForegroundApplications();
118     // get backgroundtask, including continuous task and transient, defaultly not be constricted.
119     ErrCode GetBackgroundTaskApp();
120     // get running work scheduler task and add work_scheduler flag to relative apps.
121     ErrCode GetWorkSchedulerTask();
122     void AddExemptionFlagByUid(int32_t uid, uint8_t flag);
123     // get exemption app and restrict list from standby service
124     ErrCode GetExemptionConfig();
125     ErrCode GetExemptionConfigForApp(NetLimtedAppInfo& appInfo, const std::string& bundleName);
126 
127     void AddExemptionFlag(uint32_t uid, const std::string& bundleName, uint8_t flag);
128     void RemoveExemptionFlag(uint32_t uid, uint8_t flag);
129     void GetAndCreateAppInfo(uint32_t uid, const std::string& bundleName);
130     bool GetExemptedFlag(uint8_t appNoExemptionFlag, uint8_t appExemptionFlag);
131 protected:
132     static bool isFirewallEnabled_;
133     bool isIdleMaintence_ {false};
134     static std::unordered_map<std::int32_t, NetLimtedAppInfo> netLimitedAppInfo_;
135     uint32_t nightExemptionTaskType_ {0};
136     uint32_t condition_ {0};
137     const static std::int32_t NETMANAGER_SUCCESS = 0;
138     const static std::int32_t NETMANAGER_ERR_STATUS_EXIST = 2100209;
139 };
140 }  // namespace DevStandbyMgr
141 }  // namespace OHOS
142 #endif // DEVICE_STANDBY_EXT_BASE_NETWPRK_STRATEGY_H
143