• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NETMANAGER_BASE_NET_STATS_CALLBACK_OBSERVER_H
17 #define NETMANAGER_BASE_NET_STATS_CALLBACK_OBSERVER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <utility>
22 #include <string>
23 
24 #include <napi/native_api.h>
25 #include <napi/native_node_api.h>
26 #include <uv.h>
27 
28 #include "event_manager.h"
29 #include "napi_utils.h"
30 #include "net_stats_callback_stub.h"
31 
32 namespace OHOS {
33 namespace NetManagerStandard {
34 class StatisticsCallbackObserver : public NetStatsCallbackStub {
35 public:
36     int32_t NetIfaceStatsChanged(const std::string &iface) override;
37     int32_t NetUidStatsChanged(const std::string &iface, uint32_t uid) override;
38 
39 private:
CallbackTemplate(uv_work_t * work,int status)40     template <napi_value (*MakeJsValue)(napi_env, void *)> static void CallbackTemplate(uv_work_t *work, int status)
41     {
42         (void)status;
43         if (work == nullptr) {
44             return;
45         }
46         auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
47         if (workWrapper == nullptr) {
48             delete work;
49             return;
50         }
51         napi_env env = workWrapper->env;
52         auto closeScope = [env](napi_handle_scope scope) { NapiUtils::CloseScope(env, scope); };
53         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(NapiUtils::OpenScope(env), closeScope);
54         napi_value obj = MakeJsValue(env, workWrapper->data);
55         std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
56         workWrapper->manager->Emit(workWrapper->type, arg);
57         delete workWrapper;
58         delete work;
59     }
60 
61     static napi_value CreateNetIfaceStatsChangedParam(napi_env env, void *data);
62     static napi_value CreateNetUidStatsChangedParam(napi_env env, void *data);
63 
64     static void NetIfaceStatsChangedCallback(uv_work_t *work, int status);
65     static void NetUidStatsChangedCallback(uv_work_t *work, int status);
66 };
67 } // namespace NetManagerStandard
68 } // namespace OHOS
69 #endif // NETMANAGER_BASE_NET_STATS_CALLBACK_OBSERVER_H
70