• 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 #ifndef AUDIO_HAPTIC_COMMON_NAPI_H
17 #define AUDIO_HAPTIC_COMMON_NAPI_H
18 
19 #include <string>
20 
21 #include "napi/native_api.h"
22 #include "napi/native_node_api.h"
23 
24 namespace OHOS {
25 namespace Media {
26 const int32_t  NAPI_ERR_INPUT_INVALID = 401;
27 const int32_t  NAPI_ERR_OPERATE_NOT_ALLOWED = 5400102;
28 const int32_t  NAPI_ERR_IO_ERROR = 5400103;
29 const int32_t  NAPI_ERR_SERVICE_DIED = 5400105;
30 const int32_t  NAPI_ERR_UNSUPPORTED_FORMAT = 5400106;
31 
32 class AudioHapticCommonNapi {
33 public:
34     AudioHapticCommonNapi() = delete;
35     ~AudioHapticCommonNapi() = delete;
36     static void ThrowError(napi_env env, int32_t code);
37     static void ThrowError(napi_env env, int32_t code, const std::string &errMessage);
38     static std::string GetMessageByCode(int32_t &code);
39     static std::string GetStringArgument(napi_env env, napi_value value);
40 };
41 
42 struct AutoRef {
AutoRefAutoRef43     AutoRef(napi_env env, napi_ref cb)
44         : env_(env), cb_(cb)
45     {
46     }
~AutoRefAutoRef47     ~AutoRef()
48     {
49         if (env_ != nullptr && cb_ != nullptr) {
50             (void)napi_delete_reference(env_, cb_);
51         }
52     }
53     napi_env env_;
54     napi_ref cb_;
55 };
56 } // namespace Media
57 } // namespace OHOS
58 #endif // AUDIO_HAPTIC_COMMON_NAPI_H