• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 FRAMEWORKS_ANI_SRC_INCLUDE_FILE_ASSET_ANI_H
17 #define FRAMEWORKS_ANI_SRC_INCLUDE_FILE_ASSET_ANI_H
18 
19 #include <ani.h>
20 #include <memory>
21 #include "ani_error.h"
22 #include "datashare_predicates.h"
23 #include "datashare_values_bucket.h"
24 #include "file_asset.h"
25 #include "pixel_map.h"
26 #include "userfile_manager_types.h"
27 
28 namespace OHOS {
29 namespace Media {
30 
31 constexpr int64_t SECONDS_LEVEL_LIMIT = 1e10;
32 static const std::string MEDIA_FILEMODE = "mode";
33 
34 struct FileAssetContext : public AniError {
35     std::shared_ptr<FileAsset> objectPtr;
36     OHOS::DataShare::DataShareValuesBucket valuesBucket;
37     Size size;
38     int32_t changedRows;
39     int32_t fd;
40     int32_t analysisType = AnalysisType::ANALYSIS_INVALID;
41     bool isFavorite = false;
42     bool isHidden = false;
43     std::string analysisData;
44     std::shared_ptr<PixelMap> pixelmap;
45 
46     ResultNapiType resultNapiType;
47     std::string userComment;
48 };
49 
50 struct FileAssetAniMethod {
51     ani_class cls;
52     ani_method ctor;
53     ani_method setUri;
54     ani_method setPhotoType;
55     ani_method setDisplayName;
56 };
57 
58 class FileAssetAni {
59 public:
60     FileAssetAni(std::shared_ptr<FileAsset> fileAsset);
61     FileAssetAni();
62     ~FileAssetAni();
63     std::shared_ptr<FileAsset> GetFileAssetInstance() const;
64     static FileAssetAni* CreatePhotoAsset(ani_env *env, std::shared_ptr<FileAsset> &fileAsset);
65     static FileAssetAni* CreateFileAsset(ani_env *env, std::unique_ptr<FileAsset> &fileAsset);
66 
67     static ani_status FileAssetAniInit(ani_env *env);
68     static ani_object Constructor([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_class clazz);
69     static void Destructor([[maybe_unused]] ani_env env, void *nativeObject, void *finalize_hint);
70     static ani_object Wrap(ani_env *env, FileAssetAni *fileAssetAni);
71     static ani_object Wrap(ani_env *env, FileAssetAni *fileAssetAni, const FileAssetAniMethod &fileAssetAniMethod);
72     static FileAssetAni* Unwrap(ani_env *env, ani_object object);
73     static ani_status InitFileAssetAniMethod(ani_env *env, ResultNapiType classType,
74         FileAssetAniMethod &fileAssetAniMethod);
75 
76     static void Set(ani_env *env, ani_object object, ani_string member, ani_string value);
77     static ani_object Get(ani_env *env, ani_object object, ani_string member);
78     static void CommitModify(ani_env *env, ani_object object);
79     static ani_double Open(ani_env *env, ani_object object, ani_string mode);
80     static void Close(ani_env *env, ani_object object, ani_double fd);
81     static ani_object GetThumbnail(ani_env *env, ani_object object, ani_object size);
82     static void SetUserComment([[maybe_unused]] ani_env *env, ani_object object, ani_string userComment);
83     static ani_string GetAnalysisData([[maybe_unused]] ani_env *env, ani_object object, ani_enum_item analysisType);
84     static void SetHidden([[maybe_unused]] ani_env *env, ani_object object, ani_boolean hiddenState);
85     static void SetFavorite([[maybe_unused]] ani_env *env, ani_object object, ani_boolean favoriteState);
86 
87 private:
88     static thread_local std::shared_ptr<FileAsset> sFileAsset_;
89     std::shared_ptr<FileAsset> fileAssetPtr = nullptr;
90 };
91 
92 } // namespace Media
93 } // namespace OHOS
94 
95 #endif // FRAMEWORKS_ANI_SRC_INCLUDE_FILE_ASSET_ANI_H