• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_BASE_NET_SHARE_CALLBACK_OBSERVER_H
17 #define NETMANAGER_BASE_NET_SHARE_CALLBACK_OBSERVER_H
18 
19 #include <string>
20 
21 #include <napi/native_api.h>
22 #include <napi/native_node_api.h>
23 
24 #include "event_manager.h"
25 #include "napi_utils.h"
26 #include "net_handle.h"
27 #include "sharing_event_callback_stub.h"
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 class NetShareCallbackObserver : public SharingEventCallbackStub {
32 public:
33     void OnSharingStateChanged(const bool &isRunning) override;
34     void OnInterfaceSharingStateChanged(const SharingIfaceType &type, const std::string &iface,
35                                         const SharingIfaceState &state) override;
36     void OnSharingUpstreamChanged(const sptr<NetHandle> netHandle) override;
37 
38 private:
CallbackTemplate(uv_work_t * work,int status)39     template <napi_value (*MakeJsValue)(napi_env, void *)> static void CallbackTemplate(uv_work_t *work, int status)
40     {
41         (void)status;
42         if (work == nullptr || MakeJsValue == nullptr) {
43             return;
44         }
45         auto workWrapper = static_cast<UvWorkWrapper *>(work->data);
46         if (workWrapper == nullptr) {
47             delete work;
48             return;
49         }
50         napi_env env = workWrapper->env;
51         auto closeScope = [env](napi_handle_scope scope) { NapiUtils::CloseScope(env, scope); };
52         std::unique_ptr<napi_handle_scope__, decltype(closeScope)> scope(NapiUtils::OpenScope(env), closeScope);
53         napi_value obj = MakeJsValue(env, workWrapper->data);
54         std::pair<napi_value, napi_value> arg = {NapiUtils::GetUndefined(workWrapper->env), obj};
55         workWrapper->manager->Emit(workWrapper->type, arg);
56         delete workWrapper;
57         delete work;
58     }
59 
60     static napi_value CreateSharingStateChangedParam(napi_env env, void *data);
61     static napi_value CreateInterfaceSharingStateChangedParam(napi_env env, void *data);
62     static napi_value CreateSharingUpstreamChangedParam(napi_env env, void *data);
63     static void SharingStateChangedCallback(uv_work_t *work, int status);
64     static void InterfaceSharingStateChangedCallback(uv_work_t *work, int status);
65     static void SharingUpstreamChangedCallback(uv_work_t *work, int status);
66 };
67 } // namespace NetManagerStandard
68 } // namespace OHOS
69 #endif // NETMANAGER_BASE_NET_SHARE_CALLBACK_OBSERVER_H
70