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_player_options_napi.h"
17 #include "audio_haptic_log.h"
18
19 using namespace std;
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "AudioHapticPlayerOptionsNapi"};
23 }
24
25 namespace OHOS {
26 namespace Media {
27 thread_local napi_ref AudioHapticPlayerOptionsNapi::sConstructor_ = nullptr;
28
29 bool AudioHapticPlayerOptionsNapi::sMuteAudio_ = false;
30 bool AudioHapticPlayerOptionsNapi::sMuteHaptics_ = false;
31
AudioHapticPlayerOptionsNapi()32 AudioHapticPlayerOptionsNapi::AudioHapticPlayerOptionsNapi()
33 : env_(nullptr) {
34 }
35
36 AudioHapticPlayerOptionsNapi::~AudioHapticPlayerOptionsNapi() = default;
37
Destructor(napi_env env,void * nativeObject,void * finalize_hint)38 void AudioHapticPlayerOptionsNapi::Destructor(napi_env env, void *nativeObject, void *finalize_hint)
39 {
40 if (nativeObject != nullptr) {
41 auto obj = static_cast<AudioHapticPlayerOptionsNapi *>(nativeObject);
42 delete obj;
43 obj = nullptr;
44 }
45 }
46
Init(napi_env env,napi_value exports)47 napi_value AudioHapticPlayerOptionsNapi::Init(napi_env env, napi_value exports)
48 {
49 napi_status status;
50 napi_value constructor;
51 napi_value result = nullptr;
52 napi_get_undefined(env, &result);
53
54 napi_property_descriptor audio_haptic_player_options_properties[] = {
55 DECLARE_NAPI_GETTER_SETTER("muteAudio", IsAudioMute, SetAudioMute),
56 DECLARE_NAPI_GETTER_SETTER("muteHaptics", IsHapticsMute, SetHapticsMute)
57 };
58
59 status = napi_define_class(env, AUDIO_HAPTIC_PLAYER_OPTIONS_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH, Construct,
60 nullptr, sizeof(audio_haptic_player_options_properties) / sizeof(audio_haptic_player_options_properties[0]),
61 audio_haptic_player_options_properties, &constructor);
62 if (status != napi_ok) {
63 return result;
64 }
65
66 status = napi_create_reference(env, constructor, 1, &sConstructor_);
67 if (status == napi_ok) {
68 status = napi_set_named_property(env, exports,
69 AUDIO_HAPTIC_PLAYER_OPTIONS_NAPI_CLASS_NAME.c_str(), constructor);
70 if (status == napi_ok) {
71 return exports;
72 }
73 }
74 MEDIA_LOGE("Failure in AudioHapticPlayerOptionsNapi::Init()");
75
76 return result;
77 }
78
Construct(napi_env env,napi_callback_info info)79 napi_value AudioHapticPlayerOptionsNapi::Construct(napi_env env, napi_callback_info info)
80 {
81 napi_status status;
82 napi_value jsThis = nullptr;
83 size_t argCount = 0;
84
85 status = napi_get_cb_info(env, info, &argCount, nullptr, &jsThis, nullptr);
86 if (status == napi_ok) {
87 unique_ptr<AudioHapticPlayerOptionsNapi> obj = make_unique<AudioHapticPlayerOptionsNapi>();
88 if (obj != nullptr) {
89 obj->env_ = env;
90 obj->muteAudio_ = sMuteAudio_;
91 obj->muteHaptics_ = sMuteHaptics_;
92 status = napi_wrap(env, jsThis, static_cast<void*>(obj.get()),
93 AudioHapticPlayerOptionsNapi::Destructor, nullptr, nullptr);
94 if (status == napi_ok) {
95 obj.release();
96 return jsThis;
97 }
98 }
99 }
100
101 MEDIA_LOGE("Failed in AudioHapticPlayerOptionsNapi::Construct()!");
102 napi_get_undefined(env, &jsThis);
103
104 return jsThis;
105 }
106
CreateAudioHapticPlayerOptionsWrapper(napi_env env,bool muteAudio,bool muteHaptics)107 napi_value AudioHapticPlayerOptionsNapi::CreateAudioHapticPlayerOptionsWrapper(napi_env env,
108 bool muteAudio, bool muteHaptics)
109 {
110 napi_status status;
111 napi_value result = nullptr;
112 napi_value constructor;
113
114 status = napi_get_reference_value(env, sConstructor_, &constructor);
115 if (status == napi_ok) {
116 sMuteAudio_ = muteAudio;
117 sMuteHaptics_ = muteHaptics;
118 status = napi_new_instance(env, constructor, 0, nullptr, &result);
119 if (status == napi_ok) {
120 return result;
121 }
122 }
123 MEDIA_LOGE("Failed in CreateAudioHapticPlayerOptionsWrapper, %{public}d", status);
124
125 napi_get_undefined(env, &result);
126
127 return result;
128 }
129
IsAudioMute(napi_env env,napi_callback_info info)130 napi_value AudioHapticPlayerOptionsNapi::IsAudioMute(napi_env env, napi_callback_info info)
131 {
132 napi_status status;
133 AudioHapticPlayerOptionsNapi *audioHapticPlayerOptionsNapi = nullptr;
134 size_t argc = 0;
135 napi_value jsThis = nullptr;
136 napi_value jsResult = nullptr;
137 napi_get_undefined(env, &jsResult);
138
139 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
140 if (status != napi_ok || jsThis == nullptr) {
141 MEDIA_LOGE("IsAudioMute: failed for napi_get_cb_info");
142 return jsResult;
143 }
144
145 status = napi_unwrap(env, jsThis, (void **)&audioHapticPlayerOptionsNapi);
146 if (status == napi_ok) {
147 status = napi_get_boolean(env, audioHapticPlayerOptionsNapi->muteAudio_, &jsResult);
148 if (status == napi_ok) {
149 return jsResult;
150 }
151 }
152
153 return jsResult;
154 }
155
SetAudioMute(napi_env env,napi_callback_info info)156 napi_value AudioHapticPlayerOptionsNapi::SetAudioMute(napi_env env, napi_callback_info info)
157 {
158 napi_status status;
159 AudioHapticPlayerOptionsNapi *audioHapticPlayerOptionsNapi = nullptr;
160 size_t argc = 1;
161 napi_value args[1] = { nullptr };
162 napi_value jsThis = nullptr;
163 bool muteAudio = false;
164 napi_value jsResult = nullptr;
165 napi_get_undefined(env, &jsResult);
166
167 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
168 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
169 MEDIA_LOGE("SetAudioMute: failed for napi_get_cb_info");
170 return jsResult;
171 }
172
173 status = napi_unwrap(env, jsThis, (void **)&audioHapticPlayerOptionsNapi);
174 if (status == napi_ok) {
175 napi_valuetype valueType = napi_undefined;
176 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_boolean) {
177 MEDIA_LOGE("SetAudioMute: failed for wrong data type");
178 return jsResult;
179 }
180 }
181
182 status = napi_get_value_bool(env, args[0], &muteAudio);
183 if (status == napi_ok) {
184 audioHapticPlayerOptionsNapi->muteAudio_ = muteAudio;
185 }
186
187 return jsResult;
188 }
189
IsHapticsMute(napi_env env,napi_callback_info info)190 napi_value AudioHapticPlayerOptionsNapi::IsHapticsMute(napi_env env, napi_callback_info info)
191 {
192 napi_status status;
193 AudioHapticPlayerOptionsNapi *audioHapticPlayerOptionsNapi = nullptr;
194 size_t argc = 0;
195 napi_value jsThis = nullptr;
196 napi_value jsResult = nullptr;
197 napi_get_undefined(env, &jsResult);
198
199 status = napi_get_cb_info(env, info, &argc, nullptr, &jsThis, nullptr);
200 if (status != napi_ok || jsThis == nullptr) {
201 MEDIA_LOGE("IsHapticsMute: failed for napi_get_cb_info");
202 return jsResult;
203 }
204
205 status = napi_unwrap(env, jsThis, (void **)&audioHapticPlayerOptionsNapi);
206 if (status == napi_ok) {
207 status = napi_get_boolean(env, audioHapticPlayerOptionsNapi->muteHaptics_, &jsResult);
208 if (status == napi_ok) {
209 return jsResult;
210 }
211 }
212
213 return jsResult;
214 }
215
SetHapticsMute(napi_env env,napi_callback_info info)216 napi_value AudioHapticPlayerOptionsNapi::SetHapticsMute(napi_env env, napi_callback_info info)
217 {
218 napi_status status;
219 AudioHapticPlayerOptionsNapi *audioHapticPlayerOptionsNapi = nullptr;
220 size_t argc = 1;
221 napi_value args[1] = { nullptr };
222 napi_value jsThis = nullptr;
223 bool muteHaptics = false;
224 napi_value jsResult = nullptr;
225 napi_get_undefined(env, &jsResult);
226
227 status = napi_get_cb_info(env, info, &argc, args, &jsThis, nullptr);
228 if (status != napi_ok || jsThis == nullptr || args[0] == nullptr) {
229 MEDIA_LOGE("SetHapticsMute: failed for napi_get_cb_info");
230 return jsResult;
231 }
232
233 status = napi_unwrap(env, jsThis, (void **)&audioHapticPlayerOptionsNapi);
234 if (status == napi_ok) {
235 napi_valuetype valueType = napi_undefined;
236 if (napi_typeof(env, args[0], &valueType) != napi_ok || valueType != napi_boolean) {
237 MEDIA_LOGE("SetHapticsMute: failed for wrong data type");
238 return jsResult;
239 }
240 }
241
242 status = napi_get_value_bool(env, args[0], &muteHaptics);
243 if (status == napi_ok) {
244 audioHapticPlayerOptionsNapi->muteHaptics_ = muteHaptics;
245 }
246
247 return jsResult;
248 }
249 } // namespace Media
250 } // namespace OHOS
251