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_POLICY_CALLBACK_OBSERVER_H
17 #define NETMANAGER_BASE_NET_POLICY_CALLBACK_OBSERVER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <utility>
22 #include <vector>
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_policy_callback_stub.h"
31 #include "net_quota_policy.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 class NetPolicyCallbackObserver : public NetPolicyCallbackStub {
36 public:
37     int32_t NetUidPolicyChange(uint32_t uid, uint32_t policy) override;
38     int32_t NetUidRuleChange(uint32_t uid, uint32_t rule) override;
39     int32_t NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> &quotaPolicies) override;
40     int32_t NetMeteredIfacesChange(std::vector<std::string> &ifaces) override;
41     int32_t NetBackgroundPolicyChange(bool isBackgroundPolicyAllow) override;
42 
43 private:
CallbackTemplate(uv_work_t * work,int status)44     template <napi_value (*MakeJsValue)(napi_env, void *)> static void CallbackTemplate(uv_work_t *work, int status)
45     {
46         (void)status;
47         if (work == nullptr) {
48             return;
49         }
50         auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
51         if (workWrapper == nullptr) {
52             delete work;
53             return;
54         }
55         napi_env env = workWrapper->env;
56         auto closeScope = [env](napi_handle_scope scope) { NapiUtils::CloseScope(env, scope); };
57         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(NapiUtils::OpenScope(env), closeScope);
58         napi_value obj = MakeJsValue(env, workWrapper->data);
59         std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
60         workWrapper->manager->Emit(workWrapper->type, arg);
61         delete workWrapper;
62         delete work;
63     }
64 
65     static napi_value CreateNetQuotaPolicy(napi_env env, NetQuotaPolicy *data);
66     static napi_value CreateNetUidPolicyChangeParam(napi_env env, void *data);
67     static napi_value CreateNetUidRuleChangeParam(napi_env env, void *data);
68     static napi_value CreateNetQuotaPolicyChangeParam(napi_env env, void *data);
69     static napi_value CreateMeteredIfacesChangeParam(napi_env env, void *data);
70     static napi_value CreateNetBackgroundPolicyChangeParam(napi_env env, void *data);
71     static void NetUidPolicyChangeCallback(uv_work_t *work, int status);
72     static void NetUidRuleChangeCallback(uv_work_t *work, int status);
73     static void NetQuotaPolicyChangeCallback(uv_work_t *work, int status);
74     static void NetMeteredIfacesChangeCallback(uv_work_t *work, int status);
75     static void NetBackgroundPolicyChangeCallback(uv_work_t *work, int status);
76 };
77 } // namespace NetManagerStandard
78 } // namespace OHOS
79 #endif // NETMANAGER_BASE_NET_POLICY_CALLBACK_OBSERVER_H
80