1 2 /* 3 * Copyright (C) 2024 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_SENDABLE_NAPI_H_ 18 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_SENDABLE_NAPI_H_ 19 20 #include <mutex> 21 22 #include "fetch_result.h" 23 #include "album_asset.h" 24 #include "sendable_file_asset_napi.h" 25 #include "medialibrary_napi_utils.h" 26 #include "napi/native_api.h" 27 #include "napi/native_node_api.h" 28 29 namespace OHOS { 30 namespace Media { 31 #define EXPORT __attribute__ ((visibility ("default"))) 32 static const std::string FETCH_FILE_RESULT_SENDABLE_CLASS_NAME = "FetchFileResultSendable"; 33 static const std::string PAH_FETCH_FILE_RESULT_SENDABLE_CLASS_NAME = "PhotoAccessHelperFetchFileResultSendable"; 34 35 class SendableFetchResultProperty { 36 public: 37 std::shared_ptr<FetchResult<FileAsset>> fetchFileResult_ = nullptr; 38 std::shared_ptr<FetchResult<AlbumAsset>> fetchAlbumResult_ = nullptr; 39 std::shared_ptr<FetchResult<PhotoAlbum>> fetchPhotoAlbumResult_ = nullptr; 40 std::shared_ptr<FetchResult<SmartAlbumAsset>> fetchSmartAlbumResult_ = nullptr; 41 FetchResType fetchResType_; 42 }; 43 44 class SendableFetchFileResultNapi { 45 public: 46 EXPORT SendableFetchFileResultNapi(); 47 EXPORT ~SendableFetchFileResultNapi(); 48 49 EXPORT static napi_value PhotoAccessHelperInit(napi_env env, napi_value exports); 50 51 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<FileAsset>> fileResult); 52 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<AlbumAsset>> fileResult); 53 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<PhotoAlbum>> fileResult); 54 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<SmartAlbumAsset>> fileResult); 55 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResultObject(); 56 std::shared_ptr<FetchResult<AlbumAsset>> GetFetchAlbumResultObject(); 57 std::shared_ptr<FetchResult<PhotoAlbum>> GetFetchPhotoAlbumResultObject(); 58 std::shared_ptr<FetchResult<SmartAlbumAsset>> GetFetchSmartAlbumResultObject(); 59 60 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResult() const; 61 FetchResType GetFetchResType(); 62 bool CheckIfPropertyPtrNull(); 63 static void SolveConstructorRef(unique_ptr<FetchResult<FileAsset>> &fileResult, napi_ref &constructorRef); 64 static void SolveConstructorRef(unique_ptr<FetchResult<AlbumAsset>> &fileResult, napi_ref &constructorRef); 65 static void SolveConstructorRef(unique_ptr<FetchResult<SmartAlbumAsset>> &fileResult, napi_ref &constructorRef); 66 static void SolveConstructorRef(unique_ptr<FetchResult<PhotoAlbum>> &fileResult, napi_ref &constructorRef); 67 68 private: 69 EXPORT static void FetchFileResultNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint); 70 EXPORT static void GetFetchResult(unique_ptr<SendableFetchFileResultNapi> &obj); 71 EXPORT static napi_value FetchFileResultNapiConstructor(napi_env env, napi_callback_info info); 72 73 EXPORT static napi_value JSGetCount(napi_env env, napi_callback_info info); 74 EXPORT static napi_value JSIsAfterLast(napi_env env, napi_callback_info info); 75 EXPORT static napi_value JSGetFirstObject(napi_env env, napi_callback_info info); 76 EXPORT static napi_value JSGetNextObject(napi_env env, napi_callback_info info); 77 EXPORT static napi_value JSGetAllObject(napi_env env, napi_callback_info info); 78 EXPORT static napi_value JSGetLastObject(napi_env env, napi_callback_info info); 79 EXPORT static napi_value JSGetPositionObject(napi_env env, napi_callback_info info); 80 EXPORT static napi_value JSClose(napi_env env, napi_callback_info info); 81 82 napi_env env_; 83 std::shared_ptr<SendableFetchResultProperty> propertyPtr; 84 static thread_local napi_ref photoAccessHelperConstructor_; 85 static inline thread_local std::unique_ptr<FetchResult<FileAsset>> sFetchFileResult_ = nullptr; 86 static inline thread_local std::unique_ptr<FetchResult<AlbumAsset>> sFetchAlbumResult_ = nullptr; 87 static inline thread_local std::unique_ptr<FetchResult<PhotoAlbum>> sFetchPhotoAlbumResult_ = nullptr; 88 static inline thread_local std::unique_ptr<FetchResult<SmartAlbumAsset>> sFetchSmartAlbumResult_ = nullptr; 89 static inline thread_local FetchResType sFetchResType_ = FetchResType::TYPE_FILE; 90 }; 91 92 class FetchFileResultSendableAsyncContext : public NapiError { 93 public: 94 napi_async_work work; 95 napi_ref callbackRef; 96 napi_deferred deferred; 97 SendableFetchFileResultNapi* objectInfo; 98 std::shared_ptr<SendableFetchResultProperty> objectPtr; 99 bool status; 100 int32_t position; 101 std::unique_ptr<FileAsset> fileAsset; 102 std::unique_ptr<AlbumAsset> albumAsset; 103 std::unique_ptr<PhotoAlbum> photoAlbum; 104 std::unique_ptr<SmartAlbumAsset> smartAlbumAsset; 105 std::vector<std::unique_ptr<FileAsset>> fileAssetArray; 106 std::vector<std::unique_ptr<AlbumAsset>> fileAlbumArray; 107 std::vector<std::unique_ptr<PhotoAlbum>> filePhotoAlbumArray; 108 std::vector<std::unique_ptr<SmartAlbumAsset>> fileSmartAlbumArray; 109 void GetFirstAsset(); 110 void GetObjectAtPosition(); 111 void GetAllObjectFromFetchResult(); 112 void GetLastObject(); 113 void GetNextObject(); 114 }; 115 } // namespace Media 116 } // namespace OHOS 117 118 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_SENDABLE_NAPI_H_ 119