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