1 /*
2 * Copyright (C) 2023 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 #include "thumbnail_manager.h"
17
18 #include <memory>
19 #include <mutex>
20 #include <sys/mman.h>
21 #include <sys/stat.h>
22 #include <uuid/uuid.h>
23
24 #include "ashmem.h"
25 #include "image_source.h"
26 #include "image_type.h"
27 #include "js_native_api.h"
28 #include "media_file_uri.h"
29 #include "medialibrary_errno.h"
30 #include "medialibrary_napi_log.h"
31 #include "medialibrary_napi_utils.h"
32 #include "medialibrary_tracer.h"
33 #include "pixel_map.h"
34 #include "pixel_map_napi.h"
35 #include "post_proc.h"
36 #include "securec.h"
37 #include "string_ex.h"
38 #include "thumbnail_const.h"
39 #include "unique_fd.h"
40 #include "userfile_manager_types.h"
41 #include "uv.h"
42 #include "userfile_client.h"
43
44 #ifdef IMAGE_PURGEABLE_PIXELMAP
45 #include "purgeable_pixelmap_builder.h"
46 #endif
47
48 using namespace std;
49 const int UUID_STR_LENGTH = 37;
50
51 namespace OHOS {
52 namespace Media {
53 shared_ptr<ThumbnailManager> ThumbnailManager::instance_ = nullptr;
54 mutex ThumbnailManager::mutex_;
55 bool ThumbnailManager::init_ = false;
56 static constexpr int32_t DEFAULT_FD = -1;
57
ThumbnailRequest(const RequestPhotoParams & params,napi_env env,napi_ref callback)58 ThumbnailRequest::ThumbnailRequest(const RequestPhotoParams ¶ms, napi_env env,
59 napi_ref callback) : callback_(env, callback), requestPhotoType(params.type), uri_(params.uri),
60 path_(params.path), requestSize_(params.size)
61 {
62 }
63
~ThumbnailRequest()64 ThumbnailRequest::~ThumbnailRequest()
65 {
66 }
67
ReleaseCallbackRef()68 void ThumbnailRequest::ReleaseCallbackRef()
69 {
70 std::lock_guard<std::mutex> lock(mutex_);
71 if (callback_.callBackRef_) {
72 napi_delete_reference(callback_.env_, callback_.callBackRef_);
73 callback_.callBackRef_ = nullptr;
74 }
75 }
76
UpdateStatus(ThumbnailStatus status)77 bool ThumbnailRequest::UpdateStatus(ThumbnailStatus status)
78 {
79 std::lock_guard<std::mutex> lock(mutex_);
80 if (status <= status_) {
81 return false;
82 }
83 status_ = status;
84 return true;
85 }
86
GetStatus()87 ThumbnailStatus ThumbnailRequest::GetStatus()
88 {
89 std::lock_guard<std::mutex> lock(mutex_);
90 return status_;
91 }
92
NeedContinue()93 bool ThumbnailRequest::NeedContinue()
94 {
95 return GetStatus() < ThumbnailStatus::THUMB_REMOVE;
96 }
97
IsPhotoSizeThumb(const Size & size)98 static bool IsPhotoSizeThumb(const Size &size)
99 {
100 return ((size.width >= DEFAULT_THUMB_SIZE || size.height >= DEFAULT_THUMB_SIZE) ||
101 (size.width == DEFAULT_MTH_SIZE || size.height == DEFAULT_MTH_SIZE));
102 }
103
NeedFastThumb(const Size & size,RequestPhotoType type)104 static bool NeedFastThumb(const Size &size, RequestPhotoType type)
105 {
106 return IsPhotoSizeThumb(size) && (type != RequestPhotoType::REQUEST_QUALITY_THUMBNAIL);
107 }
108
NeedQualityThumb(const Size & size,RequestPhotoType type)109 static bool NeedQualityThumb(const Size &size, RequestPhotoType type)
110 {
111 return IsPhotoSizeThumb(size) && (type != RequestPhotoType::REQUEST_FAST_THUMBNAIL);
112 }
113
MMapFdPtr(int32_t fd,bool isNeedRelease)114 MMapFdPtr::MMapFdPtr(int32_t fd, bool isNeedRelease)
115 {
116 if (fd < 0) {
117 NAPI_ERR_LOG("Fd is invalid: %{public}d", fd);
118 return;
119 }
120
121 struct stat st;
122 if (fstat(fd, &st) == -1) {
123 NAPI_ERR_LOG("fstat error, errno:%{public}d", errno);
124 return;
125 }
126 size_ = st.st_size;
127
128 // mmap ptr from fd
129 fdPtr_ = mmap(nullptr, size_, PROT_READ, MAP_SHARED, fd, 0);
130 if (fdPtr_ == MAP_FAILED || fdPtr_ == nullptr) {
131 NAPI_ERR_LOG("mmap uniqueFd failed, errno = %{public}d", errno);
132 return;
133 }
134
135 isValid_ = true;
136 isNeedRelease_ = isNeedRelease;
137 }
138
~MMapFdPtr()139 MMapFdPtr::~MMapFdPtr()
140 {
141 // munmap ptr from fd
142 if (isNeedRelease_) {
143 munmap(fdPtr_, size_);
144 }
145 }
146
GetFdPtr()147 void* MMapFdPtr::GetFdPtr()
148 {
149 return fdPtr_;
150 }
151
GetFdSize()152 off_t MMapFdPtr::GetFdSize()
153 {
154 return size_;
155 }
156
IsValid()157 bool MMapFdPtr::IsValid()
158 {
159 return isValid_;
160 }
161
GenerateRequestId()162 static string GenerateRequestId()
163 {
164 uuid_t uuid;
165 uuid_generate(uuid);
166 char str[UUID_STR_LENGTH] = {};
167 uuid_unparse(uuid, str);
168 return str;
169 }
170
GetInstance()171 shared_ptr<ThumbnailManager> ThumbnailManager::GetInstance()
172 {
173 if (instance_ == nullptr) {
174 lock_guard<mutex> lock(mutex_);
175 if (instance_ == nullptr) {
176 instance_ = shared_ptr<ThumbnailManager>(new ThumbnailManager());
177 }
178 }
179
180 return instance_;
181 }
182
Init()183 void ThumbnailManager::Init()
184 {
185 std::lock_guard<std::mutex> lock(mutex_);
186 if (init_) {
187 return;
188 }
189 init_ = true;
190 isThreadRunning_ = true;
191 for (auto i = 0; i < THREAD_NUM; i++) {
192 threads_.emplace_back(bind(&ThumbnailManager::ImageWorker, this, i));
193 threads_[i].detach();
194 }
195 return;
196 }
197
AddPhotoRequest(const RequestPhotoParams & params,napi_env env,napi_ref callback)198 string ThumbnailManager::AddPhotoRequest(const RequestPhotoParams ¶ms, napi_env env, napi_ref callback)
199 {
200 shared_ptr<ThumbnailRequest> request = make_shared<ThumbnailRequest>(params, env, callback);
201 string requestId = GenerateRequestId();
202 request->SetUUID(requestId);
203 if (!thumbRequest_.Insert(requestId, request)) {
204 return "";
205 }
206 // judge from request option
207 if (NeedFastThumb(params.size, params.type)) {
208 AddFastPhotoRequest(request);
209 } else {
210 AddQualityPhotoRequest(request);
211 }
212 return requestId;
213 }
214
RemovePhotoRequest(const string & requestId)215 void ThumbnailManager::RemovePhotoRequest(const string &requestId)
216 {
217 RequestSharedPtr ptr;
218 if (thumbRequest_.Find(requestId, ptr)) {
219 if (ptr == nullptr) {
220 return;
221 }
222 // do not need delete from queue, just update remove status.
223 ptr->UpdateStatus(ThumbnailStatus::THUMB_REMOVE);
224 ptr->ReleaseCallbackRef();
225 }
226 thumbRequest_.Erase(requestId);
227 }
228
~ThumbnailManager()229 ThumbnailManager::~ThumbnailManager()
230 {
231 isThreadRunning_ = false;
232 queueCv_.notify_all();
233 for (auto &thread : threads_) {
234 if (thread.joinable()) {
235 thread.join();
236 }
237 }
238 }
239
SetThreadName(const string & threadName,int num)240 void SetThreadName(const string &threadName, int num)
241 {
242 string name = threadName;
243 name.append(to_string(num));
244 pthread_setname_np(pthread_self(), name.c_str());
245 }
246
AddFastPhotoRequest(const RequestSharedPtr & request)247 void ThumbnailManager::AddFastPhotoRequest(const RequestSharedPtr &request)
248 {
249 request->UpdateStatus(ThumbnailStatus::THUMB_FAST);
250 fastQueue_.Push(request);
251 queueCv_.notify_one();
252 }
253
AddQualityPhotoRequest(const RequestSharedPtr & request)254 void ThumbnailManager::AddQualityPhotoRequest(const RequestSharedPtr &request)
255 {
256 request->UpdateStatus(ThumbnailStatus::THUMB_QUALITY);
257 qualityQueue_.Push(request);
258 queueCv_.notify_one();
259 }
260
GetFastThumbNewSize(const Size & size,Size & newSize)261 static bool GetFastThumbNewSize(const Size &size, Size &newSize)
262 {
263 // if thumbnail size is YEAR SIZE, do not need to request fast thumb
264 // if thumbnail size is MTH SIZE, return YEAR SIZE
265 // if thumbnail size is THUMB SIZE, return MTH SIZE
266 // else return THUMB SIZE
267 if (size.width == DEFAULT_YEAR_SIZE && size.height == DEFAULT_YEAR_SIZE) {
268 newSize.height = DEFAULT_YEAR_SIZE;
269 newSize.width = DEFAULT_YEAR_SIZE;
270 return false;
271 } else if (size.width == DEFAULT_MTH_SIZE && size.height == DEFAULT_MTH_SIZE) {
272 newSize.height = DEFAULT_YEAR_SIZE;
273 newSize.width = DEFAULT_YEAR_SIZE;
274 return true;
275 } else if (size.width <= DEFAULT_THUMB_SIZE && size.height <= DEFAULT_THUMB_SIZE) {
276 newSize.height = DEFAULT_MTH_SIZE;
277 newSize.width = DEFAULT_MTH_SIZE;
278 return true;
279 } else {
280 newSize.height = DEFAULT_THUMB_SIZE;
281 newSize.width = DEFAULT_THUMB_SIZE;
282 return true;
283 }
284 }
285
286
OpenThumbnail(const string & path,ThumbnailType type)287 static int OpenThumbnail(const string &path, ThumbnailType type)
288 {
289 if (!path.empty()) {
290 string sandboxPath = GetSandboxPath(path, type);
291 int fd = -1;
292 if (!sandboxPath.empty()) {
293 fd = open(sandboxPath.c_str(), O_RDONLY);
294 }
295 if (fd > 0) {
296 return fd;
297 }
298 }
299 return E_ERR;
300 }
301
IfSizeEqualsRatio(const Size & imageSize,const Size & targetSize)302 static bool IfSizeEqualsRatio(const Size &imageSize, const Size &targetSize)
303 {
304 if (imageSize.height <= 0 || targetSize.height <= 0) {
305 return false;
306 }
307
308 float imageSizeScale = static_cast<float>(imageSize.width) / static_cast<float>(imageSize.height);
309 float targetSizeScale = static_cast<float>(targetSize.width) / static_cast<float>(targetSize.height);
310 if (imageSizeScale - targetSizeScale > FLOAT_EPSILON || targetSizeScale - imageSizeScale > FLOAT_EPSILON) {
311 return false;
312 } else {
313 return true;
314 }
315 }
316
CreateThumbnailByAshmem(UniqueFd & uniqueFd,const Size & size)317 static PixelMapPtr CreateThumbnailByAshmem(UniqueFd &uniqueFd, const Size &size)
318 {
319 MediaLibraryTracer tracer;
320 tracer.Start("CreateThumbnailByAshmem");
321
322 Media::InitializationOptions option = {
323 .size = size,
324 };
325 PixelMapPtr pixel = Media::PixelMap::Create(option);
326 if (pixel == nullptr) {
327 NAPI_ERR_LOG("Can not create pixel");
328 return nullptr;
329 }
330
331 UniqueFd dupFd = UniqueFd(dup(uniqueFd.Get()));
332 MMapFdPtr mmapFd(dupFd.Get(), false);
333 if (!mmapFd.IsValid()) {
334 NAPI_ERR_LOG("Can not mmap by fd");
335 return nullptr;
336 }
337 auto memSize = static_cast<int32_t>(mmapFd.GetFdSize());
338
339 void* fdPtr = new int32_t();
340 *static_cast<int32_t*>(fdPtr) = dupFd.Release();
341 pixel->SetPixelsAddr(mmapFd.GetFdPtr(), fdPtr, memSize, Media::AllocatorType::SHARE_MEM_ALLOC, nullptr);
342 return pixel;
343 }
344
DecodeThumbnail(const UniqueFd & uniqueFd,const Size & size)345 static PixelMapPtr DecodeThumbnail(const UniqueFd &uniqueFd, const Size &size)
346 {
347 MediaLibraryTracer tracer;
348 tracer.Start("ImageSource::CreateImageSource");
349 SourceOptions opts;
350 uint32_t err = 0;
351 unique_ptr<ImageSource> imageSource = ImageSource::CreateImageSource(uniqueFd.Get(), opts, err);
352 if (imageSource == nullptr) {
353 NAPI_ERR_LOG("CreateImageSource err %{public}d", err);
354 return nullptr;
355 }
356
357 ImageInfo imageInfo;
358 err = imageSource->GetImageInfo(0, imageInfo);
359 if (err != E_OK) {
360 NAPI_ERR_LOG("GetImageInfo err %{public}d", err);
361 return nullptr;
362 }
363
364 bool isEqualsRatio = IfSizeEqualsRatio(imageInfo.size, size);
365 DecodeOptions decodeOpts;
366 decodeOpts.desiredSize = isEqualsRatio ? size : imageInfo.size;
367 decodeOpts.allocatorType = AllocatorType::SHARE_MEM_ALLOC;
368 unique_ptr<PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, err);
369 if (pixelMap == nullptr) {
370 NAPI_ERR_LOG("CreatePixelMap err %{public}d", err);
371 return nullptr;
372 }
373
374 PostProc postProc;
375 if (size.width != DEFAULT_ORIGINAL && !isEqualsRatio && !postProc.CenterScale(size, *pixelMap)) {
376 return nullptr;
377 }
378
379 // Make the ashmem of pixelmap to be purgeable after the operation on ashmem.
380 // And then make the pixelmap subject to PurgeableManager's control.
381 #ifdef IMAGE_PURGEABLE_PIXELMAP
382 PurgeableBuilder::MakePixelMapToBePurgeable(pixelMap, imageSource, decodeOpts, size);
383 #endif
384 return pixelMap;
385 }
386
GetPixelMapFromServer(const string & uriStr,const Size & size,const string & path)387 static int32_t GetPixelMapFromServer(const string &uriStr, const Size &size, const string &path)
388 {
389 string openUriStr = uriStr + "?" + MEDIA_OPERN_KEYWORD + "=" + MEDIA_DATA_DB_THUMBNAIL + "&" +
390 MEDIA_DATA_DB_WIDTH + "=" + to_string(size.width) + "&" + MEDIA_DATA_DB_HEIGHT + "=" +
391 to_string(size.height);
392 if (IsAsciiString(path)) {
393 openUriStr += "&" + THUMBNAIL_PATH + "=" + path;
394 }
395 Uri openUri(openUriStr);
396 return UserFileClient::OpenFile(openUri, "R");
397 }
398
QueryThumbnail(const string & uriStr,const Size & size,const string & path)399 unique_ptr<PixelMap> ThumbnailManager::QueryThumbnail(const string &uriStr, const Size &size, const string &path)
400 {
401 MediaLibraryTracer tracer;
402 tracer.Start("QueryThumbnail uri:" + uriStr);
403 tracer.Start("DataShare::OpenFile");
404 ThumbnailType thumbType = GetThumbType(size.width, size.height);
405 if (MediaFileUri::GetMediaTypeFromUri(uriStr) == MediaType::MEDIA_TYPE_AUDIO &&
406 (thumbType == ThumbnailType::MTH || thumbType == ThumbnailType::YEAR)) {
407 thumbType = ThumbnailType::THUMB;
408 }
409 UniqueFd uniqueFd(OpenThumbnail(path, thumbType));
410 if (uniqueFd.Get() == E_ERR) {
411 uniqueFd = UniqueFd(GetPixelMapFromServer(uriStr, size, path));
412 if (uniqueFd.Get() < 0) {
413 NAPI_ERR_LOG("queryThumb is null, errCode is %{public}d", uniqueFd.Get());
414 return nullptr;
415 }
416 return DecodeThumbnail(uniqueFd, size);
417 }
418 tracer.Finish();
419 if (thumbType == ThumbnailType::MTH || thumbType == ThumbnailType::YEAR) {
420 return CreateThumbnailByAshmem(uniqueFd, size);
421 } else {
422 return DecodeThumbnail(uniqueFd, size);
423 }
424 }
425
DeleteRequestIdFromMap(const string & requestId)426 void ThumbnailManager::DeleteRequestIdFromMap(const string &requestId)
427 {
428 thumbRequest_.Erase(requestId);
429 }
430
RequestFastImage(const RequestSharedPtr & request)431 bool ThumbnailManager::RequestFastImage(const RequestSharedPtr &request)
432 {
433 MediaLibraryTracer tracer;
434 tracer.Start("ThumbnailManager::RequestFastImage");
435 request->SetFd(DEFAULT_FD);
436 Size fastSize;
437 if (!GetFastThumbNewSize(request->GetRequestSize(), fastSize)) {
438 return false;
439 }
440 UniqueFd uniqueFd(OpenThumbnail(request->GetPath(), GetThumbType(fastSize.width, fastSize.height)));
441 if (uniqueFd.Get() < 0) {
442 // Can not get fast image in sandbox
443 int32_t outFd = GetPixelMapFromServer(request->GetUri(), request->GetRequestSize(), request->GetPath());
444 if (outFd <= 0) {
445 NAPI_ERR_LOG("Can not get thumbnail from server, uri=%{private}s", request->GetUri().c_str());
446 request->error = E_FAIL;
447 return false;
448 }
449 request->SetFd(outFd);
450 }
451
452 ThumbnailType thumbType = GetThumbType(fastSize.width, fastSize.height);
453 PixelMapPtr pixelMap = nullptr;
454 if (request->GetFd().Get() == DEFAULT_FD &&
455 (thumbType == ThumbnailType::MTH || thumbType == ThumbnailType::YEAR)) {
456 pixelMap = CreateThumbnailByAshmem(uniqueFd, fastSize);
457 } else {
458 pixelMap = DecodeThumbnail(request->GetFd(), fastSize);
459 }
460 if (pixelMap == nullptr) {
461 request->error = E_FAIL;
462 return false;
463 }
464 request->SetFastPixelMap(move(pixelMap));
465 return true;
466 }
467
DealWithFastRequest(const RequestSharedPtr & request)468 void ThumbnailManager::DealWithFastRequest(const RequestSharedPtr &request)
469 {
470 MediaLibraryTracer tracer;
471 tracer.Start("ThumbnailManager::DealWithFastRequest");
472
473 if (request == nullptr) {
474 return;
475 }
476
477 if (!RequestFastImage(request) && request->error != E_FAIL) {
478 // when local pixelmap not exit, must add QualityThread
479 AddQualityPhotoRequest(request);
480 return;
481 }
482
483 // callback
484 NotifyImage(request);
485 }
486
DealWithQualityRequest(const RequestSharedPtr & request)487 void ThumbnailManager::DealWithQualityRequest(const RequestSharedPtr &request)
488 {
489 MediaLibraryTracer tracer;
490 tracer.Start("ThumbnailManager::DealWithQualityRequest");
491
492 unique_ptr<PixelMap> pixelMapPtr = nullptr;
493 if (request->GetFd().Get() > 0) {
494 pixelMapPtr = DecodeThumbnail(request->GetFd(), request->GetRequestSize());
495 } else {
496 pixelMapPtr = QueryThumbnail(request->GetUri(), request->GetRequestSize(), request->GetPath());
497 }
498
499 if (pixelMapPtr == nullptr) {
500 NAPI_ERR_LOG("Can not get pixelMap");
501 request->error = E_FAIL;
502 }
503 request->SetPixelMap(move(pixelMapPtr));
504
505 // callback
506 NotifyImage(request);
507 }
508
ImageWorker(int num)509 void ThumbnailManager::ImageWorker(int num)
510 {
511 SetThreadName("ImageWorker", num);
512 while (true) {
513 if (!isThreadRunning_) {
514 return;
515 }
516 if (!fastQueue_.Empty()) {
517 RequestSharedPtr request;
518 if (fastQueue_.Pop(request) && request->NeedContinue()) {
519 DealWithFastRequest(request);
520 }
521 } else if (!qualityQueue_.Empty()) {
522 RequestSharedPtr request;
523 if (qualityQueue_.Pop(request) && request->NeedContinue()) {
524 DealWithQualityRequest(request);
525 }
526 } else {
527 std::unique_lock<std::mutex> lock(queueLock_);
528 queueCv_.wait(lock, [this]() {
529 return !isThreadRunning_ || !(qualityQueue_.Empty() && fastQueue_.Empty());
530 });
531 }
532 }
533 }
534
HandlePixelCallback(const RequestSharedPtr & request)535 static void HandlePixelCallback(const RequestSharedPtr &request)
536 {
537 napi_env env = request->callback_.env_;
538 napi_value jsCallback = nullptr;
539 napi_status status = napi_get_reference_value(env, request->callback_.callBackRef_, &jsCallback);
540 if (status != napi_ok) {
541 NAPI_ERR_LOG("Create reference fail, status: %{public}d", status);
542 return;
543 }
544
545 napi_value retVal = nullptr;
546 napi_value result[ARGS_TWO];
547 if (request->GetStatus() == ThumbnailStatus::THUMB_REMOVE) {
548 return;
549 }
550
551 if (request->error == E_FAIL) {
552 int32_t errorNum = MediaLibraryNapiUtils::TransErrorCode("requestPhoto", request->error);
553 MediaLibraryNapiUtils::CreateNapiErrorObject(env, result[PARAM0], errorNum,
554 "Failed to request Photo");
555 } else {
556 result[PARAM0] = nullptr;
557 }
558 if (request->GetStatus() == ThumbnailStatus::THUMB_FAST) {
559 result[PARAM1] = Media::PixelMapNapi::CreatePixelMap(env,
560 shared_ptr<PixelMap>(request->GetFastPixelMap()));
561 } else {
562 result[PARAM1] = Media::PixelMapNapi::CreatePixelMap(env,
563 shared_ptr<PixelMap>(request->GetPixelMap()));
564 }
565
566 status = napi_call_function(env, nullptr, jsCallback, ARGS_TWO, result, &retVal);
567 if (status != napi_ok) {
568 NAPI_ERR_LOG("CallJs napi_call_function fail, status: %{public}d", status);
569 return;
570 }
571 }
572
UvJsExecute(uv_work_t * work)573 static void UvJsExecute(uv_work_t *work)
574 {
575 // js thread
576 if (work == nullptr) {
577 return;
578 }
579
580 ThumnailUv *uvMsg = reinterpret_cast<ThumnailUv *>(work->data);
581 do {
582 if (uvMsg == nullptr || uvMsg->request_ == nullptr) {
583 break;
584 }
585 napi_env env = uvMsg->request_->callback_.env_;
586 if (!uvMsg->request_->NeedContinue()) {
587 break;
588 }
589 NapiScopeHandler scopeHandler(env);
590 if (!scopeHandler.IsValid()) {
591 break;
592 }
593 HandlePixelCallback(uvMsg->request_);
594 } while (0);
595 if (uvMsg->manager_ == nullptr) {
596 return;
597 }
598 if (uvMsg->request_->GetStatus() == ThumbnailStatus::THUMB_FAST &&
599 NeedQualityThumb(uvMsg->request_->GetRequestSize(), uvMsg->request_->requestPhotoType)) {
600 uvMsg->manager_->AddQualityPhotoRequest(uvMsg->request_);
601 } else {
602 uvMsg->manager_->DeleteRequestIdFromMap(uvMsg->request_->GetUUID());
603 uvMsg->request_->ReleaseCallbackRef();
604 }
605
606 delete uvMsg;
607 delete work;
608 }
609
NotifyImage(const RequestSharedPtr & request)610 void ThumbnailManager::NotifyImage(const RequestSharedPtr &request)
611 {
612 MediaLibraryTracer tracer;
613 tracer.Start("ThumbnailManager::NotifyImage");
614
615 if (!request->NeedContinue()) {
616 DeleteRequestIdFromMap(request->GetUUID());
617 return;
618 }
619
620 uv_loop_s *loop = nullptr;
621 napi_get_uv_event_loop(request->callback_.env_, &loop);
622 if (loop == nullptr) {
623 DeleteRequestIdFromMap(request->GetUUID());
624 return;
625 }
626
627 uv_work_t *work = new (nothrow) uv_work_t;
628 if (work == nullptr) {
629 DeleteRequestIdFromMap(request->GetUUID());
630 return;
631 }
632
633 ThumnailUv *msg = new (nothrow) ThumnailUv(request, this);
634 if (msg == nullptr) {
635 delete work;
636 DeleteRequestIdFromMap(request->GetUUID());
637 return;
638 }
639
640 work->data = reinterpret_cast<void *>(msg);
641 int ret = uv_queue_work(loop, work, [](uv_work_t *w) {}, [](uv_work_t *w, int s) {
642 UvJsExecute(w);
643 });
644 if (ret != 0) {
645 NAPI_ERR_LOG("Failed to execute libuv work queue, ret: %{public}d", ret);
646 delete msg;
647 delete work;
648 return;
649 }
650 return;
651 }
652 }
653 }
654