1/*
2 * Copyright (c) 2021-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 "net_stats_callback.h"
17
18#include "netsys_controller.h"
19#include "net_stats_constants.h"
20#include "net_mgr_log_wrapper.h"
21#include "net_stats_constants.h"
22
23namespace OHOS {
24namespace NetManagerStandard {
25void NetStatsCallback::RegisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
26{
27    if (callback == nullptr) {
28        NETMGR_LOG_E("The parameter callback is null");
29        return;
30    }
31    std::lock_guard<ffrt::mutex> lock(statsCallbackMetux_);
32    uint32_t callBackNum = netStatsCallback_.size();
33    NETMGR_LOG_D("netStatsCallback_ callback num [%{public}d]", callBackNum);
34    if (callBackNum >= LIMIT_STATS_CALLBACK_NUM) {
35        NETMGR_LOG_E("netStatsCallback_ callback num cannot more than [%{public}d]", LIMIT_STATS_CALLBACK_NUM);
36        return;
37    }
38
39    for (uint32_t i = 0; i < callBackNum; i++) {
40        if (callback->AsObject().GetRefPtr() == netStatsCallback_[i]->AsObject().GetRefPtr()) {
41            NETMGR_LOG_I("netStatsCallback_ had this callback");
42            return;
43        }
44    }
45    NETMGR_LOG_I("netStatsCallback_ add callback.");
46    netStatsCallback_.emplace_back(callback);
47}
48
49void NetStatsCallback::UnregisterNetStatsCallback(const sptr<INetStatsCallback> &callback)
50{
51    if (callback == nullptr) {
52        NETMGR_LOG_E("The parameter of callback is null");
53        return;
54    }
55    std::lock_guard<ffrt::mutex> lock(statsCallbackMetux_);
56    for (auto iter = netStatsCallback_.begin(); iter != netStatsCallback_.end(); ++iter) {
57        if (callback->AsObject().GetRefPtr() == (*iter)->AsObject().GetRefPtr()) {
58            netStatsCallback_.erase(iter);
59            NETMGR_LOG_I("netStatsCallback_ erase callback.");
60            return;
61        }
62    }
63}
64
65int32_t NetStatsCallback::NotifyNetIfaceStatsChanged(const std::string &iface)
66{
67    NETMGR_LOG_D("NotifyNetIfaceStatsChanged info: iface[%{public}s]", iface.c_str());
68    std::lock_guard<ffrt::mutex> lock(statsCallbackMetux_);
69    auto iter = std::remove_if(netStatsCallback_.begin(), netStatsCallback_.end(), [&iface](const auto &item) {
70        return item == nullptr || item->NetIfaceStatsChanged(iface) == NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
71    });
72    netStatsCallback_.erase(iter, netStatsCallback_.end());
73    return NETMANAGER_SUCCESS;
74}
75
76int32_t NetStatsCallback::NotifyNetUidStatsChanged(const std::string &iface, uint32_t uid)
77{
78    NETMGR_LOG_D("UpdateIfacesStats info: iface[%{public}s] uid[%{public}d]", iface.c_str(), uid);
79    std::lock_guard<ffrt::mutex> lock(statsCallbackMetux_);
80    auto iter = std::remove_if(netStatsCallback_.begin(), netStatsCallback_.end(), [&iface, uid](const auto &item) {
81        return item == nullptr || item->NetUidStatsChanged(iface, uid) == NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
82    });
83    netStatsCallback_.erase(iter, netStatsCallback_.end());
84    return NETMANAGER_SUCCESS;
85}
86} // namespace NetManagerStandard
87} // namespace OHOS
88