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 #include <pthread.h>
16 #include <cstdio>
17 #include <cstring>
18 #include <unistd.h>
19
20 #include "app_log_wrapper.h"
21 #include "package.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "js_runtime_utils.h"
25 #include "napi/native_node_api.h"
26
27 using namespace OHOS::AbilityRuntime;
28 namespace OHOS {
29 namespace AppExecFwk {
30 EXTERN_C_START
31 /*
32 * function for module exports
33 */
34
JsPackageInit(NativeEngine * engine,NativeValue * exports)35 static NativeValue* JsPackageInit(NativeEngine *engine, NativeValue *exports)
36 {
37 APP_LOGE("JsPackageInit is called");
38 if (engine == nullptr || exports == nullptr) {
39 APP_LOGE("Invalid input parameters");
40 return nullptr;
41 }
42
43 NativeObject* object = OHOS::AbilityRuntime::ConvertNativeValueTo<NativeObject>(exports);
44 if (object == nullptr) {
45 APP_LOGE("object is nullptr");
46 return nullptr;
47 }
48
49 std::unique_ptr<JsPackage> jsPackage = std::make_unique<JsPackage>();
50 object->SetNativePointer(jsPackage.release(), JsPackage::Finalizer, nullptr);
51
52 const char *moduleName = "JsPackage";
53 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "hasInstalled", moduleName, JsPackage::HasInstalled);
54
55 return exports;
56 }
57
Init(napi_env env,napi_value exports)58 static napi_value Init(napi_env env, napi_value exports)
59 {
60 HILOG_INFO("napi_moudule Init start ...");
61 return reinterpret_cast<napi_value>(
62 JsPackageInit(reinterpret_cast<NativeEngine*>(env), reinterpret_cast<NativeValue*>(exports)));
63 }
64 EXTERN_C_END
65
66 /*
67 * Module define
68 */
69 static napi_module _module = {
70 .nm_version = 1,
71 .nm_flags = 0,
72 .nm_filename = nullptr,
73 .nm_register_func = Init,
74 .nm_modname = "package",
75 .nm_priv = ((void *)0),
76 .reserved = {0}
77 };
78 /*
79 * Module register function
80 */
RegisterModule(void)81 extern "C" __attribute__((constructor)) void RegisterModule(void)
82 {
83 napi_module_register(&_module);
84 }
85 } // namespace AppExecFwk
86 } // namespace OHOS