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 
16 #include "napi_accessibility_event_info.h"
17 #include "hilog_wrapper.h"
18 #include "accessibility_utils.h"
19 
20 using namespace OHOS;
21 using namespace OHOS::Accessibility;
22 
DefineJSAccessibilityEventInfo(napi_env env,napi_value exports)23 void NAccessibilityEventInfo::DefineJSAccessibilityEventInfo(napi_env env, napi_value exports)
24 {
25     napi_value constructor = nullptr;
26 
27     NAPI_CALL_RETURN_VOID(env, napi_define_class(env, "EventInfo", sizeof("EventInfo"),
28         NAccessibilityEventInfo::JSConstructor, nullptr, 0, nullptr, &constructor));
29 
30     NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, exports, "EventInfo", constructor));
31 }
32 
JSConstructor(napi_env env,napi_callback_info info)33 napi_value NAccessibilityEventInfo::JSConstructor(napi_env env, napi_callback_info info)
34 {
35     size_t argc = ARGS_SIZE_THREE;
36     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
37     napi_value thisVar = nullptr;
38     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr));
39     if (argc == ARGS_SIZE_ONE) {
40         napi_valuetype valueType = napi_null;
41         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valueType));
42         if (valueType != napi_object) {
43             HILOG_ERROR("valueType %{public}d is not napi_object", valueType);
44             return nullptr;
45         }
46 
47         return argv[PARAM0];
48     } else if (argc == ARGS_SIZE_THREE) {
49         napi_valuetype valueType = napi_null;
50         NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valueType));
51         if (valueType != napi_string) {
52             HILOG_ERROR("type mismatch for parameter 0");
53             return nullptr;
54         }
55 
56         NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valueType));
57         if (valueType != napi_string) {
58             HILOG_ERROR("type mismatch for parameter 1");
59             return nullptr;
60         }
61 
62         NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valueType));
63         if (valueType != napi_string) {
64             HILOG_ERROR("type mismatch for parameter 2");
65             return nullptr;
66         }
67 
68         NAPI_CALL(env, napi_set_named_property(env, thisVar, "type", argv[PARAM0]));
69         NAPI_CALL(env, napi_set_named_property(env, thisVar, "bundleName", argv[PARAM1]));
70         NAPI_CALL(env, napi_set_named_property(env, thisVar, "triggerAction", argv[PARAM2]));
71         return thisVar;
72     }
73 
74     return nullptr;
75 }