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