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