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 #ifndef LOG_TAG
16 #define LOG_TAG "NapiAudioRingerModeCallback"
17 #endif
18 
19 #include "napi_audio_ringermode_callback.h"
20 #include "audio_errors.h"
21 #include "audio_manager_log.h"
22 #include "napi_param_utils.h"
23 #include "napi_audio_error.h"
24 #include "napi_audio_enum.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
NapiAudioRingerModeCallback(napi_env env)28 NapiAudioRingerModeCallback::NapiAudioRingerModeCallback(napi_env env)
29     : env_(env)
30 {
31     AUDIO_DEBUG_LOG("instance create");
32 }
33 
~NapiAudioRingerModeCallback()34 NapiAudioRingerModeCallback::~NapiAudioRingerModeCallback()
35 {
36     AUDIO_DEBUG_LOG("instance destroy");
37 }
38 
SaveCallbackReference(const std::string & callbackName,napi_value args)39 void NapiAudioRingerModeCallback::SaveCallbackReference(const std::string &callbackName, napi_value args)
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     napi_ref callback = nullptr;
43     const int32_t refCount = ARGS_ONE;
44     napi_status status = napi_create_reference(env_, args, refCount, &callback);
45     CHECK_AND_RETURN_LOG(status == napi_ok && callback != nullptr,
46         "NapiAudioRingerModeCallback: creating reference for callback fail");
47     std::shared_ptr<AutoRef> cb = std::make_shared<AutoRef>(env_, callback);
48     if (callbackName == RINGERMODE_CALLBACK_NAME) {
49         ringerModeCallback_ = cb;
50     } else {
51         AUDIO_ERR_LOG("NapiAudioRingerModeCallback: Unknown callback type: %{public}s", callbackName.c_str());
52     }
53 }
54 
RemoveCallbackReference(const napi_value args)55 void NapiAudioRingerModeCallback::RemoveCallbackReference(const napi_value args)
56 {
57     if (!IsSameCallback(args)) {
58         return;
59     }
60     std::lock_guard<std::mutex> lock(mutex_);
61     napi_delete_reference(env_, ringerModeCallback_->cb_);
62     ringerModeCallback_->cb_ = nullptr;
63     ringerModeCallback_ = nullptr;
64     AUDIO_INFO_LOG("Remove callback reference successful.");
65 }
66 
IsSameCallback(const napi_value args)67 bool NapiAudioRingerModeCallback::IsSameCallback(const napi_value args)
68 {
69     std::lock_guard<std::mutex> lock(mutex_);
70     if (args == nullptr) {
71         return true;
72     }
73     if (ringerModeCallback_ == nullptr) {
74         return false;
75     }
76     napi_value ringerModeCallback = nullptr;
77     napi_get_reference_value(env_, ringerModeCallback_->cb_, &ringerModeCallback);
78     bool isEquals = false;
79     CHECK_AND_RETURN_RET_LOG(napi_strict_equals(env_, args, ringerModeCallback, &isEquals) == napi_ok, false,
80         "get napi_strict_equals failed");
81     return isEquals;
82 }
83 
OnRingerModeUpdated(const AudioRingerMode & ringerMode)84 void NapiAudioRingerModeCallback::OnRingerModeUpdated(const AudioRingerMode &ringerMode)
85 {
86     std::lock_guard<std::mutex> lock(mutex_);
87     AUDIO_DEBUG_LOG("NapiAudioRingerModeCallback: ringer mode: %{public}d", ringerMode);
88     CHECK_AND_RETURN_LOG(ringerModeCallback_ != nullptr, "Cannot find the reference of ringer mode callback");
89 
90     std::unique_ptr<AudioRingerModeJsCallback> cb = std::make_unique<AudioRingerModeJsCallback>();
91     CHECK_AND_RETURN_LOG(cb != nullptr, "No memory");
92     cb->callback = ringerModeCallback_;
93     cb->callbackName = RINGERMODE_CALLBACK_NAME;
94     cb->ringerMode = ringerMode;
95     return OnJsCallbackRingerMode(cb);
96 }
97 
GetJsAudioRingMode(int32_t ringerMode)98 static NapiAudioEnum::AudioRingMode GetJsAudioRingMode(int32_t ringerMode)
99 {
100     NapiAudioEnum::AudioRingMode result;
101 
102     switch (ringerMode) {
103         case RINGER_MODE_SILENT:
104             result = NapiAudioEnum::RINGER_MODE_SILENT;
105             break;
106         case RINGER_MODE_VIBRATE:
107             result = NapiAudioEnum::RINGER_MODE_VIBRATE;
108             break;
109         case RINGER_MODE_NORMAL:
110             result = NapiAudioEnum::RINGER_MODE_NORMAL;
111             break;
112         default:
113             result = NapiAudioEnum::RINGER_MODE_NORMAL;
114             break;
115     }
116 
117     return result;
118 }
119 
WorkCallbackInterruptDone(uv_work_t * work,int status)120 void NapiAudioRingerModeCallback::WorkCallbackInterruptDone(uv_work_t *work, int status)
121 {
122     std::shared_ptr<AudioRingerModeJsCallback> context(
123         static_cast<AudioRingerModeJsCallback*>(work->data),
124         [work](AudioRingerModeJsCallback* ptr) {
125             delete ptr;
126             delete work;
127     });
128     CHECK_AND_RETURN_LOG(work != nullptr, "work is nullptr");
129     AudioRingerModeJsCallback *event = reinterpret_cast<AudioRingerModeJsCallback *>(work->data);
130     CHECK_AND_RETURN_LOG(event != nullptr, "event is nullptr");
131     std::string request = event->callbackName;
132     CHECK_AND_RETURN_LOG(event->callback != nullptr, "event is nullptr");
133     napi_env env = event->callback->env_;
134     napi_ref callback = event->callback->cb_;
135     napi_handle_scope scope = nullptr;
136     napi_open_handle_scope(env, &scope);
137     CHECK_AND_RETURN_LOG(scope != nullptr, "scope is nullptr");
138     AUDIO_DEBUG_LOG("JsCallBack %{public}s, uv_queue_work_with_qos start", request.c_str());
139     do {
140         CHECK_AND_BREAK_LOG(status != UV_ECANCELED, "%{public}s canceled", request.c_str());
141         napi_value jsCallback = nullptr;
142         napi_status nstatus = napi_get_reference_value(env, callback, &jsCallback);
143         CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
144             request.c_str());
145         napi_value args[ARGS_ONE] = { nullptr };
146         NapiParamUtils::SetValueInt32(env, GetJsAudioRingMode(event->ringerMode), args[PARAM0]);
147         CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[PARAM0] != nullptr,
148             "%{public}s fail to create ringer mode callback", request.c_str());
149 
150         const size_t argCount = ARGS_ONE;
151         napi_value result = nullptr;
152         nstatus = napi_call_function(env, nullptr, jsCallback, argCount, args, &result);
153         CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to call ringer mode callback",
154             request.c_str());
155     } while (0);
156     napi_close_handle_scope(env, scope);
157 }
158 
OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> & jsCb)159 void NapiAudioRingerModeCallback::OnJsCallbackRingerMode(std::unique_ptr<AudioRingerModeJsCallback> &jsCb)
160 {
161     uv_loop_s *loop = nullptr;
162     napi_get_uv_event_loop(env_, &loop);
163     CHECK_AND_RETURN_LOG(loop != nullptr, "loop is nullptr");
164 
165     uv_work_t *work = new(std::nothrow) uv_work_t;
166     CHECK_AND_RETURN_LOG(work != nullptr, "OnJsCallbackRingerMode: No memory");
167 
168     if (jsCb.get() == nullptr) {
169         AUDIO_ERR_LOG("NapiAudioRingerModeCallback: OnJsCallbackRingerMode: jsCb.get() is null");
170         delete work;
171         return;
172     }
173     work->data = reinterpret_cast<void *>(jsCb.get());
174     int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) {}, WorkCallbackInterruptDone,
175         uv_qos_default);
176     if (ret != 0) {
177         AUDIO_ERR_LOG("Failed to execute libuv work queue");
178         delete work;
179     } else {
180         jsCb.release();
181     }
182 }
183 } // namespace AudioStandard
184 } // namespace OHOS