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 "napi/native_api.h"
16
17 #include "hilog_wrapper.h"
18 #include "js_runtime_utils.h"
19 #include "napi/native_node_api.h"
20 #include "js_form_provider.h"
21
22 EXTERN_C_START
23 using namespace OHOS::AbilityRuntime;
24
JsProviderInit(NativeEngine * engine,NativeValue * exports)25 static NativeValue* JsProviderInit(NativeEngine* engine, NativeValue* exports)
26 {
27 HILOG_INFO("JsProviderInit is called");
28 if (engine == nullptr || exports == nullptr) {
29 HILOG_ERROR("Invalid input parameters");
30 return nullptr;
31 }
32
33 NativeObject* object = ConvertNativeValueTo<NativeObject>(exports);
34 if (object == nullptr) {
35 HILOG_ERROR("object is nullptr");
36 return nullptr;
37 }
38
39 std::unique_ptr<JsFormProvider> jsFormProvider = std::make_unique<JsFormProvider>();
40 object->SetNativePointer(jsFormProvider.release(), JsFormProvider::Finalizer, nullptr);
41
42 const char *moduleName = "JsFormProvider";
43 BindNativeFunction(*engine, *object, "getFormsInfo", moduleName, JsFormProvider::GetFormsInfo);
44 BindNativeFunction(*engine, *object, "setFormNextRefreshTime", moduleName, JsFormProvider::SetFormNextRefreshTime);
45 BindNativeFunction(*engine, *object, "updateForm", moduleName, JsFormProvider::UpdateForm);
46 BindNativeFunction(*engine, *object, "requestPublishForm", moduleName, JsFormProvider::RequestPublishForm);
47 BindNativeFunction(*engine, *object, "isRequestPublishFormSupported", moduleName,
48 JsFormProvider::IsRequestPublishFormSupported);
49 return exports;
50 }
51
52 /**
53 * @brief For N-API modules registration
54 *
55 * @param[in] env The environment that the Node-API call is invoked under
56 * @param[in] exports An empty object via the exports parameter as a convenience
57 *
58 * @return The return value from Init is treated as the exports object for the module
59 */
Init(napi_env env,napi_value exports)60 static napi_value Init(napi_env env, napi_value exports)
61 {
62 return reinterpret_cast<napi_value>(JsProviderInit(reinterpret_cast<NativeEngine*>(env),
63 reinterpret_cast<NativeValue*>(exports)));
64 }
65
66 EXTERN_C_END
67
68 // Define a Node-API module.
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 = "app.form.formProvider",
75 .nm_priv = nullptr,
76 .reserved = { nullptr }
77 };
78
79 // Registers a Node-API module.
RegisterModule(void)80 extern "C" __attribute__((constructor)) void RegisterModule(void)
81 {
82 napi_module_register(&_module);
83 }