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 #ifndef INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_CLASS_H
17 #define INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_CLASS_H
18 
19 #include "header.h"
20 
21 #include <map>
22 
23 #include "app_log_wrapper.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace LIBZIP {
28 class NapiClass final {
29 public:
30     NapiClass(const NapiClass &) = delete;
31     NapiClass &operator=(const NapiClass &) = delete;
32     static NapiClass &GetInstance();
33 
34     static std::tuple<bool, napi_value> DefineClass(napi_env env, const std::string &className,
35         napi_callback constructor, std::vector<napi_property_descriptor> &&properties);
36     static bool SaveClass(napi_env env, const std::string &className, napi_value exClass);
37     static napi_value InstantiateClass(napi_env env, const std::string &className, const std::vector<napi_value> &args);
38 
39     template <class T>
GetEntityOf(napi_env env,napi_value objStat)40     static T *GetEntityOf(napi_env env, napi_value objStat)
41     {
42         if (!env || !objStat) {
43             APP_LOGE("Empty input: env %d, obj %d", env == nullptr, objStat == nullptr);
44             return nullptr;
45         }
46         T *t = nullptr;
47         napi_status status = napi_unwrap(env, objStat, (void **)&t);
48         if (status != napi_ok) {
49             APP_LOGE("Cannot umwarp for pointer: %d", status);
50             return nullptr;
51         }
52         return t;
53     }
54 
55     template <class T>
SetEntityFor(napi_env env,napi_value obj,std::unique_ptr<T> entity)56     static bool SetEntityFor(napi_env env, napi_value obj, std::unique_ptr<T> entity)
57     {
58         napi_status status = napi_wrap(
59             env,
60             obj,
61             entity.get(),
62             [](napi_env env, void *data, void *hint) {
63                 auto entity = static_cast<T *>(data);
64                 delete entity;
65             },
66             nullptr,
67             nullptr);
68         entity.release();
69         return status == napi_ok;
70     }
71 
72     template <class T>
RemoveEntityOfFinal(napi_env env,napi_value objStat)73     static T *RemoveEntityOfFinal(napi_env env, napi_value objStat)
74     {
75         if (!env || !objStat) {
76             APP_LOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr);
77             return nullptr;
78         }
79         T *t = nullptr;
80         napi_status status = napi_remove_wrap(env, objStat, (void **)&t);
81         if (status != napi_ok) {
82             APP_LOGD("Cannot umwrap for pointer: %d", status);
83             return nullptr;
84         }
85         return t;
86     }
87 
88 private:
89     NapiClass() = default;
90     ~NapiClass() = default;
91     std::map<std::string, napi_ref> exClassMap;
92     std::mutex exClassMapLock;
93 };
94 }  // namespace LIBZIP
95 }  // namespace AppExecFwk
96 }  // namespace OHOS
97 #endif  // INTERFACES_KITS_JS_ZIP_NAPI_COMMON_NAPI_CLASS_H