1 /*
2 * Copyright (c) 2021-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 "napi/native_api.h"
16
17 #include <cstdio>
18 #include <cstring>
19 #include <pthread.h>
20 #include <unistd.h>
21
22 #include "hilog_wrapper.h"
23 #include "js_runtime_utils.h"
24 #include "napi/native_node_api.h"
25 #include "napi_form_host.h"
26
27 EXTERN_C_START
28 using namespace OHOS::AbilityRuntime;
29
NapiFormHostInit(NativeEngine * engine,NativeValue * exports)30 static NativeValue* NapiFormHostInit(NativeEngine *engine, NativeValue *exports)
31 {
32 HILOG_DEBUG("NapiFormHostInit is called");
33 if (engine == nullptr || exports == nullptr) {
34 HILOG_ERROR("Invalid input parameters");
35 return nullptr;
36 }
37
38 NativeObject *object = OHOS::AbilityRuntime::ConvertNativeValueTo<NativeObject>(exports);
39 if (object == nullptr) {
40 HILOG_ERROR("object is nullptr");
41 return nullptr;
42 }
43
44 std::unique_ptr<NapiFormHost> napiFormHost = std::make_unique<NapiFormHost>();
45 object->SetNativePointer(napiFormHost.release(), NapiFormHost::Finalizer, nullptr);
46
47 const char *moduleName = "NapiFormHost";
48 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "shareForm", moduleName, NapiFormHost::ShareForm);
49 OHOS::AbilityRuntime::BindNativeFunction(
50 *engine, *object, "disableFormsUpdate", moduleName, NapiFormHost::DisableFormsUpdate);
51 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "isSystemReady", moduleName, NapiFormHost::IsSystemReady);
52 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "deleteForm", moduleName, NapiFormHost::DeleteForm);
53 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "releaseForm", moduleName, NapiFormHost::ReleaseForm);
54 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "requestForm", moduleName, NapiFormHost::RequestForm);
55 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "castTempForm", moduleName, NapiFormHost::CastTempForm);
56 OHOS::AbilityRuntime::BindNativeFunction(
57 *engine, *object, "getAllFormsInfo", moduleName, NapiFormHost::GetAllFormsInfo);
58 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "getFormsInfo", moduleName, NapiFormHost::GetFormsInfo);
59 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "enableFormsUpdate", moduleName,
60 NapiFormHost::EnableFormsUpdate);
61 OHOS::AbilityRuntime::BindNativeFunction(*engine, *object, "notifyFormsPrivacyProtected",
62 moduleName, NapiFormHost::NotifyFormsPrivacyProtected);
63
64 return exports;
65 }
66
67 /**
68 * @brief For N-API modules registration
69 *
70 * @param[in] env The environment that the Node-API call is invoked under
71 * @param[in] exports An empty object via the exports parameter as a convenience
72 *
73 * @return The return value from Init is treated as the exports object for the module
74 */
Init(napi_env env,napi_value exports)75 static napi_value Init(napi_env env, napi_value exports)
76 {
77 HILOG_INFO("napi_module Init start...");
78 napi_property_descriptor properties[] = {
79 DECLARE_NAPI_FUNCTION("notifyVisibleForms", NAPI_NotifyVisibleForms),
80 DECLARE_NAPI_FUNCTION("notifyInvisibleForms", NAPI_NotifyInvisibleForms),
81 DECLARE_NAPI_FUNCTION("deleteInvalidForms", NAPI_DeleteInvalidForms),
82 DECLARE_NAPI_FUNCTION("acquireFormState", NAPI_AcquireFormState),
83 DECLARE_NAPI_FUNCTION("on", NAPI_RegisterFormUninstallObserver),
84 DECLARE_NAPI_FUNCTION("off", NAPI_UnregisterFormUninstallObserver),
85 DECLARE_NAPI_FUNCTION("notifyFormsVisible", NAPI_NotifyFormsVisible),
86 DECLARE_NAPI_FUNCTION("notifyFormsEnableUpdate", NAPI_NotifyFormsEnableUpdate),
87 };
88 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties));
89 HILOG_INFO("napi_module Init end...");
90
91 return reinterpret_cast<napi_value>(NapiFormHostInit(reinterpret_cast<NativeEngine*>(env),
92 reinterpret_cast<NativeValue*>(exports)));
93 }
94
95 EXTERN_C_END
96
97 // Define a Node-API module.
98 static napi_module _module = {
99 .nm_version = 1,
100 .nm_flags = 0,
101 .nm_filename = nullptr,
102 .nm_register_func = Init,
103 .nm_modname = "application.formHost",
104 .nm_priv = ((void *)0),
105 .reserved = {0}
106 };
107
108 // Registers a Node-API module.
RegisterModule(void)109 extern "C" __attribute__((constructor)) void RegisterModule(void)
110 {
111 napi_module_register(&_module);
112 }