1 2 /* 3 * Copyright (C) 2021-2022 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_NAPI_H_ 18 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_NAPI_H_ 19 20 #include <mutex> 21 22 #include "fetch_result.h" 23 #include "album_asset.h" 24 #include "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 static const std::string FETCH_FILE_RESULT_CLASS_NAME = "FetchFileResult"; 32 static const std::string UFM_FETCH_FILE_RESULT_CLASS_NAME = "UserFileMgrFetchFileResult"; 33 static const std::string PAH_FETCH_FILE_RESULT_CLASS_NAME = "PhotoAccessHelperFetchFileResult"; 34 35 class FetchResultProperty { 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 FetchFileResultNapi { 45 public: 46 FetchFileResultNapi(); 47 ~FetchFileResultNapi(); 48 49 static napi_value Init(napi_env env, napi_value exports); 50 static napi_value UserFileMgrInit(napi_env env, napi_value exports); 51 static napi_value PhotoAccessHelperInit(napi_env env, napi_value exports); 52 53 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<FileAsset>> fileResult); 54 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<AlbumAsset>> fileResult); 55 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<PhotoAlbum>> fileResult); 56 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<SmartAlbumAsset>> fileResult); 57 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResultObject(); 58 std::shared_ptr<FetchResult<AlbumAsset>> GetFetchAlbumResultObject(); 59 std::shared_ptr<FetchResult<PhotoAlbum>> GetFetchPhotoAlbumResultObject(); 60 std::shared_ptr<FetchResult<SmartAlbumAsset>> GetFetchSmartAlbumResultObject(); 61 62 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResult() const; 63 FetchResType GetFetchResType(); 64 bool CheckIfPropertyPtrNull(); 65 static void SolveConstructorRef(unique_ptr<FetchResult<FileAsset>> &fileResult, napi_ref &constructorRef); 66 static void SolveConstructorRef(unique_ptr<FetchResult<AlbumAsset>> &fileResult, napi_ref &constructorRef); 67 static void SolveConstructorRef(unique_ptr<FetchResult<SmartAlbumAsset>> &fileResult, napi_ref &constructorRef); 68 static void SolveConstructorRef(unique_ptr<FetchResult<PhotoAlbum>> &fileResult, napi_ref &constructorRef); 69 70 private: 71 static void FetchFileResultNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint); 72 static void GetFetchResult(unique_ptr<FetchFileResultNapi> &obj); 73 static napi_value FetchFileResultNapiConstructor(napi_env env, napi_callback_info info); 74 75 static napi_value JSGetCount(napi_env env, napi_callback_info info); 76 static napi_value JSIsAfterLast(napi_env env, napi_callback_info info); 77 static napi_value JSGetFirstObject(napi_env env, napi_callback_info info); 78 static napi_value JSGetNextObject(napi_env env, napi_callback_info info); 79 static napi_value JSGetLastObject(napi_env env, napi_callback_info info); 80 static napi_value JSGetPositionObject(napi_env env, napi_callback_info info); 81 static napi_value JSGetAllObject(napi_env env, napi_callback_info info); 82 static napi_value JSClose(napi_env env, napi_callback_info info); 83 84 napi_env env_; 85 std::shared_ptr<FetchResultProperty> propertyPtr; 86 static thread_local napi_ref sConstructor_; 87 static thread_local napi_ref userFileMgrConstructor_; 88 static thread_local napi_ref photoAccessHelperConstructor_; 89 static inline thread_local std::unique_ptr<FetchResult<FileAsset>> sFetchFileResult_ = nullptr; 90 static inline thread_local std::unique_ptr<FetchResult<AlbumAsset>> sFetchAlbumResult_ = nullptr; 91 static inline thread_local std::unique_ptr<FetchResult<PhotoAlbum>> sFetchPhotoAlbumResult_ = nullptr; 92 static inline thread_local std::unique_ptr<FetchResult<SmartAlbumAsset>> sFetchSmartAlbumResult_ = nullptr; 93 static inline thread_local FetchResType sFetchResType_ = FetchResType::TYPE_FILE; 94 }; 95 96 class FetchFileResultAsyncContext : public NapiError { 97 public: 98 napi_async_work work; 99 napi_ref callbackRef; 100 napi_deferred deferred; 101 FetchFileResultNapi* objectInfo; 102 std::shared_ptr<FetchResultProperty> objectPtr; 103 bool status; 104 int32_t position; 105 std::unique_ptr<FileAsset> fileAsset; 106 std::unique_ptr<AlbumAsset> albumAsset; 107 std::unique_ptr<PhotoAlbum> photoAlbum; 108 std::unique_ptr<SmartAlbumAsset> smartAlbumAsset; 109 std::vector<std::unique_ptr<FileAsset>> fileAssetArray; 110 std::vector<std::unique_ptr<AlbumAsset>> fileAlbumArray; 111 std::vector<std::unique_ptr<PhotoAlbum>> filePhotoAlbumArray; 112 std::vector<std::unique_ptr<SmartAlbumAsset>> fileSmartAlbumArray; 113 void GetFirstAsset(); 114 void GetObjectAtPosition(); 115 void GetAllObjectFromFetchResult(); 116 void GetLastObject(); 117 void GetNextObject(); 118 }; 119 } // namespace Media 120 } // namespace OHOS 121 122 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_NAPI_H_ 123