• 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     if (DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
49         SetDeviceIdlePolicy(false) != 0) {
50         STANDBYSERVICE_LOGE("SetDeviceIdlePolicy netLimited is false");
51         return;
52     }
53     if (DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
54         SetDeviceIdleTrustlist(uids, false) != 0) {
55         STANDBYSERVICE_LOGW("SetFireWallAllowedList failed");
56     }
57 }
58 
HandleEvent(const StandbyMessage & message)59 void NetworkStrategy::HandleEvent(const StandbyMessage& message)
60 {
61     STANDBYSERVICE_LOGI("enter NetworkStrategy HandleEvent, eventId is %{public}d", message.eventId_);
62     switch (message.eventId_) {
63         case StandbyMessageType::ALLOW_LIST_CHANGED:
64             UpdateAllowedList(message);
65             break;
66         case StandbyMessageType::RES_CTRL_CONDITION_CHANGED:
67             UpdateNetResourceConfig(message);
68             break;
69         case StandbyMessageType::PHASE_TRANSIT:
70             StartNetLimit(message);
71             break;
72         case StandbyMessageType::STATE_TRANSIT:
73             StopNetLimit(message);
74             break;
75         case StandbyMessageType::BG_TASK_STATUS_CHANGE:
76             UpdateBgTaskAppStatus(message);
77             break;
78         default:
79             break;
80     }
81 }
82 
UpdateAllowedList(const StandbyMessage & message)83 void NetworkStrategy::UpdateAllowedList(const StandbyMessage& message)
84 {
85     STANDBYSERVICE_LOGD("enter NetworkStrategy UpdateAllowedList, eventId is %{public}d", message.eventId_);
86     UpdateExemptionList(message);
87 }
88 
UpdateNetResourceConfig(const StandbyMessage & message)89 void NetworkStrategy::UpdateNetResourceConfig(const StandbyMessage& message)
90 {
91     condition_ = static_cast<uint32_t>(message.want_->GetIntParam(RES_CTRL_CONDITION, 0));
92     STANDBYSERVICE_LOGD("enter NetworkStrategy HandleEvent, current condition is %{public}u", condition_);
93     OnDayNightSwitched(message);
94 }
95 
StartNetLimit(const StandbyMessage & message)96 void NetworkStrategy::StartNetLimit(const StandbyMessage& message)
97 {
98     STANDBYSERVICE_LOGD("enter NetworkStrategy StartNetLimit, eventId is %{public}d", message.eventId_);
99     uint32_t current_phase = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_PHASE, 0));
100     uint32_t current_state = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_STATE, 0));
101     if ((current_state != StandbyState::SLEEP) || (current_phase != SleepStatePhase::APP_RES_DEEP)) {
102         STANDBYSERVICE_LOGD("current state is not SLEEP or current phase is not APP_RES_DEEP!");
103         return;
104     }
105     EnableNetworkFirewall(message);
106 }
107 
StopNetLimit(const StandbyMessage & message)108 void NetworkStrategy::StopNetLimit(const StandbyMessage& message)
109 {
110     STANDBYSERVICE_LOGD("enter NetworkStrategy StopNetLimit, eventId is %{public}d", message.eventId_);
111     DisableNetworkFirewall(message);
112 }
113 
SetFirewallAllowedList(const std::vector<uint32_t> & uids,bool isAdded)114 void NetworkStrategy::SetFirewallAllowedList(const std::vector<uint32_t>& uids, bool isAdded)
115 {
116     STANDBYSERVICE_LOGD("SetFireWallAllowedList, uids size %{public}d, isAdded is %{public}d",
117         static_cast<int32_t>(uids.size()), isAdded);
118     if (uids.empty()) {
119         STANDBYSERVICE_LOGD("allow list is empty");
120         return;
121     }
122     if (!isAdded && isIdleMaintence_) {
123         STANDBYSERVICE_LOGI("current is idle maintenance, do not need remove allow list");
124         return;
125     }
126     if (auto ret = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
127         SetDeviceIdleTrustlist(uids, isAdded); ret != 0) {
128         STANDBYSERVICE_LOGW("failed to SetFireWallAllowedList, err code is %{public}d", ret);
129         return;
130     }
131     STANDBYSERVICE_LOGI("SetFireWallAllowedList succeed!");
132 }
133 
SetFirewallStatus(bool enableFirewall)134 ErrCode NetworkStrategy::SetFirewallStatus(bool enableFirewall)
135 {
136     if (DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
137         SetDeviceIdlePolicy(enableFirewall) != 0) {
138         STANDBYSERVICE_LOGE("SetDeviceIdlePolicy enableFirewall is %{public}d", enableFirewall);
139         return ERR_STRATEGY_DEPENDS_SA_NOT_AVAILABLE;
140     }
141     SetNetAllowApps(enableFirewall);
142     return ERR_OK;
143 }
144 
ShellDump(const std::vector<std::string> & argsInStr,std::string & result)145 void NetworkStrategy::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
146 {
147     STANDBYSERVICE_LOGD("enter NetworkStrategy::ShellDump");
148 }
149 } // namespace DevStandbyMgr
150 } // namespace OHOS
151