1 /*
2 * Copyright (c) 2023 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_short_key_manager.h"
17
18 #undef MMI_LOG_TAG
19 #define MMI_LOG_TAG "JsShortKeyManager"
20
21 namespace OHOS {
22 namespace MMI {
23 namespace {
24 enum class ReturnType {
25 VOID,
26 BOOL,
27 NUMBER,
28 };
29 } // namespace
30
TypeOf(napi_env env,napi_value value,napi_valuetype type)31 bool JsCommon::TypeOf(napi_env env, napi_value value, napi_valuetype type)
32 {
33 napi_valuetype valueType = napi_undefined;
34 CHKRF(napi_typeof(env, value, &valueType), TYPEOF);
35 return valueType == type;
36 }
37
~AsyncContext()38 AsyncContext::~AsyncContext()
39 {
40 CALL_DEBUG_ENTER;
41 if (work != nullptr) {
42 CHKRV(napi_delete_async_work(env, work), DELETE_ASYNC_WORK);
43 }
44 if (callback != nullptr && env != nullptr) {
45 CHKRV(napi_delete_reference(env, callback), DELETE_REFERENCE);
46 env = nullptr;
47 }
48 }
49
GetResult(sptr<AsyncContext> asyncContext,napi_value * results,int32_t size)50 static bool GetResult(sptr<AsyncContext> asyncContext, napi_value * results, int32_t size)
51 {
52 CALL_DEBUG_ENTER;
53 const int32_t length = 2;
54 if (size < length) {
55 MMI_HILOGE("results size less than 2");
56 return false;
57 }
58 napi_env env = asyncContext->env;
59 if (asyncContext->errorCode != RET_OK) {
60 if (asyncContext->errorCode == RET_ERR) {
61 MMI_HILOGE("Other errors");
62 return false;
63 }
64 NapiError codeMsg;
65 if (!UtilNapiError::GetApiError(asyncContext->errorCode, codeMsg)) {
66 MMI_HILOGE("ErrorCode not found, errCode:%{public}d", asyncContext->errorCode);
67 return false;
68 }
69 napi_value errCode = nullptr;
70 napi_value errMsg = nullptr;
71 napi_value businessError = nullptr;
72 CHKRF(napi_create_int32(env, asyncContext->errorCode, &errCode), CREATE_INT32);
73 CHKRF(napi_create_string_utf8(env, codeMsg.msg.c_str(),
74 NAPI_AUTO_LENGTH, &errMsg), CREATE_STRING_UTF8);
75 CHKRF(napi_create_error(env, nullptr, errMsg, &businessError), CREATE_ERROR);
76 CHKRF(napi_set_named_property(env, businessError, ERR_CODE.c_str(), errCode), SET_NAMED_PROPERTY);
77 results[0] = businessError;
78 } else {
79 CHKRF(napi_get_undefined(env, &results[0]), GET_UNDEFINED);
80 }
81
82 ReturnType resultType;
83 asyncContext->reserve >> resultType;
84 if (resultType == ReturnType::BOOL) {
85 bool temp;
86 asyncContext->reserve >> temp;
87 CHKRF(napi_get_boolean(env, temp, &results[1]), GET_BOOLEAN);
88 } else if (resultType == ReturnType::NUMBER) {
89 int32_t temp;
90 asyncContext->reserve >> temp;
91 CHKRF(napi_create_int32(env, temp, &results[1]), CREATE_INT32);
92 } else {
93 CHKRF(napi_get_undefined(env, &results[1]), GET_UNDEFINED);
94 }
95 return true;
96 }
97
AsyncCallbackWork(sptr<AsyncContext> asyncContext)98 void AsyncCallbackWork(sptr<AsyncContext> asyncContext)
99 {
100 CALL_DEBUG_ENTER;
101 CHKPV(asyncContext);
102 CHKPV(asyncContext->env);
103 napi_env env = asyncContext->env;
104 napi_value resource = nullptr;
105 CHKRV(napi_create_string_utf8(env, "AsyncCallbackWork", NAPI_AUTO_LENGTH, &resource), CREATE_STRING_UTF8);
106 asyncContext->IncStrongRef(nullptr);
107 napi_status status = napi_create_async_work(
108 env, nullptr, resource,
109 [](napi_env env, void* data) {
110 MMI_HILOGD("async_work callback function is called");
111 },
112 [](napi_env env, napi_status status, void* data) {
113 sptr<AsyncContext> asyncContext(static_cast<AsyncContext *>(data));
114 /**
115 * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced
116 * to 0 destruction, so you need to add 1 to the asyncCallbackInfo reference count when the
117 * asynchronous task is created, and subtract 1 from the reference count after the naked
118 * pointer is converted to a pointer when the asynchronous task is executed, the reference
119 * count of the smart pointer is guaranteed to be 1.
120 */
121 asyncContext->DecStrongRef(nullptr);
122 napi_value results[2] = { 0 };
123 int32_t size = 2;
124 if (!GetResult(asyncContext, results, size)) {
125 MMI_HILOGE("Failed to create napi data");
126 return;
127 }
128 if (asyncContext->deferred) {
129 if (asyncContext->errorCode == RET_OK) {
130 CHKRV(napi_resolve_deferred(env, asyncContext->deferred, results[1]), RESOLVE_DEFERRED);
131 } else {
132 CHKRV(napi_reject_deferred(env, asyncContext->deferred, results[0]), REJECT_DEFERRED);
133 }
134 } else {
135 napi_value callback = nullptr;
136 CHKRV(napi_get_reference_value(env, asyncContext->callback, &callback), GET_REFERENCE_VALUE);
137 napi_value callResult = nullptr;
138 CHKRV(napi_call_function(env, nullptr, callback, size, results, &callResult), CALL_FUNCTION);
139 }
140 },
141 asyncContext.GetRefPtr(), &asyncContext->work);
142 if (status != napi_ok ||
143 napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_t::napi_qos_user_initiated) != napi_ok) {
144 MMI_HILOGE("Create async work failed");
145 asyncContext->DecStrongRef(nullptr);
146 }
147 }
148
SetKeyDownDuration(napi_env env,const std::string & businessId,int32_t delay,napi_value handle)149 napi_value JsShortKeyManager::SetKeyDownDuration(napi_env env, const std::string &businessId, int32_t delay,
150 napi_value handle)
151 {
152 CALL_DEBUG_ENTER;
153 int32_t ret = InputManager::GetInstance()->SetKeyDownDuration(businessId, delay);
154 if (ret == COMMON_USE_SYSAPI_ERROR) {
155 MMI_HILOGE("Non system applications use system API");
156 THROWERR_CUSTOM(env, COMMON_USE_SYSAPI_ERROR, "Non system applications use system API");
157 return nullptr;
158 } else if (ret == COMMON_PARAMETER_ERROR) {
159 MMI_HILOGE("Invalid param");
160 THROWERR_CUSTOM(env, COMMON_PARAMETER_ERROR, "param is invalid");
161 return nullptr;
162 }
163 sptr<AsyncContext> asyncContext = new (std::nothrow) AsyncContext(env);
164 CHKPP(asyncContext);
165 asyncContext->errorCode = ret;
166 asyncContext->reserve << ReturnType::VOID;
167
168 napi_value promise = nullptr;
169 if (handle != nullptr) {
170 CHKRP(napi_create_reference(env, handle, 1, &asyncContext->callback), CREATE_REFERENCE);
171 if (napi_get_undefined(env, &promise) != napi_ok) {
172 CHKRP(napi_delete_reference(env, asyncContext->callback), DELETE_REFERENCE);
173 return nullptr;
174 }
175 } else {
176 CHKRP(napi_create_promise(env, &asyncContext->deferred, &promise), CREATE_PROMISE);
177 }
178 AsyncCallbackWork(asyncContext);
179 return promise;
180 }
181 } // namespace MMI
182 } // namespace OHOS
183