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