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 <set>
19 #include <cinttypes>
20 #include "accesstoken_kit.h"
21 #include "tokenid_kit.h"
22 #include "ipc_skeleton.h"
23 #include "intell_voice_log.h"
24
25 #define LOG_TAG "IntellVoiceCommonNapi"
26
27 namespace OHOS {
28 namespace IntellVoiceNapi {
29 static const std::string NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED_INFO = "Permission denied.";
30 static const std::string NAPI_INTELLIGENT_VOICE_NO_MEMORY_INFO = "No memory.";
31 static const std::string NAPI_INTELLIGENT_VOICE_INVALID_PARAM_INFO = "Invalid parameter.";
32 static const std::string NAPI_INTELLIGENT_VOICE_INIT_FAILED_INFO = "Init failed.";
33 static const std::string NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED_INFO = "Failed to commit the enrollment.";
34 static const std::string NAPI_INTELLIGENT_VOICE_START_CAPTURER_INFO = "Start capturer failed.";
35 static const std::string NAPI_INTELLIGENT_VOICE_READ_FAILED_INFO = "Read failed.";
36 static const std::string NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR_INFO = "System error.";
37
GetMessageByCode(int32_t code)38 std::string IntellVoiceCommonNapi::GetMessageByCode(int32_t code)
39 {
40 static const std::map<int32_t, std::string> messageMap = {
41 {NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED, NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED_INFO},
42 {NAPI_INTELLIGENT_VOICE_NO_MEMORY, NAPI_INTELLIGENT_VOICE_NO_MEMORY_INFO},
43 {NAPI_INTELLIGENT_VOICE_INVALID_PARAM, NAPI_INTELLIGENT_VOICE_INVALID_PARAM_INFO},
44 {NAPI_INTELLIGENT_VOICE_INIT_FAILED, NAPI_INTELLIGENT_VOICE_INIT_FAILED_INFO},
45 {NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED, NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED_INFO},
46 {NAPI_INTELLIGENT_VOICE_START_CAPTURER_FAILED, NAPI_INTELLIGENT_VOICE_START_CAPTURER_INFO},
47 {NAPI_INTELLIGENT_VOICE_READ_FAILED, NAPI_INTELLIGENT_VOICE_READ_FAILED_INFO},
48 {NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR, NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR_INFO}
49 };
50
51 auto iter = messageMap.find(code);
52 if (iter == messageMap.end()) {
53 INTELL_VOICE_LOG_WARN("can not find message, code: %{public}d", code);
54 return "";
55 }
56
57 return iter->second;
58 }
59
ThrowError(napi_env env,int32_t code)60 void IntellVoiceCommonNapi::ThrowError(napi_env env, int32_t code)
61 {
62 std::string messageValue = GetMessageByCode(code);
63 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
64 }
65
IsSameCallback(napi_env env,napi_value callback,napi_ref callbackRef)66 bool IntellVoiceCommonNapi::IsSameCallback(napi_env env, napi_value callback, napi_ref callbackRef)
67 {
68 bool isEqual = false;
69 napi_value copyValue = nullptr;
70
71 napi_get_reference_value(env, callbackRef, ©Value);
72 napi_status status = napi_strict_equals(env, copyValue, callback, &isEqual);
73 if (status != napi_ok) {
74 INTELL_VOICE_LOG_ERROR("get napi_strict_equals failed, status:%{public}d", status);
75 return false;
76 }
77
78 return isEqual;
79 }
80
ConvertResultCode(int32_t result)81 int32_t IntellVoiceCommonNapi::ConvertResultCode(int32_t result)
82 {
83 static const std::set<int32_t> resultCodeSet = {
84 NAPI_INTELLIGENT_VOICE_SUCCESS,
85 NAPI_INTELLIGENT_VOICE_PERMISSION_DENIED,
86 NAPI_INTELLIGENT_VOICE_NOT_SYSTEM_APPLICATION,
87 NAPI_INTELLIGENT_VOICE_NO_MEMORY,
88 NAPI_INTELLIGENT_VOICE_INVALID_PARAM,
89 NAPI_INTELLIGENT_VOICE_INIT_FAILED,
90 NAPI_INTELLIGENT_VOICE_COMMIT_ENROLL_FAILED,
91 NAPI_INTELLIGENT_VOICE_START_CAPTURER_FAILED,
92 NAPI_INTELLIGENT_VOICE_READ_FAILED,
93 NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR
94 };
95
96 auto it = resultCodeSet.find(result);
97 if (it != resultCodeSet.end()) {
98 return *it;
99 }
100
101 INTELL_VOICE_LOG_WARN("can not find in set, result is %{public}d", result);
102 return NAPI_INTELLIGENT_VOICE_SYSTEM_ERROR;
103 }
104
CheckIsSystemApp()105 bool IntellVoiceCommonNapi::CheckIsSystemApp()
106 {
107 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
108 if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
109 INTELL_VOICE_LOG_WARN("Not system app, permission reject tokenid: %{public}" PRIu64 "", fullTokenId);
110 return false;
111 }
112
113 INTELL_VOICE_LOG_INFO("System app, fullTokenId:%{public}" PRIu64 "", fullTokenId);
114 return true;
115 }
116 }
117 }
118