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