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 "netmgr_ext_log_wrapper.h"
18 
19 #include "constant.h"
20 #include "mdns_addlocalservice_context.h"
21 #include "mdns_removelocalservice_context.h"
22 
23 namespace OHOS::NetManagerStandard {
24 std::mutex g_mDNSUnregisterMutex;
MDnsRemoveLocalServiceContext(napi_env env,EventManager * manager)25 MDnsRemoveLocalServiceContext::MDnsRemoveLocalServiceContext(napi_env env, EventManager *manager)
26     : MDnsBaseContext(env, manager)
27 {
28 }
29 
ParseParams(napi_value * params,size_t paramsCount)30 void MDnsRemoveLocalServiceContext::ParseParams(napi_value *params, size_t paramsCount)
31 {
32     if (!CheckParamsType(params, paramsCount)) {
33         SetNeedThrowException(true);
34         SetErrorCode(NET_MDNS_ERR_ILLEGAL_ARGUMENT);
35         return;
36     }
37 
38     std::string bundleName = GetContextIdString(GetEnv(), params[ARG_NUM_0]);
39     ParseServiceInfo(GetEnv(), params[ARG_NUM_1]);
40     std::string key = bundleName + MDNS_HOSTPORT_SPLITER_STR + GetServiceInfo().name + MDNS_DOMAIN_SPLITER_STR +
41                       GetServiceInfo().type;
42 
43     {
44         std::lock_guard<std::mutex> lock(g_mDNSUnregisterMutex);
45         regObserver_ = MDnsAddLocalServiceContext::registerCallbackMap_[key];
46         MDnsAddLocalServiceContext::registerCallbackMap_.erase(key);
47     }
48 
49     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
50         SetParseOK(SetCallback(params[ARG_NUM_2]) == napi_ok);
51         return;
52     }
53     SetParseOK(true);
54 }
55 
CheckParamsType(napi_value * params,size_t paramsCount)56 bool MDnsRemoveLocalServiceContext::CheckParamsType(napi_value *params, size_t paramsCount)
57 {
58     bool bRet = false;
59     if (paramsCount == PARAM_JUST_OPTIONS) {
60         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
61                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object;
62     } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
63         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
64                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object &&
65                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_2]) == napi_function;
66     }
67     return bRet;
68 }
69 
GetObserver()70 sptr<IRegistrationCallback> MDnsRemoveLocalServiceContext::GetObserver()
71 {
72     return regObserver_;
73 }
74 } // namespace OHOS::NetManagerStandard
75