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 #ifndef NETMANAGER_EXT_MDNS_CALLBACK_OBSERVER_H
17 #define NETMANAGER_EXT_MDNS_CALLBACK_OBSERVER_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 
23 #include "event_manager.h"
24 #include "iremote_stub.h"
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "napi_utils.h"
28 
29 #include "i_mdns_event.h"
30 #include "mdns_event_stub.h"
31 #include "mdns_service_info.h"
32 
33 namespace OHOS {
34 namespace NetManagerStandard {
35 
36 class MDnsRegistrationObserver : public RegistrationCallbackStub {
37 public:
38     MDnsRegistrationObserver() = default;
39     ~MDnsRegistrationObserver() = default;
40     void HandleRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
41     void HandleUnRegister(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
42     void HandleRegisterResult(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
43 };
44 
45 class MDnsDiscoveryObserver : public DiscoveryCallbackStub {
46 public:
47     MDnsDiscoveryObserver() = default;
48     ~MDnsDiscoveryObserver() = default;
49     void HandleStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
50     void HandleStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
51     void HandleServiceFound(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
52     void HandleServiceLost(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
53 
54     void EmitStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode);
55     void EmitStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode);
56 
57 private:
CallbackTemplate(uv_work_t * work,int32_t status)58     template <napi_value (*MakeJsValue)(napi_env, void *)> static void CallbackTemplate(uv_work_t *work, int32_t status)
59     {
60         (void)status;
61         if (work == nullptr) {
62             return;
63         }
64         auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
65         if (workWrapper == nullptr) {
66             delete work;
67             return;
68         }
69         napi_env env = workWrapper->env;
70         auto closeScope = [env](napi_handle_scope scope) { NapiUtils::CloseScope(env, scope); };
71         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(NapiUtils::OpenScope(env), closeScope);
72         napi_value obj = MakeJsValue(env, workWrapper->data);
73         std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
74         workWrapper->manager->Emit(workWrapper->type, arg);
75         delete workWrapper;
76         delete work;
77     }
78 
79     static napi_value CreateService(napi_env env, void *data);
80     static void ServiceCallback(uv_work_t *work, int32_t status);
81 
82     static napi_value CreateServiceWithError(napi_env env, void *data);
83     static void ServiceCallbackWithError(uv_work_t *work, int32_t status);
84 };
85 
86 class MDnsResolveObserver : public ResolveCallbackStub {
87 public:
88     MDnsResolveObserver() = default;
89     ~MDnsResolveObserver() = default;
90     void HandleResolveResult(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
91 
92     std::mutex mutex_;
93     std::condition_variable cv_;
94     bool resolved_ = false;
95     MDnsServiceInfo serviceInfo_;
96     int32_t retCode_ = NET_MDNS_ERR_UNKNOWN;
97 };
98 
99 } // namespace NetManagerStandard
100 } // namespace OHOS
101 #endif // NETMANAGER_EXT_MDNS_CALLBACK_OBSERVER_H
102