199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci
1699552fe9Sopenharmony_ci#include "network_strategy.h"
1799552fe9Sopenharmony_ci
1899552fe9Sopenharmony_ci#ifdef STANDBY_COMMUNICATION_NETMANAGER_BASE_ENABLE
1999552fe9Sopenharmony_ci#include "net_policy_client.h"
2099552fe9Sopenharmony_ci#endif
2199552fe9Sopenharmony_ci
2299552fe9Sopenharmony_ci#include "standby_state.h"
2399552fe9Sopenharmony_ci#include "time_provider.h"
2499552fe9Sopenharmony_ci#include "istandby_service.h"
2599552fe9Sopenharmony_ci#include "standby_service_log.h"
2699552fe9Sopenharmony_ci
2799552fe9Sopenharmony_cinamespace OHOS {
2899552fe9Sopenharmony_cinamespace DevStandbyMgr {
2999552fe9Sopenharmony_ciErrCode NetworkStrategy::OnCreated()
3099552fe9Sopenharmony_ci{
3199552fe9Sopenharmony_ci    STANDBYSERVICE_LOGI("NetworkStrategy is now OnCreated");
3299552fe9Sopenharmony_ci    condition_ = TimeProvider::GetCondition();
3399552fe9Sopenharmony_ci    ResetFirewallAllowList();
3499552fe9Sopenharmony_ci    return ERR_OK;
3599552fe9Sopenharmony_ci}
3699552fe9Sopenharmony_ci
3799552fe9Sopenharmony_ciErrCode NetworkStrategy::OnDestroy()
3899552fe9Sopenharmony_ci{
3999552fe9Sopenharmony_ci    STANDBYSERVICE_LOGI("NetworkStrategy is now OnDestroy");
4099552fe9Sopenharmony_ci    ResetFirewallAllowList();
4199552fe9Sopenharmony_ci    return ERR_OK;
4299552fe9Sopenharmony_ci}
4399552fe9Sopenharmony_ci
4499552fe9Sopenharmony_civoid NetworkStrategy::HandleEvent(const StandbyMessage& message)
4599552fe9Sopenharmony_ci{
4699552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("enter NetworkStrategy HandleEvent, eventId is %{public}d", message.eventId_);
4799552fe9Sopenharmony_ci    switch (message.eventId_) {
4899552fe9Sopenharmony_ci        case StandbyMessageType::ALLOW_LIST_CHANGED:
4999552fe9Sopenharmony_ci            UpdateAllowedList(message);
5099552fe9Sopenharmony_ci            break;
5199552fe9Sopenharmony_ci        case StandbyMessageType::RES_CTRL_CONDITION_CHANGED:
5299552fe9Sopenharmony_ci            UpdateNetResourceConfig(message);
5399552fe9Sopenharmony_ci            break;
5499552fe9Sopenharmony_ci        case StandbyMessageType::PHASE_TRANSIT:
5599552fe9Sopenharmony_ci            StartNetLimit(message);
5699552fe9Sopenharmony_ci            break;
5799552fe9Sopenharmony_ci        case StandbyMessageType::STATE_TRANSIT:
5899552fe9Sopenharmony_ci            StopNetLimit(message);
5999552fe9Sopenharmony_ci            break;
6099552fe9Sopenharmony_ci        case StandbyMessageType::BG_TASK_STATUS_CHANGE:
6199552fe9Sopenharmony_ci            UpdateBgTaskAppStatus(message);
6299552fe9Sopenharmony_ci            break;
6399552fe9Sopenharmony_ci        case StandbyMessageType::PROCESS_STATE_CHANGED:
6499552fe9Sopenharmony_ci            HandleProcessStatusChanged(message);
6599552fe9Sopenharmony_ci            break;
6699552fe9Sopenharmony_ci        default:
6799552fe9Sopenharmony_ci            break;
6899552fe9Sopenharmony_ci    }
6999552fe9Sopenharmony_ci}
7099552fe9Sopenharmony_ci
7199552fe9Sopenharmony_civoid NetworkStrategy::UpdateAllowedList(const StandbyMessage& message)
7299552fe9Sopenharmony_ci{
7399552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("enter NetworkStrategy UpdateAllowedList, eventId is %{public}d", message.eventId_);
7499552fe9Sopenharmony_ci    UpdateExemptionList(message);
7599552fe9Sopenharmony_ci}
7699552fe9Sopenharmony_ci
7799552fe9Sopenharmony_civoid NetworkStrategy::UpdateNetResourceConfig(const StandbyMessage& message)
7899552fe9Sopenharmony_ci{
7999552fe9Sopenharmony_ci    condition_ = static_cast<uint32_t>(message.want_->GetIntParam(RES_CTRL_CONDITION, 0));
8099552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("enter NetworkStrategy HandleEvent, current condition is %{public}u", condition_);
8199552fe9Sopenharmony_ci    UpdateFirewallAllowList();
8299552fe9Sopenharmony_ci}
8399552fe9Sopenharmony_ci
8499552fe9Sopenharmony_civoid NetworkStrategy::StartNetLimit(const StandbyMessage& message)
8599552fe9Sopenharmony_ci{
8699552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("enter NetworkStrategy StartNetLimit, eventId is %{public}d", message.eventId_);
8799552fe9Sopenharmony_ci    uint32_t current_phase = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_PHASE, 0));
8899552fe9Sopenharmony_ci    uint32_t current_state = static_cast<uint32_t>(message.want_->GetIntParam(CURRENT_STATE, 0));
8999552fe9Sopenharmony_ci    if ((current_state != StandbyState::SLEEP) || (current_phase != SleepStatePhase::APP_RES_DEEP)) {
9099552fe9Sopenharmony_ci        STANDBYSERVICE_LOGD("current state is not SLEEP or current phase is not APP_RES_DEEP!");
9199552fe9Sopenharmony_ci        return;
9299552fe9Sopenharmony_ci    }
9399552fe9Sopenharmony_ci    EnableNetworkFirewall(message);
9499552fe9Sopenharmony_ci}
9599552fe9Sopenharmony_ci
9699552fe9Sopenharmony_civoid NetworkStrategy::StopNetLimit(const StandbyMessage& message)
9799552fe9Sopenharmony_ci{
9899552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("enter NetworkStrategy StopNetLimit, eventId is %{public}d", message.eventId_);
9999552fe9Sopenharmony_ci    DisableNetworkFirewall(message);
10099552fe9Sopenharmony_ci}
10199552fe9Sopenharmony_ci
10299552fe9Sopenharmony_civoid NetworkStrategy::SetFirewallAllowedList(const std::vector<uint32_t>& uids, bool isAdded)
10399552fe9Sopenharmony_ci{
10499552fe9Sopenharmony_ci    STANDBYSERVICE_LOGD("SetFireWallAllowedList, uids size %{public}d, isAdded is %{public}d",
10599552fe9Sopenharmony_ci        static_cast<int32_t>(uids.size()), isAdded);
10699552fe9Sopenharmony_ci    if (uids.empty()) {
10799552fe9Sopenharmony_ci        STANDBYSERVICE_LOGD("allow list is empty");
10899552fe9Sopenharmony_ci        return;
10999552fe9Sopenharmony_ci    }
11099552fe9Sopenharmony_ci    if (!isAdded && isIdleMaintence_) {
11199552fe9Sopenharmony_ci        STANDBYSERVICE_LOGI("current is idle maintenance, do not need remove allow list");
11299552fe9Sopenharmony_ci        return;
11399552fe9Sopenharmony_ci    }
11499552fe9Sopenharmony_ci    #ifdef STANDBY_COMMUNICATION_NETMANAGER_BASE_ENABLE
11599552fe9Sopenharmony_ci    if (auto ret = DelayedSingleton<NetManagerStandard::NetPolicyClient>::GetInstance()->
11699552fe9Sopenharmony_ci        SetDeviceIdleTrustlist(uids, isAdded); ret != 0) {
11799552fe9Sopenharmony_ci        STANDBYSERVICE_LOGW("failed to SetFireWallAllowedList, err code is %{public}d", ret);
11899552fe9Sopenharmony_ci        return;
11999552fe9Sopenharmony_ci    }
12099552fe9Sopenharmony_ci    #endif
12199552fe9Sopenharmony_ci}
12299552fe9Sopenharmony_ci
12399552fe9Sopenharmony_civoid NetworkStrategy::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
12499552fe9Sopenharmony_ci{
12599552fe9Sopenharmony_ci    if (argsInStr[DUMP_FIRST_PARAM] == DUMP_DETAIL_INFO &&
12699552fe9Sopenharmony_ci        argsInStr[DUMP_SECOND_PARAM] == DUMP_STRATGY_DETAIL) {
12799552fe9Sopenharmony_ci        result.append("=================DeviceIdle=======================\n");
12899552fe9Sopenharmony_ci        BaseNetworkStrategy::ShellDump(argsInStr, result);
12999552fe9Sopenharmony_ci    }
13099552fe9Sopenharmony_ci}
13199552fe9Sopenharmony_ci} // namespace DevStandbyMgr
13299552fe9Sopenharmony_ci} // namespace OHOS
133