• 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 #define MLOG_TAG "PhotoAssetCustomRecordNapi"
16 
17 #include "photo_asset_custom_record_napi.h"
18 
19 #include "media_file_asset_columns.h"
20 #include "media_library_napi.h"
21 #include "media_smart_album_column.h"
22 #include "media_smart_map_column.h"
23 #include "medialibrary_client_errno.h"
24 #include "medialibrary_napi_log.h"
25 #include "medialibrary_tracer.h"
26 #include "media_file_utils.h"
27 #include "userfile_client.h"
28 #include "media_file_uri.h"
29 
30 using OHOS::HiviewDFX::HiLog;
31 using OHOS::HiviewDFX::HiLogLabel;
32 
33 namespace OHOS {
34 namespace Media {
35 thread_local PhotoAssetCustomRecord *PhotoAssetCustomRecordNapi::cRecordData_ = nullptr;
36 thread_local napi_ref PhotoAssetCustomRecordNapi::constructor_ = nullptr;
37 
Init(napi_env env,napi_value exports)38 napi_value PhotoAssetCustomRecordNapi::Init(napi_env env, napi_value exports)
39 {
40     NapiClassInfo info = {
41         .name = PHOTO_ASSET_CUSTOM_RECORDS_NAPI_CLASS_NAME,
42         .ref = &constructor_,
43         .constructor = Constructor,
44         .props = {
45             DECLARE_NAPI_GETTER("fileId", JSGetFileId),
46             DECLARE_NAPI_GETTER("shareCount", JSGetShareCount),
47             DECLARE_NAPI_GETTER("lcdJumpCount", JSGetLcdJumpCount),
48         }
49     };
50 
51     MediaLibraryNapiUtils::NapiDefineClass(env, exports, info);
52     return exports;
53 }
54 
SetCustomRecordNapiProperties()55 void PhotoAssetCustomRecordNapi::SetCustomRecordNapiProperties()
56 {
57     customRecordPtr = std::shared_ptr<PhotoAssetCustomRecord>(cRecordData_);
58 }
59 
Constructor(napi_env env,napi_callback_info info)60 napi_value PhotoAssetCustomRecordNapi::Constructor(napi_env env, napi_callback_info info)
61 {
62     napi_status status;
63     napi_value result = nullptr;
64     napi_value thisVar = nullptr;
65 
66     napi_get_undefined(env, &result);
67     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
68     if (status != napi_ok || thisVar == nullptr) {
69         return result;
70     }
71     std::unique_ptr<PhotoAssetCustomRecordNapi> obj = std::make_unique<PhotoAssetCustomRecordNapi>();
72     if (obj == nullptr) {
73         return result;
74     }
75     obj->env_ = env;
76     if (cRecordData_ != nullptr) {
77         obj->SetCustomRecordNapiProperties();
78     }
79     status = napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()),
80     PhotoAssetCustomRecordNapi::Destructor, nullptr, nullptr);
81     if (status == napi_ok) {
82         obj.release();
83         return thisVar;
84     } else {
85         NAPI_ERR_LOG("Failure wrapping js to native napi. status: %{public}d", status);
86     }
87     return result;
88 }
89 
Destructor(napi_env env,void * nativeObject,void * finalizeHint)90 void PhotoAssetCustomRecordNapi::Destructor(napi_env env, void* nativeObject, void* finalizeHint)
91 {
92     auto* photoAssetCustomRecordNapi = reinterpret_cast<PhotoAssetCustomRecordNapi*>(nativeObject);
93     if (photoAssetCustomRecordNapi == nullptr) {
94         NAPI_ERR_LOG("PhotoAssetCustomRecordNapi is nullptr");
95         return;
96     }
97     delete photoAssetCustomRecordNapi;
98     photoAssetCustomRecordNapi = nullptr;
99 }
100 
CreateCustomRecordNapi(napi_env env,unique_ptr<PhotoAssetCustomRecord> & cRecord)101 napi_value PhotoAssetCustomRecordNapi::CreateCustomRecordNapi(napi_env env,
102     unique_ptr<PhotoAssetCustomRecord> &cRecord)
103 {
104     if (cRecord == nullptr) {
105         return nullptr;
106     }
107 
108     napi_value constructor;
109     napi_ref constructorRef = constructor_;
110     NAPI_CALL(env, napi_get_reference_value(env, constructorRef, &constructor));
111     napi_value result = nullptr;
112     cRecordData_ = cRecord.release();
113     NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
114     cRecordData_ = nullptr;
115     return result;
116 }
117 
GetFileId() const118 int32_t PhotoAssetCustomRecordNapi::GetFileId() const
119 {
120     return customRecordPtr->GetFileId();
121 }
122 
GetShareCount() const123 int32_t PhotoAssetCustomRecordNapi::GetShareCount() const
124 {
125     return customRecordPtr->GetShareCount();
126 }
127 
GetLcdJumpCount() const128 int32_t PhotoAssetCustomRecordNapi::GetLcdJumpCount() const
129 {
130     return customRecordPtr->GetLcdJumpCount();
131 }
132 
JSGetFileId(napi_env env,napi_callback_info info)133 napi_value PhotoAssetCustomRecordNapi::JSGetFileId(napi_env env, napi_callback_info info)
134 {
135     napi_status status;
136     napi_value jsResult = nullptr;
137     napi_value undefinedResult = nullptr;
138     PhotoAssetCustomRecordNapi* obj = nullptr;
139     int32_t id;
140     napi_value thisVar = nullptr;
141 
142     napi_get_undefined(env, &undefinedResult);
143     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
144     if (status != napi_ok || thisVar == nullptr) {
145         NAPI_ERR_LOG("Invalid arguments! status: %{public}d", status);
146         return undefinedResult;
147     }
148 
149     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
150     if (status == napi_ok && obj != nullptr) {
151         id = obj->GetFileId();
152         status = napi_create_int32(env, id, &jsResult);
153         if (status == napi_ok) {
154             return jsResult;
155         }
156     }
157 
158     return undefinedResult;
159 }
160 
JSGetShareCount(napi_env env,napi_callback_info info)161 napi_value PhotoAssetCustomRecordNapi::JSGetShareCount(napi_env env, napi_callback_info info)
162 {
163     napi_status status;
164     napi_value jsResult = nullptr;
165     napi_value undefinedResult = nullptr;
166     PhotoAssetCustomRecordNapi* obj = nullptr;
167     int32_t id;
168     napi_value thisVar = nullptr;
169 
170     napi_get_undefined(env, &undefinedResult);
171     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
172     if (status != napi_ok || thisVar == nullptr) {
173         NAPI_ERR_LOG("Invalid arguments! status: %{public}d", status);
174         return undefinedResult;
175     }
176 
177     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
178     if (status == napi_ok && obj != nullptr) {
179         id = obj->GetShareCount();
180         status = napi_create_int32(env, id, &jsResult);
181         if (status == napi_ok) {
182             return jsResult;
183         }
184     }
185 
186     return undefinedResult;
187 }
188 
JSGetLcdJumpCount(napi_env env,napi_callback_info info)189 napi_value PhotoAssetCustomRecordNapi::JSGetLcdJumpCount(napi_env env, napi_callback_info info)
190 {
191     napi_status status;
192     napi_value jsResult = nullptr;
193     napi_value undefinedResult = nullptr;
194     PhotoAssetCustomRecordNapi* obj = nullptr;
195     int32_t id;
196     napi_value thisVar = nullptr;
197 
198     napi_get_undefined(env, &undefinedResult);
199     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
200     if (status != napi_ok || thisVar == nullptr) {
201         NAPI_ERR_LOG("Invalid arguments! status: %{public}d", status);
202         return undefinedResult;
203     }
204 
205     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
206     if (status == napi_ok && obj != nullptr) {
207         id = obj->GetLcdJumpCount();
208         status = napi_create_int32(env, id, &jsResult);
209         if (status == napi_ok) {
210             return jsResult;
211         }
212     }
213 
214     return undefinedResult;
215 }
216 } // namespace Media
217 } // namespace OHOS