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