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_FILE_ASSET_NAPI_SENDABLE_H_
17 #define INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FILE_ASSET_NAPI_SENDABLE_H_
18 
19 #include <mutex>
20 
21 #include "file_asset.h"
22 #include "medialibrary_type_const.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "napi_error.h"
26 #include "values_bucket.h"
27 #include "napi_remote_object.h"
28 #include "datashare_predicates.h"
29 #include "datashare_abs_result_set.h"
30 #include "datashare_helper.h"
31 #include "context.h"
32 #include "thumbnail_manager.h"
33 
34 namespace OHOS {
35 namespace Media {
36 #define EXPORT __attribute__ ((visibility ("default")))
37 static const std::string SENDABLE_FILE_ASSET_NAPI_CLASS_NAME = "SendableFileAsset";
38 static const std::string SENDABLE_USERFILEMGR_FILEASSET_NAPI_CLASS_NAME = "SendableUserFileMgrFileAsset";
39 static const std::string SENDABLE_PHOTOACCESSHELPER_FILEASSET_NAPI_CLASS_NAME = "SendablePhotoAccessHelperFileAsset";
40 
41 struct SendableAnalysisSourceInfo {
42     std::string fieldStr;
43     std::string uriStr;
44     std::vector<std::string> fetchColumn;
45 };
46 
47 class SendableFileAssetNapi {
48 public:
49     EXPORT SendableFileAssetNapi();
50     EXPORT ~SendableFileAssetNapi();
51 
52     EXPORT static napi_value PhotoAccessHelperInit(napi_env env, napi_value exports);
53     EXPORT static napi_value CreateFileAsset(napi_env env, std::unique_ptr<FileAsset> &iAsset);
54     EXPORT static napi_value CreatePhotoAsset(napi_env env, std::shared_ptr<FileAsset> &fileAsset);
55 
56     std::string GetFileDisplayName() const;
57     std::string GetRelativePath() const;
58     std::string GetFilePath() const;
59     std::string GetTitle() const;
60     std::string GetFileUri() const;
61     int32_t GetFileId() const;
62     int32_t GetOrientation() const;
63     MediaType GetMediaType() const;
64     std::string GetNetworkId() const;
65     bool IsFavorite() const;
66     void SetFavorite(bool isFavorite);
67     bool IsTrash() const;
68     void SetTrash(bool isTrash);
69     bool IsHidden() const;
70     void SetHidden(bool isHidden);
71     std::string GetAllExif() const;
72     std::string GetFrontCamera() const;
73     std::string GetUserComment() const;
74     std::shared_ptr<FileAsset> GetFileAssetInstance() const;
75 
76 private:
77     EXPORT static void FileAssetNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint);
78     EXPORT static napi_value FileAssetNapiConstructor(napi_env env, napi_callback_info info);
79 
80     EXPORT static napi_value JSGetFileUri(napi_env env, napi_callback_info info);
81     EXPORT static napi_value JSGetFileDisplayName(napi_env env, napi_callback_info info);
82     EXPORT static napi_value JSGetFilePath(napi_env env, napi_callback_info info);
83     EXPORT static napi_value JSGetMediaType(napi_env env, napi_callback_info info);
84     EXPORT static napi_value JSSetFileDisplayName(napi_env env, napi_callback_info info);
85 
86     void UpdateFileAssetInfo();
87     EXPORT static napi_value PhotoAccessHelperSet(napi_env env, napi_callback_info info);
88     EXPORT static napi_value PhotoAccessHelperGet(napi_env env, napi_callback_info info);
89     EXPORT static napi_value PhotoAccessHelperGetAnalysisData(napi_env env, napi_callback_info info);
90 
91     EXPORT static napi_value PhotoAccessHelperCommitModify(napi_env env, napi_callback_info info);
92     EXPORT static napi_value PhotoAccessHelperGetThumbnail(napi_env env, napi_callback_info info);
93     EXPORT static napi_value PhotoAccessHelperRequestSource(napi_env env, napi_callback_info info);
94     EXPORT static napi_value ConvertFromPhotoAsset(napi_env env, napi_callback_info info);
95     EXPORT static napi_value ConvertToPhotoAsset(napi_env env, napi_callback_info info);
96 
97     bool HandleParamSet(const std::string &inputKey, const std::string &value, ResultNapiType resultNapiType);
98     napi_env env_;
99 
100     static thread_local napi_ref photoAccessHelperConstructor_;
101     static thread_local FileAsset *sFileAsset_;
102     std::shared_ptr<FileAsset> fileAssetPtr = nullptr;
103     static std::shared_ptr<ThumbnailManager> thumbnailManager_;
104     std::unordered_map<std::string, std::variant<int32_t, int64_t, std::string, double>> member_;
105 };
106 struct SendableFileAssetAsyncContext : public NapiError {
107     napi_async_work work;
108     napi_deferred deferred;
109     napi_ref callbackRef;
110     bool status;
111     SendableFileAssetNapi *objectInfo;
112     std::shared_ptr<FileAsset> objectPtr = nullptr;
113     OHOS::DataShare::DataShareValuesBucket valuesBucket;
114     Size size;
115     bool isDirectory;
116     int32_t changedRows;
117     int32_t fd;
118     int32_t analysisType = AnalysisType::ANALYSIS_INVALID;
119     bool isFavorite = false;
120     bool isTrash = false;
121     bool isHidden = false;
122     bool isPending = false;
123     bool hasEdit = false;
124     std::string networkId;
125     std::string analysisData;
126     std::shared_ptr<PixelMap> pixelmap;
127 
128     size_t argc;
129     napi_value argv[NAPI_ARGC_MAX];
130     ResultNapiType resultNapiType;
131     std::string userComment;
132     std::string jsonStr;
133     std::string editData;
134     std::string uri;
135     char* editDataBuffer;
136 };
137 } // namespace Media
138 } // namespace OHOS
139 
140 #endif  // INTERFACES_KITS_JS_MEDIALIBRARY_INCLUDE_FILE_ASSET_NAPI_H_