• 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 #include "audio_haptic_common_napi.h"
17 
18 #include "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "ipc_skeleton.h"
21 #include "tokenid_kit.h"
22 
23 #include "audio_haptic_log.h"
24 
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "AudioHapticCommonNapi"};
27 }
28 
29 namespace OHOS {
30 namespace Media {
31 const std::string NAPI_ERR_INPUT_INVALID_INFO = "input parameter check failed";
32 const std::string NAPI_ERR_OPERATE_NOT_ALLOWED_INFO = "Operate not permit in current state";
33 const std::string NAPI_ERR_IO_ERROR_INFO = "input or output error";
34 const std::string NAPI_ERR_SERVICE_DIED_INFO = "service died";
35 const std::string NAPI_ERR_UNSUPPORTED_FORMAT_INFO = "unsupport format";
36 const std::string NAPI_ERR_PARAM_OUT_OF_RANGE_INFO = "Parameter out of range";
37 const std::string NAPI_ERR_NOT_SUPPORTED_INFO = "Function is not supported in current device";
38 const std::string NAPI_ERR_PERMISSION_DENIED_INFO = "Caller is not a system application";
39 
ThrowError(napi_env env,int32_t code)40 void AudioHapticCommonNapi::ThrowError(napi_env env, int32_t code)
41 {
42     std::string messageValue = AudioHapticCommonNapi::GetMessageByCode(code);
43     napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
44 }
45 
ThrowError(napi_env env,int32_t code,const std::string & errMessage)46 void AudioHapticCommonNapi::ThrowError(napi_env env, int32_t code, const std::string &errMessage)
47 {
48     napi_throw_error(env, (std::to_string(code)).c_str(), errMessage.c_str());
49 }
50 
PromiseReject(napi_env env,napi_deferred deferred,const int32_t & errCode,const std::string & errMessage)51 void AudioHapticCommonNapi::PromiseReject(napi_env env, napi_deferred deferred,
52     const int32_t& errCode, const std::string& errMessage)
53 {
54     napi_value error = nullptr;
55     napi_value message = nullptr;
56     napi_value code = nullptr;
57     napi_create_string_utf8(env, (std::to_string(errCode)).c_str(), NAPI_AUTO_LENGTH, &code);
58     napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &message);
59     napi_create_error(env, code, message, &error);
60     napi_reject_deferred(env, deferred, error);
61 }
62 
PromiseReject(napi_env env,napi_deferred deferred,int32_t errCode)63 void AudioHapticCommonNapi::PromiseReject(napi_env env, napi_deferred deferred, int32_t errCode)
64 {
65     std::string messageValue = AudioHapticCommonNapi::GetMessageByCode(errCode);
66     AudioHapticCommonNapi::PromiseReject(env, deferred, errCode, messageValue);
67 }
68 
InitNormalFunc(napi_env env,napi_callback_info info,void ** native,napi_value * argv,size_t paramLength)69 bool AudioHapticCommonNapi::InitNormalFunc(napi_env env, napi_callback_info info,
70     void** native, napi_value* argv, size_t paramLength)
71 {
72     napi_value thisVar = nullptr;
73     size_t argc = paramLength;
74     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
75     if (status != napi_ok || thisVar == nullptr) {
76         MEDIA_LOGE("napi_get_cb_info fail");
77         AudioHapticCommonNapi::ThrowError(env, NAPI_ERR_SERVICE_DIED);
78         return false;
79     }
80 
81     if (argc != paramLength) {
82         MEDIA_LOGE("invalid parameters");
83         std::string logMsg = "requires " + std::to_string(paramLength) + " parameters";
84         AudioHapticCommonNapi::ThrowError(env, NAPI_ERR_INPUT_INVALID, logMsg);
85         return false;
86     }
87 
88     status = napi_unwrap(env, thisVar, native);
89     if (status != napi_ok || *native == nullptr) {
90         MEDIA_LOGE("Failed to unwrap object");
91         AudioHapticCommonNapi::ThrowError(env, NAPI_ERR_SERVICE_DIED);
92         return false;
93     }
94     return true;
95 }
96 
InitPromiseFunc(napi_env env,napi_callback_info info,AsyncContext * asyncContext,napi_value * promise,size_t paramLength)97 bool AudioHapticCommonNapi::InitPromiseFunc(napi_env env, napi_callback_info info,
98     AsyncContext* asyncContext, napi_value* promise, size_t paramLength)
99 {
100     if (asyncContext == nullptr) {
101         AudioHapticCommonNapi::ThrowError(env, NAPI_ERR_SERVICE_DIED);
102         return false;
103     }
104     napi_create_promise(env, &asyncContext->deferred, promise);
105     napi_value thisVar = nullptr;
106     size_t argc = paramLength;
107     napi_status status = napi_get_cb_info(env, info, &argc, asyncContext->argv, &thisVar, nullptr);
108     if (status != napi_ok || thisVar == nullptr) {
109         MEDIA_LOGE("napi_get_cb_info fail");
110         AudioHapticCommonNapi::PromiseReject(env, asyncContext->deferred, NAPI_ERR_SERVICE_DIED);
111         return false;
112     }
113 
114     if (argc != paramLength) {
115         MEDIA_LOGE("invalid parameters");
116         std::string logMsg = "requires " + std::to_string(paramLength) + " parameters";
117         AudioHapticCommonNapi::ThrowError(env, NAPI_ERR_INPUT_INVALID, logMsg);
118         return false;
119     }
120     status = napi_unwrap(env, thisVar, &asyncContext->objectInfo);
121     if (status != napi_ok) {
122         MEDIA_LOGE("Failed to unwrap object");
123         AudioHapticCommonNapi::PromiseReject(env, asyncContext->deferred, NAPI_ERR_SERVICE_DIED);
124         return false;
125     }
126     return true;
127 }
128 
VerifySelfSystemPermission()129 bool AudioHapticCommonNapi::VerifySelfSystemPermission()
130 {
131     Security::AccessToken::FullTokenID selfTokenID = IPCSkeleton::GetSelfTokenID();
132     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfTokenID);
133 }
134 
GetMessageByCode(int32_t & code)135 std::string AudioHapticCommonNapi::GetMessageByCode(int32_t &code)
136 {
137     std::string errMessage;
138     switch (code) {
139         case NAPI_ERR_INPUT_INVALID:
140             errMessage = NAPI_ERR_INPUT_INVALID_INFO;
141             break;
142         case NAPI_ERR_OPERATE_NOT_ALLOWED:
143             errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
144             break;
145         case NAPI_ERR_IO_ERROR:
146             errMessage = NAPI_ERR_IO_ERROR_INFO;
147             break;
148         case NAPI_ERR_SERVICE_DIED:
149             errMessage = NAPI_ERR_SERVICE_DIED_INFO;
150             break;
151         case NAPI_ERR_UNSUPPORTED_FORMAT:
152             errMessage = NAPI_ERR_UNSUPPORTED_FORMAT_INFO;
153             break;
154         case NAPI_ERR_PARAM_OUT_OF_RANGE:
155             errMessage = NAPI_ERR_PARAM_OUT_OF_RANGE_INFO;
156             break;
157         case NAPI_ERR_NOT_SUPPORTED:
158             errMessage = NAPI_ERR_NOT_SUPPORTED_INFO;
159             break;
160         case NAPI_ERR_PERMISSION_DENIED:
161             errMessage = NAPI_ERR_PERMISSION_DENIED_INFO;
162             break;
163         default:
164             errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
165             code = NAPI_ERR_OPERATE_NOT_ALLOWED;
166             break;
167     }
168     return errMessage;
169 }
170 
GetStringArgument(napi_env env,napi_value value)171 std::string AudioHapticCommonNapi::GetStringArgument(napi_env env, napi_value value)
172 {
173     std::string strValue = "";
174     size_t bufLength = 0;
175     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
176     if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
177         char *buffer = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
178         CHECK_AND_RETURN_RET_LOG(buffer != nullptr, strValue, "GetStringArgument: no memory");
179         status = napi_get_value_string_utf8(env, value, buffer, bufLength + 1, &bufLength);
180         if (status == napi_ok) {
181             MEDIA_LOGI("GetStringArgument: argument = %{public}s", buffer);
182             strValue = buffer;
183         }
184         free(buffer);
185         buffer = nullptr;
186     }
187     return strValue;
188 }
189 } // namespace Media
190 } // namespace OHOS