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_addlocalservice_context.h"
21 #include "mdns_common.h"
22
23 namespace OHOS::NetManagerStandard {
24 std::map<std::string, sptr<IRegistrationCallback>> MDnsAddLocalServiceContext::registerCallbackMap_;
25 std::mutex g_mDNSRegisterMutex;
26
MDnsAddLocalServiceContext(napi_env env,EventManager * manager)27 MDnsAddLocalServiceContext::MDnsAddLocalServiceContext(napi_env env, EventManager *manager)
28 : MDnsBaseContext(env, manager)
29 {
30 }
31
ParseParams(napi_value * params,size_t paramsCount)32 void MDnsAddLocalServiceContext::ParseParams(napi_value *params, size_t paramsCount)
33 {
34 if (!CheckParamsType(params, paramsCount)) {
35 SetNeedThrowException(true);
36 SetErrorCode(NET_MDNS_ERR_ILLEGAL_ARGUMENT);
37 return;
38 }
39
40 std::string bundleName = GetContextIdString(GetEnv(), params[ARG_NUM_0]);
41 ParseServiceInfo(GetEnv(), params[ARG_NUM_1]);
42 std::string key = bundleName + MDNS_HOSTPORT_SPLITER_STR + GetServiceInfo().name + MDNS_DOMAIN_SPLITER_STR +
43 GetServiceInfo().type;
44
45 std::lock_guard<std::mutex> lock(g_mDNSRegisterMutex);
46 auto observer = registerCallbackMap_[key];
47 if (observer == nullptr) {
48 regObserver_ = new (std::nothrow) MDnsRegistrationObserver();
49 if (regObserver_ == nullptr) {
50 NETMANAGER_EXT_LOGE("new MDnsRegistrationObserver failed");
51 SetParseOK(false);
52 return;
53 }
54 registerCallbackMap_[key] = regObserver_;
55 } else {
56 regObserver_ = observer;
57 }
58
59 if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
60 SetParseOK(SetCallback(params[ARG_NUM_2]) == napi_ok);
61 return;
62 }
63
64 SetParseOK(true);
65 }
66
GetObserver() const67 sptr<IRegistrationCallback> MDnsAddLocalServiceContext::GetObserver() const
68 {
69 return regObserver_;
70 }
71
CheckParamsType(napi_value * params,size_t paramsCount)72 bool MDnsAddLocalServiceContext::CheckParamsType(napi_value *params, size_t paramsCount)
73 {
74 bool bRet = false;
75 if (paramsCount == PARAM_JUST_OPTIONS) {
76 bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
77 NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object;
78 } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
79 bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
80 NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object &&
81 NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_2]) == napi_function;
82 }
83 return bRet;
84 }
85 } // namespace OHOS::NetManagerStandard
86