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 #include "print_callback_ani_util.h"
16 #include "print_log.h"
17
18 namespace {
19 const char *INVOKE_METHOD_NAME = "invoke";
WrapStsError(ani_env * env,const std::string & msg)20 ani_object WrapStsError(ani_env *env, const std::string &msg)
21 {
22 ani_class cls {};
23 ani_method method {};
24 ani_object obj = nullptr;
25 ani_status status = ANI_ERROR;
26 if (env == nullptr) {
27 PRINT_HILOGE("null env");
28 return nullptr;
29 }
30
31 ani_string aniMsg = nullptr;
32 if ((status = env->String_NewUTF8(msg.c_str(), msg.size(), &aniMsg)) != ANI_OK) {
33 PRINT_HILOGE("String_NewUTF8 failed %{public}d", static_cast<int32_t>(status));
34 return nullptr;
35 }
36
37 ani_ref undefRef;
38 if ((status = env->GetUndefined(&undefRef)) != ANI_OK) {
39 PRINT_HILOGE("GetUndefined failed %{public}d", static_cast<int32_t>(status));
40 return nullptr;
41 }
42
43 if ((status = env->FindClass("Lescompat/Error;", &cls)) != ANI_OK) {
44 PRINT_HILOGE("FindClass failed %{public}d", static_cast<int32_t>(status));
45 return nullptr;
46 }
47 if ((status = env->Class_FindMethod(cls, "<ctor>", "C{std.core.String}C{escompat.ErrorOptions}:", &method)) !=
48 ANI_OK) {
49 PRINT_HILOGE("Class_FindMethod failed %{public}d", static_cast<int32_t>(status));
50 return nullptr;
51 }
52
53 if ((status = env->Object_New(cls, method, &obj, aniMsg, undefRef)) != ANI_OK) {
54 PRINT_HILOGE("Object_New failed %{public}d", static_cast<int32_t>(status));
55 return nullptr;
56 }
57 return obj;
58 }
59 }
60
61 namespace OHOS::Print {
62
AsyncCallback(ani_env * env,ani_object call,ani_object stsErrCode,ani_object retObj)63 bool AsyncCallback(ani_env *env, ani_object call, ani_object stsErrCode, ani_object retObj)
64 {
65 ani_status status = ANI_ERROR;
66 ani_class clsCall {};
67 if ((status = env->FindClass("@ohos.print.AsyncCallbackWrapper", &clsCall)) != ANI_OK) {
68 PRINT_HILOGE("FindClass fail, status: %{public}d", status);
69 return false;
70 }
71 ani_method method = {};
72 if ((status = env->Class_FindMethod(
73 clsCall, INVOKE_METHOD_NAME, nullptr, &method)) != ANI_OK) {
74 PRINT_HILOGE("Class_FindMethod fail, status: %{public}d", status);
75 return false;
76 }
77 if (retObj == nullptr) {
78 ani_ref undefinedRef = nullptr;
79 env->GetUndefined(&undefinedRef);
80 retObj = reinterpret_cast<ani_object>(undefinedRef);
81 }
82 if ((status = env->Object_CallMethod_Void(call, method, stsErrCode, retObj)) != ANI_OK) {
83 PRINT_HILOGE("Object_CallMethod_Void fail, status: %{public}d", status);
84 return false;
85 }
86 return true;
87 }
88
StsCallback(ani_env * env,ani_object call,ani_object retObj)89 bool StsCallback(ani_env *env, ani_object call, ani_object retObj)
90 {
91 ani_status status = ANI_ERROR;
92 ani_class clsCall {};
93 if ((status = env->FindClass("@ohos.print.CallbackWrapper", &clsCall)) != ANI_OK) {
94 PRINT_HILOGE("FindClass fail, status: %{public}d", status);
95 return false;
96 }
97 ani_method method = {};
98 if ((status = env->Class_FindMethod(
99 clsCall, INVOKE_METHOD_NAME, nullptr, &method)) != ANI_OK) {
100 PRINT_HILOGE("Class_FindMethod fail, status: %{public}d", status);
101 return false;
102 }
103 if (retObj == nullptr) {
104 ani_ref undefinedRef = nullptr;
105 env->GetUndefined(&undefinedRef);
106 retObj = reinterpret_cast<ani_object>(undefinedRef);
107 }
108 if ((status = env->Object_CallMethod_Void(call, method, retObj)) != ANI_OK) {
109 PRINT_HILOGE("Object_CallMethod_Void fail, status: %{public}d", status);
110 return false;
111 }
112 return true;
113 }
114
AsyncCallbackArray(ani_env * env,ani_object call,ani_object error,ani_object result)115 bool AsyncCallbackArray(ani_env *env, ani_object call, ani_object error, ani_object result)
116 {
117 ani_status status = ANI_ERROR;
118 ani_class clsCall {};
119 if ((status = env->FindClass("@ohos.print.AsyncCallbackArrayWrapper", &clsCall)) != ANI_OK) {
120 PRINT_HILOGE("FindClass fail, status: %{public}d", status);
121 return false;
122 }
123 ani_method method = {};
124 if ((status = env->Class_FindMethod(
125 clsCall, INVOKE_METHOD_NAME, nullptr, &method)) != ANI_OK) {
126 PRINT_HILOGE("Class_FindMethod fail, status: %{public}d", status);
127 return false;
128 }
129 if (result == nullptr) {
130 ani_ref undefinedRef = nullptr;
131 env->GetUndefined(&undefinedRef);
132 result = reinterpret_cast<ani_object>(undefinedRef);
133 }
134 if ((status = env->Object_CallMethod_Void(call, method, error, result)) != ANI_OK) {
135 PRINT_HILOGE("Object_CallMethod_Void fail, status: %{public}d", status);
136 return false;
137 }
138 return true;
139 }
140
CreateStsError(ani_env * env,ani_int code)141 ani_object CreateStsError(ani_env *env, ani_int code)
142 {
143 ani_class cls {};
144 ani_method method {};
145 ani_object obj = nullptr;
146 ani_status status = ANI_ERROR;
147 if (env == nullptr) {
148 PRINT_HILOGE("null env");
149 return nullptr;
150 }
151 if ((status = env->FindClass("@ohos.base.BusinessError", &cls)) != ANI_OK) {
152 PRINT_HILOGE("FindClass failed %{public}d", static_cast<int32_t>(status));
153 return nullptr;
154 }
155 if ((status = env->Class_FindMethod(cls, "<ctor>", "dC{escompat.Error}:", &method)) != ANI_OK) {
156 PRINT_HILOGE("Class_FindMethod failed %{public}d", static_cast<int32_t>(status));
157 return nullptr;
158 }
159 std::string msg;
160 ani_object error = WrapStsError(env, msg);
161 if (error == nullptr) {
162 PRINT_HILOGE("error nulll");
163 return nullptr;
164 }
165 ani_double dCode(code);
166 if ((status = env->Object_New(cls, method, &obj, dCode, error)) != ANI_OK) {
167 PRINT_HILOGE("Object_New failed %{public}d", static_cast<int32_t>(status));
168 return nullptr;
169 }
170 return obj;
171 }
172
173 } // namespace OHOS::Print