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 "subscribe_context.h"
17
18 #include "network_constant.h"
19 #include "netmanager_base_log.h"
20 #include "napi_utils.h"
21
22 static constexpr const int PARAM_NO_OPTIONS = 0;
23
24 static constexpr const int PARAM_HAS_OPTIONS = 1;
25
26 namespace OHOS::NetManagerStandard {
SubscribeContext(napi_env env,EventManager * manager)27 SubscribeContext::SubscribeContext(napi_env env, EventManager *manager)
28 : BaseContext(env, manager), successCallback_(nullptr), failCallback_(nullptr)
29 {
30 }
31
~SubscribeContext()32 SubscribeContext::~SubscribeContext()
33 {
34 if (successCallback_ != nullptr) {
35 (void)napi_delete_reference(GetEnv(), successCallback_);
36 }
37 if (failCallback_ != nullptr) {
38 (void)napi_delete_reference(GetEnv(), failCallback_);
39 }
40 }
41
ParseParams(napi_value * params,size_t paramsCount)42 void SubscribeContext::ParseParams(napi_value *params, size_t paramsCount)
43 {
44 if (!CheckParamsType(params, paramsCount)) {
45 return;
46 }
47
48 if (paramsCount == PARAM_HAS_OPTIONS) {
49 SetParseOK(SetSuccessCallback(params[0]) && SetFailCallback(params[0]));
50 return;
51 }
52 NETMANAGER_BASE_LOGI("no callback");
53 SetParseOK(true);
54 }
55
CheckParamsType(napi_value * params,size_t paramsCount)56 bool SubscribeContext::CheckParamsType(napi_value *params, size_t paramsCount)
57 {
58 if (paramsCount == PARAM_NO_OPTIONS) {
59 return true;
60 }
61
62 SetNeedPromise(false);
63
64 if (paramsCount == PARAM_HAS_OPTIONS) {
65 return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
66 }
67 return false;
68 }
69
SetSuccessCallback(napi_value options)70 bool SubscribeContext::SetSuccessCallback(napi_value options)
71 {
72 if (!NapiUtils::HasNamedProperty(GetEnv(), options, KEY_SUCCESS)) {
73 NETMANAGER_BASE_LOGI("do not need success fun");
74 return true;
75 }
76 napi_value callback = NapiUtils::GetNamedProperty(GetEnv(), options, KEY_SUCCESS);
77 if (NapiUtils::GetValueType(GetEnv(), callback) != napi_function) {
78 NETMANAGER_BASE_LOGE("success should be function");
79 return false;
80 }
81 if (successCallback_ != nullptr) {
82 (void)napi_delete_reference(GetEnv(), successCallback_);
83 }
84 return napi_create_reference(GetEnv(), callback, 1, &successCallback_) == napi_ok;
85 }
86
SetFailCallback(napi_value options)87 bool SubscribeContext::SetFailCallback(napi_value options)
88 {
89 if (!NapiUtils::HasNamedProperty(GetEnv(), options, KEY_FAIL)) {
90 NETMANAGER_BASE_LOGI("do not need fail fun");
91 return true;
92 }
93 napi_value callback = NapiUtils::GetNamedProperty(GetEnv(), options, KEY_FAIL);
94 if (NapiUtils::GetValueType(GetEnv(), callback) != napi_function) {
95 NETMANAGER_BASE_LOGE("success should be function");
96 return false;
97 }
98 if (failCallback_ != nullptr) {
99 (void)napi_delete_reference(GetEnv(), failCallback_);
100 }
101 return napi_create_reference(GetEnv(), callback, 1, &failCallback_) == napi_ok;
102 }
103
GetSuccessCallback() const104 napi_value SubscribeContext::GetSuccessCallback() const
105 {
106 if (successCallback_ == nullptr) {
107 return nullptr;
108 }
109 napi_value callback = nullptr;
110 NAPI_CALL(GetEnv(), napi_get_reference_value(GetEnv(), successCallback_, &callback));
111 return callback;
112 }
113
GetFailCallback() const114 napi_value SubscribeContext::GetFailCallback() const
115 {
116 if (failCallback_ == nullptr) {
117 return nullptr;
118 }
119 napi_value callback = nullptr;
120 NAPI_CALL(GetEnv(), napi_get_reference_value(GetEnv(), failCallback_, &callback));
121 return callback;
122 }
123
SetCap(const NetAllCapabilities & cap)124 void SubscribeContext::SetCap(const NetAllCapabilities &cap)
125 {
126 cap_ = cap;
127 }
128
GetCap()129 NetAllCapabilities SubscribeContext::GetCap()
130 {
131 return cap_;
132 }
133 } // namespace OHOS::NetManagerStandard