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 "system_sound_manager_napi.h"
17
18 #include "access_token.h"
19 #include "accesstoken_kit.h"
20 #include "audio_renderer_info_napi.h"
21 #include "ipc_skeleton.h"
22 #include "system_sound_log.h"
23 #include "tokenid_kit.h"
24
25 using namespace std;
26 using OHOS::Security::AccessToken::AccessTokenKit;
27
28 namespace {
29 /* Constants for array index */
30 const int32_t PARAM0 = 0;
31 const int32_t PARAM1 = 1;
32 const int32_t PARAM2 = 2;
33 const int32_t PARAM3 = 3;
34 const int32_t PARAM4 = 4;
35
36 /* Constants for array size */
37 const int32_t ARGS_ONE = 1;
38 const int32_t ARGS_TWO = 2;
39 const int32_t ARGS_THREE = 3;
40 const int32_t ARGS_FOUR = 4;
41 const int32_t ARGS_FIVE = 5;
42 const int32_t SIZE = 1024;
43
44 /* Constants for tone type */
45 const int32_t CARD_0 = 0;
46 const int32_t CARD_1 = 1;
47 const int32_t SYSTEM_NOTIFICATION = 32;
48
49 const int TYPEERROR = -2;
50 const int ERROR = -1;
51 const int SUCCESS = 0;
52
53 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO_NAPI, "SystemSoundManagerNapi"};
54 }
55
56 namespace OHOS {
57 namespace Media {
58 thread_local napi_ref SystemSoundManagerNapi::sConstructor_ = nullptr;
59 thread_local napi_ref SystemSoundManagerNapi::ringtoneType_ = nullptr;
60 thread_local napi_ref SystemSoundManagerNapi::systemToneType_ = nullptr;
61 thread_local napi_ref SystemSoundManagerNapi::toneCustomizedType_ = nullptr;
62 thread_local napi_ref SystemSoundManagerNapi::toneHapticsMode_ = nullptr;
63
SystemSoundManagerNapi()64 SystemSoundManagerNapi::SystemSoundManagerNapi()
65 : env_(nullptr), sysSoundMgrClient_(nullptr) {}
66
67 SystemSoundManagerNapi::~SystemSoundManagerNapi() = default;
68
AddNamedProperty(napi_env env,napi_value object,const std::string name,int32_t enumValue)69 napi_status SystemSoundManagerNapi::AddNamedProperty(napi_env env, napi_value object, const std::string name,
70 int32_t enumValue)
71 {
72 napi_status status;
73 napi_value napiValue;
74
75 status = napi_create_int32(env, enumValue, &napiValue);
76 if (status == napi_ok) {
77 status = napi_set_named_property(env, object, name.c_str(), napiValue);
78 }
79
80 return status;
81 }
82
CreateRingtoneTypeObject(napi_env env)83 napi_value SystemSoundManagerNapi::CreateRingtoneTypeObject(napi_env env)
84 {
85 napi_value result = nullptr;
86 napi_status status;
87 std::string propName;
88 int32_t refCount = 1;
89
90 status = napi_create_object(env, &result);
91 if (status == napi_ok) {
92 for (auto &iter: ringtoneTypeMap) {
93 propName = iter.first;
94 status = AddNamedProperty(env, result, propName, iter.second);
95 if (status != napi_ok) {
96 MEDIA_LOGE("CreateRingtoneTypeObject: Failed to add named prop!");
97 break;
98 }
99 propName.clear();
100 }
101 if (status == napi_ok) {
102 status = napi_create_reference(env, result, refCount, &ringtoneType_);
103 if (status == napi_ok) {
104 return result;
105 }
106 }
107 }
108 napi_get_undefined(env, &result);
109
110 return result;
111 }
112
CreateSystemToneTypeObject(napi_env env)113 napi_value SystemSoundManagerNapi::CreateSystemToneTypeObject(napi_env env)
114 {
115 napi_value result = nullptr;
116 napi_status status;
117 std::string propName;
118 int32_t refCount = 1;
119
120 status = napi_create_object(env, &result);
121 if (status == napi_ok) {
122 for (auto &iter: systemToneTypeMap) {
123 propName = iter.first;
124 status = AddNamedProperty(env, result, propName, iter.second);
125 if (status != napi_ok) {
126 MEDIA_LOGE("CreateSystemToneTypeObject: Failed to add named prop!");
127 break;
128 }
129 propName.clear();
130 }
131 if (status == napi_ok) {
132 status = napi_create_reference(env, result, refCount, &systemToneType_);
133 if (status == napi_ok) {
134 return result;
135 }
136 }
137 }
138 napi_get_undefined(env, &result);
139
140 return result;
141 }
142
CreateToneHapticsTypeObject(napi_env env)143 napi_value SystemSoundManagerNapi::CreateToneHapticsTypeObject(napi_env env)
144 {
145 napi_value result = nullptr;
146 napi_status status;
147 std::string propName;
148 int32_t refCount = 1;
149
150 status = napi_create_object(env, &result);
151 if (status == napi_ok) {
152 for (auto &iter: toneHapticsTypeMap) {
153 propName = iter.first;
154 status = AddNamedProperty(env, result, propName, iter.second);
155 if (status != napi_ok) {
156 MEDIA_LOGE("CreateToneHapticsTypeObject: Failed to add named prop!");
157 break;
158 }
159 propName.clear();
160 }
161 if (status == napi_ok) {
162 status = napi_create_reference(env, result, refCount, &systemToneType_);
163 if (status == napi_ok) {
164 return result;
165 }
166 }
167 }
168 napi_get_undefined(env, &result);
169
170 return result;
171 }
172
CreateToneCustomizedTypeObject(napi_env env)173 napi_value SystemSoundManagerNapi::CreateToneCustomizedTypeObject(napi_env env)
174 {
175 napi_value result = nullptr;
176 napi_status status;
177 std::string propName;
178 int32_t refCount = 1;
179
180 status = napi_create_object(env, &result);
181 if (status == napi_ok) {
182 for (auto &iter: toneCustomizedTypeMap) {
183 propName = iter.first;
184 status = AddNamedProperty(env, result, propName, iter.second);
185 if (status != napi_ok) {
186 MEDIA_LOGE("CreateToneCustomizedTypeObject: Failed to add named prop!");
187 break;
188 }
189 propName.clear();
190 }
191 if (status == napi_ok) {
192 status = napi_create_reference(env, result, refCount, &toneCustomizedType_);
193 if (status == napi_ok) {
194 return result;
195 }
196 }
197 }
198 napi_get_undefined(env, &result);
199
200 return result;
201 }
202
CreateToneHapticsModeObject(napi_env env)203 napi_value SystemSoundManagerNapi::CreateToneHapticsModeObject(napi_env env)
204 {
205 napi_value result = nullptr;
206 napi_status status;
207 std::string propName;
208 int32_t refCount = 1;
209
210 status = napi_create_object(env, &result);
211 if (status == napi_ok) {
212 for (auto &iter: toneHapticsModeMap) {
213 propName = iter.first;
214 status = AddNamedProperty(env, result, propName, iter.second);
215 if (status != napi_ok) {
216 MEDIA_LOGE("CreateToneHapticsModeObject: Failed to add named prop!");
217 break;
218 }
219 propName.clear();
220 }
221 if (status == napi_ok) {
222 status = napi_create_reference(env, result, refCount, &toneHapticsMode_);
223 if (status == napi_ok) {
224 return result;
225 }
226 }
227 }
228 napi_get_undefined(env, &result);
229
230 return result;
231 }
232
CreateToneCategoryRingtoneObject(napi_env env)233 napi_value SystemSoundManagerNapi::CreateToneCategoryRingtoneObject(napi_env env)
234 {
235 napi_value toneCategoryRingtone;
236 napi_create_int32(env, TONE_CATEGORY_RINGTONE, &toneCategoryRingtone);
237 return toneCategoryRingtone;
238 }
239
CreateToneCategoryTextMessageObject(napi_env env)240 napi_value SystemSoundManagerNapi::CreateToneCategoryTextMessageObject(napi_env env)
241 {
242 napi_value toneCategoryTextMessage;
243 napi_create_int32(env, TONE_CATEGORY_TEXT_MESSAGE, &toneCategoryTextMessage);
244 return toneCategoryTextMessage;
245 }
246
CreateToneCategoryNotificationObject(napi_env env)247 napi_value SystemSoundManagerNapi::CreateToneCategoryNotificationObject(napi_env env)
248 {
249 napi_value toneCategoryNotification;
250 napi_create_int32(env, TONE_CATEGORY_NOTIFICATION, &toneCategoryNotification);
251 return toneCategoryNotification;
252 }
253
CreateToneCategoryAlarmObject(napi_env env)254 napi_value SystemSoundManagerNapi::CreateToneCategoryAlarmObject(napi_env env)
255 {
256 napi_value toneCategoryAlarm;
257 napi_create_int32(env, TONE_CATEGORY_ALARM, &toneCategoryAlarm);
258 return toneCategoryAlarm;
259 }
260
CreateToneCategoryContactsObject(napi_env env)261 napi_value SystemSoundManagerNapi::CreateToneCategoryContactsObject(napi_env env)
262 {
263 napi_value toneCategoryContacts;
264 napi_create_int32(env, TONE_CATEGORY_CONTACTS, &toneCategoryContacts);
265 return toneCategoryContacts;
266 }
267
DefineClassProperties(napi_env env,napi_value & ctorObj)268 napi_status SystemSoundManagerNapi::DefineClassProperties(napi_env env, napi_value &ctorObj)
269 {
270 napi_property_descriptor syssndmgr_prop[] = {
271 DECLARE_NAPI_FUNCTION("setSystemRingtoneUri", SetRingtoneUri), // deprecated
272 DECLARE_NAPI_FUNCTION("setRingtoneUri", SetRingtoneUri),
273 DECLARE_NAPI_FUNCTION("getSystemRingtoneUri", GetRingtoneUri), // deprecated
274 DECLARE_NAPI_FUNCTION("getRingtoneUri", GetRingtoneUri),
275 DECLARE_NAPI_FUNCTION("getSystemRingtonePlayer", GetRingtonePlayer), // deprecated
276 DECLARE_NAPI_FUNCTION("getRingtonePlayer", GetRingtonePlayer),
277 DECLARE_NAPI_FUNCTION("setSystemToneUri", SetSystemToneUri),
278 DECLARE_NAPI_FUNCTION("getSystemToneUri", GetSystemToneUri),
279 DECLARE_NAPI_FUNCTION("getSystemTonePlayer", GetSystemTonePlayer),
280 DECLARE_NAPI_FUNCTION("getDefaultRingtoneAttrs", GetDefaultRingtoneAttrs),
281 DECLARE_NAPI_FUNCTION("getRingtoneAttrList", GetRingtoneAttrList),
282 DECLARE_NAPI_FUNCTION("getDefaultSystemToneAttrs", GetDefaultSystemToneAttrs),
283 DECLARE_NAPI_FUNCTION("getSystemToneAttrList", GetSystemToneAttrList),
284 DECLARE_NAPI_FUNCTION("getDefaultAlarmToneAttrs", GetDefaultAlarmToneAttrs),
285 DECLARE_NAPI_FUNCTION("setAlarmToneUri", SetAlarmToneUri),
286 DECLARE_NAPI_FUNCTION("getAlarmToneUri", GetAlarmToneUri),
287 DECLARE_NAPI_FUNCTION("getAlarmToneAttrList", GetAlarmToneAttrList),
288 DECLARE_NAPI_FUNCTION("openAlarmTone", OpenAlarmTone),
289 DECLARE_NAPI_FUNCTION("close", Close),
290 DECLARE_NAPI_FUNCTION("addCustomizedTone", AddCustomizedTone),
291 DECLARE_NAPI_FUNCTION("removeCustomizedTone", RemoveCustomizedTone),
292 DECLARE_NAPI_FUNCTION("getToneHapticsSettings", GetToneHapticsSettings),
293 DECLARE_NAPI_FUNCTION("setToneHapticsSettings", SetToneHapticsSettings),
294 DECLARE_NAPI_FUNCTION("getToneHapticsList", GetToneHapticsList),
295 DECLARE_NAPI_FUNCTION("getHapticsAttrsSyncedWithTone", GetHapticsAttrsSyncedWithTone),
296 DECLARE_NAPI_FUNCTION("openToneHaptics", OpenToneHaptics),
297 };
298
299 return napi_define_class(env, SYSTEM_SND_MNGR_NAPI_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH,
300 Construct, nullptr, sizeof(syssndmgr_prop) / sizeof(syssndmgr_prop[0]), syssndmgr_prop, &ctorObj);
301 }
302
DefineStaticProperties(napi_env env,napi_value exports)303 napi_status SystemSoundManagerNapi::DefineStaticProperties(napi_env env, napi_value exports)
304 {
305 napi_property_descriptor static_prop[] = {
306 DECLARE_NAPI_STATIC_FUNCTION("getSystemSoundManager", GetSystemSoundManager),
307 DECLARE_NAPI_PROPERTY("RingtoneType", CreateRingtoneTypeObject(env)),
308 DECLARE_NAPI_PROPERTY("SystemToneType", CreateSystemToneTypeObject(env)),
309 DECLARE_NAPI_PROPERTY("ToneHapticsType", CreateToneHapticsTypeObject(env)),
310 DECLARE_NAPI_STATIC_FUNCTION("createCustomizedToneAttrs", CreateCustomizedToneAttrs),
311 DECLARE_NAPI_PROPERTY("ToneCustomizedType", CreateToneCustomizedTypeObject(env)),
312 DECLARE_NAPI_PROPERTY("TONE_CATEGORY_RINGTONE", CreateToneCategoryRingtoneObject(env)),
313 DECLARE_NAPI_PROPERTY("TONE_CATEGORY_TEXT_MESSAGE", CreateToneCategoryTextMessageObject(env)),
314 DECLARE_NAPI_PROPERTY("TONE_CATEGORY_NOTIFICATION", CreateToneCategoryNotificationObject(env)),
315 DECLARE_NAPI_PROPERTY("TONE_CATEGORY_CONTACTS", CreateToneCategoryContactsObject(env)),
316 DECLARE_NAPI_PROPERTY("TONE_CATEGORY_ALARM", CreateToneCategoryAlarmObject(env)),
317 DECLARE_NAPI_PROPERTY("ToneHapticsMode", CreateToneHapticsModeObject(env)),
318 };
319
320 return napi_define_properties(env, exports, sizeof(static_prop) / sizeof(static_prop[0]), static_prop);
321 }
322
Init(napi_env env,napi_value exports)323 napi_value SystemSoundManagerNapi::Init(napi_env env, napi_value exports)
324 {
325 napi_value ctorObj;
326 int32_t refCount = 1;
327
328 napi_status status = DefineClassProperties(env, ctorObj);
329 if (status != napi_ok) {
330 return nullptr;
331 }
332
333 status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
334 if (status != napi_ok) {
335 return nullptr;
336 }
337
338 status = napi_set_named_property(env, exports, SYSTEM_SND_MNGR_NAPI_CLASS_NAME.c_str(), ctorObj);
339 if (status != napi_ok) {
340 return nullptr;
341 }
342
343 status = DefineStaticProperties(env, exports);
344 if (status != napi_ok) {
345 return nullptr;
346 }
347
348 return exports;
349 }
350
GetAbilityContext(napi_env env,napi_value contextArg)351 shared_ptr<AbilityRuntime::Context> SystemSoundManagerNapi::GetAbilityContext(napi_env env, napi_value contextArg)
352 {
353 MEDIA_LOGI("SystemSoundManagerNapi::GetAbilityContext");
354 bool stageMode = false;
355
356 napi_status status = AbilityRuntime::IsStageContext(env, contextArg, stageMode);
357 if (status == napi_ok && stageMode) {
358 MEDIA_LOGI("GetAbilityContext: Getting context with stage model");
359 auto context = AbilityRuntime::GetStageModeContext(env, contextArg);
360 CHECK_AND_RETURN_RET_LOG(context != nullptr, nullptr, "Failed to obtain ability in STAGE mode");
361 return context;
362 } else {
363 MEDIA_LOGI("GetAbilityContext: Getting context with FA model");
364 auto ability = AbilityRuntime::GetCurrentAbility(env);
365 CHECK_AND_RETURN_RET_LOG(ability != nullptr, nullptr, "Failed to obtain ability in FA mode");
366 auto faContext = ability->GetAbilityContext();
367 CHECK_AND_RETURN_RET_LOG(faContext != nullptr, nullptr, "GetAbilityContext returned null in FA model");
368 return faContext;
369 }
370 }
371
Construct(napi_env env,napi_callback_info info)372 napi_value SystemSoundManagerNapi::Construct(napi_env env, napi_callback_info info)
373 {
374 napi_status status;
375 napi_value result = nullptr;
376 napi_value thisVar = nullptr;
377
378 napi_get_undefined(env, &result);
379 status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
380 if (status == napi_ok && thisVar != nullptr) {
381 std::unique_ptr<SystemSoundManagerNapi> obj = std::make_unique<SystemSoundManagerNapi>();
382 if (obj != nullptr) {
383 obj->env_ = env;
384 obj->sysSoundMgrClient_ = SystemSoundManagerFactory::CreateSystemSoundManager();
385 if (obj->sysSoundMgrClient_ == nullptr) {
386 MEDIA_LOGE("Failed to create sysSoundMgrClient_ instance.");
387 return result;
388 }
389
390 status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
391 SystemSoundManagerNapi::Destructor, nullptr, nullptr);
392 if (status == napi_ok) {
393 obj.release();
394 return thisVar;
395 } else {
396 MEDIA_LOGE("Failed to wrap the native sysSoundMgr object with JS.");
397 }
398 }
399 }
400
401 return result;
402 }
403
Destructor(napi_env env,void * nativeObject,void * finalize_hint)404 void SystemSoundManagerNapi::Destructor(napi_env env, void* nativeObject, void* finalize_hint)
405 {
406 SystemSoundManagerNapi *systemSoundManagerhelper = reinterpret_cast<SystemSoundManagerNapi*>(nativeObject);
407 if (systemSoundManagerhelper != nullptr) {
408 systemSoundManagerhelper->~SystemSoundManagerNapi();
409 }
410 }
411
VerifySelfSystemPermission()412 bool SystemSoundManagerNapi::VerifySelfSystemPermission()
413 {
414 Security::AccessToken::FullTokenID selfTokenID = IPCSkeleton::GetSelfTokenID();
415 auto tokenTypeFlag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(static_cast<uint32_t>(selfTokenID));
416 if (tokenTypeFlag == Security::AccessToken::TOKEN_NATIVE ||
417 tokenTypeFlag == Security::AccessToken::TOKEN_SHELL ||
418 Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfTokenID)) {
419 return true;
420 }
421 return false;
422 }
423
VerifyRingtonePermission()424 bool SystemSoundManagerNapi::VerifyRingtonePermission()
425 {
426 Security::AccessToken::FullTokenID selfTokenID = IPCSkeleton::GetSelfTokenID();
427 if (Security::AccessToken::AccessTokenKit::VerifyAccessToken(selfTokenID, "ohos.permission.WRITE_RINGTONE")) {
428 return false;
429 }
430 return true;
431 }
432
SetSystemSoundUriAsyncCallbackComp(napi_env env,napi_status status,void * data)433 void SystemSoundManagerNapi::SetSystemSoundUriAsyncCallbackComp(napi_env env, napi_status status, void* data)
434 {
435 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
436 napi_value setUriCallback = nullptr;
437 napi_value retVal = nullptr;
438 napi_value result[2] = {};
439
440 napi_get_undefined(env, &result[PARAM1]);
441 if (!context->status) {
442 napi_get_undefined(env, &result[PARAM0]);
443 } else {
444 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
445 napi_get_undefined(env, &result[PARAM1]);
446 }
447
448 if (context->deferred) {
449 if (!context->status) {
450 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
451 } else {
452 napi_reject_deferred(env, context->deferred, result[PARAM0]);
453 }
454 } else {
455 napi_get_reference_value(env, context->callbackRef, &setUriCallback);
456 napi_call_function(env, nullptr, setUriCallback, ARGS_TWO, result, &retVal);
457 napi_delete_reference(env, context->callbackRef);
458 }
459 napi_delete_async_work(env, context->work);
460
461 delete context;
462 context = nullptr;
463 }
464
GetSystemSoundUriAsyncCallbackComp(napi_env env,napi_status status,void * data)465 void SystemSoundManagerNapi::GetSystemSoundUriAsyncCallbackComp(napi_env env, napi_status status, void* data)
466 {
467 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
468 napi_value getUriCallback = nullptr;
469 napi_value retVal = nullptr;
470 napi_value result[2] = {};
471
472 if (!context->status) {
473 napi_get_undefined(env, &result[PARAM0]);
474 napi_create_string_utf8(env, context->uri.c_str(), NAPI_AUTO_LENGTH, &result[PARAM1]);
475 } else {
476 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
477 napi_get_undefined(env, &result[PARAM1]);
478 }
479
480 if (context->deferred) {
481 if (!context->status) {
482 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
483 } else {
484 napi_reject_deferred(env, context->deferred, result[PARAM0]);
485 }
486 } else {
487 napi_get_reference_value(env, context->callbackRef, &getUriCallback);
488 napi_call_function(env, nullptr, getUriCallback, ARGS_TWO, result, &retVal);
489 napi_delete_reference(env, context->callbackRef);
490 }
491 napi_delete_async_work(env, context->work);
492
493 delete context;
494 context = nullptr;
495 }
496
GetSystemSoundManager(napi_env env,napi_callback_info info)497 napi_value SystemSoundManagerNapi::GetSystemSoundManager(napi_env env, napi_callback_info info)
498 {
499 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
500 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
501 "No system permission");
502 napi_status status;
503 napi_value result = nullptr;
504 napi_value ctor;
505
506 status = napi_get_reference_value(env, sConstructor_, &ctor);
507 if (status == napi_ok) {
508 status = napi_new_instance(env, ctor, 0, nullptr, &result);
509 if (status == napi_ok) {
510 return result;
511 } else {
512 MEDIA_LOGE("GetSystemSoundManager: new instance can not be obtained.");
513 }
514 }
515
516 napi_get_undefined(env, &result);
517 return result;
518 }
519
SetRingtoneUri(napi_env env,napi_callback_info info)520 napi_value SystemSoundManagerNapi::SetRingtoneUri(napi_env env, napi_callback_info info)
521 {
522 napi_value result = nullptr;
523 napi_value resource = nullptr;
524 size_t argc = ARGS_FOUR;
525 napi_value argv[ARGS_FOUR] = {0};
526 napi_value thisVar = nullptr;
527 const int32_t refCount = 1;
528
529 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
530 napi_get_undefined(env, &result);
531 CHECK_AND_RETURN_RET_LOG(status == napi_ok && thisVar != nullptr, result, "SetRingtoneUri: napi_get_cb_info fail");
532
533 NAPI_ASSERT(env, (argc == ARGS_THREE || argc == ARGS_FOUR), "SetRingtoneUri: requires 4 parameters maximum");
534 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
535 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
536 CHECK_AND_RETURN_RET_LOG(status == napi_ok && asyncContext->objectInfo != nullptr, result,
537 "SetRingtoneUri: Failed to unwrap object");
538
539 for (size_t i = PARAM0; i < argc; i++) {
540 napi_valuetype valueType = napi_undefined;
541 napi_typeof(env, argv[i], &valueType);
542 if (i == PARAM0) {
543 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
544 } else if (i == PARAM1 && valueType == napi_string) {
545 asyncContext->uri = ExtractStringToEnv(env, argv[i]);
546 } else if (i == PARAM2 && valueType == napi_number) {
547 napi_get_value_int32(env, argv[i], &asyncContext->ringtoneType);
548 } else if (i == PARAM3 && valueType == napi_function) {
549 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
550 break;
551 } else {
552 CHECK_AND_CONTINUE(!(i == PARAM1) && (valueType == napi_null));
553 NAPI_ASSERT(env, false, "SetRingtoneUri: type mismatch");
554 }
555 }
556
557 if (asyncContext->callbackRef == nullptr) {
558 napi_create_promise(env, &asyncContext->deferred, &result);
559 }
560
561 napi_create_string_utf8(env, "SetRingtoneUri", NAPI_AUTO_LENGTH, &resource);
562 status = napi_create_async_work(env, nullptr, resource, AsyncSetRingtoneUri,
563 SetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
564 if (status != napi_ok) {
565 MEDIA_LOGE("Failed to get create async work");
566 napi_get_undefined(env, &result);
567 } else {
568 napi_queue_async_work(env, asyncContext->work);
569 asyncContext.release();
570 }
571
572 return result;
573 }
574
AsyncSetRingtoneUri(napi_env env,void * data)575 void SystemSoundManagerNapi::AsyncSetRingtoneUri(napi_env env, void *data)
576 {
577 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
578 if (context->uri.empty()) {
579 context->status = ERROR;
580 } else {
581 context->status = context->objectInfo->sysSoundMgrClient_->SetRingtoneUri(
582 context->abilityContext_, context->uri, static_cast<RingtoneType>(context->ringtoneType));
583 }
584 if (context->status) {
585 context->errCode = NAPI_ERR_IO_ERROR;
586 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
587 }
588 }
589
GetRingtoneUri(napi_env env,napi_callback_info info)590 napi_value SystemSoundManagerNapi::GetRingtoneUri(napi_env env, napi_callback_info info)
591 {
592 napi_status status;
593 napi_value result = nullptr;
594 napi_value resource = nullptr;
595 size_t argc = ARGS_THREE;
596 napi_value argv[ARGS_THREE] = {0};
597 napi_value thisVar = nullptr;
598 const int32_t refCount = 1;
599
600 status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
601 napi_get_undefined(env, &result);
602 CHECK_AND_RETURN_RET_LOG(status == napi_ok && thisVar != nullptr, result,
603 "GetRingtoneUri: Failed to retrieve details about the callback");
604
605 NAPI_ASSERT(env, (argc == ARGS_TWO || argc == ARGS_THREE), "GetRingtoneUri: requires 3 parameters maximum");
606 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
607 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
608 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
609 for (size_t i = PARAM0; i < argc; i++) {
610 napi_valuetype valueType = napi_undefined;
611 napi_typeof(env, argv[i], &valueType);
612 if (i == PARAM0) {
613 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
614 } else if (i == PARAM1 && valueType == napi_number) {
615 napi_get_value_int32(env, argv[i], &asyncContext->ringtoneType);
616 } else if (i == PARAM2 && valueType == napi_function) {
617 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
618 break;
619 } else {
620 NAPI_ASSERT(env, false, "GetRingtoneUri: type mismatch");
621 }
622 }
623
624 if (asyncContext->callbackRef == nullptr) {
625 napi_create_promise(env, &asyncContext->deferred, &result);
626 }
627
628 napi_create_string_utf8(env, "GetRingtoneUri", NAPI_AUTO_LENGTH, &resource);
629 status = napi_create_async_work(env, nullptr, resource, AsyncGetRingtoneUri,
630 GetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
631 if (status != napi_ok) {
632 MEDIA_LOGE("GetRingtoneUri: Failed to get create async work");
633 napi_get_undefined(env, &result);
634 } else {
635 napi_queue_async_work(env, asyncContext->work);
636 asyncContext.release();
637 }
638 }
639
640 return result;
641 }
642
AsyncGetRingtoneUri(napi_env env,void * data)643 void SystemSoundManagerNapi::AsyncGetRingtoneUri(napi_env env, void *data)
644 {
645 SystemSoundManagerAsyncContext *context
646 = static_cast<SystemSoundManagerAsyncContext *>(data);
647 context->uri = context->objectInfo->sysSoundMgrClient_->GetRingtoneUri(
648 context->abilityContext_, static_cast<RingtoneType>(context->ringtoneType));
649 context->status = SUCCESS;
650 }
651
GetRingtonePlayerAsyncCallbackComp(napi_env env,napi_status status,void * data)652 void SystemSoundManagerNapi::GetRingtonePlayerAsyncCallbackComp(napi_env env, napi_status status, void* data)
653 {
654 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
655 napi_value getPlayerCallback = nullptr;
656 napi_value retVal = nullptr;
657 napi_value result[2] = {};
658 napi_value rngplrResult = nullptr;
659
660 if (context->ringtonePlayer != nullptr) {
661 rngplrResult = RingtonePlayerNapi::GetRingtonePlayerInstance(env, context->ringtonePlayer);
662 if (rngplrResult == nullptr) {
663 napi_value message = nullptr;
664 napi_create_string_utf8(env, "GetRingtonePlayer Error: Operation is not supported or failed",
665 NAPI_AUTO_LENGTH, &message);
666 napi_create_error(env, nullptr, message, &result[PARAM0]);
667 napi_get_undefined(env, &result[PARAM1]);
668 } else {
669 napi_get_undefined(env, &result[0]);
670 result[PARAM1] = rngplrResult;
671 }
672 } else {
673 MEDIA_LOGE("Failed to get system tone player!");
674 napi_value message = nullptr;
675 napi_create_string_utf8(env, "GetRingtonePlayer Error: Operation is not supported or failed",
676 NAPI_AUTO_LENGTH, &message);
677 napi_create_error(env, nullptr, message, &result[PARAM0]);
678 napi_get_undefined(env, &result[PARAM1]);
679 }
680
681 if (context->deferred) {
682 if (!context->status) {
683 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
684 } else {
685 napi_reject_deferred(env, context->deferred, result[PARAM0]);
686 }
687 } else {
688 napi_get_reference_value(env, context->callbackRef, &getPlayerCallback);
689 napi_call_function(env, nullptr, getPlayerCallback, ARGS_TWO, result, &retVal);
690 napi_delete_reference(env, context->callbackRef);
691 }
692 napi_delete_async_work(env, context->work);
693
694 delete context;
695 context = nullptr;
696 }
697
GetRingtonePlayer(napi_env env,napi_callback_info info)698 napi_value SystemSoundManagerNapi::GetRingtonePlayer(napi_env env, napi_callback_info info)
699 {
700 napi_value result = nullptr;
701 napi_value resource = nullptr;
702 size_t argc = ARGS_THREE;
703 napi_value argv[ARGS_THREE] = {0};
704 napi_value thisVar = nullptr;
705 const int32_t refCount = 1;
706
707 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
708 napi_get_undefined(env, &result);
709 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
710 "GetRingtonePlayer: Failed to retrieve details about the callback");
711
712 NAPI_ASSERT(env, (argc == ARGS_TWO || argc == ARGS_THREE), "GetRingtonePlayer: require 3 parameters max");
713 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
714 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
715 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
716 for (size_t i = PARAM0; i < argc; i++) {
717 napi_valuetype valueType = napi_undefined;
718 napi_typeof(env, argv[i], &valueType);
719 if (i == PARAM0) {
720 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
721 } else if (i == PARAM1 && valueType == napi_number) {
722 napi_get_value_int32(env, argv[i], &asyncContext->ringtoneType);
723 } else if (i == PARAM2 && valueType == napi_function) {
724 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
725 break;
726 } else {
727 NAPI_ASSERT(env, false, "GetRingtonePlayer: type mismatch");
728 }
729 }
730
731 if (asyncContext->callbackRef == nullptr) {
732 napi_create_promise(env, &asyncContext->deferred, &result);
733 }
734
735 napi_create_string_utf8(env, "GetRingtonePlayer", NAPI_AUTO_LENGTH, &resource);
736 status = napi_create_async_work(env, nullptr, resource, AsyncGetRingtonePlayer,
737 GetRingtonePlayerAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
738 if (status != napi_ok) {
739 MEDIA_LOGE("GetRingtonePlayer: Failed to get create async work");
740 napi_get_undefined(env, &result);
741 } else {
742 napi_queue_async_work(env, asyncContext->work);
743 asyncContext.release();
744 }
745 }
746
747 return result;
748 }
749
AsyncGetRingtonePlayer(napi_env env,void * data)750 void SystemSoundManagerNapi::AsyncGetRingtonePlayer(napi_env env, void *data)
751 {
752 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
753 std::shared_ptr<RingtonePlayer> ringtonePlayer =context->objectInfo->sysSoundMgrClient_->
754 GetRingtonePlayer(context->abilityContext_, static_cast<RingtoneType>(context->ringtoneType));
755 if (ringtonePlayer != nullptr) {
756 context->ringtonePlayer = ringtonePlayer;
757 context->status = SUCCESS;
758 } else {
759 context->status = ERROR;
760 }
761 }
762
SetSystemToneUri(napi_env env,napi_callback_info info)763 napi_value SystemSoundManagerNapi::SetSystemToneUri(napi_env env, napi_callback_info info)
764 {
765 napi_value result = nullptr;
766 napi_value resource = nullptr;
767 size_t argc = ARGS_FOUR;
768 napi_value argv[ARGS_FOUR] = {0};
769 napi_value thisVar = nullptr;
770 const int32_t refCount = 1;
771
772 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
773 napi_get_undefined(env, &result);
774 CHECK_AND_RETURN_RET_LOG(status == napi_ok && thisVar != nullptr, result, "SetSystemToneUri: get_cb_info failed");
775
776 NAPI_ASSERT(env, (argc == ARGS_THREE || argc == ARGS_FOUR), "SetSystemToneUri: requires 4 parameters maximum");
777 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
778 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
779 CHECK_AND_RETURN_RET_LOG(status == napi_ok && asyncContext->objectInfo != nullptr, result,
780 "SetSystemToneUri: Failed to unwrap object");
781
782 for (size_t i = PARAM0; i < argc; i++) {
783 napi_valuetype valueType = napi_undefined;
784 napi_typeof(env, argv[i], &valueType);
785 if (i == PARAM0) {
786 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
787 } else if (i == PARAM1 && valueType == napi_string) {
788 asyncContext->uri = ExtractStringToEnv(env, argv[i]);
789 } else if (i == PARAM2 && valueType == napi_number) {
790 napi_get_value_int32(env, argv[i], &asyncContext->systemToneType);
791 } else if (i == PARAM3 && valueType == napi_function) {
792 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
793 break;
794 } else {
795 CHECK_AND_CONTINUE(!(i == PARAM1) && (valueType == napi_null));
796 NAPI_ASSERT(env, false, "SetSystemToneUri: type mismatch");
797 }
798 }
799
800 if (asyncContext->callbackRef == nullptr) {
801 napi_create_promise(env, &asyncContext->deferred, &result);
802 }
803
804 napi_create_string_utf8(env, "SetSystemToneUri", NAPI_AUTO_LENGTH, &resource);
805 status = napi_create_async_work(env, nullptr, resource, AsyncSetSystemToneUri,
806 SetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
807 if (status != napi_ok) {
808 MEDIA_LOGE("Failed to get create async work");
809 napi_get_undefined(env, &result);
810 } else {
811 napi_queue_async_work(env, asyncContext->work);
812 asyncContext.release();
813 }
814
815 return result;
816 }
817
AsyncSetSystemToneUri(napi_env env,void * data)818 void SystemSoundManagerNapi::AsyncSetSystemToneUri(napi_env env, void *data)
819 {
820 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
821 if (context->uri.empty()) {
822 context->status = ERROR;
823 } else {
824 context->status = context->objectInfo->sysSoundMgrClient_->SetSystemToneUri(
825 context->abilityContext_, context->uri, static_cast<SystemToneType>(context->systemToneType));
826 }
827 if (context->status) {
828 context->errCode = NAPI_ERR_IO_ERROR;
829 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
830 }
831 }
832
GetSystemToneUri(napi_env env,napi_callback_info info)833 napi_value SystemSoundManagerNapi::GetSystemToneUri(napi_env env, napi_callback_info info)
834 {
835 napi_status status;
836 napi_value result = nullptr;
837 napi_value resource = nullptr;
838 size_t argc = ARGS_THREE;
839 napi_value argv[ARGS_THREE] = {0};
840 napi_value thisVar = nullptr;
841 const int32_t refCount = 1;
842
843 status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
844 napi_get_undefined(env, &result);
845 CHECK_AND_RETURN_RET_LOG(status == napi_ok && thisVar != nullptr, result,
846 "GetSystemToneUri: Failed to retrieve details about the callback");
847
848 NAPI_ASSERT(env, (argc == ARGS_TWO || argc == ARGS_THREE), "GetSystemToneUri: requires 3 parameters maximum");
849 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
850 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
851 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
852 for (size_t i = PARAM0; i < argc; i++) {
853 napi_valuetype valueType = napi_undefined;
854 napi_typeof(env, argv[i], &valueType);
855 if (i == PARAM0) {
856 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
857 } else if (i == PARAM1 && valueType == napi_number) {
858 napi_get_value_int32(env, argv[i], &asyncContext->systemToneType);
859 } else if (i == PARAM2 && valueType == napi_function) {
860 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
861 break;
862 } else {
863 NAPI_ASSERT(env, false, "GetSystemToneUri: type mismatch");
864 }
865 }
866
867 if (asyncContext->callbackRef == nullptr) {
868 napi_create_promise(env, &asyncContext->deferred, &result);
869 }
870
871 napi_create_string_utf8(env, "GetSystemToneUri", NAPI_AUTO_LENGTH, &resource);
872 status = napi_create_async_work(env, nullptr, resource, AsyncGetSystemToneUri,
873 GetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
874 if (status != napi_ok) {
875 MEDIA_LOGE("GetSystemToneUri: Failed to get create async work");
876 napi_get_undefined(env, &result);
877 } else {
878 napi_queue_async_work(env, asyncContext->work);
879 asyncContext.release();
880 }
881 }
882
883 return result;
884 }
885
AsyncGetSystemToneUri(napi_env env,void * data)886 void SystemSoundManagerNapi::AsyncGetSystemToneUri(napi_env env, void *data)
887 {
888 SystemSoundManagerAsyncContext *context
889 = static_cast<SystemSoundManagerAsyncContext *>(data);
890 context->uri = context->objectInfo->sysSoundMgrClient_->GetSystemToneUri(
891 context->abilityContext_, static_cast<SystemToneType>(context->systemToneType));
892 context->status = SUCCESS;
893 }
894
GetSystemTonePlayer(napi_env env,napi_callback_info info)895 napi_value SystemSoundManagerNapi::GetSystemTonePlayer(napi_env env, napi_callback_info info)
896 {
897 napi_value result = nullptr;
898 napi_value resource = nullptr;
899 size_t argc = ARGS_THREE;
900 napi_value argv[ARGS_THREE] = {0};
901 napi_value thisVar = nullptr;
902 const int32_t refCount = 1;
903
904 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
905 napi_get_undefined(env, &result);
906 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
907 "GetSystemTonePlayer: Failed to retrieve details about the callback");
908
909 NAPI_ASSERT(env, (argc == ARGS_TWO || argc == ARGS_THREE), "GetSystemTonePlayer: require 3 parameters max");
910 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
911 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
912 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
913 for (size_t i = PARAM0; i < argc; i++) {
914 napi_valuetype valueType = napi_undefined;
915 napi_typeof(env, argv[i], &valueType);
916 if (i == PARAM0) {
917 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
918 } else if (i == PARAM1 && valueType == napi_number) {
919 napi_get_value_int32(env, argv[i], &asyncContext->systemToneType);
920 } else if (i == PARAM2 && valueType == napi_function) {
921 napi_create_reference(env, argv[i], refCount, &asyncContext->callbackRef);
922 break;
923 } else {
924 NAPI_ASSERT(env, false, "GetSystemTonePlayer: type mismatch");
925 }
926 }
927
928 if (asyncContext->callbackRef == nullptr) {
929 napi_create_promise(env, &asyncContext->deferred, &result);
930 }
931
932 napi_create_string_utf8(env, "GetSystemTonePlayer", NAPI_AUTO_LENGTH, &resource);
933 status = napi_create_async_work(env, nullptr, resource, AsyncGetSystemTonePlayer,
934 GetSystemTonePlayerAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
935 if (status != napi_ok) {
936 MEDIA_LOGE("GetRingtonePlayer: Failed to get create async work");
937 napi_get_undefined(env, &result);
938 } else {
939 napi_queue_async_work(env, asyncContext->work);
940 asyncContext.release();
941 }
942 }
943
944 return result;
945 }
946
AsyncGetSystemTonePlayer(napi_env env,void * data)947 void SystemSoundManagerNapi::AsyncGetSystemTonePlayer(napi_env env, void *data)
948 {
949 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
950 std::shared_ptr<SystemTonePlayer> systemTonePlayer =context->objectInfo->sysSoundMgrClient_->
951 GetSystemTonePlayer(context->abilityContext_, static_cast<SystemToneType>(context->systemToneType));
952 if (systemTonePlayer != nullptr) {
953 context->systemTonePlayer = systemTonePlayer;
954 context->status = SUCCESS;
955 } else {
956 context->status = ERROR;
957 }
958 }
959
GetSystemTonePlayerAsyncCallbackComp(napi_env env,napi_status status,void * data)960 void SystemSoundManagerNapi::GetSystemTonePlayerAsyncCallbackComp(napi_env env, napi_status status, void* data)
961 {
962 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
963 napi_value getPlayerCallback = nullptr;
964 napi_value retVal = nullptr;
965 napi_value result[2] = {};
966 napi_value rngplrResult = nullptr;
967
968 if (context->systemTonePlayer != nullptr) {
969 rngplrResult = SystemTonePlayerNapi::GetSystemTonePlayerInstance(env, context->systemTonePlayer);
970 if (rngplrResult == nullptr) {
971 napi_value message = nullptr;
972 napi_create_string_utf8(env, "GetSystemTonePlayer Error: Operation is not supported or failed",
973 NAPI_AUTO_LENGTH, &message);
974 napi_create_error(env, nullptr, message, &result[PARAM0]);
975 napi_get_undefined(env, &result[PARAM1]);
976 } else {
977 napi_get_undefined(env, &result[0]);
978 result[PARAM1] = rngplrResult;
979 }
980 } else {
981 MEDIA_LOGE("Failed to get system tone player!");
982 napi_value message = nullptr;
983 napi_create_string_utf8(env, "GetSystemTonePlayer Error: Operation is not supported or failed",
984 NAPI_AUTO_LENGTH, &message);
985 napi_create_error(env, nullptr, message, &result[PARAM0]);
986 napi_get_undefined(env, &result[PARAM1]);
987 }
988
989 if (context->deferred) {
990 if (!context->status) {
991 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
992 } else {
993 napi_reject_deferred(env, context->deferred, result[PARAM0]);
994 }
995 } else {
996 napi_get_reference_value(env, context->callbackRef, &getPlayerCallback);
997 napi_call_function(env, nullptr, getPlayerCallback, ARGS_TWO, result, &retVal);
998 napi_delete_reference(env, context->callbackRef);
999 }
1000 napi_delete_async_work(env, context->work);
1001
1002 delete context;
1003 context = nullptr;
1004 }
1005
CreateCustomizedToneAttrs(napi_env env,napi_callback_info info)1006 napi_value SystemSoundManagerNapi::CreateCustomizedToneAttrs(napi_env env, napi_callback_info info)
1007 {
1008 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1009 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1010 "No system permission");
1011 napi_status status;
1012 napi_value result = nullptr;
1013 std::shared_ptr<ToneAttrs> nativeToneAttrs = make_shared<ToneAttrs>("default",
1014 "default", "default", CUSTOMISED, TONE_CATEGORY_INVALID);
1015 status = ToneAttrsNapi::NewInstance(env, nativeToneAttrs, result);
1016 if (status == napi_ok) {
1017 return result;
1018 } else {
1019 MEDIA_LOGE("CreateCustomizedToneAttrs: new instance can not be obtained.");
1020 }
1021 napi_get_undefined(env, &result);
1022 return result;
1023 }
1024
GetDefaultRingtoneAttrs(napi_env env,napi_callback_info info)1025 napi_value SystemSoundManagerNapi::GetDefaultRingtoneAttrs(napi_env env, napi_callback_info info)
1026 {
1027 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1028 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1029 "No system permission");
1030 napi_value result = nullptr;
1031 napi_value resource = nullptr;
1032 napi_value thisVar = nullptr;
1033 size_t argc = ARGS_TWO;
1034 napi_value argv[ARGS_TWO] = {0};
1035 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1036 napi_get_undefined(env, &result);
1037 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1038 "GetDefaultRingtoneAttrs: Failed to retrieve details about the callback");
1039 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1040 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1041 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1042 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1043 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1044 for (size_t i = PARAM0; i < argc; i++) {
1045 napi_valuetype valueType = napi_undefined;
1046 napi_typeof(env, argv[i], &valueType);
1047 if (i == PARAM0) {
1048 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1049 } else if (i == PARAM1 && valueType == napi_number) {
1050 napi_get_value_int32(env, argv[i], &asyncContext->ringtoneType);
1051 }
1052 }
1053 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr && (asyncContext->ringtoneType == CARD_0 ||
1054 asyncContext->ringtoneType == CARD_1),
1055 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "Parameter error");
1056 napi_create_promise(env, &asyncContext->deferred, &result);
1057 napi_create_string_utf8(env, "GetDefaultRingtoneAttrs", NAPI_AUTO_LENGTH, &resource);
1058 status = napi_create_async_work(env, nullptr, resource, AsyncGetDefaultRingtoneAttrs,
1059 GetDefaultAttrsAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1060 if (status != napi_ok) {
1061 napi_get_undefined(env, &result);
1062 } else {
1063 napi_queue_async_work(env, asyncContext->work);
1064 asyncContext.release();
1065 }
1066 }
1067 return result;
1068 }
1069
AsyncGetDefaultRingtoneAttrs(napi_env env,void * data)1070 void SystemSoundManagerNapi::AsyncGetDefaultRingtoneAttrs(napi_env env, void *data)
1071 {
1072 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1073 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1074 context->toneAttrs = context->objectInfo->sysSoundMgrClient_->GetDefaultRingtoneAttrs(
1075 context->abilityContext_, static_cast<RingtoneType>(context->ringtoneType));
1076 }
1077 if (context->toneAttrs == nullptr) {
1078 context->status = ERROR;
1079 context->errCode = NAPI_ERR_IO_ERROR;
1080 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1081 }
1082 }
1083
GetDefaultAttrsAsyncCallbackComp(napi_env env,napi_status status,void * data)1084 void SystemSoundManagerNapi::GetDefaultAttrsAsyncCallbackComp(napi_env env, napi_status status, void* data)
1085 {
1086 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1087 napi_value result[2] = {};
1088
1089 if (context->status == napi_ok) {
1090 napi_get_undefined(env, &result[PARAM0]);
1091 context->status = ToneAttrsNapi::NewInstance(env, context->toneAttrs, result[PARAM1]);
1092 } else {
1093 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1094 napi_get_undefined(env, &result[PARAM1]);
1095 }
1096
1097 if (context->deferred) {
1098 if (!context->status) {
1099 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1100 } else {
1101 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1102 }
1103 }
1104
1105 napi_delete_async_work(env, context->work);
1106 delete context;
1107 context = nullptr;
1108 }
1109
GetRingtoneAttrList(napi_env env,napi_callback_info info)1110 napi_value SystemSoundManagerNapi::GetRingtoneAttrList(napi_env env, napi_callback_info info)
1111 {
1112 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1113 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1114 "No system permission");
1115 napi_value result = nullptr;
1116 napi_value resource = nullptr;
1117 napi_value thisVar = nullptr;
1118 size_t argc = ARGS_TWO;
1119 napi_value argv[ARGS_TWO] = {0};
1120
1121 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1122 napi_get_undefined(env, &result);
1123 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1124 "GetRingtoneAttrList: Failed to retrieve details about the callback");
1125 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1126 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1127 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1128 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1129 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1130 for (size_t i = PARAM0; i < argc; i++) {
1131 napi_valuetype valueType = napi_undefined;
1132 napi_typeof(env, argv[i], &valueType);
1133 if (i == PARAM0) {
1134 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1135 } else if (i == PARAM1 && valueType == napi_number) {
1136 napi_get_value_int32(env, argv[i], &asyncContext->ringtoneType);
1137 }
1138 }
1139 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr && (asyncContext->ringtoneType == CARD_0 ||
1140 asyncContext->ringtoneType == CARD_1),
1141 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "Parameter error");
1142 napi_create_promise(env, &asyncContext->deferred, &result);
1143 napi_create_string_utf8(env, "GetRingtoneAttrList", NAPI_AUTO_LENGTH, &resource);
1144 status = napi_create_async_work(env, nullptr, resource, AsyncGetRingtoneAttrList,
1145 GetToneAttrsListAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1146 if (status != napi_ok) {
1147 napi_get_undefined(env, &result);
1148 } else {
1149 napi_queue_async_work(env, asyncContext->work);
1150 asyncContext.release();
1151 }
1152 }
1153 return result;
1154 }
1155
AsyncGetRingtoneAttrList(napi_env env,void * data)1156 void SystemSoundManagerNapi::AsyncGetRingtoneAttrList(napi_env env, void *data)
1157 {
1158 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1159 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1160 context->toneAttrsArray = context->objectInfo->sysSoundMgrClient_->GetRingtoneAttrList(
1161 context->abilityContext_, static_cast<RingtoneType>(context->ringtoneType));
1162 }
1163 if (context->toneAttrsArray.empty()) {
1164 context->status = ERROR;
1165 context->errCode = NAPI_ERR_IO_ERROR;
1166 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1167 }
1168 }
1169
GetToneAttrsListAsyncCallbackComp(napi_env env,napi_status status,void * data)1170 void SystemSoundManagerNapi::GetToneAttrsListAsyncCallbackComp(napi_env env, napi_status status, void* data)
1171 {
1172 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1173 napi_value result[2] = {};
1174 if (context->status == napi_ok) {
1175 napi_get_undefined(env, &result[PARAM0]);
1176 context->status = napi_create_array_with_length(env, (context->toneAttrsArray).size(), &result[PARAM1]);
1177 size_t count = 0;
1178 for (auto &toneAttrs : context->toneAttrsArray) {
1179 napi_value jsToneAttrs = nullptr;
1180 ToneAttrsNapi::NewInstance(env, toneAttrs, jsToneAttrs);
1181 context->status = napi_set_element(env, result[PARAM1], count, jsToneAttrs);
1182 count ++;
1183 }
1184 } else {
1185 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1186 napi_get_undefined(env, &result[PARAM1]);
1187 }
1188 if (context->deferred) {
1189 if (!context->status) {
1190 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1191 } else {
1192 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1193 }
1194 }
1195
1196 napi_delete_async_work(env, context->work);
1197 delete context;
1198 context = nullptr;
1199 }
1200
GetDefaultSystemToneAttrs(napi_env env,napi_callback_info info)1201 napi_value SystemSoundManagerNapi::GetDefaultSystemToneAttrs(napi_env env, napi_callback_info info)
1202 {
1203 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1204 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1205 "No system permission");
1206 napi_value result = nullptr;
1207 napi_value resource = nullptr;
1208 napi_value thisVar = nullptr;
1209 size_t argc = ARGS_TWO;
1210 napi_value argv[ARGS_TWO] = {0};
1211
1212 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1213 napi_get_undefined(env, &result);
1214 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1215 "GetDefaultSystemToneAttrs: Failed to retrieve details about the callback");
1216 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1217 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1218 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1219 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1220 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1221 for (size_t i = PARAM0; i < argc; i++) {
1222 napi_valuetype valueType = napi_undefined;
1223 napi_typeof(env, argv[i], &valueType);
1224 if (i == PARAM0) {
1225 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1226 } else if (i == PARAM1 && valueType == napi_number) {
1227 napi_get_value_int32(env, argv[i], &asyncContext->systemToneType);
1228 }
1229 }
1230 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr && (asyncContext->systemToneType == CARD_0 ||
1231 asyncContext->systemToneType == CARD_1 || asyncContext->systemToneType == SYSTEM_NOTIFICATION),
1232 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "Parameter error");
1233 napi_create_promise(env, &asyncContext->deferred, &result);
1234 napi_create_string_utf8(env, "GetDefaultSystemToneAttrs", NAPI_AUTO_LENGTH, &resource);
1235 status = napi_create_async_work(env, nullptr, resource, AsyncGetDefaultSystemToneAttrs,
1236 GetDefaultAttrsAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1237 if (status != napi_ok) {
1238 napi_get_undefined(env, &result);
1239 } else {
1240 napi_queue_async_work(env, asyncContext->work);
1241 asyncContext.release();
1242 }
1243 }
1244 return result;
1245 }
1246
AsyncGetDefaultSystemToneAttrs(napi_env env,void * data)1247 void SystemSoundManagerNapi::AsyncGetDefaultSystemToneAttrs(napi_env env, void *data)
1248 {
1249 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1250 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1251 context->toneAttrs= context->objectInfo->sysSoundMgrClient_->GetDefaultSystemToneAttrs(
1252 context->abilityContext_, static_cast<SystemToneType>(context->systemToneType));
1253 }
1254 if (context->toneAttrs == nullptr) {
1255 context->status = ERROR;
1256 context->errCode = NAPI_ERR_IO_ERROR;
1257 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1258 }
1259 }
1260
GetSystemToneAttrList(napi_env env,napi_callback_info info)1261 napi_value SystemSoundManagerNapi::GetSystemToneAttrList(napi_env env, napi_callback_info info)
1262 {
1263 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1264 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1265 "No system permission");
1266 napi_value result = nullptr;
1267 napi_value resource = nullptr;
1268 napi_value thisVar = nullptr;
1269 size_t argc = ARGS_TWO;
1270 napi_value argv[ARGS_TWO] = {0};
1271
1272 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1273 napi_get_undefined(env, &result);
1274 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1275 "GetSystemToneAttrList: Failed to retrieve details about the callback");
1276 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1277 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1278 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1279 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1280 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1281 for (size_t i = PARAM0; i < argc; i ++) {
1282 napi_valuetype valueType = napi_undefined;
1283 napi_typeof(env, argv[i], &valueType);
1284 if (i == PARAM0) {
1285 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1286 } else if (i == PARAM1 && valueType == napi_number) {
1287 napi_get_value_int32(env, argv[i], &asyncContext->systemToneType);
1288 }
1289 }
1290 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr && (asyncContext->systemToneType == CARD_0 ||
1291 asyncContext->systemToneType == CARD_1 || asyncContext->systemToneType == SYSTEM_NOTIFICATION),
1292 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "Parameter error");
1293 napi_create_promise(env, &asyncContext->deferred, &result);
1294 napi_create_string_utf8(env, "GetSystemToneAttrList", NAPI_AUTO_LENGTH, &resource);
1295 status = napi_create_async_work(env, nullptr, resource, AsyncGetSystemToneAttrList,
1296 GetToneAttrsListAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1297 if (status != napi_ok) {
1298 napi_get_undefined(env, &result);
1299 } else {
1300 napi_queue_async_work(env, asyncContext->work);
1301 asyncContext.release();
1302 }
1303 }
1304 return result;
1305 }
1306
AsyncGetSystemToneAttrList(napi_env env,void * data)1307 void SystemSoundManagerNapi::AsyncGetSystemToneAttrList(napi_env env, void *data)
1308 {
1309 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1310 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1311 context->toneAttrsArray = context->objectInfo->sysSoundMgrClient_->GetSystemToneAttrList(
1312 context->abilityContext_, static_cast<SystemToneType>(context->systemToneType));
1313 }
1314 if (context->toneAttrsArray.empty()) {
1315 context->status = ERROR;
1316 context->errCode = NAPI_ERR_IO_ERROR;
1317 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1318 }
1319 }
1320
SetAlarmToneUri(napi_env env,napi_callback_info info)1321 napi_value SystemSoundManagerNapi::SetAlarmToneUri(napi_env env, napi_callback_info info)
1322 {
1323 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1324 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1325 "No system permission");
1326 napi_value result = nullptr;
1327 napi_value resource = nullptr;
1328 napi_value thisVar = nullptr;
1329 size_t argc = ARGS_TWO;
1330 napi_value argv[ARGS_TWO] = {0};
1331 char buffer[SIZE];
1332 size_t res = 0;
1333
1334 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1335 napi_get_undefined(env, &result);
1336 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1337 "SetAlarmToneUri: Failed to retrieve details about the callback");
1338 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1339 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1340 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1341 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1342 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1343 for (size_t i = PARAM0; i < argc; i++) {
1344 napi_valuetype valueType = napi_undefined;
1345 napi_typeof(env, argv[i], &valueType);
1346 if (i == PARAM0) {
1347 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1348 } else if (i == PARAM1 && valueType == napi_string) {
1349 napi_get_value_string_utf8(env, argv[i], buffer, SIZE, &res);
1350 asyncContext->uri = std::string(buffer);
1351 }
1352 }
1353 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1354 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1355 CHECK_AND_RETURN_RET_LOG(!asyncContext->uri.empty(), ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1356 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1357 napi_create_promise(env, &asyncContext->deferred, &result);
1358 napi_create_string_utf8(env, "SetAlarmToneUri", NAPI_AUTO_LENGTH, &resource);
1359 status = napi_create_async_work(env, nullptr, resource, AsyncSetAlarmToneUri,
1360 SetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1361 if (status != napi_ok) {
1362 napi_get_undefined(env, &result);
1363 } else {
1364 napi_queue_async_work(env, asyncContext->work);
1365 asyncContext.release();
1366 }
1367 }
1368 return result;
1369 }
1370
AsyncSetAlarmToneUri(napi_env env,void * data)1371 void SystemSoundManagerNapi::AsyncSetAlarmToneUri(napi_env env, void *data)
1372 {
1373 int32_t result = ERROR;
1374 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1375 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1376 result = context->objectInfo->sysSoundMgrClient_->SetAlarmToneUri(
1377 context->abilityContext_, context->uri);
1378 }
1379 if (result == TYPEERROR) {
1380 context->status = ERROR;
1381 context->errCode = NAPI_ERR_URI_ERROR;
1382 context->errMessage = NAPI_ERR_URI_ERROR_INFO;
1383 } else if (result == ERROR) {
1384 context->status = ERROR;
1385 context->errCode = NAPI_ERR_IO_ERROR;
1386 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1387 }
1388 }
1389
GetAlarmToneUri(napi_env env,napi_callback_info info)1390 napi_value SystemSoundManagerNapi::GetAlarmToneUri(napi_env env, napi_callback_info info)
1391 {
1392 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1393 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1394 "No system permission");
1395 napi_value result = nullptr;
1396 napi_value resource = nullptr;
1397 napi_value thisVar = nullptr;
1398 size_t argc = ARGS_ONE;
1399 napi_value argv[ARGS_ONE] = {0};
1400 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1401 napi_get_undefined(env, &result);
1402 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1403 "GetAlarmToneUri: Failed to retrieve details about the callback");
1404 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1405 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1406 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1407 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1408 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1409 asyncContext->abilityContext_ = GetAbilityContext(env, argv[0]);
1410 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1411 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1412 napi_create_promise(env, &asyncContext->deferred, &result);
1413 napi_create_string_utf8(env, "GetAlarmToneUri", NAPI_AUTO_LENGTH, &resource);
1414 status = napi_create_async_work(env, nullptr, resource, AsyncGetAlarmToneUri,
1415 GetSystemSoundUriAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1416 if (status != napi_ok) {
1417 napi_get_undefined(env, &result);
1418 } else {
1419 napi_queue_async_work(env, asyncContext->work);
1420 asyncContext.release();
1421 }
1422 }
1423 return result;
1424 }
1425
AsyncGetAlarmToneUri(napi_env env,void * data)1426 void SystemSoundManagerNapi::AsyncGetAlarmToneUri(napi_env env, void *data)
1427 {
1428 SystemSoundManagerAsyncContext *context
1429 = static_cast<SystemSoundManagerAsyncContext *>(data);
1430 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1431 context->uri = context->objectInfo->sysSoundMgrClient_->GetAlarmToneUri(
1432 context->abilityContext_);
1433 }
1434 if (context->uri.empty()) {
1435 context->status = ERROR;
1436 context->errCode = NAPI_ERR_IO_ERROR;
1437 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1438 } else {
1439 context->status = SUCCESS;
1440 }
1441 }
1442
GetDefaultAlarmToneAttrs(napi_env env,napi_callback_info info)1443 napi_value SystemSoundManagerNapi::GetDefaultAlarmToneAttrs(napi_env env, napi_callback_info info)
1444 {
1445 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1446 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1447 "No system permission");
1448 napi_value result = nullptr;
1449 napi_value resource = nullptr;
1450 napi_value thisVar = nullptr;
1451 size_t argc = ARGS_ONE;
1452 napi_value argv[ARGS_ONE] = {0};
1453
1454 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1455 napi_get_undefined(env, &result);
1456 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1457 "GetDefaultAlarmToneAttrs: Failed to retrieve details about the callback");
1458 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1459 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1460 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1461 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1462 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1463 asyncContext->abilityContext_ = GetAbilityContext(env, argv[PARAM0]);
1464 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1465 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1466 napi_create_promise(env, &asyncContext->deferred, &result);
1467 napi_create_string_utf8(env, "GetDefaultAlarmToneAttrs", NAPI_AUTO_LENGTH, &resource);
1468 status = napi_create_async_work(env, nullptr, resource, AsyncGetDefaultAlarmToneAttrs,
1469 GetDefaultAttrsAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1470 if (status != napi_ok) {
1471 napi_get_undefined(env, &result);
1472 } else {
1473 napi_queue_async_work(env, asyncContext->work);
1474 asyncContext.release();
1475 }
1476 }
1477 return result;
1478 }
1479
AsyncGetDefaultAlarmToneAttrs(napi_env env,void * data)1480 void SystemSoundManagerNapi::AsyncGetDefaultAlarmToneAttrs(napi_env env, void *data)
1481 {
1482 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1483 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1484 context->toneAttrs= context->objectInfo->sysSoundMgrClient_->GetDefaultAlarmToneAttrs(
1485 context->abilityContext_);
1486 }
1487 if (context->toneAttrs == nullptr) {
1488 context->status = ERROR;
1489 context->errCode = NAPI_ERR_IO_ERROR;
1490 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1491 }
1492 }
1493
GetAlarmToneAttrList(napi_env env,napi_callback_info info)1494 napi_value SystemSoundManagerNapi::GetAlarmToneAttrList(napi_env env, napi_callback_info info)
1495 {
1496 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1497 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1498 "No system permission");
1499 napi_value result = nullptr;
1500 napi_value resource = nullptr;
1501 napi_value thisVar = nullptr;
1502 size_t argc = ARGS_ONE;
1503 napi_value argv[ARGS_ONE] = {};
1504 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1505 napi_get_undefined(env, &result);
1506 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1507 "GetAlarmToneAttrList: Failed to retrieve details about the callback");
1508 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1509 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1510 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1511 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1512 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1513 asyncContext->abilityContext_ = GetAbilityContext(env, argv[0]);
1514 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1515 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1516 napi_create_promise(env, &asyncContext->deferred, &result);
1517 napi_create_string_utf8(env, "GetAlarmToneAttrList", NAPI_AUTO_LENGTH, &resource);
1518 status = napi_create_async_work(env, nullptr, resource, AsyncGetAlarmToneAttrList,
1519 GetToneAttrsListAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1520 if (status != napi_ok) {
1521 napi_get_undefined(env, &result);
1522 } else {
1523 napi_queue_async_work(env, asyncContext->work);
1524 asyncContext.release();
1525 }
1526 }
1527 return result;
1528 }
1529
AsyncGetAlarmToneAttrList(napi_env env,void * data)1530 void SystemSoundManagerNapi::AsyncGetAlarmToneAttrList(napi_env env, void *data)
1531 {
1532 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1533 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1534 context->toneAttrsArray = context->objectInfo->sysSoundMgrClient_->GetAlarmToneAttrList(
1535 context->abilityContext_);
1536 }
1537 if (context->toneAttrsArray.empty()) {
1538 context->status = ERROR;
1539 context->errCode = NAPI_ERR_IO_ERROR;
1540 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1541 }
1542 }
1543
OpenAlarmTone(napi_env env,napi_callback_info info)1544 napi_value SystemSoundManagerNapi::OpenAlarmTone(napi_env env, napi_callback_info info)
1545 {
1546 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1547 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1548 "No system permission");
1549 napi_value result = nullptr;
1550 napi_value resource = nullptr;
1551 napi_value thisVar = nullptr;
1552 size_t argc = ARGS_TWO;
1553 napi_value argv[ARGS_TWO] = {};
1554 char buffer[SIZE];
1555 size_t res = 0;
1556
1557 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1558 napi_get_undefined(env, &result);
1559 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1560 "OpenAlarmTone: Failed to retrieve details about the callback");
1561 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1562 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1563 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1564 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1565 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1566 for (size_t i = PARAM0; i < argc; i++) {
1567 napi_valuetype valueType = napi_undefined;
1568 napi_typeof(env, argv[i], &valueType);
1569 if (i == PARAM0) {
1570 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1571 } else if (i == PARAM1 && valueType == napi_string) {
1572 napi_get_value_string_utf8(env, argv[i], buffer, SIZE, &res);
1573 asyncContext->uri = std::string(buffer);
1574 }
1575 }
1576 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1577 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1578 CHECK_AND_RETURN_RET_LOG(!asyncContext->uri.empty(), ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1579 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1580 napi_create_promise(env, &asyncContext->deferred, &result);
1581 napi_create_string_utf8(env, "OpenAlarmTone", NAPI_AUTO_LENGTH, &resource);
1582 status = napi_create_async_work(env, nullptr, resource, AsyncOpenAlarmTone,
1583 OpenAlarmToneAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1584 if (status != napi_ok) {
1585 napi_get_undefined(env, &result);
1586 } else {
1587 napi_queue_async_work(env, asyncContext->work);
1588 asyncContext.release();
1589 }
1590 }
1591 return result;
1592 }
1593
AsyncOpenAlarmTone(napi_env env,void * data)1594 void SystemSoundManagerNapi::AsyncOpenAlarmTone(napi_env env, void *data)
1595 {
1596 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1597 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1598 context->fd = context->objectInfo->sysSoundMgrClient_->OpenAlarmTone(
1599 context->abilityContext_, context->uri);
1600 }
1601 if (context->fd == TYPEERROR) {
1602 context->status = ERROR;
1603 context->errCode = NAPI_ERR_URI_ERROR;
1604 context->errMessage = NAPI_ERR_URI_ERROR_INFO;
1605 } else if (context->fd == ERROR) {
1606 context->status = ERROR;
1607 context->errCode = NAPI_ERR_IO_ERROR;
1608 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1609 }
1610 }
1611
OpenAlarmToneAsyncCallbackComp(napi_env env,napi_status status,void * data)1612 void SystemSoundManagerNapi::OpenAlarmToneAsyncCallbackComp(napi_env env, napi_status status, void* data)
1613 {
1614 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1615 napi_value result[2] = {};
1616 if (!context->status) {
1617 napi_get_undefined(env, &result[PARAM0]);
1618 napi_create_int32(env, context->fd, &result[PARAM1]);
1619 } else {
1620 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1621 napi_get_undefined(env, &result[PARAM1]);
1622 }
1623 if (context->deferred) {
1624 if (!context->status) {
1625 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1626 } else {
1627 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1628 }
1629 }
1630 napi_delete_async_work(env, context->work);
1631 delete context;
1632 context = nullptr;
1633 }
1634
Close(napi_env env,napi_callback_info info)1635 napi_value SystemSoundManagerNapi::Close(napi_env env, napi_callback_info info)
1636 {
1637 CHECK_AND_RETURN_RET_LOG(VerifySelfSystemPermission(),
1638 ThrowErrorAndReturn(env, NAPI_ERR_PERMISSION_DENIED_INFO, NAPI_ERR_PERMISSION_DENIED),
1639 "No system permission");
1640 napi_value result = nullptr;
1641 napi_value resource = nullptr;
1642 napi_value thisVar = nullptr;
1643 size_t argc = ARGS_ONE;
1644 napi_value argv[ARGS_ONE] = {0};
1645
1646 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1647 napi_get_undefined(env, &result);
1648 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1649 "Close: Failed to retrieve details about the callback");
1650 CHECK_AND_RETURN_RET_LOG(argc == ARGS_ONE, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1651 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1652 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1653 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1654 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1655 napi_valuetype valueType = napi_undefined;
1656 napi_typeof(env, argv[0], &valueType);
1657 if (valueType == napi_number) {
1658 napi_get_value_int32(env, argv[0], &asyncContext->fd);
1659 }
1660 CHECK_AND_RETURN_RET_LOG(asyncContext->fd > 0, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1661 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1662 napi_create_promise(env, &asyncContext->deferred, &result);
1663 napi_create_string_utf8(env, "Close", NAPI_AUTO_LENGTH, &resource);
1664 status = napi_create_async_work(env, nullptr, resource, AsyncClose,
1665 CloseAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1666 if (status != napi_ok) {
1667 napi_get_undefined(env, &result);
1668 } else {
1669 napi_queue_async_work(env, asyncContext->work);
1670 asyncContext.release();
1671 }
1672 }
1673 return result;
1674 }
1675
AsyncClose(napi_env env,void * data)1676 void SystemSoundManagerNapi::AsyncClose(napi_env env, void *data)
1677 {
1678 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1679 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1680 context->status = context->objectInfo->sysSoundMgrClient_->Close(context->fd);
1681 }
1682 if (context->status) {
1683 context->errCode = NAPI_ERR_IO_ERROR;
1684 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1685 }
1686 }
1687
CloseAsyncCallbackComp(napi_env env,napi_status status,void * data)1688 void SystemSoundManagerNapi::CloseAsyncCallbackComp(napi_env env, napi_status status, void* data)
1689 {
1690 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1691 napi_value result[2] = {};
1692 napi_get_undefined(env, &result[PARAM1]);
1693 if (!context->status) {
1694 napi_get_undefined(env, &result[PARAM0]);
1695 } else {
1696 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1697 napi_get_undefined(env, &result[PARAM1]);
1698 }
1699 if (context->deferred) {
1700 if (!context->status) {
1701 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1702 } else {
1703 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1704 }
1705 }
1706 napi_delete_async_work(env, context->work);
1707 delete context;
1708 context = nullptr;
1709 }
1710
AddCustomizedTone(napi_env env,napi_callback_info info)1711 napi_value SystemSoundManagerNapi::AddCustomizedTone(napi_env env, napi_callback_info info)
1712 {
1713 CHECK_AND_RETURN_RET_LOG(VerifyRingtonePermission(), ThrowErrorAndReturn(env,
1714 NAPI_ERR_NO_PERMISSION_INFO, NAPI_ERR_NO_PERMISSION), "Permission denied");
1715 napi_value result = nullptr;
1716 napi_value thisVar = nullptr;
1717 size_t argc = ARGS_FIVE;
1718 napi_value argv[ARGS_FIVE] = {};
1719 char buffer[SIZE];
1720 size_t res = 0;
1721 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1722 napi_get_undefined(env, &result);
1723 CHECK_AND_RETURN_RET_LOG((argc == ARGS_THREE || argc == ARGS_FOUR || argc == ARGS_FIVE), ThrowErrorAndReturn(env,
1724 NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1725 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1726 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1727 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1728 for (size_t i = PARAM0; i < argc; i++) {
1729 napi_valuetype valueType = napi_undefined;
1730 napi_typeof(env, argv[i], &valueType);
1731 if (i == PARAM0) {
1732 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1733 } else if (i == PARAM1 && valueType == napi_object) {
1734 napi_unwrap(env, argv[i], reinterpret_cast<void**>(&asyncContext->toneAttrsNapi));
1735 } else if (i == PARAM2 && valueType == napi_string) {
1736 napi_get_value_string_utf8(env, argv[i], buffer, SIZE, &res);
1737 asyncContext->externalUri = std::string(buffer);
1738 } else if (i == PARAM2 && valueType == napi_number) {
1739 napi_get_value_int32(env, argv[i], &asyncContext->fd);
1740 } else if (i == PARAM3 && valueType == napi_number) {
1741 napi_get_value_int32(env, argv[i], &asyncContext->offset);
1742 asyncContext->length = INT_MAX;
1743 } else if (i == PARAM4 && valueType == napi_number) {
1744 napi_get_value_int32(env, argv[i], &asyncContext->length);
1745 }
1746 }
1747 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr && asyncContext->toneAttrsNapi != nullptr,
1748 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1749 napi_create_promise(env, &asyncContext->deferred, &result);
1750 napi_create_string_utf8(env, "AddCustomizedTone", NAPI_AUTO_LENGTH, &thisVar);
1751 status = napi_create_async_work(env, nullptr, thisVar, AsyncAddCustomizedTone,
1752 AddCustomizedToneAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1753 if (status != napi_ok) {
1754 napi_get_undefined(env, &result);
1755 } else {
1756 napi_queue_async_work(env, asyncContext->work);
1757 asyncContext.release();
1758 }
1759 }
1760 return result;
1761 }
1762
AsyncAddCustomizedTone(napi_env env,void * data)1763 void SystemSoundManagerNapi::AsyncAddCustomizedTone(napi_env env, void *data)
1764 {
1765 std::string result = "TYPEERROR";
1766 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1767 if (context->objectInfo->sysSoundMgrClient_ == nullptr) {
1768 context->status = ERROR;
1769 context->errCode = NAPI_ERR_IO_ERROR;
1770 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1771 } else if (!context->externalUri.empty()) {
1772 context->uri = context->objectInfo->sysSoundMgrClient_->AddCustomizedToneByExternalUri(
1773 context->abilityContext_, context->toneAttrsNapi->GetToneAttrs(), context->externalUri);
1774 context->status = SUCCESS;
1775 } else if (context->fd > 0) {
1776 if (context->offset >= 0 && context->length >= 0) {
1777 context->uri = context->objectInfo->sysSoundMgrClient_->AddCustomizedToneByFdAndOffset(
1778 context->abilityContext_, context->toneAttrsNapi->GetToneAttrs(),
1779 context->fd, context->offset, context->length);
1780 context->status = SUCCESS;
1781 } else if (context->offset == 0 && context->length == 0) {
1782 context->uri = context->objectInfo->sysSoundMgrClient_->AddCustomizedToneByFd(
1783 context->abilityContext_, context->toneAttrsNapi->GetToneAttrs(), context->fd);
1784 context->status = SUCCESS;
1785 } else {
1786 context->status = ERROR;
1787 }
1788 } else {
1789 context->status = ERROR;
1790 }
1791 if (context->uri == result) {
1792 context->status = ERROR;
1793 context->errCode = NAPI_ERR_OPERATE_NOT_ALLOWED;
1794 context->errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
1795 } else if (context->uri.empty()) {
1796 context->status = ERROR;
1797 context->errCode = NAPI_ERR_IO_ERROR;
1798 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1799 }
1800 }
1801
AddCustomizedToneAsyncCallbackComp(napi_env env,napi_status status,void * data)1802 void SystemSoundManagerNapi::AddCustomizedToneAsyncCallbackComp(napi_env env, napi_status status, void* data)
1803 {
1804 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1805 napi_value result[2] = {};
1806 if (!context->status) {
1807 napi_get_undefined(env, &result[PARAM0]);
1808 napi_create_string_utf8(env, context->uri.c_str(), NAPI_AUTO_LENGTH, &result[PARAM1]);
1809 } else {
1810 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1811 napi_get_undefined(env, &result[PARAM1]);
1812 }
1813
1814 if (context->deferred) {
1815 if (!context->status) {
1816 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1817 } else {
1818 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1819 }
1820 }
1821
1822 napi_delete_async_work(env, context->work);
1823 delete context;
1824 context = nullptr;
1825 }
1826
RemoveCustomizedTone(napi_env env,napi_callback_info info)1827 napi_value SystemSoundManagerNapi::RemoveCustomizedTone(napi_env env, napi_callback_info info)
1828 {
1829 CHECK_AND_RETURN_RET_LOG(VerifyRingtonePermission(), ThrowErrorAndReturn(env,
1830 NAPI_ERR_NO_PERMISSION_INFO, NAPI_ERR_NO_PERMISSION), "Permission denied");
1831 napi_value result = nullptr;
1832 napi_value resource = nullptr;
1833 napi_value thisVar = nullptr;
1834 size_t argc = ARGS_TWO;
1835 napi_value argv[ARGS_TWO] = {};
1836 char buffer[SIZE];
1837 size_t res = 0;
1838
1839 napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
1840 napi_get_undefined(env, &result);
1841 CHECK_AND_RETURN_RET_LOG((status == napi_ok && thisVar != nullptr), result,
1842 "RemoveCustomizedTone: Failed to retrieve details about the callback");
1843 CHECK_AND_RETURN_RET_LOG(argc == ARGS_TWO, ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1844 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1845 std::unique_ptr<SystemSoundManagerAsyncContext> asyncContext = std::make_unique<SystemSoundManagerAsyncContext>();
1846 status = napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext->objectInfo));
1847 if (status == napi_ok && asyncContext->objectInfo != nullptr) {
1848 for (size_t i = PARAM0; i < argc; i++) {
1849 napi_valuetype valueType = napi_undefined;
1850 napi_typeof(env, argv[i], &valueType);
1851 if (i == PARAM0) {
1852 asyncContext->abilityContext_ = GetAbilityContext(env, argv[i]);
1853 } else if (i == PARAM1 && valueType == napi_string) {
1854 napi_get_value_string_utf8(env, argv[i], buffer, SIZE, &res);
1855 asyncContext->uri = std::string(buffer);
1856 }
1857 }
1858 CHECK_AND_RETURN_RET_LOG(asyncContext->abilityContext_ != nullptr,
1859 ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO, NAPI_ERR_INPUT_INVALID), "invalid arguments");
1860 CHECK_AND_RETURN_RET_LOG(!asyncContext->uri.empty(), ThrowErrorAndReturn(env, NAPI_ERR_INPUT_INVALID_INFO,
1861 NAPI_ERR_INPUT_INVALID), "invalid arguments");
1862 napi_create_promise(env, &asyncContext->deferred, &result);
1863 napi_create_string_utf8(env, "RemoveCustomizedTone", NAPI_AUTO_LENGTH, &resource);
1864 status = napi_create_async_work(env, nullptr, resource, AsyncRemoveCustomizedTone,
1865 RemoveCustomizedToneAsyncCallbackComp, static_cast<void*>(asyncContext.get()), &asyncContext->work);
1866 if (status != napi_ok) {
1867 MEDIA_LOGE("RemoveCustomizedTone: Failed to get create async work");
1868 napi_get_undefined(env, &result);
1869 } else {
1870 napi_queue_async_work(env, asyncContext->work);
1871 asyncContext.release();
1872 }
1873 }
1874 return result;
1875 }
1876
AsyncRemoveCustomizedTone(napi_env env,void * data)1877 void SystemSoundManagerNapi::AsyncRemoveCustomizedTone(napi_env env, void *data)
1878 {
1879 int32_t result = ERROR;
1880 SystemSoundManagerAsyncContext *context = static_cast<SystemSoundManagerAsyncContext *>(data);
1881 if (context->objectInfo->sysSoundMgrClient_ != nullptr) {
1882 result = context->objectInfo->sysSoundMgrClient_->RemoveCustomizedTone(
1883 context->abilityContext_, context->uri);
1884 }
1885 if (result == TYPEERROR) {
1886 context->status = ERROR;
1887 context->errCode = NAPI_ERR_OPERATE_NOT_ALLOWED;
1888 context->errMessage = NAPI_ERR_OPERATE_NOT_ALLOWED_INFO;
1889 } else if (result == ERROR) {
1890 context->status = ERROR;
1891 context->errCode = NAPI_ERR_IO_ERROR;
1892 context->errMessage = NAPI_ERR_IO_ERROR_INFO;
1893 }
1894 }
1895
RemoveCustomizedToneAsyncCallbackComp(napi_env env,napi_status status,void * data)1896 void SystemSoundManagerNapi::RemoveCustomizedToneAsyncCallbackComp(napi_env env, napi_status status, void* data)
1897 {
1898 auto context = static_cast<SystemSoundManagerAsyncContext *>(data);
1899 napi_value result[2] = {};
1900 napi_get_undefined(env, &result[PARAM1]);
1901 if (!context->status) {
1902 napi_get_undefined(env, &result[PARAM0]);
1903 } else {
1904 result[PARAM0] = AsyncThrowErrorAndReturn(env, context->errMessage, context->errCode);
1905 napi_get_undefined(env, &result[PARAM1]);
1906 }
1907 if (context->deferred) {
1908 if (!context->status) {
1909 napi_resolve_deferred(env, context->deferred, result[PARAM1]);
1910 } else {
1911 napi_reject_deferred(env, context->deferred, result[PARAM0]);
1912 }
1913 }
1914 napi_delete_async_work(env, context->work);
1915 delete context;
1916 context = nullptr;
1917 }
1918
ThrowErrorAndReturn(napi_env env,const std::string & errMsg,int32_t errCode)1919 napi_value SystemSoundManagerNapi::ThrowErrorAndReturn(napi_env env, const std::string& errMsg, int32_t errCode)
1920 {
1921 napi_value message = nullptr;
1922 napi_value code = nullptr;
1923 napi_value errVal = nullptr;
1924 napi_value errNameVal = nullptr;
1925 napi_value result{};
1926 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
1927 napi_create_error(env, nullptr, message, &errVal);
1928 napi_create_int32(env, errCode, &code);
1929 napi_set_named_property(env, errVal, "code", code);
1930 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &errNameVal);
1931 napi_set_named_property(env, errVal, "BusinessError", errNameVal);
1932 napi_throw(env, errVal);
1933 napi_get_undefined(env, &result);
1934 return result;
1935 }
1936
AsyncThrowErrorAndReturn(napi_env env,const std::string & errMsg,int32_t errCode)1937 napi_value SystemSoundManagerNapi::AsyncThrowErrorAndReturn(napi_env env, const std::string& errMsg, int32_t errCode)
1938 {
1939 napi_value message = nullptr;
1940 napi_value code = nullptr;
1941 napi_value errVal = nullptr;
1942 napi_value errNameVal = nullptr;
1943 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
1944 napi_create_error(env, nullptr, message, &errVal);
1945 napi_create_int32(env, errCode, &code);
1946 napi_set_named_property(env, errVal, "code", code);
1947 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &errNameVal);
1948 napi_set_named_property(env, errVal, "BusinessError", errNameVal);
1949 return errVal;
1950 }
1951
Init(napi_env env,napi_value exports)1952 static napi_value Init(napi_env env, napi_value exports)
1953 {
1954 ToneAttrsNapi::Init(env, exports);
1955 ToneHapticsAttrsNapi::Init(env, exports);
1956 ToneHapticsSettingsNapi::Init(env, exports);
1957 SystemSoundManagerNapi::Init(env, exports);
1958 AudioRendererInfoNapi::Init(env, exports);
1959 RingtoneOptionsNapi::Init(env, exports);
1960 RingtonePlayerNapi::Init(env, exports);
1961 SystemToneOptionsNapi::Init(env, exports);
1962 SystemTonePlayerNapi::Init(env, exports);
1963
1964 return exports;
1965 }
1966
1967 /*
1968 * module define
1969 */
1970 static napi_module g_module = {
1971 .nm_version = 1,
1972 .nm_flags = 0,
1973 .nm_filename = nullptr,
1974 .nm_register_func = Init,
1975 .nm_modname = "multimedia.systemSoundManager",
1976 .nm_priv = (reinterpret_cast<void*>(0)),
1977 .reserved = {0}
1978 };
1979
1980 /*
1981 * module register
1982 */
RegisterModule(void)1983 extern "C" __attribute__((constructor)) void RegisterModule(void)
1984 {
1985 napi_module_register(&g_module);
1986 }
1987 } // namespace Media
1988 } // namespace OHOS
1989