• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2025 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 <ani.h>
17 #include <array>
18 #include <iostream>
19 #include <vector>
20 #include "hilog_wrapper.h"
21 #include "accessibility_utils_ani.h"
22 #include <ani_signature_builder.h>
23 
24 namespace OHOS {
25 namespace AccessibilityAni {
26 
27 using namespace OHOS::Accessibility;
28 using namespace arkts::ani_signature;
29 
QueryRetMsg(RetError errorCode)30 NAccessibilityErrMsg QueryRetMsg(RetError errorCode)
31 {
32     switch (errorCode) {
33         case RetError::RET_OK:
34             return { NAccessibilityErrorCode::ACCESSIBILITY_OK, "" };
35         case RetError::RET_ERR_FAILED:
36         case RetError::RET_ERR_NULLPTR:
37         case RetError::RET_ERR_IPC_FAILED:
38         case RetError::RET_ERR_SAMGR:
39         case RetError::RET_ERR_TIME_OUT:
40         case RetError::RET_ERR_REGISTER_EXIST:
41         case RetError::RET_ERR_NO_REGISTER:
42         case RetError::RET_ERR_NO_CONNECTION:
43         case RetError::RET_ERR_NO_WINDOW_CONNECTION:
44         case RetError::RET_ERR_INVALID_ELEMENT_INFO_FROM_ACE:
45         case RetError::RET_ERR_PERFORM_ACTION_FAILED_BY_ACE:
46         case RetError::RET_ERR_NO_INJECTOR:
47             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_SYSTEM_ABNORMALITY,
48                      ERROR_MESSAGE_SYSTEM_ABNORMALITY };
49         case RetError::RET_ERR_INVALID_PARAM:
50             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_INVALID_PARAM, ERROR_MESSAGE_PARAMETER_ERROR };
51         case RetError::RET_ERR_NO_PERMISSION:
52             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NO_PERMISSION, ERROR_MESSAGE_NO_PERMISSION };
53         case RetError::RET_ERR_CONNECTION_EXIST:
54             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_TARGET_ABILITY_ALREADY_ENABLED,
55                      ERROR_MESSAGE_TARGET_ABILITY_ALREADY_ENABLED };
56         case RetError::RET_ERR_NO_CAPABILITY:
57             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NO_RIGHT, ERROR_MESSAGE_NO_RIGHT };
58         case RetError::RET_ERR_NOT_INSTALLED:
59         case RetError::RET_ERR_NOT_ENABLED:
60             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_ERROR_EXTENSION_NAME,
61                      ERROR_MESSAGE_INVALID_BUNDLE_NAME_OR_ABILITY_NAME};
62         case RetError::RET_ERR_PROPERTY_NOT_EXIST:
63             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_PROPERTY_NOT_EXIST,
64                      ERROR_MESSAGE_PROPERTY_NOT_EXIST };
65         case RetError::RET_ERR_ACTION_NOT_SUPPORT:
66             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_ACTION_NOT_SUPPORT,
67                      ERROR_MESSAGE_ACTION_NOT_SUPPORT };
68         case RetError::RET_ERR_NOT_SYSTEM_APP:
69             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_NOT_SYSTEM_APP,
70                      ERROR_MESSAGE_NOT_SYSTEM_APP };
71         default:
72             return { NAccessibilityErrorCode::ACCESSIBILITY_ERROR_SYSTEM_ABNORMALITY,
73                      ERROR_MESSAGE_SYSTEM_ABNORMALITY };
74     }
75 }
76 
ThrowBusinessError(ani_env * env,NAccessibilityErrMsg errMsg)77 void ThrowBusinessError(ani_env *env, NAccessibilityErrMsg errMsg)
78 {
79     Type errorClass = Builder::BuildClass("@ohos.base.BusinessError");
80     ani_class cls {};
81     if (env->FindClass(errorClass.Descriptor().c_str(), &cls) != ANI_OK) {
82         HILOG_ERROR("find class BusinessError failed");
83         return;
84     }
85     ani_method ctor;
86     std::string ctorName = Builder::BuildConstructorName();
87     SignatureBuilder sb{};
88     if (env->Class_FindMethod(cls, ctorName.c_str(), sb.BuildSignatureDescriptor().c_str(), &ctor) != ANI_OK) {
89         HILOG_ERROR("find method BusinessError.constructor failed");
90         return;
91     }
92     ani_double errCode = static_cast<ani_double>(errMsg.errCode);
93     ani_string errMsgStr;
94     env->String_NewUTF8(errMsg.message.c_str(), errMsg.message.length(), &errMsgStr);
95     ani_object errorObject;
96     if (env->Object_New(cls, ctor, &errorObject) != ANI_OK) {
97         HILOG_ERROR("create BusinessError object failed");
98         return;
99     }
100     if (env->Object_SetPropertyByName_Double(errorObject, "code", errCode) != ANI_OK) {
101         HILOG_ERROR("set property BusinessError.code failed!");
102         return;
103     }
104     if (env->Object_SetPropertyByName_Ref(errorObject, "message", static_cast<ani_ref>(errMsgStr)) != ANI_OK) {
105         HILOG_ERROR("set property BusinessError.message failed!");
106         return;
107     }
108     if (env->ThrowError(static_cast<ani_error>(errorObject)) != ANI_OK) {
109         HILOG_ERROR("throw BusinessError failed!");
110         return;
111     }
112     HILOG_INFO("throw BusinessError success!");
113     return;
114 }
115 } // namespace AccessibilityAni
116 } // namespace OHOS