• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "napi_mms.h"
16 
17 #include "telephony_permission.h"
18 #include "ability.h"
19 #include "napi_base_context.h"
20 #include "napi_mms_pdu.h"
21 #include "napi_mms_pdu_helper.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 namespace {
26 const std::string g_mmsFilePathName = "mmsFilePathName";
27 const std::string mmsTypeKey = "mmsType";
28 const std::string attachmentKey = "attachment";
29 const std::string SMS_PROFILE_URI = "datashare:///com.ohos.smsmmsability";
30 static const int32_t DEFAULT_REF_COUNT = 1;
31 static const uint32_t MAX_MMS_MSG_PART_LEN = 300 * 1024;
32 const bool STORE_MMS_PDU_TO_FILE = false;
33 const uint32_t MMS_PDU_MAX_SIZE = 300 * 1024;
34 } // namespace
35 
SetPropertyArray(napi_env env,napi_value object,const std::string & name,MmsAttachmentContext & context)36 static void SetPropertyArray(napi_env env, napi_value object, const std::string &name, MmsAttachmentContext &context)
37 {
38     napi_value array = nullptr;
39     napi_create_array(env, &array);
40     for (uint32_t i = 0; i < context.inBuffLen; i++) {
41         napi_value element = nullptr;
42         napi_create_int32(env, context.inBuff[i], &element);
43         napi_set_element(env, array, i, element);
44     }
45     napi_set_named_property(env, object, name.c_str(), array);
46 }
47 
WrapDecodeMmsStatus(int32_t status)48 int32_t WrapDecodeMmsStatus(int32_t status)
49 {
50     switch (status) {
51         case MmsMsgType::MMS_MSGTYPE_SEND_REQ: {
52             return MessageType::TYPE_MMS_SEND_REQ;
53         }
54         case MmsMsgType::MMS_MSGTYPE_SEND_CONF: {
55             return MessageType::TYPE_MMS_SEND_CONF;
56         }
57         case MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND: {
58             return MessageType::TYPE_MMS_NOTIFICATION_IND;
59         }
60         case MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND: {
61             return MessageType::TYPE_MMS_RESP_IND;
62         }
63         case MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF: {
64             return MessageType::TYPE_MMS_RETRIEVE_CONF;
65         }
66         case MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND: {
67             return MessageType::TYPE_MMS_ACKNOWLEDGE_IND;
68         }
69         case MmsMsgType::MMS_MSGTYPE_DELIVERY_IND: {
70             return MessageType::TYPE_MMS_DELIVERY_IND;
71         }
72         case MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND: {
73             return MessageType::TYPE_MMS_READ_ORIG_IND;
74         }
75         case MmsMsgType::MMS_MSGTYPE_READ_REC_IND: {
76             return MessageType::TYPE_MMS_READ_REC_IND;
77         }
78         default: {
79             return MESSAGE_UNKNOWN_STATUS;
80         }
81     }
82 }
83 
WrapEncodeMmsStatus(int32_t status)84 int32_t WrapEncodeMmsStatus(int32_t status)
85 {
86     switch (status) {
87         case MessageType::TYPE_MMS_SEND_REQ: {
88             return MmsMsgType::MMS_MSGTYPE_SEND_REQ;
89         }
90         case MessageType::TYPE_MMS_SEND_CONF: {
91             return MmsMsgType::MMS_MSGTYPE_SEND_CONF;
92         }
93         case MessageType::TYPE_MMS_NOTIFICATION_IND: {
94             return MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND;
95         }
96         case MessageType::TYPE_MMS_RESP_IND: {
97             return MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND;
98         }
99         case MessageType::TYPE_MMS_RETRIEVE_CONF: {
100             return MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF;
101         }
102         case MessageType::TYPE_MMS_ACKNOWLEDGE_IND: {
103             return MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND;
104         }
105         case MessageType::TYPE_MMS_DELIVERY_IND: {
106             return MmsMsgType::MMS_MSGTYPE_DELIVERY_IND;
107         }
108         case MessageType::TYPE_MMS_READ_ORIG_IND: {
109             return MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND;
110         }
111         case MessageType::TYPE_MMS_READ_REC_IND: {
112             return MmsMsgType::MMS_MSGTYPE_READ_REC_IND;
113         }
114         default: {
115             return MESSAGE_UNKNOWN_STATUS;
116         }
117     }
118 }
119 
parseDispositionValue(int32_t value)120 std::string parseDispositionValue(int32_t value)
121 {
122     switch (value) {
123         case FROM_DATA:
124             return "from-data";
125         case ATTACHMENT:
126             return "attachment";
127         case INLINE:
128             return "inline";
129         default:
130             TELEPHONY_LOGE("Invalid contentDisposition value");
131             return "";
132     }
133 }
134 
formatDispositionValue(const std::string & value)135 int32_t formatDispositionValue(const std::string &value)
136 {
137     if (std::string("from-data") == value) {
138         return FROM_DATA;
139     } else if (std::string("attachment") == value) {
140         return ATTACHMENT;
141     } else {
142         return INLINE;
143     }
144 }
145 
GetMmsSendConf(MmsMsg mmsMsg,MmsSendConfContext & asyncContext)146 void GetMmsSendConf(MmsMsg mmsMsg, MmsSendConfContext &asyncContext)
147 {
148     TELEPHONY_LOGI("napi_mms GetMmsSendConf start");
149     asyncContext.responseState = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_RESPONSE_STATUS);
150     asyncContext.transactionId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_TRANSACTION_ID);
151     asyncContext.version = static_cast<uint16_t>(mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_MMS_VERSION));
152     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
153     TELEPHONY_LOGI("napi_mms GetMmsSendConf end");
154 }
155 
GetMmsSendReq(MmsMsg mmsMsg,MmsSendReqContext & asyncContext)156 void GetMmsSendReq(MmsMsg mmsMsg, MmsSendReqContext &asyncContext)
157 {
158     TELEPHONY_LOGI("napi_mms GetMmsSendConf end");
159     asyncContext.from = mmsMsg.GetMmsFrom();
160     mmsMsg.GetMmsTo(asyncContext.to);
161     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
162     asyncContext.version = mmsMsg.GetMmsVersion();
163     asyncContext.date = mmsMsg.GetMmsDate();
164     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_CC, asyncContext.cc);
165     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_BCC, asyncContext.bcc);
166     asyncContext.subject = mmsMsg.GetMmsSubject();
167     asyncContext.messageClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS);
168     asyncContext.expiry = mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY);
169     asyncContext.priority = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY);
170     asyncContext.senderVisibility = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_SENDER_VISIBILITY);
171     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
172     asyncContext.readReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT);
173     asyncContext.contentType = mmsMsg.GetHeaderContentType();
174     TELEPHONY_LOGI("napi_mms GetMmsSendReq end");
175 }
176 
GetMmsNotificationInd(MmsMsg mmsMsg,MmsNotificationIndContext & asyncContext)177 void GetMmsNotificationInd(MmsMsg mmsMsg, MmsNotificationIndContext &asyncContext)
178 {
179     TELEPHONY_LOGI("napi_mms GetMmsNotificationInd start");
180     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
181     asyncContext.messageClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS);
182     asyncContext.messageSize = mmsMsg.GetHeaderLongValue(MmsFieldCode::MMS_MESSAGE_SIZE);
183     asyncContext.expiry = mmsMsg.GetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY);
184     asyncContext.version = mmsMsg.GetMmsVersion();
185     asyncContext.from = mmsMsg.GetMmsFrom();
186     asyncContext.subject = mmsMsg.GetMmsSubject();
187     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
188     asyncContext.contentLocation = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_CONTENT_LOCATION);
189     asyncContext.contentClass = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_CONTENT_CLASS);
190     TELEPHONY_LOGI("napi_mms GetMmsNotificationInd end");
191 }
192 
GetMmsRespInd(MmsMsg mmsMsg,MmsRespIndContext & asyncContext)193 void GetMmsRespInd(MmsMsg mmsMsg, MmsRespIndContext &asyncContext)
194 {
195     TELEPHONY_LOGI("napi_mms GetMmsRespInd start");
196     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
197     asyncContext.status = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_STATUS);
198     asyncContext.version = mmsMsg.GetMmsVersion();
199     asyncContext.reportAllowed = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED);
200     TELEPHONY_LOGI("napi_mms GetMmsRespInd end");
201 }
202 
GetMmsRetrieveConf(MmsMsg mmsMsg,MmsRetrieveConfContext & asyncContext)203 void GetMmsRetrieveConf(MmsMsg mmsMsg, MmsRetrieveConfContext &asyncContext)
204 {
205     TELEPHONY_LOGI("napi_mms GetMmsRetrieveConf start");
206     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
207     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
208     asyncContext.date = mmsMsg.GetMmsDate();
209     asyncContext.version = mmsMsg.GetMmsVersion();
210     mmsMsg.GetMmsTo(asyncContext.to);
211     asyncContext.from = mmsMsg.GetMmsFrom();
212     mmsMsg.GetHeaderAllAddressValue(MmsFieldCode::MMS_CC, asyncContext.cc);
213     asyncContext.subject = mmsMsg.GetMmsSubject();
214     asyncContext.priority = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY);
215     asyncContext.deliveryReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT);
216     asyncContext.readReport = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT);
217     asyncContext.retrieveStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_RETRIEVE_STATUS);
218     asyncContext.retrieveText = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_RETRIEVE_TEXT);
219     asyncContext.contentType = mmsMsg.GetHeaderContentType();
220     TELEPHONY_LOGI("napi_mms GetMmsRetrieveConf end");
221 }
222 
GetMmsAcknowledgeInd(MmsMsg mmsMsg,MmsAcknowledgeIndContext & asyncContext)223 void GetMmsAcknowledgeInd(MmsMsg mmsMsg, MmsAcknowledgeIndContext &asyncContext)
224 {
225     TELEPHONY_LOGI("napi_mms GetMmsAcknowledgeInd start");
226     asyncContext.transactionId = mmsMsg.GetMmsTransactionId();
227     asyncContext.version = mmsMsg.GetMmsVersion();
228     asyncContext.reportAllowed = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED);
229     TELEPHONY_LOGI("napi_mms GetMmsAcknowledgeInd end");
230 }
231 
GetMmsDeliveryInd(MmsMsg mmsMsg,MmsDeliveryIndContext & asyncContext)232 void GetMmsDeliveryInd(MmsMsg mmsMsg, MmsDeliveryIndContext &asyncContext)
233 {
234     TELEPHONY_LOGI("napi_mms GetMmsDeliveryInd start");
235     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
236     asyncContext.date = mmsMsg.GetMmsDate();
237     std::vector<MmsAddress> toAddress;
238     bool result = mmsMsg.GetMmsTo(toAddress);
239     if (result) {
240         asyncContext.to.assign(toAddress.begin(), toAddress.end());
241     }
242     asyncContext.status = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_STATUS);
243     asyncContext.version = mmsMsg.GetMmsVersion();
244 
245     TELEPHONY_LOGI("napi_mms GetMmsDeliveryInd end");
246 }
247 
GetMmsReadOrigInd(MmsMsg mmsMsg,MmsReadOrigIndContext & asyncContext)248 void GetMmsReadOrigInd(MmsMsg mmsMsg, MmsReadOrigIndContext &asyncContext)
249 {
250     TELEPHONY_LOGI("napi_mms GetMmsReadOrigInd start");
251     asyncContext.version = mmsMsg.GetMmsVersion();
252     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
253     mmsMsg.GetMmsTo(asyncContext.to);
254     asyncContext.from = mmsMsg.GetMmsFrom();
255     asyncContext.date = mmsMsg.GetMmsDate();
256     asyncContext.readStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS);
257 
258     TELEPHONY_LOGI("napi_mms GetMmsReadOrigInd end");
259 }
260 
GetMmsReadRecInd(MmsMsg mmsMsg,MmsReadRecIndContext & asyncContext)261 void GetMmsReadRecInd(MmsMsg mmsMsg, MmsReadRecIndContext &asyncContext)
262 {
263     TELEPHONY_LOGI("napi_mms GetMmsReadRecInd start");
264     asyncContext.version = mmsMsg.GetMmsVersion();
265     asyncContext.messageId = mmsMsg.GetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID);
266     mmsMsg.GetMmsTo(asyncContext.to);
267     asyncContext.from = mmsMsg.GetMmsFrom();
268     asyncContext.date = mmsMsg.GetMmsDate();
269     asyncContext.readStatus = mmsMsg.GetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS);
270 
271     TELEPHONY_LOGI("napi_mms GetMmsReadRecInd end");
272 }
273 
getAttachmentByDecodeMms(MmsMsg & mmsMsg,DecodeMmsContext & context)274 void getAttachmentByDecodeMms(MmsMsg &mmsMsg, DecodeMmsContext &context)
275 {
276     std::vector<MmsAttachment> attachment;
277     mmsMsg.GetAllAttachment(attachment);
278     if (attachment.empty()) {
279         return;
280     }
281     for (auto it : attachment) {
282         MmsAttachmentContext attachmentContext;
283         attachmentContext.path = it.GetAttachmentFilePath();
284         attachmentContext.fileName = it.GetFileName();
285         attachmentContext.contentId = it.GetContentId();
286         attachmentContext.contentLocation = it.GetContentLocation();
287         attachmentContext.contentDisposition = it.GetContentDisposition();
288         attachmentContext.contentTransferEncoding = it.GetContentTransferEncoding();
289         attachmentContext.contentType = it.GetContentType();
290         attachmentContext.isSmil = it.IsSmilFile();
291         attachmentContext.charset = static_cast<int32_t>(it.GetCharSet());
292         std::unique_ptr<char[]> buffer = nullptr;
293         buffer = it.GetDataBuffer(attachmentContext.inBuffLen);
294         attachmentContext.inBuff = std::move(buffer);
295         context.attachment.push_back(std::move(attachmentContext));
296     }
297 }
298 
NativeDecodeMms(napi_env env,void * data)299 void NativeDecodeMms(napi_env env, void *data)
300 {
301     TELEPHONY_LOGI("napi_mms NativeDecodeMms start");
302     if (data == nullptr) {
303         TELEPHONY_LOGE("napi_mms data nullptr");
304         return;
305     }
306     auto context = static_cast<DecodeMmsContext *>(data);
307     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
308         TELEPHONY_LOGE("Non-system applications use system APIs!");
309         context->errorCode = TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
310         return;
311     }
312     MmsMsg mmsMsg;
313     bool mmsResult = false;
314     if (context->messageMatchResult == TEXT_MESSAGE_PARAMETER_MATCH) {
315         mmsResult = mmsMsg.DecodeMsg(context->textFilePath);
316     } else if (context->messageMatchResult == RAW_DATA_MESSAGE_PARAMETER_MATCH) {
317         mmsResult = mmsMsg.DecodeMsg(std::move(context->inBuffer), context->inLen);
318     }
319     if (!mmsResult) {
320         TELEPHONY_LOGE("napi_mms DecodeMsg error!");
321         return;
322     }
323     mmsMsg.DumpMms();
324     int32_t messageType = WrapDecodeMmsStatus(static_cast<int32_t>(mmsMsg.GetMmsMessageType()));
325     context->messageType = messageType;
326     if (messageType == MessageType::TYPE_MMS_SEND_CONF) {
327         GetMmsSendConf(mmsMsg, context->sendConf);
328     } else if (messageType == MessageType::TYPE_MMS_SEND_REQ) {
329         GetMmsSendReq(mmsMsg, context->sendReq);
330     } else if (messageType == MessageType::TYPE_MMS_NOTIFICATION_IND) {
331         GetMmsNotificationInd(mmsMsg, context->notificationInd);
332     } else if (messageType == MessageType::TYPE_MMS_RESP_IND) {
333         GetMmsRespInd(mmsMsg, context->respInd);
334     } else if (messageType == MessageType::TYPE_MMS_RETRIEVE_CONF) {
335         GetMmsRetrieveConf(mmsMsg, context->retrieveConf);
336     } else if (messageType == MessageType::TYPE_MMS_ACKNOWLEDGE_IND) {
337         GetMmsAcknowledgeInd(mmsMsg, context->acknowledgeInd);
338     } else if (messageType == MessageType::TYPE_MMS_DELIVERY_IND) {
339         GetMmsDeliveryInd(mmsMsg, context->deliveryInd);
340     } else if (messageType == MessageType::TYPE_MMS_READ_ORIG_IND) {
341         GetMmsReadOrigInd(mmsMsg, context->readOrigInd);
342     } else if (messageType == MessageType::TYPE_MMS_READ_REC_IND) {
343         GetMmsReadRecInd(mmsMsg, context->readRecInd);
344     }
345     getAttachmentByDecodeMms(mmsMsg, *context);
346     context->errorCode = TELEPHONY_ERR_SUCCESS;
347     context->resolved = true;
348     TELEPHONY_LOGI("napi_mms NativeDecodeMms end");
349 }
350 
CreateAttachmentValue(napi_env env,MmsAttachmentContext & context)351 napi_value CreateAttachmentValue(napi_env env, MmsAttachmentContext &context)
352 {
353     TELEPHONY_LOGI("napi_mms CreateAttachmentValue  start");
354     napi_value attachment = nullptr;
355     napi_create_object(env, &attachment);
356     NapiUtil::SetPropertyStringUtf8(env, attachment, "path", context.path);
357     NapiUtil::SetPropertyStringUtf8(env, attachment, "fileName", context.fileName);
358     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentId", context.contentId);
359     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentLocation", context.contentLocation);
360     NapiUtil::SetPropertyInt32(
361         env, attachment, "contentDisposition", formatDispositionValue(context.contentDisposition));
362     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentTransferEncoding", context.contentTransferEncoding);
363     NapiUtil::SetPropertyStringUtf8(env, attachment, "contentType", context.contentType);
364     NapiUtil::SetPropertyBoolean(env, attachment, "isSmil", context.isSmil);
365     NapiUtil::SetPropertyInt32(env, attachment, "charset", context.charset);
366     SetPropertyArray(env, attachment, "inBuff", context);
367     TELEPHONY_LOGI("napi_mms CreateAttachmentValue  end");
368     return attachment;
369 }
370 
ParseAddress(napi_env env,napi_value outValue,const std::string & name,MmsAddress mmsAddress)371 void ParseAddress(napi_env env, napi_value outValue, const std::string &name, MmsAddress mmsAddress)
372 {
373     napi_value addressObj = nullptr;
374     napi_create_object(env, &addressObj);
375     NapiUtil::SetPropertyStringUtf8(env, addressObj, "address", mmsAddress.GetAddressString());
376     NapiUtil::SetPropertyInt32(env, addressObj, "charset", static_cast<int32_t>(mmsAddress.GetAddressCharset()));
377     napi_set_named_property(env, outValue, name.c_str(), addressObj);
378 }
379 
ParseAddressArr(napi_env env,napi_value outValue,const std::string & name,std::vector<MmsAddress> addressArr)380 void ParseAddressArr(napi_env env, napi_value outValue, const std::string &name, std::vector<MmsAddress> addressArr)
381 {
382     napi_value toArr = nullptr;
383     napi_create_array(env, &toArr);
384     for (size_t i = 0; i < addressArr.size(); i++) {
385         napi_value addressObj = nullptr;
386         napi_create_object(env, &addressObj);
387         NapiUtil::SetPropertyStringUtf8(env, addressObj, "address", addressArr[i].GetAddressString());
388         NapiUtil::SetPropertyInt32(env, addressObj, "charset", static_cast<int32_t>(addressArr[i].GetAddressCharset()));
389         napi_set_element(env, toArr, i, addressObj);
390     }
391     napi_set_named_property(env, outValue, name.c_str(), toArr);
392 }
393 
ParseSendReqValue(napi_env env,napi_value object,MmsSendReqContext & sendReqContext)394 void ParseSendReqValue(napi_env env, napi_value object, MmsSendReqContext &sendReqContext)
395 {
396     TELEPHONY_LOGI("napi_mms  ParseSendReqValue start");
397     napi_value sendReqObj = nullptr;
398     napi_create_object(env, &sendReqObj);
399     ParseAddress(env, sendReqObj, "from", sendReqContext.from);
400     ParseAddressArr(env, sendReqObj, "to", sendReqContext.to);
401     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "transactionId", sendReqContext.transactionId);
402     NapiUtil::SetPropertyInt32(env, sendReqObj, "version", sendReqContext.version);
403     NapiUtil::SetPropertyInt64(env, sendReqObj, "date", sendReqContext.date);
404     ParseAddressArr(env, sendReqObj, "cc", sendReqContext.cc);
405     ParseAddressArr(env, sendReqObj, "bcc", sendReqContext.bcc);
406     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "subject", sendReqContext.subject);
407     NapiUtil::SetPropertyInt32(env, sendReqObj, "messageClass", static_cast<int32_t>(sendReqContext.messageClass));
408     NapiUtil::SetPropertyInt32(env, sendReqObj, "expiry", sendReqContext.expiry);
409     NapiUtil::SetPropertyInt32(env, sendReqObj, "priority", static_cast<int32_t>(sendReqContext.priority));
410     NapiUtil::SetPropertyInt32(
411         env, sendReqObj, "senderVisibility", static_cast<int32_t>(sendReqContext.senderVisibility));
412     NapiUtil::SetPropertyInt32(env, sendReqObj, "deliveryReport", static_cast<int32_t>(sendReqContext.deliveryReport));
413     NapiUtil::SetPropertyInt32(env, sendReqObj, "readReport", static_cast<int32_t>(sendReqContext.readReport));
414     NapiUtil::SetPropertyStringUtf8(env, sendReqObj, "contentType", sendReqContext.contentType);
415     napi_set_named_property(env, object, mmsTypeKey.c_str(), sendReqObj);
416     TELEPHONY_LOGI("napi_mms  ParseSendReqValue end");
417 }
418 
ParseSendConfValue(napi_env env,napi_value object,MmsSendConfContext & sendConfContext)419 void ParseSendConfValue(napi_env env, napi_value object, MmsSendConfContext &sendConfContext)
420 {
421     TELEPHONY_LOGI("napi_mms  ParseSendConfValue start");
422     napi_value sendConfObj = nullptr;
423     napi_create_object(env, &sendConfObj);
424     NapiUtil::SetPropertyInt32(env, sendConfObj, "responseState", static_cast<int32_t>(sendConfContext.responseState));
425     NapiUtil::SetPropertyStringUtf8(env, sendConfObj, "transactionId", sendConfContext.transactionId);
426     NapiUtil::SetPropertyInt32(env, sendConfObj, "version", sendConfContext.version);
427     NapiUtil::SetPropertyStringUtf8(env, sendConfObj, "messageId", sendConfContext.messageId);
428     napi_set_named_property(env, object, mmsTypeKey.c_str(), sendConfObj);
429     TELEPHONY_LOGI("napi_mms  ParseSendConfValue end");
430 }
431 
ParseNotificationIndValue(napi_env env,napi_value object,MmsNotificationIndContext & notificationContext)432 void ParseNotificationIndValue(napi_env env, napi_value object, MmsNotificationIndContext &notificationContext)
433 {
434     TELEPHONY_LOGI("napi_mms  ParseNotificationIndValue start");
435     napi_value notificationObj = nullptr;
436     napi_create_object(env, &notificationObj);
437     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "transactionId", notificationContext.transactionId);
438     NapiUtil::SetPropertyInt32(
439         env, notificationObj, "messageClass", static_cast<int32_t>(notificationContext.messageClass));
440     NapiUtil::SetPropertyInt64(env, notificationObj, "messageSize", notificationContext.messageSize);
441     NapiUtil::SetPropertyInt32(env, notificationObj, "expiry", notificationContext.expiry);
442     NapiUtil::SetPropertyInt32(env, notificationObj, "version", notificationContext.version);
443     ParseAddress(env, notificationObj, "from", notificationContext.from);
444     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "subject", notificationContext.subject);
445     NapiUtil::SetPropertyInt32(
446         env, notificationObj, "deliveryReport", static_cast<int32_t>(notificationContext.deliveryReport));
447     NapiUtil::SetPropertyStringUtf8(env, notificationObj, "contentLocation", notificationContext.contentLocation);
448     NapiUtil::SetPropertyInt32(
449         env, notificationObj, "contentClass", static_cast<int32_t>(notificationContext.contentClass));
450     napi_set_named_property(env, object, mmsTypeKey.c_str(), notificationObj);
451     TELEPHONY_LOGI("napi_mms  ParseNotificationIndValue end");
452 }
453 
ParseRespIndValue(napi_env env,napi_value object,MmsRespIndContext & respIndContext)454 void ParseRespIndValue(napi_env env, napi_value object, MmsRespIndContext &respIndContext)
455 {
456     TELEPHONY_LOGI("napi_mms  ParseRespIndValue start");
457     napi_value respIndObj = nullptr;
458     napi_create_object(env, &respIndObj);
459     NapiUtil::SetPropertyStringUtf8(env, respIndObj, "transactionId", respIndContext.transactionId);
460     NapiUtil::SetPropertyInt32(env, respIndObj, "status", static_cast<int32_t>(respIndContext.status));
461     NapiUtil::SetPropertyInt32(env, respIndObj, "version", static_cast<int32_t>(respIndContext.version));
462     NapiUtil::SetPropertyInt32(env, respIndObj, "reportAllowed", static_cast<int32_t>(respIndContext.reportAllowed));
463     napi_set_named_property(env, object, mmsTypeKey.c_str(), respIndObj);
464     TELEPHONY_LOGI("napi_mms  ParseRespIndValue end");
465 }
466 
ParseRetrieveConfValue(napi_env env,napi_value object,MmsRetrieveConfContext & retrieveConfContext)467 void ParseRetrieveConfValue(napi_env env, napi_value object, MmsRetrieveConfContext &retrieveConfContext)
468 {
469     TELEPHONY_LOGI("napi_mms  ParseRetrieveConfValue start");
470     napi_value retrieveConfObj = nullptr;
471     napi_create_object(env, &retrieveConfObj);
472     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "transactionId", retrieveConfContext.transactionId);
473     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "messageId", retrieveConfContext.messageId);
474     NapiUtil::SetPropertyInt64(env, retrieveConfObj, "date", retrieveConfContext.date);
475     NapiUtil::SetPropertyInt32(env, retrieveConfObj, "version", retrieveConfContext.version);
476     ParseAddressArr(env, retrieveConfObj, "to", retrieveConfContext.to);
477     ParseAddress(env, retrieveConfObj, "from", retrieveConfContext.from);
478     ParseAddressArr(env, retrieveConfObj, "cc", retrieveConfContext.cc);
479     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "subject", retrieveConfContext.subject);
480     NapiUtil::SetPropertyInt32(env, retrieveConfObj, "priority", static_cast<int32_t>(retrieveConfContext.priority));
481     NapiUtil::SetPropertyInt32(
482         env, retrieveConfObj, "deliveryReport", static_cast<int32_t>(retrieveConfContext.deliveryReport));
483     NapiUtil::SetPropertyInt32(
484         env, retrieveConfObj, "readReport", static_cast<int32_t>(retrieveConfContext.readReport));
485     NapiUtil::SetPropertyInt32(
486         env, retrieveConfObj, "retrieveStatus", static_cast<int32_t>(retrieveConfContext.retrieveStatus));
487     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "retrieveText", retrieveConfContext.retrieveText);
488     NapiUtil::SetPropertyStringUtf8(env, retrieveConfObj, "contentType", retrieveConfContext.contentType);
489     napi_set_named_property(env, object, mmsTypeKey.c_str(), retrieveConfObj);
490     TELEPHONY_LOGI("napi_mms  ParseRetrieveConfValue end");
491 }
492 
ParseAcknowledgeIndValue(napi_env env,napi_value object,MmsAcknowledgeIndContext & acknowledgeIndContext)493 void ParseAcknowledgeIndValue(napi_env env, napi_value object, MmsAcknowledgeIndContext &acknowledgeIndContext)
494 {
495     TELEPHONY_LOGI("napi_mms  ParseAcknowledgeIndValue start");
496     napi_value acknowledgeIndObj = nullptr;
497     napi_create_object(env, &acknowledgeIndObj);
498     NapiUtil::SetPropertyStringUtf8(env, acknowledgeIndObj, "transactionId", acknowledgeIndContext.transactionId);
499     NapiUtil::SetPropertyInt32(env, acknowledgeIndObj, "version", acknowledgeIndContext.version);
500     NapiUtil::SetPropertyInt32(
501         env, acknowledgeIndObj, "reportAllowed", static_cast<int32_t>(acknowledgeIndContext.reportAllowed));
502     napi_set_named_property(env, object, mmsTypeKey.c_str(), acknowledgeIndObj);
503     TELEPHONY_LOGI("napi_mms  ParseAcknowledgeIndValue end");
504 }
505 
ParseDeliveryIndValue(napi_env env,napi_value object,MmsDeliveryIndContext & deliveryIndContext)506 void ParseDeliveryIndValue(napi_env env, napi_value object, MmsDeliveryIndContext &deliveryIndContext)
507 {
508     TELEPHONY_LOGI("napi_mms  ParseDeliveryIndValue start");
509     napi_value deliveryIndObj = nullptr;
510     napi_create_object(env, &deliveryIndObj);
511     NapiUtil::SetPropertyStringUtf8(env, deliveryIndObj, "messageId", deliveryIndContext.messageId);
512     NapiUtil::SetPropertyInt64(env, deliveryIndObj, "date", deliveryIndContext.date);
513     ParseAddressArr(env, deliveryIndObj, "to", deliveryIndContext.to);
514     NapiUtil::SetPropertyInt32(env, deliveryIndObj, "status", static_cast<int32_t>(deliveryIndContext.status));
515     NapiUtil::SetPropertyInt32(env, deliveryIndObj, "version", deliveryIndContext.version);
516     napi_set_named_property(env, object, mmsTypeKey.c_str(), deliveryIndObj);
517     TELEPHONY_LOGI("napi_mms  ParseDeliveryIndValue end");
518 }
519 
ParseReadOrigIndValue(napi_env env,napi_value object,MmsReadOrigIndContext & readOrigIndContext)520 void ParseReadOrigIndValue(napi_env env, napi_value object, MmsReadOrigIndContext &readOrigIndContext)
521 {
522     TELEPHONY_LOGI("napi_mms  ParseReadOrigIndValue start");
523     napi_value readOrigIndObj = nullptr;
524     napi_create_object(env, &readOrigIndObj);
525     NapiUtil::SetPropertyInt32(env, readOrigIndObj, "version", readOrigIndContext.version);
526     NapiUtil::SetPropertyStringUtf8(env, readOrigIndObj, "messageId", readOrigIndContext.messageId);
527     ParseAddressArr(env, readOrigIndObj, "to", readOrigIndContext.to);
528     ParseAddress(env, readOrigIndObj, "from", readOrigIndContext.from);
529     NapiUtil::SetPropertyInt64(env, readOrigIndObj, "date", readOrigIndContext.date);
530     NapiUtil::SetPropertyInt32(env, readOrigIndObj, "readStatus", static_cast<int32_t>(readOrigIndContext.readStatus));
531     napi_set_named_property(env, object, mmsTypeKey.c_str(), readOrigIndObj);
532     TELEPHONY_LOGI("napi_mms  ParseReadOrigIndValue end");
533 }
534 
ParseReadRecIndValue(napi_env env,napi_value object,MmsReadRecIndContext & readRecIndContext)535 void ParseReadRecIndValue(napi_env env, napi_value object, MmsReadRecIndContext &readRecIndContext)
536 {
537     TELEPHONY_LOGI("napi_mms  ParseReadRecIndValue start");
538     napi_value readRecIndObj = nullptr;
539     napi_create_object(env, &readRecIndObj);
540     NapiUtil::SetPropertyInt32(env, readRecIndObj, "version", readRecIndContext.version);
541     NapiUtil::SetPropertyStringUtf8(env, readRecIndObj, "messageId", readRecIndContext.messageId);
542     ParseAddressArr(env, readRecIndObj, "to", readRecIndContext.to);
543     ParseAddress(env, readRecIndObj, "from", readRecIndContext.from);
544     NapiUtil::SetPropertyInt64(env, readRecIndObj, "date", readRecIndContext.date);
545     NapiUtil::SetPropertyInt32(env, readRecIndObj, "readStatus", static_cast<int32_t>(readRecIndContext.readStatus));
546     napi_set_named_property(env, object, mmsTypeKey.c_str(), readRecIndObj);
547     TELEPHONY_LOGI("napi_mms  ParseReadRecIndValue end");
548 }
549 
CreateDecodeMmsValue(napi_env env,DecodeMmsContext & asyncContext)550 napi_value CreateDecodeMmsValue(napi_env env, DecodeMmsContext &asyncContext)
551 {
552     TELEPHONY_LOGI("napi_mms  CreateDecodeMmsValue start");
553     napi_value object = nullptr;
554     napi_value attachmentArr = nullptr;
555     napi_create_object(env, &object);
556     napi_create_array(env, &attachmentArr);
557     NapiUtil::SetPropertyInt32(env, object, "messageType", static_cast<int32_t>(asyncContext.messageType));
558     if (asyncContext.attachment.size() > 0) {
559         int i = 0;
560         for (std::vector<MmsAttachmentContext>::iterator it = asyncContext.attachment.begin();
561              it != asyncContext.attachment.end(); ++it) {
562             napi_value attachNapi = CreateAttachmentValue(env, *it);
563             napi_set_element(env, attachmentArr, i, attachNapi);
564             i++;
565         }
566         napi_set_named_property(env, object, attachmentKey.c_str(), attachmentArr);
567     }
568     int32_t messageType = asyncContext.messageType;
569     if (messageType == MessageType::TYPE_MMS_SEND_REQ) {
570         ParseSendReqValue(env, object, asyncContext.sendReq);
571     } else if (messageType == MessageType::TYPE_MMS_SEND_CONF) {
572         ParseSendConfValue(env, object, asyncContext.sendConf);
573     } else if (messageType == MessageType::TYPE_MMS_NOTIFICATION_IND) {
574         ParseNotificationIndValue(env, object, asyncContext.notificationInd);
575     } else if (messageType == MessageType::TYPE_MMS_RESP_IND) {
576         ParseRespIndValue(env, object, asyncContext.respInd);
577     } else if (messageType == MessageType::TYPE_MMS_RETRIEVE_CONF) {
578         ParseRetrieveConfValue(env, object, asyncContext.retrieveConf);
579     } else if (messageType == MessageType::TYPE_MMS_ACKNOWLEDGE_IND) {
580         ParseAcknowledgeIndValue(env, object, asyncContext.acknowledgeInd);
581     } else if (messageType == MessageType::TYPE_MMS_DELIVERY_IND) {
582         ParseDeliveryIndValue(env, object, asyncContext.deliveryInd);
583     } else if (messageType == MessageType::TYPE_MMS_READ_ORIG_IND) {
584         ParseReadOrigIndValue(env, object, asyncContext.readOrigInd);
585     } else if (messageType == MessageType::TYPE_MMS_READ_REC_IND) {
586         ParseReadRecIndValue(env, object, asyncContext.readRecInd);
587     }
588     TELEPHONY_LOGI("napi_mms  CreateDecodeMmsValue end");
589     return object;
590 }
591 
DecodeMmsCallback(napi_env env,napi_status status,void * data)592 void DecodeMmsCallback(napi_env env, napi_status status, void *data)
593 {
594     TELEPHONY_LOGI("napi_mms DecodeMmsCallback start");
595     if (data == nullptr) {
596         TELEPHONY_LOGE("data nullptr");
597         return;
598     }
599     auto decodeMmsContext = static_cast<DecodeMmsContext *>(data);
600     napi_value callbackValue = nullptr;
601 
602     if (status == napi_ok) {
603         if (decodeMmsContext->resolved) {
604             callbackValue = CreateDecodeMmsValue(env, *decodeMmsContext);
605         } else {
606             JsError error = NapiUtil::ConverErrorMessageForJs(decodeMmsContext->errorCode);
607             callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
608         }
609     } else {
610         callbackValue =
611             NapiUtil::CreateErrorMessage(env, "decode mms error,cause napi_status = " + std::to_string(status));
612     }
613     NapiUtil::Handle2ValueCallback(env, decodeMmsContext, callbackValue);
614     TELEPHONY_LOGI("napi_mms DecodeMmsCallback end");
615 }
616 
ParseDecodeMmsParam(napi_env env,napi_value object,DecodeMmsContext & context)617 void ParseDecodeMmsParam(napi_env env, napi_value object, DecodeMmsContext &context)
618 {
619     TELEPHONY_LOGI("napi_mms ParseDecodeMmsParam start");
620     if (context.messageMatchResult == TEXT_MESSAGE_PARAMETER_MATCH) {
621         TELEPHONY_LOGI("napi_mms  messageMatchResult == TEXT");
622         char contentChars[MAX_TEXT_SHORT_MESSAGE_LENGTH] = {0};
623         size_t contentLength = 0;
624         napi_get_value_string_utf8(env, object, contentChars, MAX_TEXT_SHORT_MESSAGE_LENGTH, &contentLength);
625         context.textFilePath = std::string(contentChars, 0, contentLength);
626     } else if (context.messageMatchResult == RAW_DATA_MESSAGE_PARAMETER_MATCH) {
627         TELEPHONY_LOGI("napi_mms  messageMatchResult  == RAW_DATA");
628         napi_value elementValue = nullptr;
629         int32_t element = 0;
630         uint32_t arrayLength = 0;
631         napi_get_array_length(env, object, &arrayLength);
632         if (arrayLength > MAX_MMS_MSG_PART_LEN) {
633             TELEPHONY_LOGE("arrayLength over size error");
634             return;
635         }
636         context.inLen = arrayLength;
637         TELEPHONY_LOGI("napi_mms ParseDecodeMmsParam arrayLength = %{public}d", arrayLength);
638         context.inBuffer = std::make_unique<char[]>(arrayLength);
639         if (context.inBuffer == nullptr) {
640             TELEPHONY_LOGE("make unique error");
641             return;
642         }
643         for (uint32_t i = 0; i < arrayLength; i++) {
644             napi_get_element(env, object, i, &elementValue);
645             napi_get_value_int32(env, elementValue, &element);
646             context.inBuffer[i] = (char)element;
647         }
648     }
649     TELEPHONY_LOGI("napi_mms ParseDecodeMmsParam end");
650 }
651 
GetMatchDecodeMmsResult(napi_env env,const napi_value parameters[],size_t parameterCount)652 int32_t GetMatchDecodeMmsResult(napi_env env, const napi_value parameters[], size_t parameterCount)
653 {
654     TELEPHONY_LOGI("napi_mms GetMatchDecodeMmsResult start");
655     int32_t paramsTypeMatched = MESSAGE_PARAMETER_NOT_MATCH;
656     switch (parameterCount) {
657         case ONE_PARAMETER:
658             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object }) ||
659                                 NapiUtil::MatchParameters(env, parameters, { napi_string });
660             break;
661         case TWO_PARAMETERS:
662             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, { napi_object, napi_function }) ||
663                                 NapiUtil::MatchParameters(env, parameters, { napi_string, napi_function });
664             break;
665         default:
666             return MESSAGE_PARAMETER_NOT_MATCH;
667     }
668     if (!paramsTypeMatched) {
669         return MESSAGE_PARAMETER_NOT_MATCH;
670     }
671 
672     bool filePathIsStr = NapiUtil::MatchValueType(env, parameters[0], napi_string);
673     bool filePathIsObj = NapiUtil::MatchValueType(env, parameters[0], napi_object);
674     bool filePathIsArray = false;
675     if (filePathIsObj) {
676         napi_is_array(env, parameters[0], &filePathIsArray);
677     }
678     if (filePathIsStr) {
679         return TEXT_MESSAGE_PARAMETER_MATCH;
680     } else if (filePathIsArray) {
681         return RAW_DATA_MESSAGE_PARAMETER_MATCH;
682     } else {
683         return MESSAGE_PARAMETER_NOT_MATCH;
684     }
685 }
686 
DecodeMms(napi_env env,napi_callback_info info)687 napi_value NapiMms::DecodeMms(napi_env env, napi_callback_info info)
688 {
689     TELEPHONY_LOGI("napi_mms DecodeMms start");
690     napi_value result = nullptr;
691     size_t parameterCount = TWO_PARAMETERS;
692     napi_value parameters[TWO_PARAMETERS] = { 0 };
693     napi_value thisVar = nullptr;
694     void *data = nullptr;
695     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
696     int32_t messageMatchResult = GetMatchDecodeMmsResult(env, parameters, parameterCount);
697     if (messageMatchResult == MESSAGE_PARAMETER_NOT_MATCH) {
698         TELEPHONY_LOGE("DecodeMms parameter matching failed.");
699         NapiUtil::ThrowParameterError(env);
700         return nullptr;
701     }
702     auto context = std::make_unique<DecodeMmsContext>().release();
703     if (context == nullptr) {
704         TELEPHONY_LOGE("DecodeMms DecodeMmsContext is nullptr.");
705         NapiUtil::ThrowParameterError(env);
706         return nullptr;
707     }
708     context->messageMatchResult = messageMatchResult;
709     ParseDecodeMmsParam(env, parameters[0], *context);
710     if (parameterCount == TWO_PARAMETERS) {
711         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
712     }
713     result = NapiUtil::HandleAsyncWork(env, context, "DecodeMms", NativeDecodeMms, DecodeMmsCallback);
714     TELEPHONY_LOGI("napi_mms DecodeMms end");
715     return result;
716 }
717 
MatchEncodeMms(napi_env env,const napi_value parameters[],size_t parameterCount)718 bool MatchEncodeMms(napi_env env, const napi_value parameters[], size_t parameterCount)
719 {
720     TELEPHONY_LOGI("napi_mms MatchEncodeMms start");
721     bool paramsTypeMatched = false;
722     switch (parameterCount) {
723         case 1:
724             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, {napi_object});
725             break;
726         case 2:
727             paramsTypeMatched = NapiUtil::MatchParameters(env, parameters, {napi_object, napi_function});
728             break;
729         default:
730             return false;
731     }
732     if (!paramsTypeMatched) {
733         TELEPHONY_LOGE("encodeMms parameter not match");
734         return false;
735     }
736     if (NapiUtil::HasNamedProperty(env, parameters[0], "attachment")) {
737         return NapiUtil::MatchObjectProperty(env, parameters[0],
738             {
739                 {"messageType", napi_number},
740                 {"mmsType", napi_object},
741                 {"attachment", napi_object},
742             });
743     } else {
744         return NapiUtil::MatchObjectProperty(env, parameters[0],
745             {
746                 {"messageType", napi_number},
747                 {"mmsType", napi_object},
748             });
749     }
750     return false;
751 }
752 
GetNapiBooleanValue(napi_env env,napi_value napiValue,std::string name,bool defValue=false)753 bool GetNapiBooleanValue(napi_env env, napi_value napiValue, std::string name, bool defValue = false)
754 {
755     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
756     if (value != nullptr) {
757         bool result = defValue;
758         napi_get_value_bool(env, value, &result);
759         return result;
760     } else {
761         return defValue;
762     }
763 }
764 
GetNapiStringValue(napi_env env,napi_value napiValue,std::string name,std::string defValue="")765 std::string GetNapiStringValue(napi_env env, napi_value napiValue, std::string name, std::string defValue = "")
766 {
767     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
768     if (value != nullptr) {
769         return NapiUtil::GetStringFromValue(env, value);
770     } else {
771         return defValue;
772     }
773 }
774 
GetNapiInt32Value(napi_env env,napi_value napiValue,std::string name,int32_t defValue=0)775 int32_t GetNapiInt32Value(napi_env env, napi_value napiValue, std::string name, int32_t defValue = 0)
776 {
777     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
778     if (value != nullptr) {
779         int32_t intValue = 0;
780         napi_status getIntStatus = napi_get_value_int32(env, value, &intValue);
781         if (getIntStatus == napi_ok) {
782             return intValue;
783         }
784     }
785     return defValue;
786 }
787 
GetNapiInt64Value(napi_env env,napi_value napiValue,std::string name,int64_t defValue=0)788 int64_t GetNapiInt64Value(napi_env env, napi_value napiValue, std::string name, int64_t defValue = 0)
789 {
790     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
791     if (value != nullptr) {
792         int64_t intValue = 0;
793         napi_status getIntStatus = napi_get_value_int64(env, value, &intValue);
794         if (getIntStatus == napi_ok) {
795             return intValue;
796         }
797     }
798     return defValue;
799 }
800 
GetNapiUint32Value(napi_env env,napi_value napiValue,std::string name,uint32_t defValue=0)801 uint32_t GetNapiUint32Value(napi_env env, napi_value napiValue, std::string name, uint32_t defValue = 0)
802 {
803     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
804     if (value != nullptr) {
805         uint32_t uint32Value = 0;
806         napi_status status = napi_get_value_uint32(env, value, &uint32Value);
807         if (status == napi_ok) {
808             return uint32Value;
809         }
810     }
811     return defValue;
812 }
813 
GetNapiUint8Value(napi_env env,napi_value napiValue,const std::string & name,uint8_t defValue=0)814 uint8_t GetNapiUint8Value(napi_env env, napi_value napiValue, const std::string &name, uint8_t defValue = 0)
815 {
816     return uint8_t(GetNapiInt32Value(env, napiValue, name, defValue));
817 }
818 
formatMmsCharSet(int32_t charsetInt)819 MmsCharSets formatMmsCharSet(int32_t charsetInt)
820 {
821     switch (charsetInt) {
822         case static_cast<int32_t>(MmsCharSets::BIG5):
823             return MmsCharSets::BIG5;
824         case static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2):
825             return MmsCharSets::ISO_10646_UCS_2;
826         case static_cast<int32_t>(MmsCharSets::ISO_8859_1):
827             return MmsCharSets::ISO_8859_1;
828         case static_cast<int32_t>(MmsCharSets::ISO_8859_2):
829             return MmsCharSets::ISO_8859_2;
830         case static_cast<int32_t>(MmsCharSets::ISO_8859_3):
831             return MmsCharSets::ISO_8859_3;
832         case static_cast<int32_t>(MmsCharSets::ISO_8859_4):
833             return MmsCharSets::ISO_8859_4;
834         case static_cast<int32_t>(MmsCharSets::ISO_8859_5):
835             return MmsCharSets::ISO_8859_5;
836         case static_cast<int32_t>(MmsCharSets::ISO_8859_6):
837             return MmsCharSets::ISO_8859_6;
838         case static_cast<int32_t>(MmsCharSets::ISO_8859_7):
839             return MmsCharSets::ISO_8859_7;
840         case static_cast<int32_t>(MmsCharSets::ISO_8859_8):
841             return MmsCharSets::ISO_8859_8;
842         case static_cast<int32_t>(MmsCharSets::ISO_8859_9):
843             return MmsCharSets::ISO_8859_9;
844         case static_cast<int32_t>(MmsCharSets::SHIFT_JIS):
845             return MmsCharSets::SHIFT_JIS;
846         case static_cast<int32_t>(MmsCharSets::US_ASCII):
847             return MmsCharSets::US_ASCII;
848         default:
849             return MmsCharSets::UTF_8;
850     }
851 }
852 
ReadMmsAddress(napi_env env,napi_value value)853 MmsAddress ReadMmsAddress(napi_env env, napi_value value)
854 {
855     std::string address = GetNapiStringValue(env, value, "address");
856     int32_t charset = GetNapiInt32Value(env, value, "charset");
857     MmsAddress mmsAddress(address, formatMmsCharSet(charset));
858     return mmsAddress;
859 }
860 
ReadMmsAddress(napi_env env,napi_value napiValue,std::string name,std::vector<MmsAddress> & array)861 void ReadMmsAddress(napi_env env, napi_value napiValue, std::string name, std::vector<MmsAddress> &array)
862 {
863     napi_value value = NapiUtil::GetNamedProperty(env, napiValue, name);
864     if (value != nullptr) {
865         uint32_t arrayLength = 0;
866         napi_get_array_length(env, value, &arrayLength);
867         napi_value elementValue = nullptr;
868         for (uint32_t i = 0; i < arrayLength; i++) {
869             napi_get_element(env, value, i, &elementValue);
870             MmsAddress eachMmsAddress = ReadMmsAddress(env, elementValue);
871             array.push_back(eachMmsAddress);
872         }
873     }
874 }
875 
HasNamedProperty(napi_env env,napi_value napiValue,std::vector<std::string> checkName)876 bool HasNamedProperty(napi_env env, napi_value napiValue, std::vector<std::string> checkName)
877 {
878     for (std::string item : checkName) {
879         if (!NapiUtil::HasNamedProperty(env, napiValue, item)) {
880             TELEPHONY_LOGE("Missed param with %{public}s", item.c_str());
881             return false;
882         }
883     }
884     return true;
885 }
886 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsSendReqContext & sendReq)887 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsSendReqContext &sendReq)
888 {
889     TELEPHONY_LOGI("mms_napi ReadEncodeMmsType start");
890     if (!HasNamedProperty(env, napiValue, {"from", "transactionId", "contentType"})) {
891         return false;
892     }
893     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
894     sendReq.from = ReadMmsAddress(env, from);
895     if (sendReq.from.GetAddressString().empty()) {
896         return false;
897     }
898     ReadMmsAddress(env, napiValue, "to", sendReq.to);
899     sendReq.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
900     sendReq.version = GetNapiInt32Value(env, napiValue, "version");
901     sendReq.date = GetNapiInt64Value(env, napiValue, "date");
902     ReadMmsAddress(env, napiValue, "cc", sendReq.cc);
903     ReadMmsAddress(env, napiValue, "bcc", sendReq.bcc);
904     sendReq.subject = GetNapiStringValue(env, napiValue, "subject");
905     sendReq.messageClass = GetNapiUint8Value(env, napiValue, "messageClass");
906     sendReq.expiry = GetNapiInt32Value(env, napiValue, "expiry");
907     sendReq.priority = GetNapiUint8Value(env, napiValue, "priority");
908     sendReq.senderVisibility = GetNapiUint8Value(env, napiValue, "senderVisibility");
909     sendReq.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
910     sendReq.readReport = GetNapiUint8Value(env, napiValue, "readReport");
911     sendReq.contentType = GetNapiStringValue(env, napiValue, "contentType");
912     TELEPHONY_LOGI("mms_napi ReadEncodeMmsType end");
913     return true;
914 }
915 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsSendConfContext & sendConf)916 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsSendConfContext &sendConf)
917 {
918     if (!HasNamedProperty(env, napiValue, {"responseState", "transactionId"})) {
919         return false;
920     }
921     sendConf.responseState = GetNapiUint8Value(env, napiValue, "responseState");
922     sendConf.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
923     sendConf.version = GetNapiInt32Value(env, napiValue, "version");
924     sendConf.messageId = GetNapiStringValue(env, napiValue, "messageId");
925     return true;
926 }
927 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsNotificationIndContext & notificationInd)928 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsNotificationIndContext &notificationInd)
929 {
930     std::vector<std::string> checkName = {
931         "transactionId", "messageClass", "messageSize", "expiry", "contentLocation"};
932     if (!HasNamedProperty(env, napiValue, checkName)) {
933         return false;
934     }
935     notificationInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
936     notificationInd.messageClass = GetNapiUint8Value(env, napiValue, "messageClass");
937     notificationInd.messageSize = GetNapiInt64Value(env, napiValue, "messageSize");
938     notificationInd.expiry = GetNapiInt32Value(env, napiValue, "expiry");
939     notificationInd.version = static_cast<uint16_t>(GetNapiInt32Value(env, napiValue, "version"));
940     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
941     notificationInd.from = ReadMmsAddress(env, from);
942     notificationInd.subject = GetNapiStringValue(env, napiValue, "subject");
943     notificationInd.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
944     notificationInd.contentLocation = GetNapiStringValue(env, napiValue, "contentLocation");
945     notificationInd.contentClass = GetNapiInt32Value(env, napiValue, "contentClass");
946     notificationInd.charset = GetNapiUint32Value(env, napiValue, "charset", static_cast<uint32_t>(MmsCharSets::UTF_8));
947     return true;
948 }
949 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsRespIndContext & respInd)950 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsRespIndContext &respInd)
951 {
952     if (!HasNamedProperty(env, napiValue, {"transactionId", "status"})) {
953         return false;
954     }
955     respInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
956     respInd.status = GetNapiUint8Value(env, napiValue, "status");
957     respInd.version = GetNapiInt32Value(env, napiValue, "version");
958     respInd.reportAllowed = GetNapiUint8Value(env, napiValue, "reportAllowed");
959     TELEPHONY_LOGI("respInd.reportAllowed = %{public}d", respInd.reportAllowed);
960     return true;
961 }
962 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsRetrieveConfContext & retrieveConf)963 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsRetrieveConfContext &retrieveConf)
964 {
965     if (!HasNamedProperty(env, napiValue, {"transactionId", "messageId", "date", "contentType"})) {
966         return false;
967     }
968     retrieveConf.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
969     retrieveConf.messageId = GetNapiStringValue(env, napiValue, "messageId");
970     retrieveConf.date = GetNapiInt64Value(env, napiValue, "date");
971     retrieveConf.version = (uint16_t)GetNapiInt32Value(env, napiValue, "version");
972     ReadMmsAddress(env, napiValue, "to", retrieveConf.to);
973     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
974     retrieveConf.from = ReadMmsAddress(env, from);
975     ReadMmsAddress(env, napiValue, "cc", retrieveConf.cc);
976     retrieveConf.subject = GetNapiStringValue(env, napiValue, "subject");
977     retrieveConf.priority = GetNapiUint8Value(env, napiValue, "priority");
978     retrieveConf.deliveryReport = GetNapiUint8Value(env, napiValue, "deliveryReport");
979     retrieveConf.readReport = GetNapiUint8Value(env, napiValue, "readReport");
980     retrieveConf.retrieveStatus = GetNapiUint8Value(env, napiValue, "retrieveStatus");
981     retrieveConf.retrieveText = GetNapiStringValue(env, napiValue, "retrieveText");
982     retrieveConf.contentType = GetNapiStringValue(env, napiValue, "contentType");
983     return true;
984 }
985 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsAcknowledgeIndContext & acknowledgeInd)986 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsAcknowledgeIndContext &acknowledgeInd)
987 {
988     if (!HasNamedProperty(env, napiValue, {"transactionId"})) {
989         return false;
990     }
991     acknowledgeInd.transactionId = GetNapiStringValue(env, napiValue, "transactionId");
992     acknowledgeInd.version = GetNapiInt32Value(env, napiValue, "version");
993     acknowledgeInd.reportAllowed = GetNapiUint8Value(env, napiValue, "reportAllowed");
994     return true;
995 }
996 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsDeliveryIndContext & deliveryInd)997 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsDeliveryIndContext &deliveryInd)
998 {
999     if (!HasNamedProperty(env, napiValue, {"messageId", "date", "to", "status"})) {
1000         return false;
1001     }
1002     deliveryInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
1003     deliveryInd.date = GetNapiInt64Value(env, napiValue, "date");
1004     ReadMmsAddress(env, napiValue, "to", deliveryInd.to);
1005     deliveryInd.status = GetNapiUint8Value(env, napiValue, "status");
1006     deliveryInd.version = GetNapiInt32Value(env, napiValue, "version");
1007     return true;
1008 }
1009 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsReadOrigIndContext & readOrigInd)1010 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsReadOrigIndContext &readOrigInd)
1011 {
1012     if (!HasNamedProperty(env, napiValue, {"version", "messageId", "to", "from", "date", "readStatus"})) {
1013         return false;
1014     }
1015     readOrigInd.version = GetNapiInt32Value(env, napiValue, "version");
1016     readOrigInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
1017     ReadMmsAddress(env, napiValue, "to", readOrigInd.to);
1018     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
1019     readOrigInd.from = ReadMmsAddress(env, from);
1020     readOrigInd.date = GetNapiInt64Value(env, napiValue, "date");
1021     readOrigInd.readStatus = GetNapiUint8Value(env, napiValue, "readStatus");
1022     return true;
1023 }
1024 
ReadEncodeMmsType(napi_env env,napi_value napiValue,MmsReadRecIndContext & readRecInd)1025 bool ReadEncodeMmsType(napi_env env, napi_value napiValue, MmsReadRecIndContext &readRecInd)
1026 {
1027     if (!HasNamedProperty(env, napiValue, {"version", "messageId", "to", "from", "date", "readStatus"})) {
1028         return false;
1029     }
1030     readRecInd.version = GetNapiInt32Value(env, napiValue, "version");
1031     readRecInd.messageId = GetNapiStringValue(env, napiValue, "messageId");
1032     ReadMmsAddress(env, napiValue, "to", readRecInd.to);
1033     napi_value from = NapiUtil::GetNamedProperty(env, napiValue, "from");
1034     readRecInd.from = ReadMmsAddress(env, from);
1035     readRecInd.date = GetNapiInt64Value(env, napiValue, "date");
1036     readRecInd.readStatus = GetNapiUint8Value(env, napiValue, "readStatus");
1037     TELEPHONY_LOGI("context->readRecInd.readStatus = %{public}d", readRecInd.readStatus);
1038     return true;
1039 }
1040 
BuildMmsAttachment(napi_env env,napi_value value)1041 MmsAttachmentContext BuildMmsAttachment(napi_env env, napi_value value)
1042 {
1043     MmsAttachmentContext attachmentContext;
1044     attachmentContext.path = GetNapiStringValue(env, value, "path");
1045     attachmentContext.fileName = GetNapiStringValue(env, value, "fileName");
1046     attachmentContext.contentId = GetNapiStringValue(env, value, "contentId");
1047     attachmentContext.contentLocation = GetNapiStringValue(env, value, "contentLocation");
1048     attachmentContext.contentDisposition = parseDispositionValue(GetNapiInt32Value(env, value, "contentDisposition"));
1049     attachmentContext.contentTransferEncoding = GetNapiStringValue(env, value, "contentTransferEncoding");
1050     attachmentContext.contentType = GetNapiStringValue(env, value, "contentType");
1051     attachmentContext.isSmil = GetNapiBooleanValue(env, value, "isSmil");
1052     napi_value inBuffValue = NapiUtil::GetNamedProperty(env, value, "inBuff");
1053     if (inBuffValue != nullptr) {
1054         uint32_t arrayLength = 0;
1055         int32_t elementInt = 0;
1056         napi_get_array_length(env, inBuffValue, &arrayLength);
1057         if (arrayLength > MAX_MMS_MSG_PART_LEN) {
1058             TELEPHONY_LOGE("arrayLength over size error");
1059             return attachmentContext;
1060         }
1061         attachmentContext.inBuffLen = arrayLength;
1062         attachmentContext.inBuff = std::make_unique<char[]>(arrayLength);
1063         if (attachmentContext.inBuff == nullptr) {
1064             TELEPHONY_LOGE("make unique error");
1065             return attachmentContext;
1066         }
1067         napi_value elementValue = nullptr;
1068         for (uint32_t i = 0; i < arrayLength; i++) {
1069             napi_get_element(env, inBuffValue, i, &elementValue);
1070             napi_get_value_int32(env, elementValue, &elementInt);
1071             attachmentContext.inBuff[i] = static_cast<char>(elementInt);
1072         }
1073     }
1074     attachmentContext.charset = GetNapiInt32Value(env, value, "charset", static_cast<int32_t>(MmsCharSets::UTF_8));
1075     return attachmentContext;
1076 }
1077 
ReadEncodeMmsAttachment(napi_env env,napi_value value,std::vector<MmsAttachmentContext> & attachment)1078 bool ReadEncodeMmsAttachment(napi_env env, napi_value value, std::vector<MmsAttachmentContext> &attachment)
1079 {
1080     uint32_t arrayLength = 0;
1081     napi_get_array_length(env, value, &arrayLength);
1082     napi_value elementValue = nullptr;
1083     for (uint32_t i = 0; i < arrayLength; i++) {
1084         napi_get_element(env, value, i, &elementValue);
1085         MmsAttachmentContext mmsAttachmentContext = BuildMmsAttachment(env, elementValue);
1086         attachment.push_back(std::move(mmsAttachmentContext));
1087     }
1088     return true;
1089 }
1090 
EncodeMmsType(napi_env env,napi_value mmsTypeValue,EncodeMmsContext & context)1091 bool EncodeMmsType(napi_env env, napi_value mmsTypeValue, EncodeMmsContext &context)
1092 {
1093     bool result = false;
1094     switch (context.messageType) {
1095         case TYPE_MMS_SEND_REQ:
1096             result = ReadEncodeMmsType(env, mmsTypeValue, context.sendReq);
1097             break;
1098         case TYPE_MMS_SEND_CONF:
1099             result = ReadEncodeMmsType(env, mmsTypeValue, context.sendConf);
1100             break;
1101         case TYPE_MMS_NOTIFICATION_IND:
1102             result = ReadEncodeMmsType(env, mmsTypeValue, context.notificationInd);
1103             break;
1104         case TYPE_MMS_RESP_IND:
1105             result = ReadEncodeMmsType(env, mmsTypeValue, context.respInd);
1106             break;
1107         case TYPE_MMS_RETRIEVE_CONF:
1108             result = ReadEncodeMmsType(env, mmsTypeValue, context.retrieveConf);
1109             break;
1110         case TYPE_MMS_ACKNOWLEDGE_IND:
1111             result = ReadEncodeMmsType(env, mmsTypeValue, context.acknowledgeInd);
1112             break;
1113         case TYPE_MMS_DELIVERY_IND:
1114             result = ReadEncodeMmsType(env, mmsTypeValue, context.deliveryInd);
1115             break;
1116         case TYPE_MMS_READ_ORIG_IND:
1117             result = ReadEncodeMmsType(env, mmsTypeValue, context.readOrigInd);
1118             break;
1119         case TYPE_MMS_READ_REC_IND:
1120             result = ReadEncodeMmsType(env, mmsTypeValue, context.readRecInd);
1121             break;
1122         default:
1123             TELEPHONY_LOGE("napi_mms EncodeMms param messageType is incorrect");
1124             break;
1125     }
1126     return result;
1127 }
1128 
ParseEncodeMmsParam(napi_env env,napi_value object,EncodeMmsContext & context)1129 bool ParseEncodeMmsParam(napi_env env, napi_value object, EncodeMmsContext &context)
1130 {
1131     TELEPHONY_LOGI("mms_napi ParseEncodeMmsParam start");
1132     napi_value messageTypeValue = NapiUtil::GetNamedProperty(env, object, "messageType");
1133     if (messageTypeValue == nullptr) {
1134         TELEPHONY_LOGE("messageTypeValue == nullptr");
1135         return false;
1136     }
1137     napi_get_value_int32(env, messageTypeValue, &context.messageType);
1138 
1139     napi_value mmsTypeValue = NapiUtil::GetNamedProperty(env, object, "mmsType");
1140     if (mmsTypeValue == nullptr) {
1141         TELEPHONY_LOGE("mmsTypeValue == nullptr");
1142         return false;
1143     }
1144     bool result = EncodeMmsType(env, mmsTypeValue, context);
1145     if (NapiUtil::HasNamedProperty(env, object, "attachment")) {
1146         napi_value napiAttachment = NapiUtil::GetNamedProperty(env, object, "attachment");
1147         if (napiAttachment != nullptr) {
1148             result = ReadEncodeMmsAttachment(env, napiAttachment, context.attachment);
1149         }
1150     }
1151     return result;
1152 }
1153 
setAttachmentToCore(MmsMsg & mmsMsg,std::vector<MmsAttachmentContext> & attachment)1154 void setAttachmentToCore(MmsMsg &mmsMsg, std::vector<MmsAttachmentContext> &attachment)
1155 {
1156     if (attachment.size() > 0) {
1157         int i = 0;
1158         for (auto it = attachment.begin(); it != attachment.end(); it++) {
1159             MmsAttachment itAttachment;
1160             if (it->path.size() > 0) {
1161                 itAttachment.SetAttachmentFilePath(it->path, it->isSmil);
1162             }
1163             itAttachment.SetIsSmilFile(it->isSmil);
1164             if (it->fileName.size() > 0) {
1165                 itAttachment.SetFileName(it->fileName);
1166             }
1167             if (it->contentId.size() > 0) {
1168                 itAttachment.SetContentId(it->contentId);
1169             }
1170             if (it->contentLocation.size() > 0) {
1171                 itAttachment.SetContentLocation(it->contentLocation);
1172             }
1173             if (it->contentDisposition.size() > 0) {
1174                 itAttachment.SetContentDisposition(it->contentDisposition);
1175             }
1176             if (it->contentTransferEncoding.size() > 0) {
1177                 itAttachment.SetContentTransferEncoding(it->contentTransferEncoding);
1178             }
1179             if (it->contentType.size() > 0) {
1180                 itAttachment.SetContentType(it->contentType);
1181             }
1182             if (it->charset != DEFAULT_ERROR) {
1183                 itAttachment.SetCharSet(it->charset);
1184             }
1185             if (it->inBuffLen > 0) {
1186                 itAttachment.SetDataBuffer(std::move(it->inBuff), it->inBuffLen);
1187             }
1188             mmsMsg.AddAttachment(itAttachment);
1189             i++;
1190         }
1191     }
1192 }
1193 
setSendReqToCore(MmsMsg & mmsMsg,MmsSendReqContext & context)1194 void setSendReqToCore(MmsMsg &mmsMsg, MmsSendReqContext &context)
1195 {
1196     mmsMsg.SetMmsFrom(context.from);
1197     if (context.to.size() > 0) {
1198         mmsMsg.SetMmsTo(context.to);
1199     }
1200     if (context.transactionId.size() > 0) {
1201         mmsMsg.SetMmsTransactionId(context.transactionId);
1202     }
1203     if (context.version > 0) {
1204         mmsMsg.SetMmsVersion(context.version);
1205     }
1206     if (context.date > 0) {
1207         mmsMsg.SetMmsDate(context.date);
1208     }
1209     if (context.cc.size() > 0) {
1210         for (MmsAddress address : context.cc) {
1211             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_CC, address);
1212         }
1213     }
1214     if (context.bcc.size() > 0) {
1215         for (MmsAddress address : context.bcc) {
1216             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_BCC, address);
1217         }
1218     }
1219     if (context.subject.size() > 0) {
1220         mmsMsg.SetMmsSubject(context.subject);
1221     }
1222     if (context.messageClass > 0) {
1223         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS, context.messageClass);
1224     }
1225     if (context.expiry > 0) {
1226         mmsMsg.SetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY, context.expiry);
1227     }
1228     if (context.priority > 0) {
1229         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY, context.priority);
1230     }
1231     if (context.senderVisibility > 0) {
1232         mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_SENDER_VISIBILITY, context.senderVisibility);
1233     }
1234     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1235     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT, context.readReport);
1236     mmsMsg.SetHeaderContentType(context.contentType);
1237 }
1238 
setSendConfToCore(MmsMsg & mmsMsg,MmsSendConfContext & context)1239 void setSendConfToCore(MmsMsg &mmsMsg, MmsSendConfContext &context)
1240 {
1241     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_RESPONSE_STATUS, context.responseState);
1242     mmsMsg.SetMmsTransactionId(context.transactionId);
1243     mmsMsg.SetMmsVersion(context.version);
1244     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1245 }
1246 
setNotificationIndToCore(MmsMsg & mmsMsg,MmsNotificationIndContext & context)1247 void setNotificationIndToCore(MmsMsg &mmsMsg, MmsNotificationIndContext &context)
1248 {
1249     mmsMsg.SetMmsTransactionId(context.transactionId);
1250     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_MESSAGE_CLASS, context.messageClass);
1251     mmsMsg.SetHeaderLongValue(MmsFieldCode::MMS_MESSAGE_SIZE, context.messageSize);
1252     mmsMsg.SetHeaderIntegerValue(MmsFieldCode::MMS_EXPIRY, context.expiry);
1253     mmsMsg.SetMmsVersion(context.version);
1254     mmsMsg.SetMmsFrom(context.from);
1255     mmsMsg.SetMmsSubject(context.subject);
1256     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1257     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_CONTENT_LOCATION, context.contentLocation);
1258     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_CONTENT_CLASS, context.contentClass);
1259 }
1260 
setRespIndToCore(MmsMsg & mmsMsg,MmsRespIndContext & context)1261 void setRespIndToCore(MmsMsg &mmsMsg, MmsRespIndContext &context)
1262 {
1263     mmsMsg.SetMmsTransactionId(context.transactionId);
1264     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_STATUS, context.status);
1265     mmsMsg.SetMmsVersion(context.version);
1266     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED, context.reportAllowed);
1267 }
1268 
setRetrieveConfToCore(MmsMsg & mmsMsg,MmsRetrieveConfContext & context)1269 void setRetrieveConfToCore(MmsMsg &mmsMsg, MmsRetrieveConfContext &context)
1270 {
1271     mmsMsg.SetMmsTransactionId(context.transactionId);
1272     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1273     mmsMsg.SetMmsDate(context.date);
1274     mmsMsg.SetMmsVersion(context.version);
1275     mmsMsg.SetMmsTo(context.to);
1276     mmsMsg.SetMmsFrom(context.from);
1277     if (context.cc.size() > 0) {
1278         for (MmsAddress address : context.cc) {
1279             mmsMsg.AddHeaderAddressValue(MmsFieldCode::MMS_CC, address);
1280         }
1281     }
1282     mmsMsg.SetMmsSubject(context.subject);
1283     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_PRIORITY, context.priority);
1284     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_DELIVERY_REPORT, context.deliveryReport);
1285     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_REPORT, context.readReport);
1286     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_RETRIEVE_STATUS, context.retrieveStatus);
1287     if (!context.retrieveText.empty()) {
1288         mmsMsg.SetHeaderEncodedStringValue(
1289             MmsFieldCode::MMS_RETRIEVE_TEXT, context.retrieveText, (uint32_t)MmsCharSets::UTF_8);
1290     }
1291     mmsMsg.SetHeaderContentType(context.contentType);
1292 }
1293 
setAcknowledgeIndToCore(MmsMsg & mmsMsg,MmsAcknowledgeIndContext & context)1294 void setAcknowledgeIndToCore(MmsMsg &mmsMsg, MmsAcknowledgeIndContext &context)
1295 {
1296     mmsMsg.SetMmsTransactionId(context.transactionId);
1297     mmsMsg.SetMmsVersion(context.version);
1298     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_REPORT_ALLOWED, context.reportAllowed);
1299 }
1300 
setDeliveryIndToCore(MmsMsg & mmsMsg,MmsDeliveryIndContext & context)1301 void setDeliveryIndToCore(MmsMsg &mmsMsg, MmsDeliveryIndContext &context)
1302 {
1303     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1304     mmsMsg.SetMmsDate(context.date);
1305     mmsMsg.SetMmsTo(context.to);
1306     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_STATUS, context.status);
1307     mmsMsg.SetMmsVersion(context.version);
1308 }
setReadOrigIndToCore(MmsMsg & mmsMsg,MmsReadOrigIndContext & context)1309 void setReadOrigIndToCore(MmsMsg &mmsMsg, MmsReadOrigIndContext &context)
1310 {
1311     mmsMsg.SetMmsVersion(context.version);
1312     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1313     mmsMsg.SetMmsTo(context.to);
1314     mmsMsg.SetMmsFrom(context.from);
1315     if (context.date != 0) {
1316         mmsMsg.SetMmsDate(context.date);
1317     }
1318     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS, context.readStatus);
1319 }
1320 
setReadRecIndToCore(MmsMsg & mmsMsg,MmsReadRecIndContext & context)1321 void setReadRecIndToCore(MmsMsg &mmsMsg, MmsReadRecIndContext &context)
1322 {
1323     mmsMsg.SetMmsVersion(context.version);
1324     mmsMsg.SetHeaderStringValue(MmsFieldCode::MMS_MESSAGE_ID, context.messageId);
1325     mmsMsg.SetMmsTo(context.to);
1326     mmsMsg.SetMmsFrom(context.from);
1327     if (context.date != 0) {
1328         mmsMsg.SetMmsDate(context.date);
1329     }
1330     mmsMsg.SetHeaderOctetValue(MmsFieldCode::MMS_READ_STATUS, context.readStatus);
1331 }
1332 
SetRequestToCore(MmsMsg & mmsMsg,EncodeMmsContext * context)1333 void SetRequestToCore(MmsMsg &mmsMsg, EncodeMmsContext *context)
1334 {
1335     if (context == nullptr) {
1336         TELEPHONY_LOGE("context is nullptr");
1337         return;
1338     }
1339     switch (context->messageType) {
1340         case MessageType::TYPE_MMS_SEND_REQ:
1341             setSendReqToCore(mmsMsg, context->sendReq);
1342             break;
1343         case MessageType::TYPE_MMS_SEND_CONF:
1344             setSendConfToCore(mmsMsg, context->sendConf);
1345             break;
1346         case MessageType::TYPE_MMS_NOTIFICATION_IND:
1347             setNotificationIndToCore(mmsMsg, context->notificationInd);
1348             break;
1349         case MessageType::TYPE_MMS_RESP_IND:
1350             setRespIndToCore(mmsMsg, context->respInd);
1351             break;
1352         case MessageType::TYPE_MMS_RETRIEVE_CONF:
1353             setRetrieveConfToCore(mmsMsg, context->retrieveConf);
1354             break;
1355         case MessageType::TYPE_MMS_ACKNOWLEDGE_IND:
1356             setAcknowledgeIndToCore(mmsMsg, context->acknowledgeInd);
1357             break;
1358         case MessageType::TYPE_MMS_DELIVERY_IND:
1359             setDeliveryIndToCore(mmsMsg, context->deliveryInd);
1360             break;
1361         case MessageType::TYPE_MMS_READ_ORIG_IND:
1362             setReadOrigIndToCore(mmsMsg, context->readOrigInd);
1363             break;
1364         case MessageType::TYPE_MMS_READ_REC_IND:
1365             setReadRecIndToCore(mmsMsg, context->readRecInd);
1366             break;
1367         default:
1368             break;
1369     }
1370 }
1371 
GetDataAbilityHelper(napi_env env)1372 std::shared_ptr<OHOS::DataShare::DataShareHelper> GetDataAbilityHelper(napi_env env)
1373 {
1374     auto ability = OHOS::AbilityRuntime::GetCurrentAbility(env);
1375     if (ability == nullptr) {
1376         TELEPHONY_LOGE("ability is nullptr.");
1377         return nullptr;
1378     }
1379     auto context = ability->GetContext();
1380     if (context == nullptr) {
1381         TELEPHONY_LOGE("Failed to get native context instance");
1382         return nullptr;
1383     }
1384     return OHOS::DataShare::DataShareHelper::Creator(context->GetToken(), SMS_PROFILE_URI);
1385 }
1386 
NativeEncodeMms(napi_env env,void * data)1387 void NativeEncodeMms(napi_env env, void *data)
1388 {
1389     if (data == nullptr) {
1390         TELEPHONY_LOGE("NativeEncodeMms data is nullptr");
1391         NapiUtil::ThrowParameterError(env);
1392         return;
1393     }
1394 
1395     EncodeMmsContext *context = static_cast<EncodeMmsContext *>(data);
1396     if (context == nullptr) {
1397         TELEPHONY_LOGE("context is nullptr");
1398         return;
1399     }
1400     if (!TelephonyPermission::CheckCallerIsSystemApp()) {
1401         TELEPHONY_LOGE("Non-system applications use system APIs!");
1402         context->errorCode = TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
1403         return;
1404     }
1405     MmsMsg mmsMsg;
1406     mmsMsg.SetMmsMessageType(static_cast<uint8_t>(WrapEncodeMmsStatus(context->messageType)));
1407     setAttachmentToCore(mmsMsg, context->attachment);
1408     SetRequestToCore(mmsMsg, context);
1409     auto encodeResult = mmsMsg.EncodeMsg(context->bufferLen);
1410     if (encodeResult != nullptr) {
1411         context->outBuffer = std::move(encodeResult);
1412         context->errorCode = TELEPHONY_ERR_SUCCESS;
1413         context->resolved = true;
1414     } else {
1415         context->errorCode = TELEPHONY_ERR_FAIL;
1416         context->resolved = false;
1417     }
1418     TELEPHONY_LOGD("napi_mms NativeEncodeMms length:%{private}d", context->bufferLen);
1419 }
1420 
EncodeMmsCallback(napi_env env,napi_status status,void * data)1421 void EncodeMmsCallback(napi_env env, napi_status status, void *data)
1422 {
1423     auto context = static_cast<EncodeMmsContext *>(data);
1424 
1425     napi_value callbackValue = nullptr;
1426     if (context->resolved) {
1427         napi_create_array(env, &callbackValue);
1428         for (uint32_t i = 0; i < context->bufferLen; i++) {
1429             napi_value itemValue = nullptr;
1430             int32_t element = context->outBuffer[i];
1431             napi_create_int32(env, element, &itemValue);
1432             napi_set_element(env, callbackValue, i, itemValue);
1433         }
1434     } else {
1435         JsError error = NapiUtil::ConverErrorMessageForJs(context->errorCode);
1436         callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
1437     }
1438     NapiUtil::Handle2ValueCallback(env, context, callbackValue);
1439 }
1440 
EncodeMms(napi_env env,napi_callback_info info)1441 napi_value NapiMms::EncodeMms(napi_env env, napi_callback_info info)
1442 {
1443     TELEPHONY_LOGI("napi_mms EncodeMms start");
1444     napi_value result = nullptr;
1445     size_t parameterCount = TWO_PARAMETERS;
1446     napi_value parameters[TWO_PARAMETERS] = { 0 };
1447     napi_value thisVar = nullptr;
1448     void *data = nullptr;
1449     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
1450     if (!MatchEncodeMms(env, parameters, parameterCount)) {
1451         TELEPHONY_LOGE("EncodeMms parameter matching failed.");
1452         NapiUtil::ThrowParameterError(env);
1453         return nullptr;
1454     }
1455     auto context = std::make_unique<EncodeMmsContext>().release();
1456     if (context == nullptr) {
1457         TELEPHONY_LOGE("EncodeMms EncodeMmsContext is nullptr.");
1458         NapiUtil::ThrowParameterError(env);
1459         return nullptr;
1460     }
1461 
1462     if (!ParseEncodeMmsParam(env, parameters[0], *context)) {
1463         free(context);
1464         context = nullptr;
1465         NapiUtil::ThrowParameterError(env);
1466         return nullptr;
1467     }
1468     if (parameterCount == TWO_PARAMETERS) {
1469         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
1470     }
1471 
1472     result = NapiUtil::HandleAsyncWork(env, context, "EncodeMms", NativeEncodeMms, EncodeMmsCallback);
1473     TELEPHONY_LOGI("napi_mms EncodeMms end");
1474     return result;
1475 }
1476 
GetMmsPduFromFile(const std::string & fileName,std::string & mmsPdu)1477 bool GetMmsPduFromFile(const std::string &fileName, std::string &mmsPdu)
1478 {
1479     char realPath[PATH_MAX] = { 0 };
1480     if (fileName.empty() || realpath(fileName.c_str(), realPath) == nullptr) {
1481         TELEPHONY_LOGE("path or realPath is nullptr");
1482         return false;
1483     }
1484 
1485     FILE *pFile = fopen(realPath, "rb");
1486     if (pFile == nullptr) {
1487         TELEPHONY_LOGE("openFile Error");
1488         return false;
1489     }
1490 
1491     (void)fseek(pFile, 0, SEEK_END);
1492     long fileLen = ftell(pFile);
1493     if (fileLen <= 0 || fileLen > static_cast<long>(MMS_PDU_MAX_SIZE)) {
1494         (void)fclose(pFile);
1495         TELEPHONY_LOGE("fileLen Over Max Error");
1496         return false;
1497     }
1498 
1499     std::unique_ptr<char[]> pduBuffer = std::make_unique<char[]>(fileLen);
1500     if (!pduBuffer) {
1501         (void)fclose(pFile);
1502         TELEPHONY_LOGE("make unique pduBuffer nullptr Error");
1503         return false;
1504     }
1505     (void)fseek(pFile, 0, SEEK_SET);
1506     int32_t totolLength = static_cast<int32_t>(fread(pduBuffer.get(), 1, MMS_PDU_MAX_SIZE, pFile));
1507     TELEPHONY_LOGI("fread totolLength%{private}d", totolLength);
1508 
1509     long i = 0;
1510     while (i < fileLen) {
1511         mmsPdu += pduBuffer[i];
1512         i++;
1513     }
1514     (void)fclose(pFile);
1515     return true;
1516 }
1517 
StoreSendMmsPduToDataBase(NapiMmsPduHelper & helper)1518 void StoreSendMmsPduToDataBase(NapiMmsPduHelper &helper)
1519 {
1520     std::shared_ptr<NAPIMmsPdu> mmsPduObj = std::make_shared<NAPIMmsPdu>();
1521     if (mmsPduObj == nullptr) {
1522         TELEPHONY_LOGE("mmsPduObj nullptr");
1523         helper.NotifyAll();
1524         return;
1525     }
1526     std::string mmsPdu;
1527     if (!GetMmsPduFromFile(helper.GetPduFileName(), mmsPdu)) {
1528         TELEPHONY_LOGE("get mmsPdu fail");
1529         helper.NotifyAll();
1530         return;
1531     }
1532     mmsPduObj->InsertMmsPdu(helper, mmsPdu);
1533 }
1534 
StoreTempDataToDataBase(NapiMmsPduHelper & helper)1535 void StoreTempDataToDataBase(NapiMmsPduHelper &helper)
1536 {
1537     std::shared_ptr<NAPIMmsPdu> mmsPduObj = std::make_shared<NAPIMmsPdu>();
1538     if (mmsPduObj == nullptr) {
1539         TELEPHONY_LOGE("mmsPduObj nullptr");
1540         helper.NotifyAll();
1541         return;
1542     }
1543     std::string mmsPdu = "tempData";
1544     mmsPduObj->InsertMmsPdu(helper, mmsPdu);
1545 }
1546 
NativeSendMms(napi_env env,void * data)1547 void NativeSendMms(napi_env env, void *data)
1548 {
1549     auto asyncContext = static_cast<MmsContext *>(data);
1550     if (asyncContext == nullptr) {
1551         TELEPHONY_LOGE("asyncContext nullptr");
1552         return;
1553     }
1554     if (!STORE_MMS_PDU_TO_FILE) {
1555         std::string pduFileName = NapiUtil::ToUtf8(asyncContext->data);
1556         if (pduFileName.empty()) {
1557             asyncContext->errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
1558             asyncContext->resolved = false;
1559             TELEPHONY_LOGE("pduFileName empty");
1560             return;
1561         }
1562         auto dbHelper = GetDataAbilityHelper(env);
1563         if (dbHelper == nullptr) {
1564             asyncContext->errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1565             asyncContext->resolved = false;
1566             TELEPHONY_LOGE("dbHelper is nullptr");
1567             return;
1568         }
1569         NapiMmsPduHelper helper;
1570         helper.SetDataAbilityHelper(dbHelper);
1571         helper.SetPduFileName(pduFileName);
1572         if (!helper.Run(StoreSendMmsPduToDataBase, helper)) {
1573             TELEPHONY_LOGE("StoreMmsPdu fail");
1574             asyncContext->errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1575             asyncContext->resolved = false;
1576             return;
1577         }
1578 
1579         asyncContext->data = NapiUtil::ToUtf16(helper.GetDbUrl());
1580     }
1581     asyncContext->errorCode =
1582         DelayedSingleton<SmsServiceManagerClient>::GetInstance()->SendMms(asyncContext->slotId, asyncContext->mmsc,
1583             asyncContext->data, asyncContext->mmsConfig.userAgent, asyncContext->mmsConfig.userAgentProfile);
1584     if (asyncContext->errorCode == TELEPHONY_ERR_SUCCESS) {
1585         asyncContext->resolved = true;
1586     } else {
1587         asyncContext->resolved = false;
1588     }
1589     TELEPHONY_LOGI("NativeSendMms end resolved = %{public}d", asyncContext->resolved);
1590 }
1591 
SendMmsCallback(napi_env env,napi_status status,void * data)1592 void SendMmsCallback(napi_env env, napi_status status, void *data)
1593 {
1594     auto context = static_cast<MmsContext *>(data);
1595     if (context == nullptr) {
1596         TELEPHONY_LOGE("SendMmsCallback context nullptr");
1597         return;
1598     }
1599     napi_value callbackValue = nullptr;
1600     if (context->resolved) {
1601         napi_get_undefined(env, &callbackValue);
1602     } else {
1603         JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
1604             context->errorCode, "sendMms", "ohos.permission.SEND_MESSAGES");
1605         callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
1606     }
1607     NapiUtil::Handle1ValueCallback(env, context, callbackValue);
1608 }
1609 
MatchMmsParameters(napi_env env,napi_value parameters[],size_t parameterCount)1610 bool MatchMmsParameters(napi_env env, napi_value parameters[], size_t parameterCount)
1611 {
1612     bool typeMatch = false;
1613     switch (parameterCount) {
1614         case ONE_PARAMETER: {
1615             typeMatch = NapiUtil::MatchParameters(env, parameters, { napi_object });
1616             break;
1617         }
1618         case TWO_PARAMETERS: {
1619             typeMatch = NapiUtil::MatchParameters(env, parameters, { napi_object, napi_function });
1620             break;
1621         }
1622         default: {
1623             break;
1624         }
1625     }
1626     if (typeMatch) {
1627         return NapiUtil::MatchObjectProperty(env, parameters[0],
1628             {
1629                 { "slotId", napi_number },
1630                 { "mmsc", napi_string },
1631                 { "data", napi_string },
1632                 { "mmsConfig", napi_object },
1633             });
1634     }
1635     return false;
1636 }
1637 
GetMmsValueLength(napi_env env,napi_value param)1638 static bool GetMmsValueLength(napi_env env, napi_value param)
1639 {
1640     size_t len = 0;
1641     napi_status status = napi_get_value_string_utf8(env, param, nullptr, 0, &len);
1642     if (status != napi_ok) {
1643         TELEPHONY_LOGE("Get length failed");
1644         return false;
1645     }
1646     return (len > 0) && (len < BUFF_LENGTH);
1647 }
1648 
GetMmsNameProperty(napi_env env,napi_value param,MmsContext & context)1649 static void GetMmsNameProperty(napi_env env, napi_value param, MmsContext &context)
1650 {
1651     napi_value slotIdValue = NapiUtil::GetNamedProperty(env, param, "slotId");
1652     if (slotIdValue != nullptr) {
1653         napi_get_value_int32(env, slotIdValue, &(context.slotId));
1654     }
1655     napi_value mmscValue = NapiUtil::GetNamedProperty(env, param, "mmsc");
1656     if (mmscValue != nullptr && GetMmsValueLength(env, mmscValue)) {
1657         char strChars[NORMAL_STRING_SIZE] = { 0 };
1658         size_t strLength = 0;
1659         napi_get_value_string_utf8(env, mmscValue, strChars, BUFF_LENGTH, &strLength);
1660         std::string str8(strChars, strLength);
1661         context.mmsc = NapiUtil::ToUtf16(str8);
1662     }
1663     napi_value dataValue = NapiUtil::GetNamedProperty(env, param, "data");
1664     if (dataValue != nullptr && GetMmsValueLength(env, dataValue)) {
1665         char strChars[NORMAL_STRING_SIZE] = { 0 };
1666         size_t strLength = 0;
1667         napi_get_value_string_utf8(env, dataValue, strChars, BUFF_LENGTH, &strLength);
1668         std::string str8(strChars, strLength);
1669         context.data = NapiUtil::ToUtf16(str8);
1670     }
1671     napi_value configValue = NapiUtil::GetNamedProperty(env, param, "mmsConfig");
1672     if (configValue != nullptr) {
1673         napi_value uaValue = NapiUtil::GetNamedProperty(env, configValue, "userAgent");
1674         if (uaValue != nullptr && GetMmsValueLength(env, uaValue)) {
1675             char strChars[NORMAL_STRING_SIZE] = { 0 };
1676             size_t strLength = 0;
1677             napi_get_value_string_utf8(env, uaValue, strChars, BUFF_LENGTH, &strLength);
1678             std::string str8(strChars, strLength);
1679             context.mmsConfig.userAgent = NapiUtil::ToUtf16(str8);
1680         }
1681         napi_value uaprofValue = NapiUtil::GetNamedProperty(env, configValue, "userAgentProfile");
1682         if (uaprofValue != nullptr && GetMmsValueLength(env, uaprofValue)) {
1683             char strChars[NORMAL_STRING_SIZE] = { 0 };
1684             size_t strLength = 0;
1685             napi_get_value_string_utf8(env, uaprofValue, strChars, BUFF_LENGTH, &strLength);
1686             std::string str8(strChars, strLength);
1687             context.mmsConfig.userAgentProfile = NapiUtil::ToUtf16(str8);
1688         }
1689     }
1690 }
1691 
SendMms(napi_env env,napi_callback_info info)1692 napi_value NapiMms::SendMms(napi_env env, napi_callback_info info)
1693 {
1694     size_t parameterCount = TWO_PARAMETERS;
1695     napi_value parameters[TWO_PARAMETERS] = { 0 };
1696     napi_value thisVar = nullptr;
1697     void *data = nullptr;
1698 
1699     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
1700     if (!MatchMmsParameters(env, parameters, parameterCount)) {
1701         TELEPHONY_LOGE("parameter matching failed.");
1702         NapiUtil::ThrowParameterError(env);
1703         return nullptr;
1704     }
1705     auto context = std::make_unique<MmsContext>().release();
1706     if (context == nullptr) {
1707         TELEPHONY_LOGE("MmsContext is nullptr.");
1708         NapiUtil::ThrowParameterError(env);
1709         return nullptr;
1710     }
1711     GetMmsNameProperty(env, parameters[0], *context);
1712     if (parameterCount == TWO_PARAMETERS) {
1713         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
1714     }
1715     napi_value result = NapiUtil::HandleAsyncWork(env, context, "SendMms", NativeSendMms, SendMmsCallback);
1716     return result;
1717 }
1718 
WriteBufferToFile(const std::unique_ptr<char[]> & buff,uint32_t len,const std::string & strPathName)1719 bool WriteBufferToFile(const std::unique_ptr<char[]> &buff, uint32_t len, const std::string &strPathName)
1720 {
1721     if (buff == nullptr) {
1722         TELEPHONY_LOGE("buff nullptr");
1723         return false;
1724     }
1725 
1726     char realPath[PATH_MAX] = { 0 };
1727     if (strPathName.empty() || realpath(strPathName.c_str(), realPath) == nullptr) {
1728         TELEPHONY_LOGE("path or realPath is nullptr");
1729         return false;
1730     }
1731 
1732     FILE *pFile = fopen(realPath, "wb");
1733     if (pFile == nullptr) {
1734         TELEPHONY_LOGE("openFile Error");
1735         return false;
1736     }
1737     uint32_t fileLen = fwrite(buff.get(), len, 1, pFile);
1738     (void)fclose(pFile);
1739     if (fileLen > 0) {
1740         TELEPHONY_LOGI("write mms buffer to file success");
1741         return true;
1742     } else {
1743         TELEPHONY_LOGI("write mms buffer to file error");
1744         return false;
1745     }
1746 }
1747 
StoreMmsPduToFile(const std::string & fileName,const std::string & mmsPdu)1748 bool StoreMmsPduToFile(const std::string &fileName, const std::string &mmsPdu)
1749 {
1750     uint32_t len = static_cast<uint32_t>(mmsPdu.size());
1751     if (len > MMS_PDU_MAX_SIZE || len == 0) {
1752         TELEPHONY_LOGE("MMS pdu length invalid");
1753         return false;
1754     }
1755 
1756     std::unique_ptr<char[]> resultResponse = std::make_unique<char[]>(len);
1757     if (memset_s(resultResponse.get(), len, 0x00, len) != EOK) {
1758         TELEPHONY_LOGE("memset_s err");
1759         return false;
1760     }
1761     if (memcpy_s(resultResponse.get(), len, &mmsPdu[0], len) != EOK) {
1762         TELEPHONY_LOGE("memcpy_s error");
1763         return false;
1764     }
1765 
1766     TELEPHONY_LOGI("len:%{public}d", len);
1767     if (!WriteBufferToFile(std::move(resultResponse), len, fileName)) {
1768         TELEPHONY_LOGE("write to file error");
1769         return false;
1770     }
1771     return true;
1772 }
1773 
GetMmsPduFromDataBase(NapiMmsPduHelper & helper)1774 void GetMmsPduFromDataBase(NapiMmsPduHelper &helper)
1775 {
1776     NAPIMmsPdu mmsPduObj;
1777     std::string mmsPdu = mmsPduObj.GetMmsPdu(helper);
1778     if (mmsPdu.empty()) {
1779         TELEPHONY_LOGE("from dataBase empty");
1780         return;
1781     }
1782 
1783     mmsPduObj.DeleteMmsPdu(helper);
1784     if (!StoreMmsPduToFile(helper.GetStoreFileName(), mmsPdu)) {
1785         TELEPHONY_LOGE("store mmsPdu fail");
1786     }
1787     helper.NotifyAll();
1788 }
1789 
StoreDownloadMmsPduToDataBase(MmsContext & context,std::string & dbUrl,std::string & storeFileName,std::shared_ptr<OHOS::DataShare::DataShareHelper> dbHelper)1790 static bool StoreDownloadMmsPduToDataBase(MmsContext &context, std::string &dbUrl, std::string &storeFileName,
1791     std::shared_ptr<OHOS::DataShare::DataShareHelper> dbHelper)
1792 {
1793     if (!STORE_MMS_PDU_TO_FILE) {
1794         storeFileName = NapiUtil::ToUtf8(context.data);
1795         if (storeFileName.empty()) {
1796             TELEPHONY_LOGE("storeFileName empty");
1797             context.errorCode = TELEPHONY_ERR_ARGUMENT_INVALID;
1798             context.resolved = false;
1799             return false;
1800         }
1801         NapiMmsPduHelper helper;
1802         helper.SetDataAbilityHelper(dbHelper);
1803         if (!helper.Run(StoreTempDataToDataBase, helper)) {
1804             TELEPHONY_LOGE("StoreMmsPdu fail");
1805             context.errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1806             context.resolved = false;
1807             return false;
1808         }
1809         dbUrl = helper.GetDbUrl();
1810         context.data = NapiUtil::ToUtf16(dbUrl);
1811     }
1812     return true;
1813 }
1814 
NativeDownloadMms(napi_env env,void * data)1815 void NativeDownloadMms(napi_env env, void *data)
1816 {
1817     auto asyncContext = static_cast<MmsContext *>(data);
1818     if (asyncContext == nullptr) {
1819         TELEPHONY_LOGE("asyncContext nullptr");
1820         asyncContext->errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1821         asyncContext->resolved = false;
1822         return;
1823     }
1824     auto dbHelper = GetDataAbilityHelper(env);
1825     if (dbHelper == nullptr) {
1826         TELEPHONY_LOGE("dbHelper is nullptr");
1827         asyncContext->errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1828         asyncContext->resolved = false;
1829         return;
1830     }
1831     std::string dbUrl;
1832     std::string storeFileName;
1833     if (!StoreDownloadMmsPduToDataBase(*asyncContext, dbUrl, storeFileName, dbHelper)) {
1834         TELEPHONY_LOGE("store mms pdu fail");
1835         return;
1836     }
1837 
1838     asyncContext->errorCode =
1839         DelayedSingleton<SmsServiceManagerClient>::GetInstance()->DownloadMms(asyncContext->slotId, asyncContext->mmsc,
1840             asyncContext->data, asyncContext->mmsConfig.userAgent, asyncContext->mmsConfig.userAgentProfile);
1841 
1842     if (asyncContext->errorCode == TELEPHONY_ERR_SUCCESS) {
1843         asyncContext->resolved = true;
1844         if (!STORE_MMS_PDU_TO_FILE) {
1845             NapiMmsPduHelper helper;
1846             helper.SetDataAbilityHelper(dbHelper);
1847             helper.SetDbUrl(dbUrl);
1848             helper.SetStoreFileName(storeFileName);
1849             if (!helper.Run(GetMmsPduFromDataBase, helper)) {
1850                 TELEPHONY_LOGE("StoreMmsPdu fail");
1851                 asyncContext->errorCode = TELEPHONY_ERR_LOCAL_PTR_NULL;
1852                 asyncContext->resolved = false;
1853                 return;
1854             }
1855         }
1856     } else {
1857         asyncContext->resolved = false;
1858     }
1859     TELEPHONY_LOGI("NativeDownloadMms end resolved = %{public}d", asyncContext->resolved);
1860 }
1861 
DownloadMmsCallback(napi_env env,napi_status status,void * data)1862 void DownloadMmsCallback(napi_env env, napi_status status, void *data)
1863 {
1864     auto context = static_cast<MmsContext *>(data);
1865     napi_value callbackValue = nullptr;
1866     if (context->resolved) {
1867         napi_get_undefined(env, &callbackValue);
1868     } else {
1869         JsError error = NapiUtil::ConverErrorMessageWithPermissionForJs(
1870             context->errorCode, "downloadMms", "ohos.permission.RECEIVE_MMS");
1871         callbackValue = NapiUtil::CreateErrorMessage(env, error.errorMessage, error.errorCode);
1872     }
1873     NapiUtil::Handle1ValueCallback(env, context, callbackValue);
1874 }
1875 
DownloadMms(napi_env env,napi_callback_info info)1876 napi_value NapiMms::DownloadMms(napi_env env, napi_callback_info info)
1877 {
1878     size_t parameterCount = TWO_PARAMETERS;
1879     napi_value parameters[TWO_PARAMETERS] = { 0 };
1880     napi_value thisVar = nullptr;
1881     void *data = nullptr;
1882 
1883     napi_get_cb_info(env, info, &parameterCount, parameters, &thisVar, &data);
1884     if (!MatchMmsParameters(env, parameters, parameterCount)) {
1885         TELEPHONY_LOGE("DownloadMms parameter matching failed.");
1886         NapiUtil::ThrowParameterError(env);
1887         return nullptr;
1888     }
1889     auto context = std::make_unique<MmsContext>().release();
1890     if (context == nullptr) {
1891         TELEPHONY_LOGE("DownloadMms MmsContext is nullptr.");
1892         NapiUtil::ThrowParameterError(env);
1893         return nullptr;
1894     }
1895     GetMmsNameProperty(env, parameters[0], *context);
1896     if (parameterCount == TWO_PARAMETERS) {
1897         napi_create_reference(env, parameters[1], DEFAULT_REF_COUNT, &context->callbackRef);
1898     }
1899     napi_value result = NapiUtil::HandleAsyncWork(env, context, "DownloadMms", NativeDownloadMms, DownloadMmsCallback);
1900     return result;
1901 }
1902 
InitEnumMmsCharSets(napi_env env,napi_value exports)1903 napi_value NapiMms::InitEnumMmsCharSets(napi_env env, napi_value exports)
1904 {
1905     napi_property_descriptor desc[] = {
1906         DECLARE_NAPI_STATIC_PROPERTY("BIG5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::BIG5))),
1907         DECLARE_NAPI_STATIC_PROPERTY(
1908             "ISO_10646_UCS_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2))),
1909         DECLARE_NAPI_STATIC_PROPERTY(
1910             "ISO_8859_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_1))),
1911         DECLARE_NAPI_STATIC_PROPERTY(
1912             "ISO_8859_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_2))),
1913         DECLARE_NAPI_STATIC_PROPERTY(
1914             "ISO_8859_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_3))),
1915         DECLARE_NAPI_STATIC_PROPERTY(
1916             "ISO_8859_4", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_4))),
1917         DECLARE_NAPI_STATIC_PROPERTY(
1918             "ISO_8859_5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_5))),
1919         DECLARE_NAPI_STATIC_PROPERTY(
1920             "ISO_8859_6", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_6))),
1921         DECLARE_NAPI_STATIC_PROPERTY(
1922             "ISO_8859_7", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_7))),
1923         DECLARE_NAPI_STATIC_PROPERTY(
1924             "ISO_8859_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_8))),
1925         DECLARE_NAPI_STATIC_PROPERTY(
1926             "ISO_8859_9", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_9))),
1927         DECLARE_NAPI_STATIC_PROPERTY(
1928             "SHIFT_JIS", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::SHIFT_JIS))),
1929         DECLARE_NAPI_STATIC_PROPERTY(
1930             "US_ASCII", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::US_ASCII))),
1931         DECLARE_NAPI_STATIC_PROPERTY("UTF_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::UTF_8))),
1932     };
1933     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
1934     return exports;
1935 }
1936 
CreateEnumConstructor(napi_env env,napi_callback_info info)1937 static napi_value CreateEnumConstructor(napi_env env, napi_callback_info info)
1938 {
1939     napi_value thisArg = nullptr;
1940     void *data = nullptr;
1941     napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
1942     napi_value global = nullptr;
1943     napi_get_global(env, &global);
1944     return thisArg;
1945 }
1946 
InitSupportEnumMmsCharSets(napi_env env,napi_value exports)1947 napi_value NapiMms::InitSupportEnumMmsCharSets(napi_env env, napi_value exports)
1948 {
1949     napi_property_descriptor desc[] = {
1950         DECLARE_NAPI_STATIC_PROPERTY("BIG5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::BIG5))),
1951         DECLARE_NAPI_STATIC_PROPERTY(
1952             "ISO_10646_UCS_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_10646_UCS_2))),
1953         DECLARE_NAPI_STATIC_PROPERTY(
1954             "ISO_8859_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_1))),
1955         DECLARE_NAPI_STATIC_PROPERTY(
1956             "ISO_8859_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_2))),
1957         DECLARE_NAPI_STATIC_PROPERTY(
1958             "ISO_8859_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_3))),
1959         DECLARE_NAPI_STATIC_PROPERTY(
1960             "ISO_8859_4", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_4))),
1961         DECLARE_NAPI_STATIC_PROPERTY(
1962             "ISO_8859_5", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_5))),
1963         DECLARE_NAPI_STATIC_PROPERTY(
1964             "ISO_8859_6", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_6))),
1965         DECLARE_NAPI_STATIC_PROPERTY(
1966             "ISO_8859_7", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_7))),
1967         DECLARE_NAPI_STATIC_PROPERTY(
1968             "ISO_8859_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_8))),
1969         DECLARE_NAPI_STATIC_PROPERTY(
1970             "ISO_8859_9", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::ISO_8859_9))),
1971         DECLARE_NAPI_STATIC_PROPERTY(
1972             "SHIFT_JIS", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::SHIFT_JIS))),
1973         DECLARE_NAPI_STATIC_PROPERTY(
1974             "US_ASCII", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::US_ASCII))),
1975         DECLARE_NAPI_STATIC_PROPERTY("UTF_8", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsCharSets::UTF_8))),
1976     };
1977     napi_value result = nullptr;
1978     napi_define_class(env, "MmsCharSets", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
1979         sizeof(desc) / sizeof(*desc), desc, &result);
1980     napi_set_named_property(env, exports, "MmsCharSets", result);
1981     return exports;
1982 }
1983 
InitEnumMessageType(napi_env env,napi_value exports)1984 napi_value NapiMms::InitEnumMessageType(napi_env env, napi_value exports)
1985 {
1986     napi_property_descriptor desc[] = {
1987         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_REQ",
1988             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_REQ))),
1989         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_CONF",
1990             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_CONF))),
1991         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_NOTIFICATION_IND",
1992             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND))),
1993         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RESP_IND",
1994             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND))),
1995         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RETRIEVE_CONF",
1996             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF))),
1997         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_ACKNOWLEDGE_IND",
1998             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND))),
1999         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_DELIVERY_IND",
2000             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_DELIVERY_IND))),
2001         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_REC_IND",
2002             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_REC_IND))),
2003         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_ORIG_IND",
2004             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND))),
2005     };
2006     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
2007     return exports;
2008 }
2009 
InitSupportEnumMessageType(napi_env env,napi_value exports)2010 napi_value NapiMms::InitSupportEnumMessageType(napi_env env, napi_value exports)
2011 {
2012     napi_property_descriptor desc[] = {
2013         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_REQ",
2014             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_REQ))),
2015         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_SEND_CONF",
2016             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_SEND_CONF))),
2017         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_NOTIFICATION_IND",
2018             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFICATION_IND))),
2019         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RESP_IND",
2020             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_NOTIFYRESP_IND))),
2021         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_RETRIEVE_CONF",
2022             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_RETRIEVE_CONF))),
2023         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_ACKNOWLEDGE_IND",
2024             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_ACKNOWLEDGE_IND))),
2025         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_DELIVERY_IND",
2026             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_DELIVERY_IND))),
2027         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_REC_IND",
2028             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_REC_IND))),
2029         DECLARE_NAPI_STATIC_PROPERTY("TYPE_MMS_READ_ORIG_IND",
2030             NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsMsgType::MMS_MSGTYPE_READ_ORIG_IND))),
2031     };
2032     napi_value result = nullptr;
2033     napi_define_class(env, "MessageType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
2034         sizeof(desc) / sizeof(*desc), desc, &result);
2035     napi_set_named_property(env, exports, "MessageType", result);
2036     return exports;
2037 }
2038 
InitEnumPriorityType(napi_env env,napi_value exports)2039 napi_value NapiMms::InitEnumPriorityType(napi_env env, napi_value exports)
2040 {
2041     napi_property_descriptor desc[] = {
2042         DECLARE_NAPI_STATIC_PROPERTY(
2043             "MMS_LOW", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_LOW))),
2044         DECLARE_NAPI_STATIC_PROPERTY(
2045             "MMS_NORMAL", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_NORMAL))),
2046         DECLARE_NAPI_STATIC_PROPERTY(
2047             "MMS_HIGH", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_HIGH))),
2048     };
2049     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
2050     return exports;
2051 }
2052 
InitSupportEnumPriorityType(napi_env env,napi_value exports)2053 napi_value NapiMms::InitSupportEnumPriorityType(napi_env env, napi_value exports)
2054 {
2055     napi_property_descriptor desc[] = {
2056         DECLARE_NAPI_STATIC_PROPERTY(
2057             "MMS_LOW", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_LOW))),
2058         DECLARE_NAPI_STATIC_PROPERTY(
2059             "MMS_NORMAL", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_NORMAL))),
2060         DECLARE_NAPI_STATIC_PROPERTY(
2061             "MMS_HIGH", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsPriority::MMS_HIGH))),
2062     };
2063     napi_value result = nullptr;
2064     napi_define_class(env, "MmsPriorityType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
2065         sizeof(desc) / sizeof(*desc), desc, &result);
2066     napi_set_named_property(env, exports, "MmsPriorityType", result);
2067     return exports;
2068 }
2069 
InitEnumVersionType(napi_env env,napi_value exports)2070 napi_value NapiMms::InitEnumVersionType(napi_env env, napi_value exports)
2071 {
2072     napi_property_descriptor desc[] = {
2073         DECLARE_NAPI_STATIC_PROPERTY(
2074             "MMS_VERSION_1_0", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_0))),
2075         DECLARE_NAPI_STATIC_PROPERTY(
2076             "MMS_VERSION_1_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_1))),
2077         DECLARE_NAPI_STATIC_PROPERTY(
2078             "MMS_VERSION_1_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_2))),
2079         DECLARE_NAPI_STATIC_PROPERTY(
2080             "MMS_VERSION_1_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_3))),
2081     };
2082     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
2083     return exports;
2084 }
2085 
InitSupportEnumVersionType(napi_env env,napi_value exports)2086 napi_value NapiMms::InitSupportEnumVersionType(napi_env env, napi_value exports)
2087 {
2088     napi_property_descriptor desc[] = {
2089         DECLARE_NAPI_STATIC_PROPERTY(
2090             "MMS_VERSION_1_0", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_0))),
2091         DECLARE_NAPI_STATIC_PROPERTY(
2092             "MMS_VERSION_1_1", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_1))),
2093         DECLARE_NAPI_STATIC_PROPERTY(
2094             "MMS_VERSION_1_2", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_2))),
2095         DECLARE_NAPI_STATIC_PROPERTY(
2096             "MMS_VERSION_1_3", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsVersionType::MMS_VERSION_1_3))),
2097     };
2098     napi_value result = nullptr;
2099     napi_define_class(env, "MmsVersionType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
2100         sizeof(desc) / sizeof(*desc), desc, &result);
2101     napi_set_named_property(env, exports, "MmsVersionType", result);
2102     return exports;
2103 }
2104 
InitEnumDispositionType(napi_env env,napi_value exports)2105 napi_value NapiMms::InitEnumDispositionType(napi_env env, napi_value exports)
2106 {
2107     napi_property_descriptor desc[] = {
2108         DECLARE_NAPI_STATIC_PROPERTY(
2109             "FROM_DATA", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::FROM_DATA))),
2110         DECLARE_NAPI_STATIC_PROPERTY(
2111             "ATTACHMENT", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::ATTACHMENT))),
2112         DECLARE_NAPI_STATIC_PROPERTY(
2113             "INLINE", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::INLINE))),
2114     };
2115     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
2116     return exports;
2117 }
2118 
InitSupportEnumDispositionType(napi_env env,napi_value exports)2119 napi_value NapiMms::InitSupportEnumDispositionType(napi_env env, napi_value exports)
2120 {
2121     napi_property_descriptor desc[] = {
2122         DECLARE_NAPI_STATIC_PROPERTY(
2123             "FROM_DATA", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::FROM_DATA))),
2124         DECLARE_NAPI_STATIC_PROPERTY(
2125             "ATTACHMENT", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::ATTACHMENT))),
2126         DECLARE_NAPI_STATIC_PROPERTY(
2127             "INLINE", NapiUtil::ToInt32Value(env, static_cast<int32_t>(DispositionValue::INLINE))),
2128     };
2129     napi_value result = nullptr;
2130     napi_define_class(env, "DispositionType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
2131         sizeof(desc) / sizeof(*desc), desc, &result);
2132     napi_set_named_property(env, exports, "DispositionType", result);
2133     return exports;
2134 }
2135 
InitEnumReportAllowedType(napi_env env,napi_value exports)2136 napi_value NapiMms::InitEnumReportAllowedType(napi_env env, napi_value exports)
2137 {
2138     napi_property_descriptor desc[] = {
2139         DECLARE_NAPI_STATIC_PROPERTY(
2140             "MMS_YES", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_YES))),
2141         DECLARE_NAPI_STATIC_PROPERTY(
2142             "MMS_NO", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_NO))),
2143     };
2144     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
2145     return exports;
2146 }
2147 
InitSupportEnumReportAllowedType(napi_env env,napi_value exports)2148 napi_value NapiMms::InitSupportEnumReportAllowedType(napi_env env, napi_value exports)
2149 {
2150     napi_property_descriptor desc[] = {
2151         DECLARE_NAPI_STATIC_PROPERTY(
2152             "MMS_YES", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_YES))),
2153         DECLARE_NAPI_STATIC_PROPERTY(
2154             "MMS_NO", NapiUtil::ToInt32Value(env, static_cast<int32_t>(MmsBoolType::MMS_NO))),
2155     };
2156     napi_value result = nullptr;
2157     napi_define_class(env, "ReportType", NAPI_AUTO_LENGTH, CreateEnumConstructor, nullptr,
2158         sizeof(desc) / sizeof(*desc), desc, &result);
2159     napi_set_named_property(env, exports, "ReportType", result);
2160     return exports;
2161 }
2162 } // namespace Telephony
2163 } // namespace OHOS
2164