• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
GetState(napi_env env,napi_callback_info info)49 napi_value JSPromptController::GetState(napi_env env, napi_callback_info info)
50 {
51     size_t argc = 1;
52     napi_value argv[1] = { nullptr };
53     napi_value thisArg = nullptr;
54     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, nullptr));
55     if (argc < 1) {
56         NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID);
57         return nullptr;
58     }
59 
60     PromptDialogController* controller = nullptr;
61     napi_status status = napi_unwrap(env, argv[0], reinterpret_cast<void**>(&controller));
62     if (status != napi_ok || !controller) {
63         return nullptr;
64     }
65     auto state = controller->GetState();
66     napi_value dialogState;
67     napi_status createStatus = napi_create_int32(env, static_cast<int32_t>(state), &dialogState);
68     if (createStatus != napi_ok) {
69         return nullptr;
70     }
71     return dialogState;
72 }
73 
DialogControllerDestructor(napi_env env,void * data,void * hint)74 void JSPromptController::DialogControllerDestructor(napi_env env, void* data, void* hint)
75 {
76     PromptDialogController* controller = reinterpret_cast<PromptDialogController*>(data);
77     CHECK_NULL_VOID(controller);
78     delete controller;
79     controller = nullptr;
80 }
81 
DialogControllerConstructor(napi_env env,napi_callback_info info)82 napi_value JSPromptController::DialogControllerConstructor(napi_env env, napi_callback_info info)
83 {
84     size_t argc = 1;
85     napi_value argv[1] = { nullptr };
86     napi_value thisArg = nullptr;
87     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, nullptr));
88     if (argc < 1) {
89         NapiThrow(env, "The number of parameters is incorrect.", ERROR_CODE_PARAM_INVALID);
90         return nullptr;
91     }
92 
93     PromptDialogController* controller = new (std::nothrow) PromptDialogController();
94     napi_status status = napi_wrap(env, argv[0], controller, DialogControllerDestructor, nullptr, nullptr);
95     if (status != napi_ok) {
96         LOGE("Failed to wrap PromptDialogController.");
97         return nullptr;
98     }
99     return thisArg;
100 }
101 
Define(napi_env env,napi_value exports)102 napi_status JSPromptController::Define(napi_env env, napi_value exports)
103 {
104     napi_value promptController = nullptr;
105     napi_create_object(env, &promptController);
106 
107     napi_value bindDialogFunc = nullptr;
108     napi_create_function(env, "bindDialog", NAPI_AUTO_LENGTH, DialogControllerConstructor, nullptr, &bindDialogFunc);
109     napi_set_named_property(env, promptController, "bindDialog", bindDialogFunc);
110     napi_value closeDialogFunc = nullptr;
111     napi_create_function(env, "closeDialog", NAPI_AUTO_LENGTH, CloseDialog, nullptr, &closeDialogFunc);
112     napi_set_named_property(env, promptController, "closeDialog", closeDialogFunc);
113     napi_value getStateFunc = nullptr;
114     napi_create_function(env, "getState", NAPI_AUTO_LENGTH, GetState, nullptr, &getStateFunc);
115     napi_set_named_property(env, promptController, "getState", getStateFunc);
116 
117     napi_set_named_property(env, exports, "PromptController", promptController);
118     return napi_ok;
119 }
120 
121 } // namespace OHOS::Ace::Napi
122