1 /*
2 * Copyright (c) 2025 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 "interfaceregister_context.h"
17
18 #include "constant.h"
19 #include "napi_constant.h"
20 #include "napi_utils.h"
21 #include "netmanager_base_log.h"
22 #include "net_interface_callback_observer.h"
23
24 namespace OHOS::NetManagerStandard {
IfaceRegisterContext(napi_env env,std::shared_ptr<EventManager> & manager)25 IfaceRegisterContext::IfaceRegisterContext(napi_env env, std::shared_ptr<EventManager>& manager)
26 : BaseContext(env, manager)
27 {
28 if (manager) {
29 auto iface = static_cast<NetInterface *>(manager->GetData());
30 if (iface) {
31 callback_ = iface->GetObserver();
32 iface_ = *iface;
33 }
34 }
35 }
36
GetNetInterfaceCallback()37 wptr<NetInterfaceCallbackObserver> IfaceRegisterContext::GetNetInterfaceCallback()
38 {
39 return callback_;
40 }
41
GetIface()42 NetInterface IfaceRegisterContext::GetIface()
43 {
44 return iface_;
45 }
46
ParseParams(napi_value * params,size_t paramsCount)47 void IfaceRegisterContext::ParseParams(napi_value *params, size_t paramsCount)
48 {
49 if (!CheckParamsType(params, paramsCount)) {
50 return;
51 }
52
53 if (paramsCount == PARAM_JUST_CALLBACK) {
54 SetParseOK(SetCallback(params[ARG_INDEX_0]) == napi_ok);
55 return;
56 }
57 SetParseOK(true);
58 }
59
CheckParamsType(napi_value * params,size_t paramsCount)60 bool IfaceRegisterContext::CheckParamsType(napi_value *params, size_t paramsCount)
61 {
62 if (paramsCount == PARAM_NONE) {
63 return true;
64 }
65
66 if (paramsCount == PARAM_JUST_CALLBACK) {
67 return NapiUtils::GetValueType(GetEnv(), params[ARG_INDEX_0]) == napi_function;
68 }
69 return false;
70 }
71 } // namespace OHOS::NetManagerStandard
72