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