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 "fms_log_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(napi_env env,napi_value exports)30 static napi_value NapiFormHostInit(napi_env env, napi_value exports)
31 {
32 HILOG_DEBUG("call");
33 if (env == nullptr || exports == nullptr) {
34 HILOG_ERROR("Invalid input parameters");
35 return nullptr;
36 }
37
38 std::unique_ptr<NapiFormHost> napiFormHost = std::make_unique<NapiFormHost>();
39 napi_wrap(env, exports, napiFormHost.release(), NapiFormHost::Finalizer, nullptr, nullptr);
40
41 const char *moduleName = "NapiFormHost";
42 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "shareForm", moduleName, NapiFormHost::ShareForm);
43 OHOS::AbilityRuntime::BindNativeFunction(
44 env, exports, "disableFormsUpdate", moduleName, NapiFormHost::DisableFormsUpdate);
45 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "isSystemReady", moduleName, NapiFormHost::IsSystemReady);
46 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "deleteForm", moduleName, NapiFormHost::DeleteForm);
47 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "releaseForm", moduleName, NapiFormHost::ReleaseForm);
48 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "requestForm", moduleName, NapiFormHost::RequestForm);
49 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "castTempForm", moduleName, NapiFormHost::CastTempForm);
50 OHOS::AbilityRuntime::BindNativeFunction(
51 env, exports, "getAllFormsInfo", moduleName, NapiFormHost::GetAllFormsInfo);
52 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "getFormsInfo", moduleName, NapiFormHost::GetFormsInfo);
53 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "enableFormsUpdate", moduleName,
54 NapiFormHost::EnableFormsUpdate);
55 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "notifyFormsPrivacyProtected",
56 moduleName, NapiFormHost::NotifyFormsPrivacyProtected);
57 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "notifyVisibleForms",
58 moduleName, NapiFormHost::NotifyVisibleForms);
59 OHOS::AbilityRuntime::BindNativeFunction(env, exports, "notifyInvisibleForms",
60 moduleName, NapiFormHost::NotifyInVisibleForms);
61 OHOS::AbilityRuntime::BindNativeFunction(
62 env, exports, "deleteInvalidForms", moduleName, NapiFormHost::DeleteInvalidForms);
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("Init start");
78 napi_property_descriptor properties[] = {
79 DECLARE_NAPI_FUNCTION("acquireFormState", NAPI_AcquireFormState),
80 DECLARE_NAPI_FUNCTION("on", NAPI_RegisterFormUninstallObserver),
81 DECLARE_NAPI_FUNCTION("off", NAPI_UnregisterFormUninstallObserver),
82 DECLARE_NAPI_FUNCTION("notifyFormsVisible", NAPI_NotifyFormsVisible),
83 DECLARE_NAPI_FUNCTION("notifyFormsEnableUpdate", NAPI_NotifyFormsEnableUpdate),
84 };
85 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(properties) / sizeof(properties[0]), properties));
86 HILOG_INFO("Init end");
87
88 return NapiFormHostInit(env, exports);
89 }
90
91 EXTERN_C_END
92
93 // Define a Node-API module.
94 static napi_module _module = {
95 .nm_version = 1,
96 .nm_flags = 0,
97 .nm_filename = nullptr,
98 .nm_register_func = Init,
99 .nm_modname = "application.formHost",
100 .nm_priv = ((void *)0),
101 .reserved = {0}
102 };
103
104 // Registers a Node-API module.
RegisterModule(void)105 extern "C" __attribute__((constructor)) void RegisterModule(void)
106 {
107 napi_module_register(&_module);
108 }