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