• 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 #ifndef ENGINE_EVENT_CALLBACK_H
17 #define ENGINE_EVENT_CALLBACK_H
18 
19 #include <set>
20 #include <mutex>
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 #include "intell_voice_napi_util.h"
25 #include "i_intell_voice_engine_callback.h"
26 
27 namespace OHOS {
28 namespace IntellVoiceNapi {
29 using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback;
30 using OHOS::IntellVoiceEngine::IntellVoiceEngineCallBackEvent;
31 
32 struct EngineCallBackInfo {
33     int32_t eventId;
34     bool isSuccess;
35     std::string context;
36 };
37 
38 class EngineEventCallbackNapi : public IIntellVoiceEngineEventCallback {
39 public:
40     explicit EngineEventCallbackNapi(napi_env env);
~EngineEventCallbackNapi()41     ~EngineEventCallbackNapi() override {};
42 
43     void SaveCallbackReference(napi_value callback);
44     void RemoveCallbackReference(napi_value callback);
45     void RemoveAllCallbackReference();
46     uint32_t GetCbReferenceSetSize();
47     void OnEvent(const IntellVoiceEngineCallBackEvent &event) override;
48 
49 private:
50     struct EngineEventCallbackData {
51         EngineCallBackInfo cbInfo;
52         std::shared_ptr<IntellVoiceRef> callback;
53     };
54 
55     static napi_value GetCallBackInfoNapiValue(napi_env env, const EngineCallBackInfo &callbackInfo);
56     void OnJsCallbackEngineEvent(const EngineCallBackInfo &cbInfo, std::shared_ptr<IntellVoiceRef> cbRef);
57 
58     std::mutex mutex_;
59     napi_env env_ = nullptr;
60     uv_loop_s *loop_ = nullptr;
61     std::set<std::shared_ptr<IntellVoiceRef>> callbackRefSet_;
62 };
63 }  // namespace IntellVoiceNapi
64 }  // namespace OHOS
65 #endif
66