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_GET_CALL_TRANSFER, "getCallTransferInfo" },
42 { CALL_MANAGER_SET_CALL_TRANSFER, "setCallTransfer" },
43 { CALL_MANAGER_ENABLE_IMS_SWITCH, "enableImsSwitch" },
44 { CALL_MANAGER_DISABLE_IMS_SWITCH, "disableImsSwitch" },
45 { CALL_MANAGER_IS_EMERGENCY_CALL, "isInEmergencyCall" },
46 { CALL_MANAGER_IS_RINGING, "isRinging" },
47 { CALL_MANAGER_MUTE_RINGER, "muteRinger" },
48 };
49
50 static std::unordered_map<int32_t, const char *> eventPermissionMap_ = {
51 { CALL_MANAGER_DIAL_CALL, PLACE_CALL },
52 { CALL_MANAGER_ANSWER_CALL, ANSWER_CALL },
53 { CALL_MANAGER_REJECT_CALL, ANSWER_CALL },
54 { CALL_MANAGER_DISCONNECT_CALL, ANSWER_CALL },
55 { CALL_MANAGER_HOLD_CALL, ANSWER_CALL },
56 { CALL_MANAGER_UNHOLD_CALL, ANSWER_CALL },
57 { CALL_MANAGER_SWITCH_CALL, ANSWER_CALL },
58 { CALL_MANAGER_GET_CALL_WAITING, GET_TELEPHONY_STATE },
59 { CALL_MANAGER_SET_CALL_WAITING, SET_TELEPHONY_STATE },
60 { CALL_MANAGER_GET_CALL_RESTRICTION, GET_TELEPHONY_STATE },
61 { CALL_MANAGER_SET_CALL_RESTRICTION, SET_TELEPHONY_STATE },
62 { CALL_MANAGER_GET_CALL_TRANSFER, GET_TELEPHONY_STATE },
63 { CALL_MANAGER_SET_CALL_TRANSFER, SET_TELEPHONY_STATE },
64 { CALL_MANAGER_ENABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
65 { CALL_MANAGER_DISABLE_IMS_SWITCH, SET_TELEPHONY_STATE },
66 { CALL_MANAGER_IS_EMERGENCY_CALL, SET_TELEPHONY_STATE },
67 { CALL_MANAGER_IS_RINGING, SET_TELEPHONY_STATE },
68 { CALL_MANAGER_MUTE_RINGER, SET_TELEPHONY_STATE },
69 };
70
GetEventName(int32_t eventId)71 std::string NapiCallManagerUtils::GetEventName(int32_t eventId)
72 {
73 std::string result = "";
74 auto iter = eventNameMap_.find(eventId);
75 if (iter == eventNameMap_.end()) {
76 TELEPHONY_LOGE("NapiCallManagerUtils::GetEventName return null.");
77 return result;
78 }
79 result = iter->second;
80 TELEPHONY_LOGI("NapiCallManagerUtils::GetEventName message = %{public}s", result.c_str());
81 return result;
82 }
83
GetEventPermission(int32_t eventId)84 std::string NapiCallManagerUtils::GetEventPermission(int32_t eventId)
85 {
86 std::string result = "";
87 auto iter = eventPermissionMap_.find(eventId);
88 if (iter == eventPermissionMap_.end()) {
89 TELEPHONY_LOGE("NapiCallManagerUtils::GetEventPermission return null.");
90 return result;
91 }
92 result = iter->second;
93 TELEPHONY_LOGI("NapiCallManagerUtils::GetEventPermission message = %{public}s", result.c_str());
94 return result;
95 }
96
CreateErrorCodeAndMessageForJs(napi_env env,int32_t errorCode,int32_t eventId)97 napi_value NapiCallManagerUtils::CreateErrorCodeAndMessageForJs(napi_env env, int32_t errorCode, int32_t eventId)
98 {
99 JsError error = {};
100 switch (errorCode) {
101 case TELEPHONY_ERR_PERMISSION_ERR: {
102 std::string funcName = GetEventName(eventId);
103 std::string permission = GetEventPermission(eventId);
104 error = NapiUtil::ConverErrorMessageWithPermissionForJs(errorCode, funcName, permission);
105 break;
106 }
107 default:
108 error = NapiUtil::ConverErrorMessageForJs(errorCode);
109 break;
110 }
111 return CreateErrorMessageWithErrorCode(env, error.errorMessage, error.errorCode);
112 }
113
MatchValueType(napi_env env,napi_value value,napi_valuetype targetType)114 bool NapiCallManagerUtils::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
115 {
116 napi_valuetype valueType = napi_undefined;
117 napi_typeof(env, value, &valueType);
118 return valueType == targetType;
119 }
120
CreateUndefined(napi_env env)121 napi_value NapiCallManagerUtils::CreateUndefined(napi_env env)
122 {
123 napi_value result = nullptr;
124 napi_get_undefined(env, &result);
125 return result;
126 }
127
CreateErrorMessage(napi_env env,std::string msg)128 napi_value NapiCallManagerUtils::CreateErrorMessage(napi_env env, std::string msg)
129 {
130 napi_value result = nullptr;
131 napi_value message = nullptr;
132 napi_create_string_utf8(env, msg.c_str(), msg.length(), &message);
133 napi_create_error(env, nullptr, message, &result);
134 return result;
135 }
136
CreateErrorMessageWithErrorCode(napi_env env,std::string msg,int32_t errorCode)137 napi_value NapiCallManagerUtils::CreateErrorMessageWithErrorCode(napi_env env, std::string msg, int32_t errorCode)
138 {
139 napi_value result = nullptr;
140 napi_value message = nullptr;
141 NAPI_CALL(env, napi_create_string_utf8(env, msg.c_str(), msg.length(), &message));
142 napi_value codeValue = nullptr;
143 NAPI_CALL(env, napi_create_int32(env, errorCode, &codeValue));
144 NAPI_CALL(env, napi_create_object(env, &result));
145 NAPI_CALL(env, napi_set_named_property(env, result, "code", codeValue));
146 NAPI_CALL(env, napi_set_named_property(env, result, "message", message));
147 return result;
148 }
149
ToInt32Value(napi_env env,int32_t value)150 napi_value NapiCallManagerUtils::ToInt32Value(napi_env env, int32_t value)
151 {
152 napi_value staticValue = nullptr;
153 napi_create_int32(env, value, &staticValue);
154 return staticValue;
155 }
156
GetNamedProperty(napi_env env,napi_value object,const std::string & propertyName)157 napi_value NapiCallManagerUtils::GetNamedProperty(napi_env env, napi_value object, const std::string &propertyName)
158 {
159 napi_value value = nullptr;
160 napi_get_named_property(env, object, propertyName.c_str(), &value);
161 return value;
162 }
163
GetStringProperty(napi_env env,napi_value object,const std::string & propertyName)164 std::string NapiCallManagerUtils::GetStringProperty(napi_env env, napi_value object, const std::string &propertyName)
165 {
166 napi_value value = nullptr;
167 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
168 if (getNameStatus == napi_ok) {
169 char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
170 size_t len = 0;
171 napi_status getStringStatus =
172 napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
173 if (getStringStatus == napi_ok && len > 0) {
174 return std::string(tmpStr, len);
175 }
176 }
177 TELEPHONY_LOGW("GetStringProperty failed!");
178 return "";
179 }
180
GetIntProperty(napi_env env,napi_value object,const std::string & propertyName)181 int32_t NapiCallManagerUtils::GetIntProperty(napi_env env, napi_value object, const std::string &propertyName)
182 {
183 int32_t intValue = 0;
184 napi_value value = nullptr;
185 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
186 if (getNameStatus == napi_ok) {
187 napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
188 if (getIntStatus == napi_ok) {
189 return intValue;
190 }
191 }
192 TELEPHONY_LOGW("GetIntProperty failed!");
193 return intValue;
194 }
195
GetUssdIntProperty(napi_env env,napi_value object,const std::string & propertyName,int32_t & result)196 bool NapiCallManagerUtils::GetUssdIntProperty(
197 napi_env env, napi_value object, const std::string &propertyName, int32_t &result)
198 {
199 napi_value value = nullptr;
200 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
201 if (getNameStatus == napi_ok) {
202 napi_status getIntStatus = napi_get_value_int32(env, value, &result);
203 if (getIntStatus == napi_ok) {
204 return true;
205 }
206 }
207 TELEPHONY_LOGW("GetUssdIntProperty failed!");
208 return false;
209 }
210
GetUssdStringProperty(napi_env env,napi_value object,const std::string & propertyName,std::string & result)211 bool NapiCallManagerUtils::GetUssdStringProperty(
212 napi_env env, napi_value object, const std::string &propertyName, std::string &result)
213 {
214 napi_value value = nullptr;
215 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
216 if (getNameStatus == napi_ok) {
217 char tmpStr[MESSAGE_CONTENT_MAXIMUM_LIMIT + 1] = { 0 };
218 size_t len = 0;
219 napi_status getStringStatus =
220 napi_get_value_string_utf8(env, value, tmpStr, MESSAGE_CONTENT_MAXIMUM_LIMIT, &len);
221 if (getStringStatus == napi_ok && len > 0) {
222 result = std::string(tmpStr, len);
223 return true;
224 }
225 }
226 TELEPHONY_LOGW("GetUssdStringProperty failed!");
227 return false;
228 }
229
GetBoolProperty(napi_env env,napi_value object,const std::string & propertyName)230 bool NapiCallManagerUtils::GetBoolProperty(napi_env env, napi_value object, const std::string &propertyName)
231 {
232 bool boolValue = false;
233 napi_value value = nullptr;
234 napi_status getNameStatus = napi_get_named_property(env, object, propertyName.c_str(), &value);
235 if (getNameStatus == napi_ok) {
236 napi_status getIntStatus = napi_get_value_bool(env, value, &boolValue);
237 if (getIntStatus == napi_ok) {
238 return boolValue;
239 }
240 }
241 TELEPHONY_LOGW("GetBoolProperty failed!");
242 return boolValue;
243 }
244
SetPropertyInt32(napi_env env,napi_value object,std::string name,int32_t value)245 void NapiCallManagerUtils::SetPropertyInt32(napi_env env, napi_value object, std::string name, int32_t value)
246 {
247 napi_value peopertyValue = nullptr;
248 napi_create_int32(env, value, &peopertyValue);
249 napi_set_named_property(env, object, name.c_str(), peopertyValue);
250 }
251
SetPropertyStringUtf8(napi_env env,napi_value object,std::string name,std::string value)252 void NapiCallManagerUtils::SetPropertyStringUtf8(napi_env env, napi_value object, std::string name, std::string value)
253 {
254 napi_value peopertyValue = nullptr;
255 napi_create_string_utf8(env, value.c_str(), value.length(), &peopertyValue);
256 napi_set_named_property(env, object, name.c_str(), peopertyValue);
257 }
258
SetPropertyBoolean(napi_env env,napi_value object,std::string name,int32_t value)259 void NapiCallManagerUtils::SetPropertyBoolean(napi_env env, napi_value object, std::string name, int32_t value)
260 {
261 napi_value peopertyValue = nullptr;
262 napi_get_boolean(env, value, &peopertyValue);
263 napi_set_named_property(env, object, name.c_str(), peopertyValue);
264 }
265
CreateEnumConstructor(napi_env env,napi_callback_info info)266 napi_value NapiCallManagerUtils::CreateEnumConstructor(napi_env env, napi_callback_info info)
267 {
268 napi_value thisArg = nullptr;
269 void *data = nullptr;
270 napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
271 napi_value global = nullptr;
272 napi_get_global(env, &global);
273 return thisArg;
274 }
275 } // namespace Telephony
276 } // namespace OHOS
277