• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MEDIA_LIBRARY_NAPI_H_
17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_LIBRARY_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 "fetch_file_result_napi.h"
27 #include "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 MEDIA_LIB_NAPI_CLASS_NAME = "MediaLibrary";
44 static const std::string USERFILE_MGR_NAPI_CLASS_NAME = "UserFileManager";
45 static const std::string PHOTOACCESSHELPER_NAPI_CLASS_NAME = "PhotoAccessHelper";
46 
47 enum ListenerType {
48     INVALID_LISTENER = -1,
49 
50     AUDIO_LISTENER,
51     VIDEO_LISTENER,
52     IMAGE_LISTENER,
53     FILE_LISTENER,
54     SMARTALBUM_LISTENER,
55     DEVICE_LISTENER,
56     REMOTEFILE_LISTENER,
57     ALBUM_LISTENER
58 };
59 
60 enum ReplaceSelectionMode {
61     DEFAULT = 0,
62     ADD_DOCS_TO_RELATIVE_PATH,
63 };
64 
65 struct MediaChangeListener {
66     MediaType mediaType;
67     OHOS::DataShare::DataShareObserver::ChangeInfo changeInfo;
68     std::string strUri;
69 };
70 
71 struct AnalysisProperty {
72     std::string enumName;
73     int32_t enumValue;
74 };
75 
76 class MediaOnNotifyObserver;
77 class ChangeListenerNapi {
78 public:
79     class UvChangeMsg {
80     public:
UvChangeMsg(napi_env env,napi_ref ref,OHOS::DataShare::DataShareObserver::ChangeInfo & changeInfo,std::string strUri)81         UvChangeMsg(napi_env env, napi_ref ref,
82                     OHOS::DataShare::DataShareObserver::ChangeInfo &changeInfo,
83                     std::string strUri)
84             : env_(env), ref_(ref), changeInfo_(changeInfo),
85             strUri_(std::move(strUri)) {}
~UvChangeMsg()86         ~UvChangeMsg() {}
87         napi_env env_;
88         napi_ref ref_;
89         OHOS::DataShare::DataShareObserver::ChangeInfo changeInfo_;
90         uint8_t *data_;
91         std::string strUri_;
92     };
93 
ChangeListenerNapi(napi_env env)94     explicit ChangeListenerNapi(napi_env env) : env_(env) {}
95 
ChangeListenerNapi(const ChangeListenerNapi & listener)96     ChangeListenerNapi(const ChangeListenerNapi &listener)
97     {
98         this->env_ = listener.env_;
99         this->cbOnRef_ = listener.cbOnRef_;
100         this->cbOffRef_ = listener.cbOffRef_;
101     }
102 
103     ChangeListenerNapi& operator=(const ChangeListenerNapi &listener)
104     {
105         this->env_ = listener.env_;
106         this->cbOnRef_ = listener.cbOnRef_;
107         this->cbOffRef_ = listener.cbOffRef_;
108         return *this;
109     }
110 
~ChangeListenerNapi()111     ~ChangeListenerNapi() {};
112 
113     void OnChange(MediaChangeListener &listener, const napi_ref cbRef);
114     int UvQueueWork(uv_loop_s *loop, uv_work_t *work);
115     static napi_value SolveOnChange(napi_env env, UvChangeMsg *msg);
116     static string GetTrashAlbumUri();
117     static std::string trashAlbumUri_;
118     napi_ref cbOnRef_ = nullptr;
119     napi_ref cbOffRef_ = nullptr;
120     sptr<AAFwk::IDataAbilityObserver> audioDataObserver_ = nullptr;
121     sptr<AAFwk::IDataAbilityObserver> videoDataObserver_ = nullptr;
122     sptr<AAFwk::IDataAbilityObserver> imageDataObserver_ = nullptr;
123     sptr<AAFwk::IDataAbilityObserver> fileDataObserver_ = nullptr;
124     sptr<AAFwk::IDataAbilityObserver> smartAlbumDataObserver_ = nullptr;
125     sptr<AAFwk::IDataAbilityObserver> deviceDataObserver_ = nullptr;
126     sptr<AAFwk::IDataAbilityObserver> remoteFileDataObserver_ = nullptr;
127     sptr<AAFwk::IDataAbilityObserver> albumDataObserver_ = nullptr;
128     std::vector<std::shared_ptr<MediaOnNotifyObserver>> observers_;
129 private:
130     napi_env env_ = nullptr;
131 };
132 
133 class MediaObserver : public AAFwk::DataAbilityObserverStub {
134 public:
MediaObserver(const ChangeListenerNapi & listObj,MediaType mediaType)135     MediaObserver(const ChangeListenerNapi &listObj, MediaType mediaType) : listObj_(listObj)
136     {
137         mediaType_ = mediaType;
138     }
139 
140     ~MediaObserver() = default;
141 
OnChange()142     void OnChange() override
143     {
144         MediaChangeListener listener;
145         listener.mediaType = mediaType_;
146         listObj_.OnChange(listener, listObj_.cbOnRef_);
147     }
148 
149     ChangeListenerNapi listObj_;
150     MediaType mediaType_;
151 };
152 
153 class MediaOnNotifyObserver : public DataShare::DataShareObserver  {
154 public:
MediaOnNotifyObserver(const ChangeListenerNapi & listObj,std::string uri,napi_ref ref)155     MediaOnNotifyObserver(const ChangeListenerNapi &listObj, std::string uri, napi_ref ref) : listObj_(listObj)
156     {
157         uri_ = uri;
158         ref_ = ref;
159     }
160 
161     ~MediaOnNotifyObserver() = default;
162 
OnChange(const ChangeInfo & changeInfo)163     void OnChange(const ChangeInfo &changeInfo) override
164     {
165         MediaChangeListener listener;
166         listener.changeInfo = changeInfo;
167         listener.strUri = uri_;
168         listObj_.OnChange(listener, ref_);
169     }
170     ChangeListenerNapi listObj_;
171     std::string uri_;
172     napi_ref ref_;
173 };
174 class MediaLibraryNapi {
175 public:
176     EXPORT static napi_value Init(napi_env env, napi_value exports);
177     EXPORT static napi_value UserFileMgrInit(napi_env env, napi_value exports);
178     EXPORT static napi_value PhotoAccessHelperInit(napi_env env, napi_value exports);
179 
180     static void ReplaceSelection(std::string &selection, std::vector<std::string> &selectionArgs,
181         const std::string &key, const std::string &keyInstead, const int32_t mode = ReplaceSelectionMode::DEFAULT);
182 
183     EXPORT MediaLibraryNapi();
184     EXPORT ~MediaLibraryNapi();
185 
186     static std::mutex sUserFileClientMutex_;
187 
188 private:
189     EXPORT static void MediaLibraryNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint);
190     EXPORT static napi_value MediaLibraryNapiConstructor(napi_env env, napi_callback_info info);
191 
192     EXPORT static napi_value GetMediaLibraryNewInstance(napi_env env, napi_callback_info info);
193     EXPORT static napi_value GetMediaLibraryNewInstanceAsync(napi_env env, napi_callback_info info);
194 
195     EXPORT static napi_value JSGetPublicDirectory(napi_env env, napi_callback_info info);
196     EXPORT static napi_value JSGetFileAssets(napi_env env, napi_callback_info info);
197     EXPORT static napi_value JSGetAlbums(napi_env env, napi_callback_info info);
198 
199     EXPORT static napi_value JSCreateAsset(napi_env env, napi_callback_info info);
200     EXPORT static napi_value JSDeleteAsset(napi_env env, napi_callback_info info);
201 
202     EXPORT static napi_value JSOnCallback(napi_env env, napi_callback_info info);
203     EXPORT static napi_value JSOffCallback(napi_env env, napi_callback_info info);
204 
205     EXPORT static napi_value JSRelease(napi_env env, napi_callback_info info);
206 
207     EXPORT static napi_value JSGetActivePeers(napi_env env, napi_callback_info info);
208     EXPORT static napi_value JSGetAllPeers(napi_env env, napi_callback_info info);
209     EXPORT static napi_value CreateMediaTypeEnum(napi_env env);
210     EXPORT static napi_value CreateFileKeyEnum(napi_env env);
211     EXPORT static napi_value CreateDirectoryTypeEnum(napi_env env);
212     EXPORT static napi_value CreateVirtualAlbumTypeEnum(napi_env env);
213     EXPORT static napi_value CreatePrivateAlbumTypeEnum(napi_env env);
214     EXPORT static napi_value CreateDeliveryModeEnum(napi_env env);
215     EXPORT static napi_value CreateSourceModeEnum(napi_env env);
216 
217     EXPORT static napi_value CreatePhotoKeysEnum(napi_env env);
218     EXPORT static napi_value CreateHiddenPhotosDisplayModeEnum(napi_env env);
219 
220     EXPORT static napi_value CreateMediaTypeUserFileEnum(napi_env env);
221 
222     EXPORT static napi_value JSGetSmartAlbums(napi_env env, napi_callback_info info);
223     EXPORT static napi_value JSGetPrivateAlbum(napi_env env, napi_callback_info info);
224     EXPORT static napi_value JSCreateSmartAlbum(napi_env env, napi_callback_info info);
225     EXPORT static napi_value JSDeleteSmartAlbum(napi_env env, napi_callback_info info);
226 
227     EXPORT static napi_value JSStoreMediaAsset(napi_env env, napi_callback_info info);
228     EXPORT static napi_value JSStartImagePreview(napi_env env, napi_callback_info info);
229     EXPORT static napi_value JSGetMediaRemoteStub(napi_env env, napi_callback_info info);
230 
231     EXPORT static napi_value GetUserFileMgr(napi_env env, napi_callback_info info);
232     EXPORT static napi_value GetUserFileMgrAsync(napi_env env, napi_callback_info info);
233     EXPORT static napi_value UserFileMgrCreatePhotoAsset(napi_env env, napi_callback_info info);
234     EXPORT static napi_value UserFileMgrCreateAudioAsset(napi_env env, napi_callback_info info);
235     EXPORT static napi_value UserFileMgrDeleteAsset(napi_env env, napi_callback_info info);
236     EXPORT static napi_value UserFileMgrTrashAsset(napi_env env, napi_callback_info info);
237     EXPORT static napi_value JSGetPhotoAlbums(napi_env env, napi_callback_info info);
238     EXPORT static napi_value JSGetPhotoAssets(napi_env env, napi_callback_info info);
239     EXPORT static napi_value JSGetJsonPhotoAssets(napi_env env, napi_callback_info info);
240     EXPORT static napi_value JSGetAudioAssets(napi_env env, napi_callback_info info);
241     EXPORT static napi_value UserFileMgrGetPrivateAlbum(napi_env env, napi_callback_info info);
242     EXPORT static napi_value UserFileMgrCreateFileKeyEnum(napi_env env);
243     EXPORT static napi_value UserFileMgrOnCallback(napi_env env, napi_callback_info info);
244     EXPORT static napi_value UserFileMgrOffCallback(napi_env env, napi_callback_info info);
245     EXPORT static napi_value CreateAudioKeyEnum(napi_env env);
246     EXPORT static napi_value CreateImageVideoKeyEnum(napi_env env);
247     EXPORT static napi_value CreateAlbumKeyEnum(napi_env env);
248     EXPORT static napi_value CreatePositionTypeEnum(napi_env env);
249     EXPORT static napi_value CreatePhotoSubTypeEnum(napi_env env);
250 
251     EXPORT static napi_value GetPhotoAccessHelper(napi_env env, napi_callback_info info);
252     EXPORT static napi_value StartPhotoPicker(napi_env env, napi_callback_info info);
253     EXPORT static napi_value GetPhotoAccessHelperAsync(napi_env env, napi_callback_info info);
254     EXPORT static napi_value CreateDeleteRequest(napi_env env, napi_callback_info info);
255     EXPORT static napi_value PhotoAccessHelperCreatePhotoAsset(napi_env env, napi_callback_info info);
256     EXPORT static napi_value PhotoAccessHelperTrashAsset(napi_env env, napi_callback_info info);
257     EXPORT static napi_value PhotoAccessHelperOnCallback(napi_env env, napi_callback_info info);
258     EXPORT static napi_value PhotoAccessHelperOffCallback(napi_env env, napi_callback_info info);
259     EXPORT static napi_value PhotoAccessGetPhotoAssets(napi_env env, napi_callback_info info);
260     EXPORT static napi_value PhotoAccessCreatePhotoAlbum(napi_env env, napi_callback_info info);
261     EXPORT static napi_value PhotoAccessDeletePhotoAlbums(napi_env env, napi_callback_info info);
262     EXPORT static napi_value PhotoAccessGetPhotoAlbums(napi_env env, napi_callback_info info);
263     EXPORT static napi_value PhotoAccessSaveFormInfo(napi_env env, napi_callback_info info);
264     EXPORT static napi_value PhotoAccessRemoveFormInfo(napi_env env, napi_callback_info info);
265 
266     EXPORT static napi_value SetHidden(napi_env env, napi_callback_info info);
267     EXPORT static napi_value UfmGetHiddenAlbums(napi_env env, napi_callback_info info);
268     EXPORT static napi_value PahGetHiddenAlbums(napi_env env, napi_callback_info info);
269 
270     EXPORT static napi_value CreateAlbumTypeEnum(napi_env env);
271     EXPORT static napi_value CreateAlbumSubTypeEnum(napi_env env);
272     EXPORT static napi_value CreateNotifyTypeEnum(napi_env env);
273     EXPORT static napi_value CreateDefaultChangeUriEnum(napi_env env);
274     EXPORT static napi_value CreateAnalysisTypeEnum(napi_env env);
275     EXPORT static napi_value CreateRequestPhotoTypeEnum(napi_env env);
276     EXPORT static napi_value CreateResourceTypeEnum(napi_env env);
277 
278     EXPORT static napi_value CreatePhotoAlbum(napi_env env, napi_callback_info info);
279     EXPORT static napi_value DeletePhotoAlbums(napi_env env, napi_callback_info info);
280     EXPORT static napi_value GetPhotoAlbums(napi_env env, napi_callback_info info);
281     EXPORT static napi_value JSGetPhotoIndex(napi_env env, napi_callback_info info);
282     EXPORT static napi_value PhotoAccessGetPhotoIndex(napi_env env, napi_callback_info info);
283 
284     EXPORT static napi_value JSApplyChanges(napi_env env, napi_callback_info info);
285 
286     int32_t GetListenerType(const std::string &str) const;
287     void RegisterChange(napi_env env, const std::string &type, ChangeListenerNapi &listObj);
288     void UnregisterChange(napi_env env, const std::string &type, ChangeListenerNapi &listObj);
289     void RegisterNotifyChange(napi_env env,
290         const std::string &uri, bool isDerived, napi_ref ref, ChangeListenerNapi &listObj);
291     void UnRegisterNotifyChange(napi_env env, const std::string &uri, napi_ref ref, ChangeListenerNapi &listObj);
292     static bool CheckRef(napi_env env,
293         napi_ref ref, ChangeListenerNapi &listObj, bool isOff, const std::string &uri);
294     napi_env env_;
295 
296     static thread_local napi_ref sConstructor_;
297     static thread_local napi_ref userFileMgrConstructor_;
298     static thread_local napi_ref photoAccessHelperConstructor_;
299     static thread_local napi_ref sMediaTypeEnumRef_;
300     static thread_local napi_ref sDirectoryEnumRef_;
301     static thread_local napi_ref sVirtualAlbumTypeEnumRef_;
302     static thread_local napi_ref sFileKeyEnumRef_;
303     static thread_local napi_ref sPrivateAlbumEnumRef_;
304 
305     static thread_local napi_ref sUserFileMgrFileKeyEnumRef_;
306     static thread_local napi_ref sAudioKeyEnumRef_;
307     static thread_local napi_ref sImageVideoKeyEnumRef_;
308     static thread_local napi_ref sPhotoKeysEnumRef_;
309     static thread_local napi_ref sAlbumKeyEnumRef_;
310     static thread_local napi_ref sAlbumType_;
311     static thread_local napi_ref sAlbumSubType_;
312     static thread_local napi_ref sPositionTypeEnumRef_;
313     static thread_local napi_ref sHiddenPhotosDisplayModeEnumRef_;
314     static thread_local napi_ref sPhotoSubType_;
315     static thread_local napi_ref sNotifyType_;
316     static thread_local napi_ref sDefaultChangeUriRef_;
317     static thread_local napi_ref sAnalysisType_;
318     static thread_local napi_ref sRequestPhotoTypeEnumRef_;
319     static thread_local napi_ref sResourceTypeEnumRef_;
320     static thread_local napi_ref sDeliveryModeEnumRef_;
321     static thread_local napi_ref sSourceModeEnumRef_;
322 
323     static std::mutex sOnOffMutex_;
324 };
325 
326 struct PickerCallBack {
327     bool ready = false;
328     bool isOrigin;
329     int32_t resultCode;
330     vector<string> uris;
331 };
332 
333 constexpr int32_t DEFAULT_PRIVATEALBUMTYPE = 3;
334 struct MediaLibraryAsyncContext : public NapiError {
335     napi_async_work work;
336     napi_deferred deferred;
337     napi_ref callbackRef;
338     bool status;
339     bool isDelete;
340     bool isCreateByComponent;
341     NapiAssetType assetType;
342     AlbumType albumType;
343     MediaLibraryNapi *objectInfo;
344     std::string selection;
345     std::vector<std::string> selectionArgs;
346     std::string order;
347     std::string uri;
348     std::string networkId;
349     std::string extendArgs;
350     std::unique_ptr<FetchResult<FileAsset>> fetchFileResult;
351     std::unique_ptr<FetchResult<AlbumAsset>> fetchAlbumResult;
352     std::unique_ptr<FetchResult<PhotoAlbum>> fetchPhotoAlbumResult;
353     std::unique_ptr<FetchResult<SmartAlbumAsset>> fetchSmartAlbumResult;
354     std::unique_ptr<FileAsset> fileAsset;
355     std::unique_ptr<PhotoAlbum> photoAlbumData;
356     std::unique_ptr<SmartAlbumAsset> smartAlbumData;
357     OHOS::DataShare::DataShareValuesBucket valuesBucket;
358     unsigned int dirType = 0;
359     int32_t privateAlbumType = DEFAULT_PRIVATEALBUMTYPE;
360     int32_t retVal;
361     std::string directoryRelativePath;
362     std::vector<std::unique_ptr<AlbumAsset>> albumNativeArray;
363     std::vector<std::unique_ptr<SmartAlbumAsset>> smartAlbumNativeArray;
364     std::vector<std::unique_ptr<SmartAlbumAsset>> privateSmartAlbumNativeArray;
365     Ability *ability_;
366     std::string storeMediaSrc;
367     int32_t imagePreviewIndex;
368     int32_t parentSmartAlbumId = 0;
369     int32_t smartAlbumId = -1;
370     int32_t isAnalysisAlbum = 0;
371     int32_t isLocationAlbum = 0;
372     size_t argc;
373     napi_value argv[NAPI_ARGC_MAX];
374     ResultNapiType resultNapiType;
375     std::string tableName;
376     std::vector<uint32_t> mediaTypes;
377     OHOS::DataShare::DataSharePredicates predicates;
378     std::vector<std::string> fetchColumn;
379     std::vector<std::string> uris;
380     bool hiddenOnly = false;
381     int32_t hiddenAlbumFetchMode = -1;
382     std::string formId;
383     std::shared_ptr<PickerCallBack> pickerCallBack;
384 };
385 
386 struct MediaLibraryInitContext : public NapiError  {
387     napi_async_work work;
388     napi_deferred deferred;
389     napi_ref callbackRef;
390     size_t argc;
391     napi_value argv[NAPI_ARGC_MAX];
392     napi_ref resultRef_;
393     sptr<IRemoteObject> token_;
394 };
395 } // namespace Media
396 } // namespace OHOS
397 
398 #endif  // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_MEDIA_LIBRARY_NAPI_H_
399