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 "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), failCallback_(nullptr)
29 {
30 }
31 
~SubscribeContext()32 SubscribeContext::~SubscribeContext()
33 {
34     if (failCallback_ != nullptr) {
35         (void)napi_delete_reference(GetEnv(), failCallback_);
36     }
37 }
38 
ParseParams(napi_value * params,size_t paramsCount)39 void SubscribeContext::ParseParams(napi_value *params, size_t paramsCount)
40 {
41     if (!CheckParamsType(params, paramsCount)) {
42         return;
43     }
44 
45     if (paramsCount == PARAM_HAS_OPTIONS) {
46         SetParseOK(SetSuccessCallback(params[0]) && SetFailCallback(params[0]));
47         return;
48     }
49     NETMANAGER_BASE_LOGI("no callback");
50     SetParseOK(true);
51 }
52 
CheckParamsType(napi_value * params,size_t paramsCount)53 bool SubscribeContext::CheckParamsType(napi_value *params, size_t paramsCount)
54 {
55     if (paramsCount == PARAM_NO_OPTIONS) {
56         return true;
57     }
58 
59     SetNeedPromise(false);
60 
61     if (paramsCount == PARAM_HAS_OPTIONS) {
62         return NapiUtils::GetValueType(GetEnv(), params[0]) == napi_object;
63     }
64     return false;
65 }
66 
SetSuccessCallback(napi_value options)67 bool SubscribeContext::SetSuccessCallback(napi_value options)
68 {
69     if (!NapiUtils::HasNamedProperty(GetEnv(), options, KEY_SUCCESS)) {
70         NETMANAGER_BASE_LOGI("do not need success fun");
71         return true;
72     }
73     napi_value callback = NapiUtils::GetNamedProperty(GetEnv(), options, KEY_SUCCESS);
74     if (NapiUtils::GetValueType(GetEnv(), callback) != napi_function) {
75         NETMANAGER_BASE_LOGE("success should be function");
76         return false;
77     }
78 
79     GetManager()->AddListener(GetEnv(), EVENT_SUBSCRIBE, callback, false, false);
80     return true;
81 }
82 
SetFailCallback(napi_value options)83 bool SubscribeContext::SetFailCallback(napi_value options)
84 {
85     if (!NapiUtils::HasNamedProperty(GetEnv(), options, KEY_FAIL)) {
86         NETMANAGER_BASE_LOGI("do not need fail fun");
87         return true;
88     }
89     napi_value callback = NapiUtils::GetNamedProperty(GetEnv(), options, KEY_FAIL);
90     if (NapiUtils::GetValueType(GetEnv(), callback) != napi_function) {
91         NETMANAGER_BASE_LOGE("success should be function");
92         return false;
93     }
94     if (failCallback_ != nullptr) {
95         (void)napi_delete_reference(GetEnv(), failCallback_);
96     }
97     return napi_create_reference(GetEnv(), callback, 1, &failCallback_) == napi_ok;
98 }
99 
GetFailCallback() const100 napi_value SubscribeContext::GetFailCallback() const
101 {
102     if (failCallback_ == nullptr) {
103         return nullptr;
104     }
105     napi_value callback = nullptr;
106     NAPI_CALL(GetEnv(), napi_get_reference_value(GetEnv(), failCallback_, &callback));
107     return callback;
108 }
109 } // namespace OHOS::NetManagerStandard