• 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 #define MLOG_TAG "FetchResultNapi"
16 
17 #include "fetch_file_result_napi.h"
18 
19 #include "album_napi.h"
20 #include "hitrace_meter.h"
21 #include "medialibrary_client_errno.h"
22 #include "medialibrary_napi_log.h"
23 #include "medialibrary_tracer.h"
24 #include "photo_album_napi.h"
25 #include "smart_album_napi.h"
26 #include "media_file_utils.h"
27 
28 using OHOS::HiviewDFX::HiLog;
29 using OHOS::HiviewDFX::HiLogLabel;
30 using namespace std;
31 
32 namespace OHOS {
33 namespace Media {
34 thread_local napi_ref FetchFileResultNapi::sConstructor_ = nullptr;
35 thread_local napi_ref FetchFileResultNapi::userFileMgrConstructor_ = nullptr;
36 thread_local napi_ref FetchFileResultNapi::photoAccessHelperConstructor_ = nullptr;
37 
FetchFileResultNapi()38 FetchFileResultNapi::FetchFileResultNapi()
39     : env_(nullptr) {}
40 
~FetchFileResultNapi()41 FetchFileResultNapi::~FetchFileResultNapi()
42 {
43     propertyPtr = nullptr;
44 }
45 
FetchFileResultNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)46 void FetchFileResultNapi::FetchFileResultNapiDestructor(napi_env env, void *nativeObject, void *finalize_hint)
47 {
48     FetchFileResultNapi *fetchFileResultObj = reinterpret_cast<FetchFileResultNapi*>(nativeObject);
49     if (fetchFileResultObj != nullptr) {
50         delete fetchFileResultObj;
51         fetchFileResultObj = nullptr;
52     }
53 }
54 
Init(napi_env env,napi_value exports)55 napi_value FetchFileResultNapi::Init(napi_env env, napi_value exports)
56 {
57     napi_status status;
58     napi_value ctorObj;
59     int32_t refCount = 1;
60 
61     napi_property_descriptor fetch_file_result_props[] = {
62         DECLARE_NAPI_FUNCTION("getCount", JSGetCount),
63         DECLARE_NAPI_FUNCTION("isAfterLast", JSIsAfterLast),
64         DECLARE_NAPI_FUNCTION("getFirstObject", JSGetFirstObject),
65         DECLARE_NAPI_FUNCTION("getNextObject", JSGetNextObject),
66         DECLARE_NAPI_FUNCTION("getLastObject", JSGetLastObject),
67         DECLARE_NAPI_FUNCTION("getPositionObject", JSGetPositionObject),
68         DECLARE_NAPI_FUNCTION("getAllObject", JSGetAllObject),
69         DECLARE_NAPI_FUNCTION("close", JSClose)
70     };
71 
72     status = napi_define_class(env, FETCH_FILE_RESULT_CLASS_NAME.c_str(), NAPI_AUTO_LENGTH,
73                                FetchFileResultNapiConstructor, nullptr,
74                                sizeof(fetch_file_result_props) / sizeof(fetch_file_result_props[PARAM0]),
75                                fetch_file_result_props, &ctorObj);
76     if (status == napi_ok) {
77         status = napi_create_reference(env, ctorObj, refCount, &sConstructor_);
78         if (status == napi_ok) {
79             status = napi_set_named_property(env, exports, FETCH_FILE_RESULT_CLASS_NAME.c_str(), ctorObj);
80             if (status == napi_ok) {
81                 return exports;
82             }
83         }
84     }
85     NAPI_DEBUG_LOG("Init success");
86     return nullptr;
87 }
88 
GetFetchResult(unique_ptr<FetchFileResultNapi> & obj)89 void FetchFileResultNapi::GetFetchResult(unique_ptr<FetchFileResultNapi> &obj)
90 {
91     switch (sFetchResType_) {
92         case FetchResType::TYPE_FILE: {
93             auto fileResult = make_shared<FetchResult<FileAsset>>(move(sFetchFileResult_->GetDataShareResultSet()));
94             obj->propertyPtr->fetchFileResult_ = fileResult;
95             obj->propertyPtr->fetchFileResult_->SetInfo(sFetchFileResult_);
96             obj->propertyPtr->fetchFileResult_->SetUserId(sFetchFileResult_->GetUserId());
97             break;
98         }
99         case FetchResType::TYPE_ALBUM: {
100             auto albumResult = make_shared<FetchResult<AlbumAsset>>(move(sFetchAlbumResult_->GetDataShareResultSet()));
101             obj->propertyPtr->fetchAlbumResult_ = albumResult;
102             obj->propertyPtr->fetchAlbumResult_->SetInfo(sFetchAlbumResult_);
103             obj->propertyPtr->fetchAlbumResult_->SetUserId(sFetchAlbumResult_->GetUserId());
104             break;
105         }
106         case FetchResType::TYPE_PHOTOALBUM: {
107             auto photoAlbumResult =
108                 make_shared<FetchResult<PhotoAlbum>>(move(sFetchPhotoAlbumResult_->GetDataShareResultSet()));
109             obj->propertyPtr->fetchPhotoAlbumResult_ = photoAlbumResult;
110             obj->propertyPtr->fetchPhotoAlbumResult_->SetInfo(sFetchPhotoAlbumResult_);
111             obj->propertyPtr->fetchPhotoAlbumResult_->SetUserId(sFetchPhotoAlbumResult_->GetUserId());
112             break;
113         }
114         case FetchResType::TYPE_SMARTALBUM: {
115             auto smartResult =
116                 make_shared<FetchResult<SmartAlbumAsset>>(move(sFetchSmartAlbumResult_->GetDataShareResultSet()));
117             obj->propertyPtr->fetchSmartAlbumResult_ = smartResult;
118             obj->propertyPtr->fetchSmartAlbumResult_->SetInfo(sFetchSmartAlbumResult_);
119             break;
120         }
121         default:
122             NAPI_ERR_LOG("unsupported FetchResType");
123             break;
124     }
125 }
126 
127 // Constructor callback
FetchFileResultNapiConstructor(napi_env env,napi_callback_info info)128 napi_value FetchFileResultNapi::FetchFileResultNapiConstructor(napi_env env, napi_callback_info info)
129 {
130     MediaLibraryTracer tracer;
131     tracer.Start("FetchFileResultNapiConstructor");
132 
133     napi_status status;
134     napi_value result = nullptr;
135     napi_value thisVar = nullptr;
136 
137     napi_get_undefined(env, &result);
138     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
139     if (status != napi_ok || thisVar == nullptr) {
140         NAPI_ERR_LOG("Get Js obj failed, status: %{public}d, (thisVar == nullptr) = %{public}d",
141             status, (thisVar == nullptr));
142         return result;
143     }
144 
145     unique_ptr<FetchFileResultNapi> obj = make_unique<FetchFileResultNapi>();
146     if (obj == nullptr) {
147         NAPI_ERR_LOG("Get FetchFileResultNapi failed");
148         return result;
149     }
150     obj->env_ = env;
151     obj->propertyPtr = make_shared<FetchResultProperty>();
152     GetFetchResult(obj);
153     obj->propertyPtr->fetchResType_ = sFetchResType_;
154     status = napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()),
155                        FetchFileResultNapi::FetchFileResultNapiDestructor, nullptr, nullptr);
156     if (status == napi_ok) {
157         obj.release();
158         return thisVar;
159     } else {
160         NAPI_ERR_LOG("Failure wrapping js to native napi, status: %{public}d", status);
161     }
162     return result;
163 }
164 
GetFetchResType()165 FetchResType FetchFileResultNapi::GetFetchResType()
166 {
167     return propertyPtr->fetchResType_;
168 }
169 
SolveConstructorRef(unique_ptr<FetchResult<FileAsset>> & fileResult,napi_ref & constructorRef)170 void FetchFileResultNapi::SolveConstructorRef(unique_ptr<FetchResult<FileAsset>> &fileResult,
171     napi_ref &constructorRef)
172 {
173     switch(fileResult->GetResultNapiType()) {
174         case ResultNapiType::TYPE_USERFILE_MGR: {
175             constructorRef = userFileMgrConstructor_;
176             break;
177         }
178         case ResultNapiType::TYPE_PHOTOACCESS_HELPER: {
179             constructorRef = photoAccessHelperConstructor_;
180             break;
181         }
182         default:
183             constructorRef = sConstructor_;
184             break;
185     }
186 }
187 
SolveConstructorRef(unique_ptr<FetchResult<AlbumAsset>> & fileResult,napi_ref & constructorRef)188 void FetchFileResultNapi::SolveConstructorRef(unique_ptr<FetchResult<AlbumAsset>> &fileResult,
189     napi_ref &constructorRef)
190 {
191     switch(fileResult->GetResultNapiType()) {
192         case ResultNapiType::TYPE_USERFILE_MGR: {
193             constructorRef = userFileMgrConstructor_;
194             break;
195         }
196         case ResultNapiType::TYPE_PHOTOACCESS_HELPER: {
197             constructorRef = photoAccessHelperConstructor_;
198             break;
199         }
200         default:
201             constructorRef = sConstructor_;
202             break;
203     }
204 }
205 
SolveConstructorRef(unique_ptr<FetchResult<SmartAlbumAsset>> & fileResult,napi_ref & constructorRef)206 void FetchFileResultNapi::SolveConstructorRef(unique_ptr<FetchResult<SmartAlbumAsset>> &fileResult,
207     napi_ref &constructorRef)
208 {
209     switch(fileResult->GetResultNapiType()) {
210         case ResultNapiType::TYPE_USERFILE_MGR: {
211             constructorRef = userFileMgrConstructor_;
212             break;
213         }
214         case ResultNapiType::TYPE_PHOTOACCESS_HELPER: {
215             constructorRef = photoAccessHelperConstructor_;
216             break;
217         }
218         default:
219             constructorRef = sConstructor_;
220             break;
221     }
222 }
223 
SolveConstructorRef(unique_ptr<FetchResult<PhotoAlbum>> & fileResult,napi_ref & constructorRef)224 void FetchFileResultNapi::SolveConstructorRef(unique_ptr<FetchResult<PhotoAlbum>> &fileResult,
225     napi_ref &constructorRef)
226 {
227     switch(fileResult->GetResultNapiType()) {
228         case ResultNapiType::TYPE_USERFILE_MGR: {
229             constructorRef = userFileMgrConstructor_;
230             break;
231         }
232         case ResultNapiType::TYPE_PHOTOACCESS_HELPER: {
233             constructorRef = photoAccessHelperConstructor_;
234             break;
235         }
236         default:
237             constructorRef = sConstructor_;
238             break;
239     }
240 }
241 
CreateFetchFileResult(napi_env env,unique_ptr<FetchResult<FileAsset>> fileResult)242 napi_value FetchFileResultNapi::CreateFetchFileResult(napi_env env, unique_ptr<FetchResult<FileAsset>> fileResult)
243 {
244     MediaLibraryTracer tracer;
245     tracer.Start("CreateFetchFileResult");
246     napi_value constructor;
247     napi_ref constructorRef;
248 
249     FetchFileResultNapi::SolveConstructorRef(fileResult, constructorRef);
250     NAPI_CALL(env, napi_get_reference_value(env, constructorRef, &constructor));
251     sFetchResType_ = fileResult->GetFetchResType();
252     sFetchFileResult_ = move(fileResult);
253     napi_value result = nullptr;
254     NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
255     sFetchFileResult_ = nullptr;
256     return result;
257 }
258 
CreateFetchFileResult(napi_env env,unique_ptr<FetchResult<AlbumAsset>> fileResult)259 napi_value FetchFileResultNapi::CreateFetchFileResult(napi_env env, unique_ptr<FetchResult<AlbumAsset>> fileResult)
260 {
261     MediaLibraryTracer tracer;
262     tracer.Start("CreateFetchFileResult");
263     napi_value constructor;
264     napi_ref constructorRef;
265     FetchFileResultNapi::SolveConstructorRef(fileResult, constructorRef);
266     NAPI_CALL(env, napi_get_reference_value(env, constructorRef, &constructor));
267     sFetchResType_ = fileResult->GetFetchResType();
268     sFetchAlbumResult_ = move(fileResult);
269     napi_value result = nullptr;
270     NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
271     sFetchAlbumResult_ = nullptr;
272     return result;
273 }
274 
CreateFetchFileResult(napi_env env,unique_ptr<FetchResult<PhotoAlbum>> fileResult)275 napi_value FetchFileResultNapi::CreateFetchFileResult(napi_env env, unique_ptr<FetchResult<PhotoAlbum>> fileResult)
276 {
277     MediaLibraryTracer tracer;
278     tracer.Start("CreateFetchFileResult");
279     napi_value constructor;
280     napi_ref constructorRef;
281     FetchFileResultNapi::SolveConstructorRef(fileResult, constructorRef);
282     NAPI_CALL(env, napi_get_reference_value(env, constructorRef, &constructor));
283     sFetchResType_ = fileResult->GetFetchResType();
284     sFetchPhotoAlbumResult_ = move(fileResult);
285     napi_value result = nullptr;
286     NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
287     sFetchPhotoAlbumResult_ = nullptr;
288     return result;
289 }
290 
CreateFetchFileResult(napi_env env,unique_ptr<FetchResult<SmartAlbumAsset>> fileResult)291 napi_value FetchFileResultNapi::CreateFetchFileResult(napi_env env, unique_ptr<FetchResult<SmartAlbumAsset>> fileResult)
292 {
293     MediaLibraryTracer tracer;
294     tracer.Start("CreateFetchFileResult");
295     napi_value constructor;
296     napi_ref constructorRef;
297     FetchFileResultNapi::SolveConstructorRef(fileResult, constructorRef);
298     NAPI_CALL(env, napi_get_reference_value(env, constructorRef, &constructor));
299     sFetchResType_ = fileResult->GetFetchResType();
300     sFetchSmartAlbumResult_ = move(fileResult);
301     napi_value result = nullptr;
302     NAPI_CALL(env, napi_new_instance(env, constructor, 0, nullptr, &result));
303     sFetchSmartAlbumResult_ = nullptr;
304     return result;
305 }
306 
GetFetchFileResult() const307 std::shared_ptr<FetchResult<FileAsset>> FetchFileResultNapi::GetFetchFileResult() const
308 {
309     return propertyPtr->fetchFileResult_;
310 }
311 
UserFileMgrInit(napi_env env,napi_value exports)312 napi_value FetchFileResultNapi::UserFileMgrInit(napi_env env, napi_value exports)
313 {
314     NapiClassInfo info = {
315         .name = UFM_FETCH_FILE_RESULT_CLASS_NAME,
316         .ref = &userFileMgrConstructor_,
317         .constructor = FetchFileResultNapiConstructor,
318         .props = {
319             DECLARE_NAPI_FUNCTION("getCount", JSGetCount),
320             DECLARE_NAPI_FUNCTION("isAfterLast", JSIsAfterLast),
321             DECLARE_NAPI_FUNCTION("getFirstObject", JSGetFirstObject),
322             DECLARE_NAPI_FUNCTION("getNextObject", JSGetNextObject),
323             DECLARE_NAPI_FUNCTION("getLastObject", JSGetLastObject),
324             DECLARE_NAPI_FUNCTION("getPositionObject", JSGetPositionObject),
325             DECLARE_NAPI_FUNCTION("getAllObject", JSGetAllObject),
326             DECLARE_NAPI_FUNCTION("close", JSClose)
327         }
328     };
329     MediaLibraryNapiUtils::NapiDefineClass(env, exports, info);
330     return exports;
331 }
332 
PhotoAccessHelperInit(napi_env env,napi_value exports)333 napi_value FetchFileResultNapi::PhotoAccessHelperInit(napi_env env, napi_value exports)
334 {
335     NapiClassInfo info = {
336         .name = PAH_FETCH_FILE_RESULT_CLASS_NAME,
337         .ref = &photoAccessHelperConstructor_,
338         .constructor = FetchFileResultNapiConstructor,
339         .props = {
340             DECLARE_NAPI_FUNCTION("getCount", JSGetCount),
341             DECLARE_NAPI_FUNCTION("isAfterLast", JSIsAfterLast),
342             DECLARE_NAPI_FUNCTION("getFirstObject", JSGetFirstObject),
343             DECLARE_NAPI_FUNCTION("getNextObject", JSGetNextObject),
344             DECLARE_NAPI_FUNCTION("getLastObject", JSGetLastObject),
345             DECLARE_NAPI_FUNCTION("getObjectByPosition", JSGetPositionObject),
346             DECLARE_NAPI_FUNCTION("getAllObjects", JSGetAllObject),
347             DECLARE_NAPI_FUNCTION("close", JSClose)
348         }
349     };
350     MediaLibraryNapiUtils::NapiDefineClass(env, exports, info);
351     return exports;
352 }
353 
CheckIfFFRNapiNotEmpty(FetchFileResultNapi * obj)354 static bool CheckIfFFRNapiNotEmpty(FetchFileResultNapi* obj)
355 {
356     if (obj == nullptr) {
357         NAPI_INFO_LOG("FetchFileResultNapi is nullptr");
358         return false;
359     }
360     if (obj->CheckIfPropertyPtrNull()) {
361         NAPI_INFO_LOG("PropertyPtr in FetchFileResultNapi is nullptr");
362         return false;
363     }
364     return true;
365 }
366 
JSGetCount(napi_env env,napi_callback_info info)367 napi_value FetchFileResultNapi::JSGetCount(napi_env env, napi_callback_info info)
368 {
369     napi_status status;
370     napi_value jsResult = nullptr;
371     FetchFileResultNapi* obj = nullptr;
372     int32_t count = 0;
373     napi_value thisVar = nullptr;
374 
375     MediaLibraryTracer tracer;
376     tracer.Start("JSGetCount");
377 
378     napi_get_undefined(env, &jsResult);
379     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
380     if (status != napi_ok || thisVar == nullptr) {
381         NAPI_ERR_LOG("JSGetCount Invalid arguments!, status: %{public}d", status);
382         NAPI_ASSERT(env, false, "JSGetCount thisVar == nullptr");
383     }
384 
385     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
386     if ((status == napi_ok) && CheckIfFFRNapiNotEmpty(obj)) {
387         switch (obj->GetFetchResType()) {
388             case FetchResType::TYPE_FILE:
389                 count = obj->GetFetchFileResultObject()->GetCount();
390                 break;
391             case FetchResType::TYPE_ALBUM:
392                 count = obj->GetFetchAlbumResultObject()->GetCount();
393                 break;
394             case FetchResType::TYPE_PHOTOALBUM:
395                 count = obj->GetFetchPhotoAlbumResultObject()->GetCount();
396                 break;
397             case FetchResType::TYPE_SMARTALBUM:
398                 count = obj->GetFetchSmartAlbumResultObject()->GetCount();
399                 break;
400             default:
401                 NAPI_ERR_LOG("unsupported FetchResType");
402                 break;
403         }
404         if (count < 0) {
405             NapiError::ThrowError(env, JS_INNER_FAIL, "Failed to get count");
406             return nullptr;
407         }
408         napi_create_int32(env, count, &jsResult);
409     } else {
410         NapiError::ThrowError(env, JS_ERR_PARAMETER_INVALID, "Failed to get native obj");
411         return nullptr;
412     }
413 
414     return jsResult;
415 }
416 
JSIsAfterLast(napi_env env,napi_callback_info info)417 napi_value FetchFileResultNapi::JSIsAfterLast(napi_env env, napi_callback_info info)
418 {
419     napi_status status;
420     napi_value jsResult = nullptr;
421     FetchFileResultNapi* obj = nullptr;
422     bool isAfterLast = false;
423     napi_value thisVar = nullptr;
424 
425     MediaLibraryTracer tracer;
426     tracer.Start("JSIsAfterLast");
427 
428     napi_get_undefined(env, &jsResult);
429     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
430     if (status != napi_ok || thisVar == nullptr) {
431         NAPI_ERR_LOG("JSIsAfterLast Invalid arguments!, status: %{public}d", status);
432         NAPI_ASSERT(env, false, "JSIsAfterLast thisVar == nullptr");
433     }
434 
435     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
436     if ((status == napi_ok) && CheckIfFFRNapiNotEmpty(obj)) {
437         switch (obj->GetFetchResType()) {
438             case FetchResType::TYPE_FILE:
439                 isAfterLast = obj->GetFetchFileResultObject()->IsAtLastRow();
440                 break;
441             case FetchResType::TYPE_ALBUM:
442                 isAfterLast = obj->GetFetchAlbumResultObject()->IsAtLastRow();
443                 break;
444             case FetchResType::TYPE_PHOTOALBUM:
445                 isAfterLast = obj->GetFetchPhotoAlbumResultObject()->IsAtLastRow();
446                 break;
447             case FetchResType::TYPE_SMARTALBUM:
448                 isAfterLast = obj->GetFetchSmartAlbumResultObject()->IsAtLastRow();
449                 break;
450             default:
451                 NAPI_ERR_LOG("unsupported FetchResType");
452                 break;
453         }
454         napi_get_boolean(env, isAfterLast, &jsResult);
455     } else {
456         NAPI_ERR_LOG("JSIsAfterLast obj == nullptr, status: %{public}d", status);
457         NAPI_ASSERT(env, false, "JSIsAfterLast obj == nullptr");
458     }
459 
460     return jsResult;
461 }
462 
GetNapiResFromAsset(napi_env env,FetchFileResultAsyncContext * context,unique_ptr<JSAsyncContextOutput> & jsContext)463 static void GetNapiResFromAsset(napi_env env, FetchFileResultAsyncContext *context,
464     unique_ptr<JSAsyncContextOutput> &jsContext)
465 {
466     napi_value jsAsset;
467     switch (context->objectPtr->fetchResType_) {
468         case FetchResType::TYPE_FILE:
469             if (context->fileAsset != nullptr && context->objectPtr->fetchFileResult_ != nullptr) {
470                 context->fileAsset->SetUserId(context->objectPtr->fetchFileResult_->GetUserId());
471             }
472             jsAsset = FileAssetNapi::CreateFileAsset(env, context->fileAsset);
473             break;
474         case FetchResType::TYPE_ALBUM:
475             jsAsset = AlbumNapi::CreateAlbumNapi(env, context->albumAsset);
476             break;
477         case FetchResType::TYPE_PHOTOALBUM:
478             if (context->photoAlbum != nullptr && context->objectPtr->fetchPhotoAlbumResult_ != nullptr) {
479                 context->photoAlbum->SetUserId(context->objectPtr->fetchPhotoAlbumResult_->GetUserId());
480             }
481             jsAsset = PhotoAlbumNapi::CreatePhotoAlbumNapi(env, context->photoAlbum);
482             break;
483         case FetchResType::TYPE_SMARTALBUM:
484             jsAsset = SmartAlbumNapi::CreateSmartAlbumNapi(env, context->smartAlbumAsset);
485             break;
486         default:
487             NAPI_ERR_LOG("unsupported FetchResType");
488             break;
489     }
490 
491     if (jsAsset == nullptr) {
492         NAPI_ERR_LOG("Failed to get file asset napi object");
493         napi_get_undefined(env, &jsContext->data);
494         MediaLibraryNapiUtils::CreateNapiErrorObject(env, jsContext->error, JS_INNER_FAIL,
495             "System inner fail");
496     } else {
497         jsContext->data = jsAsset;
498         napi_get_undefined(env, &jsContext->error);
499         jsContext->status = true;
500     }
501 }
502 
GetPositionObjectCompleteCallback(napi_env env,napi_status status,FetchFileResultAsyncContext * context)503 static void GetPositionObjectCompleteCallback(napi_env env, napi_status status, FetchFileResultAsyncContext* context)
504 {
505     MediaLibraryTracer tracer;
506     tracer.Start("GetPositionObjectCompleteCallback");
507 
508     CHECK_NULL_PTR_RETURN_VOID(context, "Async context is null");
509 
510     unique_ptr<JSAsyncContextOutput> jsContext = make_unique<JSAsyncContextOutput>();
511     jsContext->status = false;
512 
513     GetNapiResFromAsset(env, context, jsContext);
514 
515     if (context->work != nullptr) {
516         MediaLibraryNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef,
517                                                    context->work, *jsContext);
518     }
519 
520     delete context;
521 }
522 
JSGetFirstObject(napi_env env,napi_callback_info info)523 napi_value FetchFileResultNapi::JSGetFirstObject(napi_env env, napi_callback_info info)
524 {
525     napi_status status;
526     napi_value result = nullptr;
527     const int32_t refCount = 1;
528     napi_value resource = nullptr;
529     size_t argc = ARGS_ONE;
530     napi_value argv[ARGS_ONE] = {0};
531     napi_value thisVar = nullptr;
532 
533     GET_JS_ARGS(env, info, argc, argv, thisVar);
534     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameter");
535     napi_get_undefined(env, &result);
536 
537     unique_ptr<FetchFileResultAsyncContext> asyncContext = make_unique<FetchFileResultAsyncContext>();
538     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&asyncContext->objectInfo));
539     if (status == napi_ok && CheckIfFFRNapiNotEmpty(asyncContext->objectInfo)) {
540         if (argc == ARGS_ONE) {
541             GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
542         }
543 
544         NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
545         NAPI_CREATE_RESOURCE_NAME(env, resource, "JSGetFirstObject", asyncContext);
546 
547         asyncContext->objectPtr = asyncContext->objectInfo->propertyPtr;
548         CHECK_NULL_PTR_RETURN_UNDEFINED(env, asyncContext->objectPtr, result, "propertyPtr is nullptr");
549 
550         status = napi_create_async_work(
551             env, nullptr, resource, [](napi_env env, void *data) {
552                 auto context = static_cast<FetchFileResultAsyncContext*>(data);
553                 context->GetFirstAsset();
554             },
555             reinterpret_cast<napi_async_complete_callback>(GetPositionObjectCompleteCallback),
556             static_cast<void *>(asyncContext.get()), &asyncContext->work);
557         if (status != napi_ok) {
558             napi_get_undefined(env, &result);
559         } else {
560             napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
561             asyncContext.release();
562         }
563     } else {
564         NAPI_ERR_LOG("JSGetFirstObject obj == nullptr, status: %{public}d", status);
565         NAPI_ASSERT(env, false, "JSGetFirstObject obj == nullptr");
566     }
567 
568     return result;
569 }
570 
JSGetNextObject(napi_env env,napi_callback_info info)571 napi_value FetchFileResultNapi::JSGetNextObject(napi_env env, napi_callback_info info)
572 {
573     napi_status status;
574     napi_value result = nullptr;
575     const int32_t refCount = 1;
576     napi_value resource = nullptr;
577     size_t argc = ARGS_ONE;
578     napi_value argv[ARGS_ONE] = {0};
579     napi_value thisVar = nullptr;
580 
581     GET_JS_ARGS(env, info, argc, argv, thisVar);
582     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameter");
583 
584     napi_get_undefined(env, &result);
585     unique_ptr<FetchFileResultAsyncContext> asyncContext = make_unique<FetchFileResultAsyncContext>();
586     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&asyncContext->objectInfo));
587     if (status == napi_ok && CheckIfFFRNapiNotEmpty(asyncContext->objectInfo)) {
588         if (argc == ARGS_ONE) {
589             GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
590         }
591 
592         NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
593         NAPI_CREATE_RESOURCE_NAME(env, resource, "JSGetNextObject", asyncContext);
594 
595         asyncContext->objectPtr = asyncContext->objectInfo->propertyPtr;
596         CHECK_NULL_PTR_RETURN_UNDEFINED(env, asyncContext->objectPtr, result, "propertyPtr is nullptr");
597 
598         status = napi_create_async_work(
599             env, nullptr, resource, [](napi_env env, void *data) {
600                 auto context = static_cast<FetchFileResultAsyncContext*>(data);
601                 context->GetNextObject();
602             },
603             reinterpret_cast<napi_async_complete_callback>(GetPositionObjectCompleteCallback),
604             static_cast<void *>(asyncContext.get()), &asyncContext->work);
605         if (status != napi_ok) {
606             napi_get_undefined(env, &result);
607         } else {
608             napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
609             asyncContext.release();
610         }
611     } else {
612         NAPI_ERR_LOG("JSGetNextObject obj == nullptr, status: %{public}d", status);
613         NAPI_ASSERT(env, false, "JSGetNextObject obj == nullptr");
614     }
615 
616     return result;
617 }
618 
JSGetLastObject(napi_env env,napi_callback_info info)619 napi_value FetchFileResultNapi::JSGetLastObject(napi_env env, napi_callback_info info)
620 {
621     napi_status status;
622     napi_value result = nullptr;
623     const int32_t refCount = 1;
624     napi_value resource = nullptr;
625     size_t argc = ARGS_ONE;
626     napi_value argv[ARGS_ONE] = {0};
627     napi_value thisVar = nullptr;
628 
629     GET_JS_ARGS(env, info, argc, argv, thisVar);
630     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameter");
631 
632     napi_get_undefined(env, &result);
633     unique_ptr<FetchFileResultAsyncContext> asyncContext = make_unique<FetchFileResultAsyncContext>();
634     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&asyncContext->objectInfo));
635     if (status == napi_ok && CheckIfFFRNapiNotEmpty(asyncContext->objectInfo)) {
636         if (argc == ARGS_ONE) {
637             GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
638         }
639 
640         NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
641         NAPI_CREATE_RESOURCE_NAME(env, resource, "JSGetLastObject", asyncContext);
642 
643         asyncContext->objectPtr = asyncContext->objectInfo->propertyPtr;
644         CHECK_NULL_PTR_RETURN_UNDEFINED(env, asyncContext->objectPtr, result, "propertyPtr is nullptr");
645 
646         status = napi_create_async_work(
647             env, nullptr, resource, [](napi_env env, void *data) {
648                 auto context = static_cast<FetchFileResultAsyncContext*>(data);
649                 context->GetLastObject();
650             },
651             reinterpret_cast<napi_async_complete_callback>(GetPositionObjectCompleteCallback),
652             static_cast<void *>(asyncContext.get()), &asyncContext->work);
653         if (status != napi_ok) {
654             napi_get_undefined(env, &result);
655         } else {
656             napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
657             asyncContext.release();
658         }
659     } else {
660         NAPI_ERR_LOG("JSGetLastObject obj == nullptr, status: %{public}d", status);
661         NAPI_ASSERT(env, false, "JSGetLastObject obj == nullptr");
662     }
663 
664     return result;
665 }
666 
JSGetPositionObject(napi_env env,napi_callback_info info)667 napi_value FetchFileResultNapi::JSGetPositionObject(napi_env env, napi_callback_info info)
668 {
669     napi_status status;
670     napi_value result = nullptr;
671     const int32_t refCount = 1;
672     napi_valuetype type = napi_undefined;
673     napi_value resource = nullptr;
674     size_t argc = ARGS_TWO;
675     napi_value argv[ARGS_TWO] = {0};
676     napi_value thisVar = nullptr;
677 
678     MediaLibraryTracer tracer;
679     tracer.Start("JSGetPositionObject");
680 
681     GET_JS_ARGS(env, info, argc, argv, thisVar);
682     NAPI_ASSERT(env, (argc == ARGS_ONE || argc == ARGS_TWO), "requires 2 parameter maximum");
683 
684     napi_get_undefined(env, &result);
685     unique_ptr<FetchFileResultAsyncContext> asyncContext = make_unique<FetchFileResultAsyncContext>();
686     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&asyncContext->objectInfo));
687     if (status == napi_ok && CheckIfFFRNapiNotEmpty(asyncContext->objectInfo)) {
688         // Check the arguments and their types
689         napi_typeof(env, argv[PARAM0], &type);
690         if (type == napi_number) {
691             napi_get_value_int32(env, argv[PARAM0], &(asyncContext->position));
692         } else {
693             NAPI_ERR_LOG("Argument mismatch, type: %{public}d", type);
694             return result;
695         }
696 
697         if (argc == ARGS_TWO) {
698             GET_JS_ASYNC_CB_REF(env, argv[PARAM1], refCount, asyncContext->callbackRef);
699         }
700 
701         NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
702         NAPI_CREATE_RESOURCE_NAME(env, resource, "JSGetPositionObject", asyncContext);
703 
704         asyncContext->objectPtr = asyncContext->objectInfo->propertyPtr;
705         CHECK_NULL_PTR_RETURN_UNDEFINED(env, asyncContext->objectPtr, result, "propertyPtr is nullptr");
706 
707         status = napi_create_async_work(
708             env, nullptr, resource, [](napi_env env, void *data) {
709                 auto context = static_cast<FetchFileResultAsyncContext*>(data);
710                 context->GetObjectAtPosition();
711             },
712             reinterpret_cast<napi_async_complete_callback>(GetPositionObjectCompleteCallback),
713             static_cast<void *>(asyncContext.get()), &asyncContext->work);
714         if (status != napi_ok) {
715             napi_get_undefined(env, &result);
716         } else {
717             napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
718             asyncContext.release();
719         }
720     } else {
721         NAPI_ERR_LOG("JSGetPositionObject obj == nullptr, status: %{public}d", status);
722         NAPI_ASSERT(env, false, "JSGetPositionObject obj == nullptr");
723     }
724 
725     return result;
726 }
727 
GetAsset(napi_env env,vector<std::unique_ptr<FileAsset>> & array,int index)728 static napi_value GetAsset(napi_env env, vector<std::unique_ptr<FileAsset>> &array, int index)
729 {
730     return FileAssetNapi::CreateFileAsset(env, array[index]);
731 }
732 
GetAsset(napi_env env,vector<std::unique_ptr<AlbumAsset>> & array,int index)733 static napi_value GetAsset(napi_env env, vector<std::unique_ptr<AlbumAsset>> &array, int index)
734 {
735     return AlbumNapi::CreateAlbumNapi(env, array[index]);
736 }
737 
GetAsset(napi_env env,vector<std::unique_ptr<PhotoAlbum>> & array,int index)738 static napi_value GetAsset(napi_env env, vector<std::unique_ptr<PhotoAlbum>> &array, int index)
739 {
740     return PhotoAlbumNapi::CreatePhotoAlbumNapi(env, array[index]);
741 }
742 
GetAsset(napi_env env,vector<std::unique_ptr<SmartAlbumAsset>> & array,int index)743 static napi_value GetAsset(napi_env env, vector<std::unique_ptr<SmartAlbumAsset>> &array, int index)
744 {
745     return SmartAlbumNapi::CreateSmartAlbumNapi(env, array[index]);
746 }
747 
748 template<class T>
GetAssetFromArray(napi_env env,FetchFileResultAsyncContext * context,T & array,unique_ptr<JSAsyncContextOutput> & jsContext)749 static void GetAssetFromArray(napi_env env, FetchFileResultAsyncContext* context, T& array,
750     unique_ptr<JSAsyncContextOutput> &jsContext)
751 {
752     napi_value jsFileArray = nullptr;
753     napi_create_array_with_length(env, array.size(), &jsFileArray);
754     napi_value jsFileAsset = nullptr;
755     size_t i = 0;
756     for (i = 0; i < array.size(); i++) {
757         jsFileAsset = GetAsset(env, array, i);
758         if ((jsFileAsset == nullptr) || (napi_set_element(env, jsFileArray, i, jsFileAsset) != napi_ok)) {
759             NAPI_ERR_LOG("Failed to get file asset napi object");
760             napi_get_undefined(env, &jsContext->data);
761             MediaLibraryNapiUtils::CreateNapiErrorObject(env, jsContext->error, ERR_MEM_ALLOCATION,
762                 "Failed to create js object");
763             break;
764         }
765     }
766     if (i == array.size()) {
767         jsContext->data = jsFileArray;
768         napi_get_undefined(env, &jsContext->error);
769         jsContext->status = true;
770     }
771 }
772 
GetAllObjectCompleteCallback(napi_env env,napi_status status,FetchFileResultAsyncContext * context)773 static void GetAllObjectCompleteCallback(napi_env env, napi_status status, FetchFileResultAsyncContext* context)
774 {
775     MediaLibraryTracer tracer;
776     tracer.Start("GetAllObjectCompleteCallback");
777     CHECK_NULL_PTR_RETURN_VOID(context, "Async context is null");
778     unique_ptr<JSAsyncContextOutput> jsContext = make_unique<JSAsyncContextOutput>();
779     jsContext->status = false;
780 
781     switch (context->objectPtr->fetchResType_) {
782         case FetchResType::TYPE_FILE:
783             GetAssetFromArray(env, context, context->fileAssetArray, jsContext);
784             break;
785         case FetchResType::TYPE_ALBUM:
786             GetAssetFromArray(env, context, context->fileAlbumArray, jsContext);
787             break;
788         case FetchResType::TYPE_PHOTOALBUM:
789             GetAssetFromArray(env, context, context->filePhotoAlbumArray, jsContext);
790             break;
791         case FetchResType::TYPE_SMARTALBUM:
792             GetAssetFromArray(env, context, context->fileSmartAlbumArray, jsContext);
793             break;
794         default:
795             NAPI_ERR_LOG("unsupported FetchResType");
796             napi_get_undefined(env, &jsContext->data);
797             MediaLibraryNapiUtils::CreateNapiErrorObject(env, jsContext->error, ERR_INVALID_OUTPUT,
798                 "Failed to obtain fileAsset array from DB");
799     }
800 
801     if (context->work != nullptr) {
802         int64_t start = MediaFileUtils::UTCTimeMilliSeconds();
803         MediaLibraryNapiUtils::InvokeJSAsyncMethod(env, context->deferred, context->callbackRef,
804                                                    context->work, *jsContext);
805         int64_t end = MediaFileUtils::UTCTimeMilliSeconds();
806         int64_t normalTime = 500;
807         if ((long)(end - start) >= normalTime) {
808             NAPI_INFO_LOG("InvokeJSAsync dir cost: %{public}ld", (long)(end - start));
809         }
810     }
811 
812     delete context;
813 }
814 
GetFetchFileResultObject()815 std::shared_ptr<FetchResult<FileAsset>> FetchFileResultNapi::GetFetchFileResultObject()
816 {
817     return propertyPtr->fetchFileResult_;
818 }
819 
GetFetchAlbumResultObject()820 std::shared_ptr<FetchResult<AlbumAsset>> FetchFileResultNapi::GetFetchAlbumResultObject()
821 {
822     return propertyPtr->fetchAlbumResult_;
823 }
824 
GetFetchPhotoAlbumResultObject()825 std::shared_ptr<FetchResult<PhotoAlbum>> FetchFileResultNapi::GetFetchPhotoAlbumResultObject()
826 {
827     return propertyPtr->fetchPhotoAlbumResult_;
828 }
829 
GetFetchSmartAlbumResultObject()830 std::shared_ptr<FetchResult<SmartAlbumAsset>> FetchFileResultNapi::GetFetchSmartAlbumResultObject()
831 {
832     return propertyPtr->fetchSmartAlbumResult_;
833 }
834 
GetAllObjectFromFetchResult(const FetchFileResultAsyncContext & asyncContext)835 void GetAllObjectFromFetchResult(const FetchFileResultAsyncContext &asyncContext)
836 {
837     MediaLibraryTracer tracer;
838     tracer.Start("GetAllObjectFromFetchResult");
839 
840     FetchFileResultAsyncContext *context = const_cast<FetchFileResultAsyncContext *>(&asyncContext);
841     context->GetAllObjectFromFetchResult();
842 }
843 
JSGetAllObject(napi_env env,napi_callback_info info)844 napi_value FetchFileResultNapi::JSGetAllObject(napi_env env, napi_callback_info info)
845 {
846     napi_status status;
847     napi_value result = nullptr;
848     const int32_t refCount = 1;
849     napi_value resource = nullptr;
850     size_t argc = ARGS_ONE;
851     napi_value argv[ARGS_ONE] = {0};
852     napi_value thisVar = nullptr;
853 
854     MediaLibraryTracer tracer;
855     tracer.Start("JSGetAllObject");
856 
857     GET_JS_ARGS(env, info, argc, argv, thisVar);
858     NAPI_ASSERT(env, argc <= ARGS_ONE, "requires 1 parameter maximum");
859 
860     napi_get_undefined(env, &result);
861     unique_ptr<FetchFileResultAsyncContext> asyncContext = make_unique<FetchFileResultAsyncContext>();
862     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&asyncContext->objectInfo));
863     if (status == napi_ok && CheckIfFFRNapiNotEmpty(asyncContext->objectInfo)) {
864         if (argc == ARGS_ONE) {
865             GET_JS_ASYNC_CB_REF(env, argv[PARAM0], refCount, asyncContext->callbackRef);
866         }
867 
868         NAPI_CREATE_PROMISE(env, asyncContext->callbackRef, asyncContext->deferred, result);
869         NAPI_CREATE_RESOURCE_NAME(env, resource, "JSGetAllObject", asyncContext);
870 
871         asyncContext->objectPtr = asyncContext->objectInfo->propertyPtr;
872         CHECK_NULL_PTR_RETURN_UNDEFINED(env, asyncContext->objectPtr, result, "propertyPtr is nullptr");
873 
874         status = napi_create_async_work(
875             env, nullptr, resource, [](napi_env env, void *data) {
876                 auto context = static_cast<FetchFileResultAsyncContext*>(data);
877                 GetAllObjectFromFetchResult(*context);
878             },
879             reinterpret_cast<napi_async_complete_callback>(GetAllObjectCompleteCallback),
880             static_cast<void *>(asyncContext.get()), &asyncContext->work);
881         if (status != napi_ok) {
882             napi_get_undefined(env, &result);
883         } else {
884             napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated);
885             asyncContext.release();
886         }
887     } else {
888         NAPI_ERR_LOG("JSGetAllObject obj == nullptr, status: %{public}d", status);
889         NAPI_ASSERT(env, false, "JSGetAllObject obj == nullptr");
890     }
891 
892     return result;
893 }
894 
JSClose(napi_env env,napi_callback_info info)895 napi_value FetchFileResultNapi::JSClose(napi_env env, napi_callback_info info)
896 {
897     napi_status status;
898     napi_value jsResult = nullptr;
899     FetchFileResultNapi* obj = nullptr;
900     napi_value thisVar = nullptr;
901 
902     MediaLibraryTracer tracer;
903     tracer.Start("JSClose");
904 
905     napi_get_undefined(env, &jsResult);
906     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
907     if (status != napi_ok || thisVar == nullptr) {
908         NAPI_ERR_LOG("Invalid arguments!, status: %{public}d", status);
909         return jsResult;
910     }
911     status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&obj));
912     if ((status == napi_ok) && (obj != nullptr)) {
913         obj->propertyPtr = nullptr;
914     }
915     status = napi_remove_wrap(env, thisVar, reinterpret_cast<void **>(&obj));
916     if ((status == napi_ok) && (obj != nullptr)) {
917         napi_create_int32(env, E_SUCCESS, &jsResult);
918     } else {
919         NAPI_DEBUG_LOG("JSClose obj == nullptr");
920     }
921 
922     NAPI_DEBUG_LOG("JSClose OUT!");
923     return jsResult;
924 }
925 
GetFirstAsset()926 void FetchFileResultAsyncContext::GetFirstAsset()
927 {
928     switch (objectPtr->fetchResType_) {
929         case FetchResType::TYPE_FILE: {
930             fileAsset = objectPtr->fetchFileResult_->GetFirstObject();
931             break;
932         }
933         case FetchResType::TYPE_ALBUM: {
934             albumAsset = objectPtr->fetchAlbumResult_->GetFirstObject();
935             break;
936         }
937         case FetchResType::TYPE_PHOTOALBUM: {
938             photoAlbum = objectPtr->fetchPhotoAlbumResult_->GetFirstObject();
939             break;
940         }
941         case FetchResType::TYPE_SMARTALBUM: {
942             smartAlbumAsset = objectPtr->fetchSmartAlbumResult_->GetFirstObject();
943             break;
944         }
945         default:
946             NAPI_ERR_LOG("unsupported FetchResType");
947             break;
948     }
949 }
950 
GetObjectAtPosition()951 void FetchFileResultAsyncContext::GetObjectAtPosition()
952 {
953     switch (objectPtr->fetchResType_) {
954         case FetchResType::TYPE_FILE: {
955             fileAsset = objectPtr->fetchFileResult_->GetObjectAtPosition(position);
956             break;
957         }
958         case FetchResType::TYPE_ALBUM: {
959             albumAsset = objectPtr->fetchAlbumResult_->GetObjectAtPosition(position);
960             break;
961         }
962         case FetchResType::TYPE_PHOTOALBUM: {
963             photoAlbum = objectPtr->fetchPhotoAlbumResult_->GetObjectAtPosition(position);
964             break;
965         }
966         case FetchResType::TYPE_SMARTALBUM: {
967             smartAlbumAsset = objectPtr->fetchSmartAlbumResult_->GetObjectAtPosition(position);
968             break;
969         }
970         default:
971             NAPI_ERR_LOG("unsupported FetchResType");
972             break;
973     }
974 }
975 
GetLastObject()976 void FetchFileResultAsyncContext::GetLastObject()
977 {
978     switch (objectPtr->fetchResType_) {
979         case FetchResType::TYPE_FILE: {
980             fileAsset = objectPtr->fetchFileResult_->GetLastObject();
981             break;
982         }
983         case FetchResType::TYPE_ALBUM: {
984             albumAsset = objectPtr->fetchAlbumResult_->GetLastObject();
985             break;
986         }
987         case FetchResType::TYPE_PHOTOALBUM: {
988             photoAlbum = objectPtr->fetchPhotoAlbumResult_->GetLastObject();
989             break;
990         }
991         case FetchResType::TYPE_SMARTALBUM: {
992             smartAlbumAsset = objectPtr->fetchSmartAlbumResult_->GetLastObject();
993             break;
994         }
995         default:
996             NAPI_ERR_LOG("unsupported FetchResType");
997             break;
998     }
999 }
1000 
GetNextObject()1001 void FetchFileResultAsyncContext::GetNextObject()
1002 {
1003     switch (objectPtr->fetchResType_) {
1004         case FetchResType::TYPE_FILE: {
1005             fileAsset = objectPtr->fetchFileResult_->GetNextObject();
1006             break;
1007         }
1008         case FetchResType::TYPE_ALBUM: {
1009             albumAsset = objectPtr->fetchAlbumResult_->GetNextObject();
1010             break;
1011         }
1012         case FetchResType::TYPE_PHOTOALBUM: {
1013             photoAlbum = objectPtr->fetchPhotoAlbumResult_->GetNextObject();
1014             break;
1015         }
1016         case FetchResType::TYPE_SMARTALBUM: {
1017             smartAlbumAsset = objectPtr->fetchSmartAlbumResult_->GetNextObject();
1018             break;
1019         }
1020         default:
1021             NAPI_ERR_LOG("unsupported FetchResType");
1022             break;
1023     }
1024 }
1025 
GetAllObjectFromFetchResult()1026 void FetchFileResultAsyncContext::GetAllObjectFromFetchResult()
1027 {
1028     switch (objectPtr->fetchResType_) {
1029         case FetchResType::TYPE_FILE: {
1030             auto fetchResult = objectPtr->fetchFileResult_;
1031             auto file = fetchResult->GetFirstObject();
1032             while (file != nullptr) {
1033                 file->SetUserId(fetchResult->GetUserId());
1034                 fileAssetArray.push_back(move(file));
1035                 file = fetchResult->GetNextObject();
1036             }
1037             break;
1038         }
1039         case FetchResType::TYPE_ALBUM: {
1040             auto fetchResult = objectPtr->fetchAlbumResult_;
1041             auto album = fetchResult->GetFirstObject();
1042             while (album != nullptr) {
1043                 fileAlbumArray.push_back(move(album));
1044                 album = fetchResult->GetNextObject();
1045             }
1046             break;
1047         }
1048         case FetchResType::TYPE_PHOTOALBUM: {
1049             auto fetchResult = objectPtr->fetchPhotoAlbumResult_;
1050             auto photoAlbum = fetchResult->GetFirstObject();
1051             while (photoAlbum != nullptr) {
1052                 photoAlbum->SetUserId(fetchResult->GetUserId());
1053                 filePhotoAlbumArray.push_back(move(photoAlbum));
1054                 photoAlbum = fetchResult->GetNextObject();
1055             }
1056             break;
1057         }
1058         case FetchResType::TYPE_SMARTALBUM: {
1059             auto fetchResult = objectPtr->fetchSmartAlbumResult_;
1060             auto smartAlbum = fetchResult->GetFirstObject();
1061             while (smartAlbum != nullptr) {
1062                 fileSmartAlbumArray.push_back(move(smartAlbum));
1063                 smartAlbum = fetchResult->GetNextObject();
1064             }
1065             break;
1066         }
1067         default:
1068             NAPI_ERR_LOG("unsupported FetchResType");
1069             break;
1070     }
1071 }
1072 
CheckIfPropertyPtrNull()1073 bool FetchFileResultNapi::CheckIfPropertyPtrNull()
1074 {
1075     return propertyPtr == nullptr;
1076 }
1077 } // namespace Media
1078 } // namespace OHOS