1 /*
2  * Copyright (C) 2024 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_PHOTO_ACCESS_HELPER_NAPI_H_
17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_PHOTO_ACCESS_HELPER_NAPI_H_
18 
19 #include <mutex>
20 #include <vector>
21 #include "abs_shared_result_set.h"
22 #include "album_napi.h"
23 #include "data_ability_helper.h"
24 #include "data_ability_observer_stub.h"
25 #include "data_ability_predicates.h"
26 #include "sendable_fetch_file_result_napi.h"
27 #include "sendable_file_asset_napi.h"
28 #include "napi_base_context.h"
29 #include "napi/native_api.h"
30 #include "napi/native_node_api.h"
31 #include "napi_error.h"
32 #include "photo_album.h"
33 #include "smart_album_asset.h"
34 #include "values_bucket.h"
35 #include "napi_remote_object.h"
36 #include "datashare_helper.h"
37 #include "datashare_predicates.h"
38 #include "uv.h"
39 
40 namespace OHOS {
41 namespace Media {
42 #define EXPORT __attribute__ ((visibility ("default")))
43 static const std::string SENDABLE_PHOTOACCESSHELPER_NAPI_CLASS_NAME = "SendablePhotoAccessHelper";
44 
45 enum SendableReplaceSelectionMode {
46     SENDABLE_DEFAULT = 0,
47     SENDABLE_ADD_DOCS_TO_RELATIVE_PATH,
48 };
49 
50 class SendablePhotoAccessHelper {
51 public:
52     EXPORT static napi_value Init(napi_env env, napi_value exports);
53 
54     static void ReplaceSelection(std::string &selection, std::vector<std::string> &selectionArgs,
55         const std::string &key, const std::string &keyInstead, const int32_t mode =
56             SendableReplaceSelectionMode::SENDABLE_DEFAULT);
57     static void OnThumbnailGenerated(napi_env env, napi_value cb, void *context, void *data);
58 
59     EXPORT SendablePhotoAccessHelper();
60     EXPORT ~SendablePhotoAccessHelper();
61 
62     static std::mutex sUserFileClientMutex_;
63 
64 private:
65     EXPORT static void MediaLibraryNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint);
66     EXPORT static napi_value MediaLibraryNapiConstructor(napi_env env, napi_callback_info info);
67 
68     EXPORT static napi_value JSRelease(napi_env env, napi_callback_info info);
69 
70     EXPORT static napi_value CreateMediaTypeUserFileEnum(napi_env env);
71     EXPORT static napi_value CreatePhotoSubTypeEnum(napi_env env);
72     EXPORT static napi_value CreateAlbumTypeEnum(napi_env env);
73     EXPORT static napi_value CreateAlbumSubTypeEnum(napi_env env);
74     EXPORT static napi_value CreatePositionTypeEnum(napi_env env);
75     EXPORT static napi_value CreateMovingPhotoEffectModeEnum(napi_env env);
76 
77     EXPORT static napi_value GetPhotoAccessHelper(napi_env env, napi_callback_info info);
78     EXPORT static napi_value PhotoAccessHelperCreatePhotoAsset(napi_env env, napi_callback_info info);
79     EXPORT static napi_value PhotoAccessGetPhotoAssets(napi_env env, napi_callback_info info);
80     EXPORT static napi_value PhotoAccessGetBurstAssets(napi_env env, napi_callback_info info);
81     EXPORT static napi_value PhotoAccessGetPhotoAlbums(napi_env env, napi_callback_info info);
82     EXPORT static napi_value PhotoAccessGetSharedPhotoAssets(napi_env env, napi_callback_info info);
83 
84     EXPORT static napi_value PahGetHiddenAlbums(napi_env env, napi_callback_info info);
85 
86     napi_env env_;
87 
88     static thread_local napi_ref photoAccessHelperConstructor_;
89     static thread_local napi_ref sMediaTypeEnumRef_;
90 
91     static thread_local napi_ref sPhotoKeysEnumRef_;
92     static thread_local napi_ref sAlbumKeyEnumRef_;
93     static thread_local napi_ref sAlbumType_;
94     static thread_local napi_ref sAlbumSubType_;
95     static thread_local napi_ref sDeliveryModeEnumRef_;
96     static thread_local napi_ref sPositionTypeEnumRef_;
97     static thread_local napi_ref sHiddenPhotosDisplayModeEnumRef_;
98     static thread_local napi_ref sPhotoSubType_;
99     static thread_local napi_ref sNotifyType_;
100     static thread_local napi_ref sDefaultChangeUriRef_;
101     static thread_local napi_ref sAnalysisType_;
102     static thread_local napi_ref sRequestPhotoTypeEnumRef_;
103     static thread_local napi_ref sResourceTypeEnumRef_;
104     static thread_local napi_ref sSourceModeEnumRef_;
105     static thread_local napi_ref sHighlightAlbumInfoType_;
106     static thread_local napi_ref sHighlightUserActionType_;
107     static thread_local napi_ref sMovingPhotoEffectModeEnumRef_;
108 
109     static std::mutex sOnOffMutex_;
110 };
111 
112 constexpr int32_t SENDABLE_DEFAULT_PRIVATEALBUMTYPE = 3;
113 struct SendablePhotoAccessHelperAsyncContext : public NapiError {
114     napi_async_work work;
115     napi_deferred deferred;
116     napi_ref callbackRef;
117     bool status;
118     bool isDelete;
119     bool isCreateByComponent;
120     NapiAssetType assetType;
121     AlbumType albumType;
122     SendablePhotoAccessHelper *objectInfo;
123     std::string selection;
124     std::vector<std::string> selectionArgs;
125     std::string order;
126     std::string uri;
127     std::string networkId;
128     std::string extendArgs;
129     std::unique_ptr<FetchResult<FileAsset>> fetchFileResult;
130     std::unique_ptr<FetchResult<AlbumAsset>> fetchAlbumResult;
131     std::unique_ptr<FetchResult<PhotoAlbum>> fetchPhotoAlbumResult;
132     std::unique_ptr<FetchResult<SmartAlbumAsset>> fetchSmartAlbumResult;
133     std::unique_ptr<FileAsset> fileAsset;
134     std::unique_ptr<PhotoAlbum> photoAlbumData;
135     std::unique_ptr<SmartAlbumAsset> smartAlbumData;
136     OHOS::DataShare::DataShareValuesBucket valuesBucket;
137     unsigned int dirType = 0;
138     int32_t privateAlbumType = SENDABLE_DEFAULT_PRIVATEALBUMTYPE;
139     int32_t retVal;
140     std::string directoryRelativePath;
141     std::vector<std::unique_ptr<AlbumAsset>> albumNativeArray;
142     std::vector<std::unique_ptr<SmartAlbumAsset>> smartAlbumNativeArray;
143     std::vector<std::unique_ptr<SmartAlbumAsset>> privateSmartAlbumNativeArray;
144     Ability *ability_;
145     std::string storeMediaSrc;
146     int32_t imagePreviewIndex;
147     int32_t parentSmartAlbumId = 0;
148     int32_t smartAlbumId = -1;
149     int32_t isLocationAlbum = 0;
150     int32_t isHighlightAlbum = 0;
151     size_t argc;
152     napi_value argv[NAPI_ARGC_MAX];
153     ResultNapiType resultNapiType;
154     std::string tableName;
155     std::vector<uint32_t> mediaTypes;
156     OHOS::DataShare::DataSharePredicates predicates;
157     std::vector<std::string> fetchColumn;
158     std::vector<std::string> uris;
159     bool hiddenOnly = false;
160     bool isAnalysisAlbum = false;
161     int32_t hiddenAlbumFetchMode = -1;
162     std::string formId;
163     std::string indexProgress;
164 };
165 
166 struct SendablePhotoAccessHelperInitContext : public NapiError  {
167     napi_async_work work;
168     napi_deferred deferred;
169     napi_ref callbackRef;
170     size_t argc;
171     napi_value argv[NAPI_ARGC_MAX];
172     napi_ref resultRef_;
173     sptr<IRemoteObject> token_;
174 };
175 } // namespace Media
176 } // namespace OHOS
177 
178 #endif  // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_PHOTO_ACCESS_HELPER_NAPI_H_
179