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 #include "intell_voice_common_napi.h"
16
17 #include <map>
18 #include "intell_voice_log.h"
19
20 #define LOG_TAG "IntellVoiceCommonNapi"
21
22 namespace OHOS {
23 namespace IntellVoiceNapi {
24 static const std::string NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED_INFO = "Permission denied.";
25 static const std::string NAPI_INTELLIGENT_VOICE_NO_MEMORY_INFO = "No memory.";
26 static const std::string NAPI_INTELLIGENT_VOICE_INVALID_PARAM_INFO = "Invalid parameter.";
27 static const std::string NAPI_INTELLIGENT_VOICE_INIT_FAILED_INFO = "Init failed.";
28 static const std::string NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED_INFO = "Failed to commit the enrollment.";
29 static const std::string NAPI_INTELLIGENT_VOICE_START_CAPTURER_INFO = "Start capturer failed.";
30 static const std::string NAPI_INTELLIGENT_VOICE_READ_FAILED_INFO = "Read failed.";
31 static const std::string NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR_INFO = "System error.";
32
GetMessageByCode(int32_t code)33 std::string IntellVoiceCommonNapi::GetMessageByCode(int32_t code)
34 {
35 static const std::map<int32_t, std::string> messageMap = {
36 {NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED, NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED_INFO},
37 {NAPI_INTELLIGENT_VOICE_NO_MEMORY, NAPI_INTELLIGENT_VOICE_NO_MEMORY_INFO},
38 {NAPI_INTELLIGENT_VOICE_INVALID_PARAM, NAPI_INTELLIGENT_VOICE_INVALID_PARAM_INFO},
39 {NAPI_INTELLIGENT_VOICE_INIT_FAILED, NAPI_INTELLIGENT_VOICE_INIT_FAILED_INFO},
40 {NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED, NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED_INFO},
41 {NAPI_INTELLIGENT_VOICE_START_CAPTURER_FAILED, NAPI_INTELLIGENT_VOICE_START_CAPTURER_INFO},
42 {NAPI_INTELLIGENT_VOICE_READ_FAILED, NAPI_INTELLIGENT_VOICE_READ_FAILED_INFO},
43 {NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR, NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR_INFO}
44 };
45
46 auto iter = messageMap.find(code);
47 if (iter == messageMap.end()) {
48 INTELL_VOICE_LOG_ERROR("invalid code: %{public}d", code);
49 return "";
50 }
51
52 return iter->second;
53 }
54
ThrowError(napi_env env,int32_t code)55 void IntellVoiceCommonNapi::ThrowError(napi_env env, int32_t code)
56 {
57 std::string messageValue = GetMessageByCode(code);
58 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
59 }
60
IsSameCallback(napi_env env,napi_value callback,napi_ref callbackRef)61 bool IntellVoiceCommonNapi::IsSameCallback(napi_env env, napi_value callback, napi_ref callbackRef)
62 {
63 bool isEqual = false;
64 napi_value copyValue = nullptr;
65
66 napi_get_reference_value(env, callbackRef, ©Value);
67 napi_status status = napi_strict_equals(env, copyValue, callback, &isEqual);
68 if (status != napi_ok) {
69 INTELL_VOICE_LOG_ERROR("get napi_strict_equals failed, status:%{public}d", status);
70 return false;
71 }
72
73 return isEqual;
74 }
75 }
76 }
77