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