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 #ifndef INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_DATA_HANDLER_NAPI_H 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_DATA_HANDLER_NAPI_H 18 19 #include "medialibrary_napi_utils.h" 20 21 namespace OHOS { 22 namespace Media { 23 enum class ReturnDataType { 24 TYPE_IMAGE_SOURCE = 0, 25 TYPE_ARRAY_BUFFER, 26 TYPE_MOVING_PHOTO, 27 TYPE_TARGET_PATH, 28 TYPE_PICTURE, 29 }; 30 31 enum class DeliveryMode { 32 FAST = 0, 33 HIGH_QUALITY, 34 BALANCED_MODE, 35 }; 36 37 enum class SourceMode { 38 ORIGINAL_MODE = 0, 39 EDITED_MODE, 40 }; 41 42 enum class NotifyMode : int32_t { 43 FAST_NOTIFY = 0, 44 WAIT_FOR_HIGH_QUALITY, 45 }; 46 47 enum class CompatibleMode { 48 ORIGINAL_FORMAT_MODE = 0, 49 COMPATIBLE_FORMAT_MODE = 1, 50 }; 51 52 constexpr const char* ON_DATA_PREPARED_FUNC = "onDataPrepared"; 53 constexpr const char* ON_PROGRESS_FUNC = "onProgress"; 54 55 class NapiMediaAssetDataHandler { 56 public: 57 NapiMediaAssetDataHandler(napi_env env, napi_ref dataHandler, ReturnDataType dataType, const std::string &uri, 58 const std::string &destUri, SourceMode sourceMode); 59 void DeleteNapiReference(napi_env env); 60 ReturnDataType GetReturnDataType(); 61 std::string GetRequestUri(); 62 std::string GetDestUri(); 63 SourceMode GetSourceMode(); 64 void SetNotifyMode(NotifyMode trigger); 65 NotifyMode GetNotifyMode(); 66 void JsOnDataPrepared(napi_env env, napi_value exports, napi_value extraInfo); 67 void JsOnDataPrepared(napi_env env, napi_value pictures, napi_value exports, napi_value extraInfo); 68 69 CompatibleMode GetCompatibleMode(); 70 void SetCompatibleMode(const CompatibleMode &compatibleMode); 71 std::string GetRequestId(); 72 void SetRequestId(std::string requestId); 73 napi_ref GetProgressHandlerRef(); 74 void SetProgressHandlerRef(napi_ref &progressHandlerRef); GetThreadsafeFunction()75 napi_threadsafe_function GetThreadsafeFunction() 76 { 77 return threadsafeFunction_; 78 } SetThreadsafeFunction(napi_threadsafe_function & threadsafeFunction)79 void SetThreadsafeFunction(napi_threadsafe_function &threadsafeFunction) 80 { 81 threadsafeFunction_ = threadsafeFunction; 82 } 83 84 private: 85 napi_env env_ = nullptr; 86 napi_ref dataHandlerRef_ = nullptr; 87 ReturnDataType dataType_; 88 std::string requestUri_; 89 std::string destUri_; 90 SourceMode sourceMode_; 91 NotifyMode notifyMode_ = NotifyMode::FAST_NOTIFY; 92 CompatibleMode compatibleMode_ {0}; 93 napi_ref progressHandlerRef_ = nullptr; 94 napi_threadsafe_function threadsafeFunction_ = nullptr; 95 std::string requestId_; 96 static std::mutex dataHandlerRefMutex_; 97 }; 98 } // Media 99 } // OHOS 100 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_ASSET_DATA_HANDLER_NAPI_H 101