1 /*
2 * Copyright (c) 2022-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 "statistics_callback_observer.h"
17
18 #include "constant.h"
19 #include "napi_constant.h"
20 #include "netmanager_base_log.h"
21 #include "statistics_observer_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
NetIfaceStatsChanged(const std::string & iface)25 int32_t StatisticsCallbackObserver::NetIfaceStatsChanged(const std::string &iface)
26 {
27 if (!StatisticsObserverWrapper::GetInstance().GetEventManager()->HasEventListener(EVENT_STATS_CHANGE)) {
28 NETMANAGER_BASE_LOGE("no event listener find %{public}s", EVENT_STATS_CHANGE);
29 return 0;
30 }
31 auto pair = new std::pair<std::string, uint32_t>;
32 pair->first = iface;
33 pair->second = KEY_UID_ARG;
34 StatisticsObserverWrapper::GetInstance().GetEventManager()->EmitByUv(EVENT_STATS_CHANGE, pair,
35 NetIfaceStatsChangedCallback);
36 return 0;
37 }
38
NetUidStatsChanged(const std::string & iface,uint32_t uid)39 int32_t StatisticsCallbackObserver::NetUidStatsChanged(const std::string &iface, uint32_t uid)
40 {
41 if (!StatisticsObserverWrapper::GetInstance().GetEventManager()->HasEventListener(EVENT_STATS_CHANGE)) {
42 NETMANAGER_BASE_LOGE("no event listener find %{public}s", EVENT_STATS_CHANGE);
43 return 0;
44 }
45 auto pair = new std::pair<std::string, uint32_t>;
46 pair->first = iface;
47 pair->second = uid;
48 StatisticsObserverWrapper::GetInstance().GetEventManager()->EmitByUv(EVENT_STATS_CHANGE, pair,
49 NetUidStatsChangedCallback);
50 return 0;
51 }
52
CreateNetIfaceStatsChangedParam(napi_env env,void * data)53 napi_value StatisticsCallbackObserver::CreateNetIfaceStatsChangedParam(napi_env env, void *data)
54 {
55 auto pair(reinterpret_cast<std::pair<std::string, uint32_t> *>(data));
56 napi_value obj = NapiUtils::CreateObject(env);
57 NapiUtils::SetStringPropertyUtf8(env, obj, KEY_IFACE, pair->first);
58 delete pair;
59 return obj;
60 }
61
CreateNetUidStatsChangedParam(napi_env env,void * data)62 napi_value StatisticsCallbackObserver::CreateNetUidStatsChangedParam(napi_env env, void *data)
63 {
64 auto pair(reinterpret_cast<std::pair<std::string, uint32_t> *>(data));
65 napi_value obj = NapiUtils::CreateObject(env);
66 NapiUtils::SetStringPropertyUtf8(env, obj, KEY_IFACE, pair->first);
67 NapiUtils::SetUint32Property(env, obj, KEY_UID, pair->second);
68 delete pair;
69 return obj;
70 }
71
NetIfaceStatsChangedCallback(uv_work_t * work,int status)72 void StatisticsCallbackObserver::NetIfaceStatsChangedCallback(uv_work_t *work, int status)
73 {
74 CallbackTemplate<CreateNetIfaceStatsChangedParam>(work, status);
75 }
76
NetUidStatsChangedCallback(uv_work_t * work,int status)77 void StatisticsCallbackObserver::NetUidStatsChangedCallback(uv_work_t *work, int status)
78 {
79 CallbackTemplate<CreateNetUidStatsChangedParam>(work, status);
80 }
81 } // namespace NetManagerStandard
82 } // namespace OHOS
83