1 /*
2 * Copyright (c) 2021-2022 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
23 namespace OHOS {
24 namespace NetManagerStandard {
RegisterNetStatsCallback(const sptr<INetStatsCallback> & callback)25 void 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 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
46 netStatsCallback_.emplace_back(callback);
47 }
48
UnregisterNetStatsCallback(const sptr<INetStatsCallback> & callback)49 void 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 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 return;
60 }
61 }
62 }
63
NotifyNetIfaceStatsChanged(const std::string & iface)64 int32_t NetStatsCallback::NotifyNetIfaceStatsChanged(const std::string &iface)
65 {
66 NETMGR_LOG_D("NotifyNetIfaceStatsChanged info: iface[%{public}s]", iface.c_str());
67 std::lock_guard lock(statsCallbackMetux_);
68 for (auto it = netStatsCallback_.begin(); it < netStatsCallback_.end();) {
69 if ((*it) == nullptr || (*it)->NetIfaceStatsChanged(iface) == NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL) {
70 netStatsCallback_.erase(it);
71 } else {
72 ++it;
73 }
74 }
75 return NETMANAGER_SUCCESS;
76 }
77
NotifyNetUidStatsChanged(const std::string & iface,uint32_t uid)78 int32_t NetStatsCallback::NotifyNetUidStatsChanged(const std::string &iface, uint32_t uid)
79 {
80 NETMGR_LOG_D("UpdateIfacesStats info: iface[%{public}s] uid[%{public}d]", iface.c_str(), uid);
81 std::lock_guard lock(statsCallbackMetux_);
82 for (auto it = netStatsCallback_.begin(); it < netStatsCallback_.end();) {
83 if ((*it) == nullptr || (*it)->NetUidStatsChanged(iface, uid) == NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL) {
84 netStatsCallback_.erase(it);
85 } else {
86 ++it;
87 }
88 }
89 return NETMANAGER_SUCCESS;
90 }
91 } // namespace NetManagerStandard
92 } // namespace OHOS
93