1 /*
2 * Copyright (C) 2021-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_call_manager_utils.h"
17
18 #include "napi_util.h"
19 #include "system_ability_definition.h"
20 #include "telephony_log_wrapper.h"
21
22 namespace OHOS {
23 namespace Telephony {
24 static constexpr const char *PLACE_CALL = "ohos.permission.PLACE_CALL";
25 static constexpr const char *ANSWER_CALL = "ohos.permission.ANSWER_CALL";
26 static constexpr const char *SET_TELEPHONY_STATE = "ohos.permission.SET_TELEPHONY_STATE";
27 static constexpr const char *GET_TELEPHONY_STATE = "ohos.permission.GET_TELEPHONY_STATE";
28
29 static std::unordered_map<int32_t, const char *> eventNameMap_ = {
30 { CALL_MANAGER_DIAL_CALL, "dial" },
31 { CALL_MANAGER_ANSWER_CALL, "answer" },
32 { CALL_MANAGER_REJECT_CALL, "reject" },
33 { CALL_MANAGER_DISCONNECT_CALL, "hangup" },
34 { CALL_MANAGER_HOLD_CALL, "holdCall" },
35 { CALL_MANAGER_UNHOLD_CALL, "unHoldCall" },
36 { CALL_MANAGER_SWITCH_CALL, "switchCall" },
37 { CALL_MANAGER_GET_CALL_WAITING, "getCallWaitingStatus" },
38 { CALL_MANAGER_SET_CALL_WAITING, "setCallWaiting" },
39 { CALL_MANAGER_GET_CALL_RESTRICTION, "getCallRestrictionStatus" },
40 { CALL_MANAGER_SET_CALL_RESTRICTION, "setCallRestriction" },
41 { CALL_MANAGER_SET_CALL_RESTRICTION_PASSWORD, "setCallRestrictionPassword" },
42 { CALL_MANAGER_GET_CALL_TRANSFER, "getCallTransferInfo" },
43 { CALL_MANAGER_SET_CALL_TRANSFER, "setCallTransfer" },
44 { CALL_MANAGER_CAN_SET_CALL_TRANSFER_TIME, "canSetCallTransferTime" },
45 { CALL_MANAGER_ENABLE_IMS_SWITCH, "enableImsSwitch" },
46 { CALL_MANAGER_DISABLE_IMS_SWITCH, "disableImsSwitch" },
47 { CALL_MANAGER_IS_EMERGENCY_CALL, "isInEmergencyCall" },
48 { CALL_MANAGER_IS_RINGING, "isRinging" },
49 { CALL_MANAGER_MUTE_RINGER, "muteRinger" },
50 { CALL_MANAGER_SET_VONR_STATE, "setVoNRState" },
51 { CALL_MANAGER_GET_VONR_STATE, "getVoNRState" },
52 { CALL_MANAGER_SET_VOIP_CALL_STATE, "setVoIPCallState" },
53 { CALL_MANAGER_CONTROL_CAMERA, "controlCamera" },
54 { CALL_MANAGER_SET_PREVIEW_WINDOW, "setPreviewWindow" },
55 { CALL_MANAGER_SET_DISPLAY_WINDOW, "setDisplayWindow" },
56 { CALL_MANAGER_SET_PAUSE_PICTURE, "setPausePicture" },
57 { CALL_MANAGER_SET_DEVICE_DIRECTION, "setDeviceDirection" },
58 { CALL_MANAGER_QUERY_CAMERA_CAPABILITIES, "queryCameraCapabilities" },
59 { CALL_MANAGER_CANCEL_CALL_UPGRADE, "cancelCallUpgrade" },
60 { CALL_MANAGER_SEND_USSD_RESPONSE, "sendUssdResponse" },
61 };
62
63 static std::unordered_map<int32_t, const char *> eventPermissionMap_ = {
64 { CALL_MANAGER_DIAL_CALL, PLACE_CALL },
65 { CALL_MANAGER_ANSWER_CALL, ANSWER_CALL },
66 { CALL_MANAGER_REJECT_CALL, ANSWER_CALL },
67 { CALL_MANAGER_DISCONNECT_CALL, ANSWER_CALL },
68 { CALL_MANAGER_HOLD_CALL, ANSWER_CALL },
69 { CALL_MANAGER_UNHOLD_CALL, ANSWER_CALL },
70 { CALL_MANAGER_SWITCH_CALL, ANSWER_CALL },
71 { CALL_MANAGER_GET_CALL_WAITING, GET_TELEPHONY_STATE },
72 { CALL_MANAGER_SET_CALL_WAITING, SET_TELEPHONY_STATE },
73 { CALL_MANAGER_GET_CALL_RESTRICTION, GET_TELEPHONY_STATE },
74 { CALL_MANAGER_SET_CALL_RESTRICTION, SET_TELEPHONY_STATE },
75 { CALL_MANAGER_SET_CALL_RESTRICTION_PASSWORD, SET_TELEPHONY_STATE },
76 { CALL_MANAGER_GET_CALL_TRANSFER, GET_TELEPHONY_STATE },
77 { CALL_MANAGER_SET_CALL_TRANSFER, SET_TELEPHONY_STATE },
78 { CALL_MANAGER_CAN_SET_CALL_TRANSFER_TIME, GET_TELEPHONY_STATE },
79 { CALL_MANAGER_ENABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
80 { CALL_MANAGER_DISABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
81 { CALL_MANAGER_IS_EMERGENCY_CALL, SET_TELEPHONY_STATE },
82 { CALL_MANAGER_IS_RINGING, SET_TELEPHONY_STATE },
83 { CALL_MANAGER_MUTE_RINGER, SET_TELEPHONY_STATE },
84 { CALL_MANAGER_SET_VONR_STATE, SET_TELEPHONY_STATE },
85 { CALL_MANAGER_GET_VONR_STATE, GET_TELEPHONY_STATE },
86 { CALL_MANAGER_CONTROL_CAMERA, SET_TELEPHONY_STATE },
87 { CALL_MANAGER_SET_PREVIEW_WINDOW, SET_TELEPHONY_STATE },
88 { CALL_MANAGER_SET_DISPLAY_WINDOW, SET_TELEPHONY_STATE },
89 { CALL_MANAGER_SET_PAUSE_PICTURE, SET_TELEPHONY_STATE },
90 { CALL_MANAGER_SET_DEVICE_DIRECTION, SET_TELEPHONY_STATE },
91 { CALL_MANAGER_QUERY_CAMERA_CAPABILITIES, GET_TELEPHONY_STATE },
92 { CALL_MANAGER_CANCEL_CALL_UPGRADE, PLACE_CALL },
93 { CALL_MANAGER_SEND_USSD_RESPONSE, SET_TELEPHONY_STATE },
94 };
95
GetEventName(int32_t eventId)96 std::string NapiCallManagerUtils::GetEventName(int32_t eventId)
97 {
98 std::string result = "";
99 auto iter = eventNameMap_.find(eventId);
100 if (iter == eventNameMap_.end()) {
101 TELEPHONY_LOGE("NapiCallManagerUtils::GetEventName return null.");
102 return result;
103 }
104 result = iter->second;
105 TELEPHONY_LOGI("NapiCallManagerUtils::GetEventName message = %{public}s", result.c_str());
106 return result;
107 }
108
GetEventPermission(int32_t eventId)109 std::string NapiCallManagerUtils::GetEventPermission(int32_t eventId)
110 {
111 std::string result = "";
112 auto iter = eventPermissionMap_.find(eventId);
113 if (iter == eventPermissionMap_.end()) {
114 TELEPHONY_LOGE("NapiCallManagerUtils::GetEventPermission return null.");
115 return result;
116 }
117 result = iter->second;
118 TELEPHONY_LOGI("NapiCallManagerUtils::GetEventPermission message = %{public}s", result.c_str());
119 return result;
120 }
121
CreateErrorCodeAndMessageForJs(napi_env env,int32_t errorCode,int32_t eventId)122 napi_value NapiCallManagerUtils::CreateErrorCodeAndMessageForJs(napi_env env, int32_t errorCode, int32_t eventId)
123 {
124 JsError error = {};
125 switch (errorCode) {
126 case TELEPHONY_ERR_PERMISSION_ERR: {
127 std::string funcName = GetEventName(eventId);
128 std::string permission = GetEventPermission(eventId);
129 error = NapiUtil::ConverErrorMessageWithPermissionForJs(errorCode, funcName, permission);
130 break;
131 }
132 default:
133 error = NapiUtil::ConverErrorMessageForJs(errorCode);
134 break;
135 }
136 return CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
137 }
138
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)139 bool NapiCallManagerUtils::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
140 {
141 napi_valuetype valueType = napi_undefined;
142 napi_typeof(env, value, &valueType);
143 return valueType == targetType;
144 }
145
CreateUndefined(napi_env env)146 napi_value NapiCallManagerUtils::CreateUndefined(napi_env env)
147 {
148 napi_value result = nullptr;
149 napi_get_undefined(env, &result);
150 return result;
151 }
152
CreateErrorMessage(napi_env env,std::string msg)153 napi_value NapiCallManagerUtils::CreateErrorMessage(napi_env env, std::string msg)
154 {
155 napi_value result = nullptr;
156 napi_value message = nullptr;
157 napi_create_string_utf8(env, msg.c_str(), msg.length(), &message);
158 napi_create_error(env, nullptr, message, &result);
159 return result;
160 }
161
CreateErrorMessageWithErrorCode(napi_env env,std::string msg,int32_t errorCode)162 napi_value NapiCallManagerUtils::CreateErrorMessageWithErrorCode(napi_env env, std::string msg, int32_t errorCode)
163 {
164 napi_value message = nullptr;
165 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &message));
166 napi_value codeValue = nullptr;
167 NAPI_CALL(env, napi_create_int32(env, errorCode, &codeValue));
168 napi_value result = nullptr;
169 NAPI_CALL(env, napi_create_object(env, &result));
170 NAPI_CALL(env, napi_set_named_property(env, result, "code", codeValue));
171 NAPI_CALL(env, napi_set_named_property(env, result, "message", message));
172 return result;
173 }
174
ToInt32Value(napi_env env,int32_t value)175 napi_value NapiCallManagerUtils::ToInt32Value(napi_env env, int32_t value)
176 {
177 napi_value staticValue = nullptr;
178 napi_create_int32(env, value, &staticValue);
179 return staticValue;
180 }
181
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)182 napi_value NapiCallManagerUtils::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
183 {
184 napi_value value = nullptr;
185 napi_get_named_property(env, object, propertyName.c_str(), &value);
186 return value;
187 }
188
GetStringProperty(napi_env env,napi_value object,const std::string & propertyName)189 std::string NapiCallManagerUtils::GetStringProperty(napi_env env, napi_value object, const std::string &propertyName)
190 {
191 napi_value value = nullptr;
192 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
193 if (getNameStatus == napi_ok) {
194 char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
195 size_t len = 0;
196 napi_status getStringStatus =
197 napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
198 if (getStringStatus == napi_ok && len > 0) {
199 return std::string(tmpStr, len);
200 }
201 }
202 TELEPHONY_LOGW("GetStringProperty failed!");
203 return "";
204 }
205
GetWantParamsProperty(napi_env env,napi_value object,const std::string & propertyName)206 AAFwk::WantParams NapiCallManagerUtils::GetWantParamsProperty(napi_env env, napi_value object,
207 const std::string &propertyName)
208 {
209 bool hasParam = false;
210 napi_value value = nullptr;
211 AAFwk::WantParams wantParams;
212 napi_has_named_property(env, object, propertyName.c_str(), &hasParam);
213 if (hasParam) {
214 napi_get_named_property(env, object, propertyName.c_str(), &value);
215 napi_valuetype valueType = napi_undefined;
216 napi_typeof(env, value, &valueType);
217 if ((valueType != napi_undefined) && (valueType != napi_null) &&
218 AppExecFwk::UnwrapWantParams(env, value, wantParams)) {
219 return wantParams;
220 }
221 }
222 TELEPHONY_LOGW("GetWantParamsProperty failed!");
223 return wantParams;
224 }
225
GetIntProperty(napi_env env,napi_value object,const std::string & propertyName)226 int32_t NapiCallManagerUtils::GetIntProperty(napi_env env, napi_value object, const std::string &propertyName)
227 {
228 int32_t intValue = 0;
229 napi_value value = nullptr;
230 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
231 if (getNameStatus == napi_ok) {
232 napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
233 if (getIntStatus == napi_ok) {
234 return intValue;
235 }
236 }
237 TELEPHONY_LOGW("GetIntProperty failed!");
238 return intValue;
239 }
240
GetUssdIntProperty(napi_env env,napi_value object,const std::string & propertyName,int32_t & result)241 bool NapiCallManagerUtils::GetUssdIntProperty(
242 napi_env env, napi_value object, const std::string &propertyName, int32_t &result)
243 {
244 napi_value value = nullptr;
245 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
246 if (getNameStatus == napi_ok) {
247 napi_status getIntStatus = napi_get_value_int32(env, value, &result);
248 if (getIntStatus == napi_ok) {
249 return true;
250 }
251 }
252 TELEPHONY_LOGW("GetUssdIntProperty failed!");
253 return false;
254 }
255
GetUssdStringProperty(napi_env env,napi_value object,const std::string & propertyName,std::string & result)256 bool NapiCallManagerUtils::GetUssdStringProperty(
257 napi_env env, napi_value object, const std::string &propertyName, std::string &result)
258 {
259 napi_value value = nullptr;
260 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
261 if (getNameStatus == napi_ok) {
262 char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
263 size_t len = 0;
264 napi_status getStringStatus =
265 napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
266 if (getStringStatus == napi_ok && len > 0) {
267 result = std::string(tmpStr, len);
268 return true;
269 }
270 }
271 TELEPHONY_LOGW("GetUssdStringProperty failed!");
272 return false;
273 }
274
GetBoolProperty(napi_env env,napi_value object,const std::string & propertyName)275 bool NapiCallManagerUtils::GetBoolProperty(napi_env env, napi_value object, const std::string &propertyName)
276 {
277 bool boolValue = false;
278 napi_value value = nullptr;
279 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
280 if (getNameStatus == napi_ok) {
281 napi_status getIntStatus = napi_get_value_bool(env, value, &boolValue);
282 if (getIntStatus == napi_ok) {
283 return boolValue;
284 }
285 }
286 TELEPHONY_LOGW("GetBoolProperty failed!");
287 return boolValue;
288 }
289
SetPropertyInt32(napi_env env,napi_value object,std::string name,int32_t value)290 void NapiCallManagerUtils::SetPropertyInt32(napi_env env, napi_value object, std::string name, int32_t value)
291 {
292 napi_value peopertyValue = nullptr;
293 napi_create_int32(env, value, &peopertyValue);
294 napi_set_named_property(env, object, name.c_str(), peopertyValue);
295 }
296
SetPropertyInt64(napi_env env,napi_value object,std::string name,int64_t value)297 void NapiCallManagerUtils::SetPropertyInt64(napi_env env, napi_value object, std::string name, int64_t value)
298 {
299 napi_value peopertyValue = nullptr;
300 napi_create_int64(env, value, &peopertyValue);
301 napi_set_named_property(env, object, name.c_str(), peopertyValue);
302 }
303
SetPropertyStringUtf8(napi_env env,napi_value object,std::string name,std::string value)304 void NapiCallManagerUtils::SetPropertyStringUtf8(napi_env env, napi_value object, std::string name, std::string value)
305 {
306 napi_value peopertyValue = nullptr;
307 napi_create_string_utf8(env, value.c_str(), value.length(), &peopertyValue);
308 napi_set_named_property(env, object, name.c_str(), peopertyValue);
309 }
310
SetPropertyBoolean(napi_env env,napi_value object,std::string name,int32_t value)311 void NapiCallManagerUtils::SetPropertyBoolean(napi_env env, napi_value object, std::string name, int32_t value)
312 {
313 napi_value peopertyValue = nullptr;
314 napi_get_boolean(env, value, &peopertyValue);
315 napi_set_named_property(env, object, name.c_str(), peopertyValue);
316 }
317
CreateEnumConstructor(napi_env env,napi_callback_info info)318 napi_value NapiCallManagerUtils::CreateEnumConstructor(napi_env env, napi_callback_info info)
319 {
320 napi_value thisArg = nullptr;
321 void *data = nullptr;
322 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
323 napi_value global = nullptr;
324 napi_get_global(env, &global);
325 return thisArg;
326 }
327 } // namespace Telephony
328 } // namespace OHOS
329