• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 INTELL_VOICEUPDATE_CALLBACK_NAPI_H
17 #define INTELL_VOICEUPDATE_CALLBACK_NAPI_H
18 
19 #include <queue>
20 #include <map>
21 #include <uv.h>
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "intell_voice_napi_util.h"
25 #include "i_intell_voice_update_callback.h"
26 #include "intell_voice_napi_queue.h"
27 #include "intell_voice_info.h"
28 
29 namespace OHOS {
30 namespace IntellVoiceNapi {
31 using namespace OHOS::IntellVoice;
32 using ProcessWorkFunc = std::function<int32_t(AsyncContext *)>;
33 
34 class UpdateAsyncContext : public AsyncContext {
35 public:
UpdateAsyncContext(napi_env env)36     explicit UpdateAsyncContext(napi_env env) : AsyncContext(env) {};
37     ProcessWorkFunc processWork = nullptr;
38     int32_t result = OHOS::IntellVoice::EnrollResult::UNKNOWN_ERROR;
39     std::vector <OHOS::IntellVoice::WakeupSourceFile> cloneFiles;
40     std::string wakeupInfo;
41 };
42 
43 class IntellVoiceUpdateCallbackNapi : public IIntellVoiceUpdateCallback {
44 public:
45     explicit IntellVoiceUpdateCallbackNapi(const napi_env env);
46     virtual ~IntellVoiceUpdateCallbackNapi();
47 
48     void OnUpdateComplete(const int result) override;
49     void QueueAsyncWork(UpdateAsyncContext *context);
50     void ClearAsyncWork(bool error, const std::string &msg);
51 
52 private:
53     void OnJsCallBack(UpdateAsyncContext *context);
54     static void UvWorkCallBack(uv_work_t *work, int status);
55 
56 private:
57     std::mutex mutex_;
58     napi_env env_ = nullptr;
59     uv_loop_s *loop_ = nullptr;
60     std::queue<UpdateAsyncContext *> context_;
61 };
62 }  // namespace IntellVoiceNapi
63 }  // namespace OHOS
64 #endif
65