• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         }
53         registerCallbackMap_[key] = regObserver_;
54     } else {
55         regObserver_ = observer;
56     }
57 
58     if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
59         SetParseOK(SetCallback(params[ARG_NUM_2]) == napi_ok);
60         return;
61     }
62 
63     SetParseOK(true);
64 }
65 
GetObserver() const66 sptr<IRegistrationCallback> MDnsAddLocalServiceContext::GetObserver() const
67 {
68     return regObserver_;
69 }
70 
CheckParamsType(napi_value * params,size_t paramsCount)71 bool MDnsAddLocalServiceContext::CheckParamsType(napi_value *params, size_t paramsCount)
72 {
73     bool bRet = false;
74     if (paramsCount == PARAM_JUST_OPTIONS) {
75         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
76                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object;
77     } else if (paramsCount == PARAM_OPTIONS_AND_CALLBACK) {
78         bRet = NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_0]) == napi_object &&
79                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_1]) == napi_object &&
80                NapiUtils::GetValueType(GetEnv(), params[ARG_NUM_2]) == napi_function;
81     }
82     return bRet;
83 }
84 } // namespace OHOS::NetManagerStandard
85