1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "audio_ringermode_callback_napi.h"
17
18 #include <uv.h>
19
20 #include "audio_errors.h"
21 #include "audio_log.h"
22
23 namespace {
24 const std::string RINGERMODE_CALLBACK_NAME = "ringerModeChange";
25 }
26
27 namespace OHOS {
28 namespace AudioStandard {
AudioRingerModeCallbackNapi(napi_env env)29 AudioRingerModeCallbackNapi::AudioRingerModeCallbackNapi(napi_env env)
30 : env_(env)
31 {
32 AUDIO_DEBUG_LOG("AudioRingerModeCallbackNapi: instance create");
33 }
34
~AudioRingerModeCallbackNapi()35 AudioRingerModeCallbackNapi::~AudioRingerModeCallbackNapi()
36 {
37 AUDIO_DEBUG_LOG("AudioRingerModeCallbackNapi: instance destroy");
38 }
39
SaveCallbackReference(const std::string & callbackName,napi_value args)40 void AudioRingerModeCallbackNapi::SaveCallbackReference(const std::string &callbackName, napi_value args)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 napi_ref callback = nullptr;
44 const int32_t refCount = 1;
45 napi_status status = napi_create_reference(env_, args, refCount, &callback);
46 CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
47 "AudioRingerModeCallbackNapi: creating reference for callback fail");
48
49 std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
50 if (callbackName == RINGERMODE_CALLBACK_NAME) {
51 ringerModeCallback_ = cb;
52 } else {
53 AUDIO_ERR_LOG("AudioRingerModeCallbackNapi: Unknown callback type: %{public}s", callbackName.c_str());
54 }
55 }
56
OnRingerModeUpdated(const AudioRingerMode & ringerMode)57 void AudioRingerModeCallbackNapi::OnRingerModeUpdated(const AudioRingerMode &ringerMode)
58 {
59 std::lock_guard<std::mutex> lock(mutex_);
60 AUDIO_DEBUG_LOG("AudioRingerModeCallbackNapi: OnRingerModeUpdated is called");
61 AUDIO_DEBUG_LOG("AudioRingerModeCallbackNapi: ringer mode: %{public}d", ringerMode);
62 CHECK_AND_RETURN_LOG(ringerModeCallback_ != nullptr, "Cannot find the reference of ringer mode callback");
63
64 std::unique_ptr<AudioRingerModeJsCallback> cb = std::make_unique<AudioRingerModeJsCallback>();
65 CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
66 cb->callback = ringerModeCallback_;
67 cb->callbackName = RINGERMODE_CALLBACK_NAME;
68 cb->ringerMode = ringerMode;
69 return OnJsCallbackRingerMode(cb);
70 }
71
GetJsAudioRingMode(int32_t ringerMode)72 static AudioManagerNapi::AudioRingMode GetJsAudioRingMode(int32_t ringerMode)
73 {
74 AudioManagerNapi::AudioRingMode result;
75
76 switch (ringerMode) {
77 case RINGER_MODE_SILENT:
78 result = AudioManagerNapi::RINGER_MODE_SILENT;
79 break;
80 case RINGER_MODE_VIBRATE:
81 result = AudioManagerNapi::RINGER_MODE_VIBRATE;
82 break;
83 case RINGER_MODE_NORMAL:
84 result = AudioManagerNapi::RINGER_MODE_NORMAL;
85 break;
86 default:
87 result = AudioManagerNapi::RINGER_MODE_NORMAL;
88 break;
89 }
90
91 return result;
92 }
93
OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> & jsCb)94 void AudioRingerModeCallbackNapi::OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> &jsCb)
95 {
96 uv_loop_s *loop = nullptr;
97 napi_get_uv_event_loop(env_, &loop);
98 if (loop == nullptr) {
99 return;
100 }
101
102 uv_work_t *work = new(std::nothrow) uv_work_t;
103 if (work == nullptr) {
104 AUDIO_ERR_LOG("AudioRingerModeCallbackNapi: OnJsCallbackRingerMode: No memory");
105 return;
106 }
107 if (jsCb.get() == nullptr) {
108 AUDIO_ERR_LOG("AudioRingerModeCallbackNapi: OnJsCallbackRingerMode: jsCb.get() is null");
109 delete work;
110 return;
111 }
112 work->data = reinterpret_cast<void *>(jsCb.get());
113
114 int ret = uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
115 // Js Thread
116 AudioRingerModeJsCallback *event = reinterpret_cast<AudioRingerModeJsCallback *>(work->data);
117 std::string request = event->callbackName;
118 napi_env env = event->callback->env_;
119 napi_ref callback = event->callback->cb_;
120 AUDIO_DEBUG_LOG("AudioRingerModeCallbackNapi: JsCallBack %{public}s, uv_queue_work start", request.c_str());
121 do {
122 CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
123
124 napi_value jsCallback = nullptr;
125 napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
126 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
127 request.c_str());
128
129 // Call back function
130 napi_value args[1] = { nullptr };
131 napi_create_int32(env, GetJsAudioRingMode(event->ringerMode), &args[0]);
132 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
133 "%{public}s fail to create ringer mode callback", request.c_str());
134
135 const size_t argCount = 1;
136 napi_value result = nullptr;
137 nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
138 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call ringer mode callback", request.c_str());
139 } while (0);
140 delete event;
141 delete work;
142 });
143 if (ret != 0) {
144 AUDIO_ERR_LOG("Failed to execute libuv work queue");
145 delete work;
146 } else {
147 jsCb.release();
148 }
149 }
150 } // namespace AudioStandard
151 } // namespace OHOS
152