• 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 
16 #include "network_strategy.h"
17 
18 #include "standby_state.h"
19 #include "time_provider.h"
20 #include "istandby_service.h"
21 #include "standby_service_log.h"
22 #include "net_policy_client.h"
23 
24 namespace OHOS {
25 namespace DevStandbyMgr {
OnCreated()26 ErrCode NetworkStrategy::OnCreated()
27 {
28     STANDBYSERVICE_LOGI("NetworkStrategy is now OnCreated");
29     condition_ = TimeProvider::GetCondition();
30     ResetFirewallAllowList();
31     return ERR_OK;
32 }
33 
OnDestroy()34 ErrCode NetworkStrategy::OnDestroy()
35 {
36     STANDBYSERVICE_LOGI("NetworkStrategy is now OnDestroy");
37     ResetFirewallAllowList();
38     return ERR_OK;
39 }
40 
ResetFirewallAllowList()41 void NetworkStrategy::ResetFirewallAllowList()
42 {
43     STANDBYSERVICE_LOGI("ResetFirewallAllowList list");
44     std::vector<uint32_t> uids;
45     if (DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->GetDeviceIdleTrustlist(uids) != 0) {
46         return;
47     }
48 
49     int32_t ret = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->SetDeviceIdlePolicy(false);
50     if (ret != 0 && ret != NETMANAGER_ERR_STATUS_EXIST) {
51         STANDBYSERVICE_LOGE("SetDeviceIdlePolicy netLimited is false");
52         return;
53     }
54     if (DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
55         SetDeviceIdleTrustlist(uids, false) != 0) {
56         STANDBYSERVICE_LOGW("SetFireWallAllowedList failed");
57     }
58 }
59 
HandleEvent(const StandbyMessage & message)60 void NetworkStrategy::HandleEvent(const StandbyMessage& message)
61 {
62     STANDBYSERVICE_LOGD("enter NetworkStrategy HandleEvent, eventId is %{public}d", message.eventId_);
63     switch (message.eventId_) {
64         case StandbyMessageType::ALLOW_LIST_CHANGED:
65             UpdateAllowedList(message);
66             break;
67         case StandbyMessageType::RES_CTRL_CONDITION_CHANGED:
68             UpdateNetResourceConfig(message);
69             break;
70         case StandbyMessageType::PHASE_TRANSIT:
71             StartNetLimit(message);
72             break;
73         case StandbyMessageType::STATE_TRANSIT:
74             StopNetLimit(message);
75             break;
76         case StandbyMessageType::BG_TASK_STATUS_CHANGE:
77             UpdateBgTaskAppStatus(message);
78             break;
79         case StandbyMessageType::PROCESS_STATE_CHANGED:
80             HandleProcessStatusChanged(message);
81             break;
82         default:
83             break;
84     }
85 }
86 
UpdateAllowedList(const StandbyMessage & message)87 void NetworkStrategy::UpdateAllowedList(const StandbyMessage& message)
88 {
89     STANDBYSERVICE_LOGD("enter NetworkStrategy UpdateAllowedList, eventId is %{public}d", message.eventId_);
90     UpdateExemptionList(message);
91 }
92 
UpdateNetResourceConfig(const StandbyMessage & message)93 void NetworkStrategy::UpdateNetResourceConfig(const StandbyMessage& message)
94 {
95     condition_ = static_cast<uint32_t>(message.want_->GetIntParam(RES_CTRL_CONDITION, 0));
96     STANDBYSERVICE_LOGD("enter NetworkStrategy HandleEvent, current condition is %{public}u", condition_);
97     OnDayNightSwitched(message);
98 }
99 
StartNetLimit(const StandbyMessage & message)100 void NetworkStrategy::StartNetLimit(const StandbyMessage& message)
101 {
102     STANDBYSERVICE_LOGD("enter NetworkStrategy StartNetLimit, eventId is %{public}d", message.eventId_);
103     uint32_t current_phase = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_PHASE, 0));
104     uint32_t current_state = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_STATE, 0));
105     if ((current_state != StandbyState::SLEEP) || (current_phase != SleepStatePhase::APP_RES_DEEP)) {
106         STANDBYSERVICE_LOGD("current state is not SLEEP or current phase is not APP_RES_DEEP!");
107         return;
108     }
109     EnableNetworkFirewall(message);
110 }
111 
StopNetLimit(const StandbyMessage & message)112 void NetworkStrategy::StopNetLimit(const StandbyMessage& message)
113 {
114     STANDBYSERVICE_LOGD("enter NetworkStrategy StopNetLimit, eventId is %{public}d", message.eventId_);
115     DisableNetworkFirewall(message);
116 }
117 
SetFirewallAllowedList(const std::vector<uint32_t> & uids,bool isAdded)118 void NetworkStrategy::SetFirewallAllowedList(const std::vector<uint32_t>& uids, bool isAdded)
119 {
120     STANDBYSERVICE_LOGD("SetFireWallAllowedList, uids size %{public}d, isAdded is %{public}d",
121         static_cast<int32_t>(uids.size()), isAdded);
122     if (uids.empty()) {
123         STANDBYSERVICE_LOGD("allow list is empty");
124         return;
125     }
126     if (!isAdded && isIdleMaintence_) {
127         STANDBYSERVICE_LOGI("current is idle maintenance, do not need remove allow list");
128         return;
129     }
130     if (auto ret = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
131         SetDeviceIdleTrustlist(uids, isAdded); ret != 0) {
132         STANDBYSERVICE_LOGW("failed to SetFireWallAllowedList, err code is %{public}d", ret);
133         return;
134     }
135 }
136 
SetFirewallStatus(bool enableFirewall)137 ErrCode NetworkStrategy::SetFirewallStatus(bool enableFirewall)
138 {
139     int32_t ret = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
140         SetDeviceIdlePolicy(enableFirewall);
141     if (ret == 0 || (!enableFirewall && ret == NETMANAGER_ERR_STATUS_EXIST)) {
142         SetNetAllowApps(enableFirewall);
143         return ERR_OK;
144     }
145     STANDBYSERVICE_LOGE("SetDeviceIdlePolicy enableFirewall is %{public}d res is %{public}d",
146         enableFirewall, ret);
147     return ERR_STRATEGY_DEPENDS_SA_NOT_AVAILABLE;
148 }
149 
ShellDump(const std::vector<std::string> & argsInStr,std::string & result)150 void NetworkStrategy::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
151 {
152     if (argsInStr[DUMP_FIRST_PARAM] == DUMP_DETAIL_INFO &&
153         argsInStr[DUMP_SECOND_PARAM] == DUMP_STRATGY_DETAIL) {
154         result.append("=================DeviceIdle=======================\n");
155         BaseNetworkStrategy::ShellDump(argsInStr, result);
156     }
157 }
158 } // namespace DevStandbyMgr
159 } // namespace OHOS
160