1 /*
2 * Copyright (C) 2024 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 #include "js_prompt_controller.h"
17 #include "prompt_controller.h"
18
19 #include "interfaces/napi/kits/utils/napi_utils.h"
20 #include "napi/native_api.h"
21 #include "napi/native_node_api.h"
22
23 #include "base/log/log.h"
24 #include "base/utils/utils.h"
25
26 namespace OHOS::Ace::Napi {
27
CloseDialog(napi_env env,napi_callback_info info)28 napi_value JSPromptController::CloseDialog(napi_env env, napi_callback_info info)
29 {
30 size_t argc = 1;
31 napi_value argv[1] = { nullptr };
32 napi_value thisArg = nullptr;
33 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, nullptr));
34 if (argc < 1) {
35 NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID);
36 return nullptr;
37 }
38
39 PromptDialogController* controller = nullptr;
40 napi_status status = napi_unwrap(env, argv[0], reinterpret_cast<void**>(&controller));
41 if (status != napi_ok || !controller) {
42 LOGE("Failed to unwrap PromptDialogController.");
43 return nullptr;
44 }
45 controller->Close();
46 return thisArg;
47 }
48
DialogControllerDestructor(napi_env env,void * data,void * hint)49 void JSPromptController::DialogControllerDestructor(napi_env env, void* data, void* hint)
50 {
51 PromptDialogController* controller = reinterpret_cast<PromptDialogController*>(data);
52 CHECK_NULL_VOID(controller);
53 delete controller;
54 controller = nullptr;
55 }
56
DialogControllerConstructor(napi_env env,napi_callback_info info)57 napi_value JSPromptController::DialogControllerConstructor(napi_env env, napi_callback_info info)
58 {
59 size_t argc = 1;
60 napi_value argv[1] = { nullptr };
61 napi_value thisArg = nullptr;
62 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, nullptr));
63 if (argc < 1) {
64 NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID);
65 return nullptr;
66 }
67
68 PromptDialogController* controller = new (std::nothrow) PromptDialogController();
69 napi_status status = napi_wrap(env, argv[0], controller, DialogControllerDestructor, nullptr, nullptr);
70 if (status != napi_ok) {
71 LOGE("Failed to wrap PromptDialogController.");
72 return nullptr;
73 }
74 return thisArg;
75 }
76
Define(napi_env env,napi_value exports)77 napi_status JSPromptController::Define(napi_env env, napi_value exports)
78 {
79 napi_value promptController = nullptr;
80 napi_create_object(env, &promptController);
81
82 napi_value bindDialogFunc = nullptr;
83 napi_create_function(env, "bindDialog", NAPI_AUTO_LENGTH, DialogControllerConstructor, nullptr, &bindDialogFunc);
84 napi_set_named_property(env, promptController, "bindDialog", bindDialogFunc);
85 napi_value closeDialogFunc = nullptr;
86 napi_create_function(env, "closeDialog", NAPI_AUTO_LENGTH, CloseDialog, nullptr, &closeDialogFunc);
87 napi_set_named_property(env, promptController, "closeDialog", closeDialogFunc);
88
89 napi_set_named_property(env, exports, "PromptController", promptController);
90 return napi_ok;
91 }
92
93 } // namespace OHOS::Ace::Napi
94