1 /* 2 * Copyright (c) 2022-2024 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 OHOS_NAPI_AVCAST_PICKER_HELPER_H 17 #define OHOS_NAPI_AVCAST_PICKER_HELPER_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "avsession_log.h" 22 #include "napi_async_callback.h" 23 #include "ui_content.h" 24 #include "ui_extension_context.h" 25 26 namespace OHOS::AVSession { 27 28 struct PickerCallBack { 29 bool ready = false; 30 int32_t resultCode; 31 }; 32 33 class ModalUICallback { 34 public: 35 explicit ModalUICallback(Ace::UIContent* uiContent, PickerCallBack& pickerCallBack); 36 void SetSessionId(int32_t sessionId); 37 void OnRelease(int32_t releaseCode); 38 void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result); 39 void OnReceive(const OHOS::AAFwk::WantParams& request); 40 void OnError(int32_t code, const std::string& name, const std::string& message); 41 void OnRemoteReady(const std::shared_ptr<Ace::ModalUIExtensionProxy>& uiProxy); 42 void OnDestroy(); 43 44 private: 45 int32_t sessionId_ = 0; 46 Ace::UIContent* uiContent_; 47 PickerCallBack pickerCallBack_; 48 }; 49 50 struct NapiAVCastPickerOptions { 51 std::string sessionType = "audio"; 52 }; 53 54 class NapiAVCastPickerHelper { 55 public: 56 enum { 57 EVENT_PICPKER_STATE_CHANGE, 58 EVENT_TYPE_MAX 59 }; 60 61 static napi_value Init(napi_env env, napi_value exports); 62 63 using OnEventHandlerType = std::function<napi_status(napi_env, NapiAVCastPickerHelper*, napi_value)>; 64 using OffEventHandlerType = std::function<napi_status(napi_env, NapiAVCastPickerHelper*, napi_value)>; 65 66 napi_status AddCallback(napi_env env, int32_t event, napi_value callback); 67 napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback); 68 69 bool IsCallbacksEmpty(int32_t event); 70 71 private: 72 explicit NapiAVCastPickerHelper(Ace::UIContent* uiContent); 73 ~NapiAVCastPickerHelper(); 74 75 static napi_value ConstructorCallback(napi_env env, napi_callback_info info); 76 77 static napi_value OnEvent(napi_env env, napi_callback_info info); 78 static napi_value OffEvent(napi_env env, napi_callback_info info); 79 80 static napi_value SelectAVPicker(napi_env env, napi_callback_info info); 81 82 static napi_status OnPickerStateChange(napi_env env, NapiAVCastPickerHelper* napiAVPicker, napi_value callback); 83 static napi_status OffPickerStateChange(napi_env env, NapiAVCastPickerHelper* napiAVPicker, napi_value callback); 84 85 static std::map<std::string, std::pair<OnEventHandlerType, OffEventHandlerType>> eventHandlers_; 86 87 template<typename T> 88 void HandleEvent(int32_t event, const T& param); 89 90 std::mutex lock_; 91 std::shared_ptr<NapiAsyncCallback> asyncCallback_; 92 std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {}; 93 std::shared_ptr<bool> isValid_; 94 95 Ace::UIContent *uiContent_; 96 napi_ref wrapperRef_{}; 97 98 static constexpr size_t ARGC_ZERO = 0; 99 static constexpr size_t ARGC_ONE = 1; 100 static constexpr size_t ARGC_TWO = 2; 101 static constexpr size_t ARGC_THREE = 3; 102 103 static constexpr size_t ARGV_FIRST = 0; 104 static constexpr size_t ARGV_SECOND = 1; 105 static constexpr size_t ARGV_THIRD = 2; 106 107 static constexpr uint32_t STATE_APPEARING = 0; 108 static constexpr uint32_t STATE_DISAPPEARING = 1; 109 }; 110 } // namespace OHOS::AVSession 111 #endif // OHOS_NAPI_AVCAST_PICKER_HELPER_H 112