1 /* 2 * Copyright (C) 2021-2022 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_FETCH_FILE_RESULT_NAPI_H_ 17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_NAPI_H_ 18 19 #include <mutex> 20 21 #include "fetch_result.h" 22 #include "album_asset.h" 23 #include "file_asset_napi.h" 24 #include "medialibrary_napi_utils.h" 25 #include "napi/native_api.h" 26 #include "napi/native_node_api.h" 27 28 namespace OHOS { 29 namespace Media { 30 static const std::string FETCH_FILE_RESULT_CLASS_NAME = "FetchFileResult"; 31 static const std::string UFM_FETCH_FILE_RESULT_CLASS_NAME = "UserFileMgrFetchFileResult"; 32 33 class FetchResultProperty { 34 public: 35 std::shared_ptr<FetchResult<FileAsset>> fetchFileResult_ = nullptr; 36 std::shared_ptr<FetchResult<AlbumAsset>> fetchAlbumResult_ = nullptr; 37 std::shared_ptr<FetchResult<SmartAlbumAsset>> fetchSmartAlbumResult_ = nullptr; 38 FetchResType fetchResType_; 39 std::string typeMask_; 40 }; 41 42 class FetchFileResultNapi { 43 public: 44 FetchFileResultNapi(); 45 ~FetchFileResultNapi(); 46 47 static napi_value Init(napi_env env, napi_value exports); 48 static napi_value UserFileMgrInit(napi_env env, napi_value exports); 49 50 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<FileAsset>> fileResult); 51 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<AlbumAsset>> fileResult); 52 static napi_value CreateFetchFileResult(napi_env env, std::unique_ptr<FetchResult<SmartAlbumAsset>> fileResult); 53 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResultObject(); 54 std::shared_ptr<FetchResult<AlbumAsset>> GetFetchAlbumResultObject(); 55 std::shared_ptr<FetchResult<SmartAlbumAsset>> GetFetchSmartAlbumResultObject(); 56 57 std::shared_ptr<FetchResult<FileAsset>> GetFetchFileResult() const; 58 FetchResType GetFetchResType(); 59 bool CheckIfPropertyPtrNull(); 60 61 private: 62 static void FetchFileResultNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint); 63 static void GetFetchResult(unique_ptr<FetchFileResultNapi> &obj); 64 static napi_value FetchFileResultNapiConstructor(napi_env env, napi_callback_info info); 65 66 static napi_value JSGetCount(napi_env env, napi_callback_info info); 67 static napi_value JSIsAfterLast(napi_env env, napi_callback_info info); 68 static napi_value JSGetFirstObject(napi_env env, napi_callback_info info); 69 static napi_value JSGetNextObject(napi_env env, napi_callback_info info); 70 static napi_value JSGetLastObject(napi_env env, napi_callback_info info); 71 static napi_value JSGetPositionObject(napi_env env, napi_callback_info info); 72 static napi_value JSGetAllObject(napi_env env, napi_callback_info info); 73 static napi_value JSClose(napi_env env, napi_callback_info info); 74 75 napi_env env_; 76 std::shared_ptr<FetchResultProperty> propertyPtr; 77 static thread_local napi_ref sConstructor_; 78 static thread_local napi_ref userFileMgrConstructor_; 79 static inline thread_local std::unique_ptr<FetchResult<FileAsset>> sFetchFileResult_ = nullptr; 80 static inline thread_local std::unique_ptr<FetchResult<AlbumAsset>> sFetchAlbumResult_ = nullptr; 81 static inline thread_local std::unique_ptr<FetchResult<SmartAlbumAsset>> sFetchSmartAlbumResult_ = nullptr; 82 static inline thread_local FetchResType sFetchResType_ = FetchResType::TYPE_FILE; 83 }; 84 85 class FetchFileResultAsyncContext : public NapiError { 86 public: 87 napi_async_work work; 88 napi_ref callbackRef; 89 napi_deferred deferred; 90 FetchFileResultNapi* objectInfo; 91 std::shared_ptr<FetchResultProperty> objectPtr; 92 bool status; 93 int32_t position; 94 std::unique_ptr<FileAsset> fileAsset; 95 std::unique_ptr<AlbumAsset> albumAsset; 96 std::unique_ptr<SmartAlbumAsset> smartAlbumAsset; 97 std::vector<std::unique_ptr<FileAsset>> fileAssetArray; 98 std::vector<std::unique_ptr<AlbumAsset>> fileAlbumArray; 99 std::vector<std::unique_ptr<SmartAlbumAsset>> fileSmartAlbumArray; 100 void GetFirstAsset(); 101 void GetObjectAtPosition(); 102 void GetAllObjectFromFetchResult(); 103 void GetLastObject(); 104 void GetNextObject(); 105 }; 106 } // namespace Media 107 } // namespace OHOS 108 109 #endif // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FETCH_FILE_RESULT_NAPI_H_ 110