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