1 /* 2 * Copyright (C) 2025 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 #ifndef SYSTEM_TONE_PLAYER_CALLBACK_NAPI_H 17 #define SYSTEM_TONE_PLAYER_CALLBACK_NAPI_H 18 19 #include "system_tone_player.h" 20 #include "common_napi.h" 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 24 namespace OHOS { 25 namespace Media { 26 class SystemTonePlayerCallbackNapi : public SystemTonePlayerFinishedAndErrorCallback { 27 public: 28 explicit SystemTonePlayerCallbackNapi(napi_env env); 29 virtual ~SystemTonePlayerCallbackNapi(); 30 void SavePlayFinishedCallbackReference(const std::string &callbackName, napi_value args, int32_t streamId); 31 void SaveCallbackReference(const std::string &callbackName, napi_value args); 32 void RemovePlayFinishedCallbackReference(int32_t streamId); 33 void RemoveCallbackReference(const std::string &callbackName, const napi_value &args); 34 void OnEndOfStream(int32_t streamId) override; 35 void OnError(int32_t errorCode) override; 36 37 private: 38 struct SystemTonePlayerJsCallback { 39 std::shared_ptr<AutoRef> callback = nullptr; 40 std::string callbackName = "unknown"; 41 int32_t streamId = 0; 42 int32_t errorCode = 0; 43 std::string errorMsg = "error"; 44 }; 45 46 bool IsSameCallback(std::shared_ptr<AutoRef> cb, const napi_value args); 47 bool IsExistCallback(const std::list<std::shared_ptr<AutoRef>> &cbList, const napi_value args); 48 void ProcessFinishedCallbacks(int32_t listenerId, int32_t streamId); 49 void OnJsCallbackPlayFinished(std::shared_ptr<SystemTonePlayerJsCallback> &jsCb); 50 void OnJsCallbackError(std::shared_ptr<SystemTonePlayerJsCallback> &jsCb); 51 52 std::mutex mutex_; 53 napi_env env_ = nullptr; 54 std::map<int32_t, std::list<std::shared_ptr<AutoRef>>> playFinishedCallbackMap_; 55 std::list<std::shared_ptr<AutoRef>> errorCallback_; 56 }; 57 } // namespace Media 58 } // namespace OHOS 59 #endif // SYSTEM_TONE_PLAYER_CALLBACK_NAPI_H 60