1 /*
2  * Copyright (C) 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 "napi_utils.h"
17 #include "netmanager_ext_log.h"
18 
19 #include "constant.h"
20 #include "mdns_resolvelocalservice_context.h"
21 
22 namespace OHOS::NetManagerStandard {
23 std::map<std::string, sptr<IResolveCallback>> MDnsResolveLocalServiceContext::resolveCallbackMap_;
24 std::mutex g_mDNSResolveMutex;
25 
MDnsResolveLocalServiceContext(napi_env env,EventManager * manager)26 MDnsResolveLocalServiceContext::MDnsResolveLocalServiceContext(napi_env env, EventManager *manager)
27     : MDnsBaseContext(env, manager), resolveObserver_(new (std::nothrow) MDnsResolveObserver())
28 {
29 }
30 
ParseParams(napi_value * params,size_t paramsCount)31 void MDnsResolveLocalServiceContext::ParseParams(napi_value *params, size_t paramsCount)
32 {
33     if (!CheckParamsType(params, paramsCount)) {
34         SetNeedThrowException(true);
35         SetErrorCode(NET_MDNS_ERR_ILLEGAL_ARGUMENT);
36         return;
37     }
38 
39     ParseServiceInfo(GetEnv(), params[ARG_NUM_1]);
40 
41     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
42         SetParseOK(SetCallback(params[ARG_NUM_2]) == napi_ok);
43         return;
44     }
45     SetParseOK(true);
46 }
47 
GetObserver()48 sptr<IResolveCallback> MDnsResolveLocalServiceContext::GetObserver()
49 {
50     return resolveObserver_;
51 }
52 
CheckParamsType(napi_value * params,size_t paramsCount)53 bool MDnsResolveLocalServiceContext::CheckParamsType(napi_value *params, size_t paramsCount)
54 {
55     bool bRet = false;
56     if (paramsCount == PARAM_JUST_OPTIONS) {
57         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
58                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object;
59     } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
60         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
61                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object &&
62                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_2]) == napi_function;
63     }
64     return bRet;
65 }
66 } // namespace OHOS::NetManagerStandard
67