• 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 "ringtone_common_napi.h"
17 #include <climits>
18 #include "system_sound_log.h"
19 
20 namespace {
21 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "RingtonePlayerCallbackNapi"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
GetStringArgument(napi_env env,napi_value value)26 std::string RingtoneCommonNapi::GetStringArgument(napi_env env, napi_value value)
27 {
28     std::string strValue = "";
29     size_t bufLength = 0;
30     napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
31     if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
32         char *buffer = static_cast<char *>(malloc((bufLength + 1) * sizeof(char)));
33         CHECK_AND_RETURN_RET_LOG(buffer != nullptr, strValue, "no memory");
34         status = napi_get_value_string_utf8(env, value, buffer, bufLength + 1, &bufLength);
35         if (status == napi_ok) {
36             MEDIA_LOGI("argument = %{public}s", buffer);
37             strValue = buffer;
38         }
39         free(buffer);
40         buffer = nullptr;
41     }
42     return strValue;
43 }
44 
ThrowError(napi_env env,int32_t code,const std::string & errMessage)45 void RingtoneCommonNapi::ThrowError(napi_env env, int32_t code, const std::string &errMessage)
46 {
47     std::string messageValue;
48     if (code == NAPI_ERR_INVALID_PARAM || code == NAPI_ERR_INPUT_INVALID) {
49         messageValue = errMessage.c_str();
50     } else {
51         messageValue = RingtoneCommonNapi::GetMessageByCode(code);
52     }
53     napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
54 }
55 
GetMessageByCode(int32_t & code)56 std::string RingtoneCommonNapi::GetMessageByCode(int32_t &code)
57 {
58     std::string errMessage;
59     switch (code) {
60         case NAPI_ERR_INVALID_PARAM:
61             errMessage = NAPI_ERR_INVALID_PARAM_INFO;
62             break;
63         case NAPI_ERR_NO_MEMORY:
64             errMessage = NAPI_ERR_NO_MEMORY_INFO;
65             break;
66         case NAPI_ERR_SYSTEM:
67             errMessage = NAPI_ERR_SYSTEM_INFO;
68             break;
69         case NAPI_ERR_INPUT_INVALID:
70             errMessage = NAPI_ERR_INPUT_INVALID_INFO;
71             break;
72         default:
73             errMessage = NAPI_ERR_SYSTEM_INFO;
74             code = NAPI_ERR_SYSTEM;
75             break;
76     }
77     return errMessage;
78 }
79 } // namespace Media
80 } // namespace OHOS