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