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 #include "photo_proxy_napi.h"
17 
18 #include "medialibrary_napi_log.h"
19 #include "napi_error.h"
20 
21 namespace OHOS {
22 namespace Media {
23 thread_local napi_ref PhotoProxyNapi::sConstructor_ = nullptr;
24 thread_local sptr<PhotoProxy> PhotoProxyNapi::sPhotoProxy_ = nullptr;
PhotoProxyNapi()25 PhotoProxyNapi::PhotoProxyNapi() : env_(nullptr), wrapper_(nullptr)
26 {
27 }
28 
~PhotoProxyNapi()29 PhotoProxyNapi::~PhotoProxyNapi()
30 {
31     NAPI_DEBUG_LOG("~PhotoProxyNapi is called");
32     if (wrapper_ != nullptr) {
33         napi_delete_reference(env_, wrapper_);
34     }
35     if (photoProxy_) {
36         photoProxy_ = nullptr;
37     }
38 }
39 
40 // Constructor callback
PhotoProxyNapiConstructor(napi_env env,napi_callback_info info)41 napi_value PhotoProxyNapi::PhotoProxyNapiConstructor(napi_env env, napi_callback_info info)
42 {
43     NAPI_DEBUG_LOG("PhotoProxyNapiConstructor is called");
44     napi_status status;
45     napi_value result = nullptr;
46     napi_value thisVar = nullptr;
47     napi_get_undefined(env, &result);
48     GET_JS_OBJ_WITH_ZERO_ARGS(env, info, status, thisVar);
49     if (status == napi_ok && thisVar != nullptr) {
50         std::unique_ptr<PhotoProxyNapi> obj = std::make_unique<PhotoProxyNapi>();
51         obj->env_ = env;
52         obj->photoProxy_ = sPhotoProxy_;
53         status = napi_wrap(env, thisVar, reinterpret_cast<void*>(obj.get()),
54                            PhotoProxyNapi::PhotoProxyNapiDestructor, nullptr, nullptr);
55         if (status == napi_ok) {
56             obj.release();
57             return thisVar;
58         } else {
59             NAPI_ERR_LOG("Failure wrapping js to native napi");
60         }
61     }
62     NAPI_ERR_LOG("PhotoProxyNapiConstructor call Failed!");
63     return result;
64 }
65 
PhotoProxyNapiDestructor(napi_env env,void * nativeObject,void * finalize_hint)66 void PhotoProxyNapi::PhotoProxyNapiDestructor(napi_env env, void* nativeObject, void* finalize_hint)
67 {
68     NAPI_DEBUG_LOG("PhotoProxyNapiDestructor is called");
69     PhotoProxyNapi* deferredPhotoProxy = reinterpret_cast<PhotoProxyNapi*>(nativeObject);
70     if (deferredPhotoProxy != nullptr) {
71         delete deferredPhotoProxy;
72     }
73 }
74 
Init(napi_env env,napi_value exports)75 napi_value PhotoProxyNapi::Init(napi_env env, napi_value exports)
76 {
77     NAPI_DEBUG_LOG("Init is called");
78     napi_status status;
79     napi_value ctorObj;
80     int32_t refCount = 1;
81 
82     status = napi_define_class(env, PHOTO_PROXY_NAPI_CLASS_NAME, NAPI_AUTO_LENGTH,
83         PhotoProxyNapiConstructor, nullptr, 0, nullptr, &ctorObj);
84     if (status == napi_ok) {
85         if (napi_create_reference(env, ctorObj, refCount, &sConstructor_) == napi_ok) {
86             status = napi_set_named_property(env, exports, PHOTO_PROXY_NAPI_CLASS_NAME, ctorObj);
87             if (status == napi_ok) {
88                 return exports;
89             }
90         }
91     }
92     NAPI_ERR_LOG("Init call Failed!");
93     return nullptr;
94 }
95 } // Media
96 } // OHOS