1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "itypes_util.h"
17
18 #include "global.h"
19 #include "iremote_object.h"
20
21 namespace OHOS {
22 namespace MiscServices {
Marshal(MessageParcel & data)23 bool ITypesUtil::Marshal(MessageParcel &data)
24 {
25 return true;
26 }
27
Unmarshal(MessageParcel & data)28 bool ITypesUtil::Unmarshal(MessageParcel &data)
29 {
30 return true;
31 }
32
Marshalling(bool input,MessageParcel & data)33 bool ITypesUtil::Marshalling(bool input, MessageParcel &data)
34 {
35 return data.WriteBool(input);
36 }
37
Unmarshalling(bool & output,MessageParcel & data)38 bool ITypesUtil::Unmarshalling(bool &output, MessageParcel &data)
39 {
40 return data.ReadBool(output);
41 }
42
Marshalling(uint32_t input,MessageParcel & data)43 bool ITypesUtil::Marshalling(uint32_t input, MessageParcel &data)
44 {
45 return data.WriteUint32(input);
46 }
47
Unmarshalling(uint32_t & output,MessageParcel & data)48 bool ITypesUtil::Unmarshalling(uint32_t &output, MessageParcel &data)
49 {
50 return data.ReadUint32(output);
51 }
52
Marshalling(int32_t input,MessageParcel & data)53 bool ITypesUtil::Marshalling(int32_t input, MessageParcel &data)
54 {
55 return data.WriteInt32(input);
56 }
57
Unmarshalling(int32_t & output,MessageParcel & data)58 bool ITypesUtil::Unmarshalling(int32_t &output, MessageParcel &data)
59 {
60 return data.ReadInt32(output);
61 }
62
Marshalling(uint64_t input,MessageParcel & data)63 bool ITypesUtil::Marshalling(uint64_t input, MessageParcel &data)
64 {
65 return data.WriteUint64(input);
66 }
67
Unmarshalling(uint64_t & output,MessageParcel & data)68 bool ITypesUtil::Unmarshalling(uint64_t &output, MessageParcel &data)
69 {
70 return data.ReadUint64(output);
71 }
72
Marshalling(int64_t input,MessageParcel & data)73 bool ITypesUtil::Marshalling(int64_t input, MessageParcel &data)
74 {
75 return data.WriteInt64(input);
76 }
77
Unmarshalling(int64_t & output,MessageParcel & data)78 bool ITypesUtil::Unmarshalling(int64_t &output, MessageParcel &data)
79 {
80 return data.ReadInt64(output);
81 }
82
Marshalling(ClientType input,MessageParcel & data)83 bool ITypesUtil::Marshalling(ClientType input, MessageParcel &data)
84 {
85 return data.WriteUint32(static_cast<uint32_t>(input));
86 }
87
Unmarshalling(ClientType & output,MessageParcel & data)88 bool ITypesUtil::Unmarshalling(ClientType &output, MessageParcel &data)
89 {
90 uint32_t ret = 0;
91 if (!data.ReadUint32(ret)) {
92 return false;
93 }
94 output = static_cast<ClientType>(ret);
95 return true;
96 }
97
Marshalling(double input,MessageParcel & data)98 bool ITypesUtil::Marshalling(double input, MessageParcel &data)
99 {
100 return data.WriteDouble(input);
101 }
102
Unmarshalling(double & output,MessageParcel & data)103 bool ITypesUtil::Unmarshalling(double &output, MessageParcel &data)
104 {
105 return data.ReadDouble(output);
106 }
107
Marshalling(const std::string & input,MessageParcel & data)108 bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data)
109 {
110 return data.WriteString(input);
111 }
112
Unmarshalling(std::string & output,MessageParcel & data)113 bool ITypesUtil::Unmarshalling(std::string &output, MessageParcel &data)
114 {
115 return data.ReadString(output);
116 }
117
Marshalling(const std::u16string & input,MessageParcel & data)118 bool ITypesUtil::Marshalling(const std::u16string &input, MessageParcel &data)
119 {
120 return data.WriteString16(input);
121 }
122
Unmarshalling(std::u16string & output,MessageParcel & data)123 bool ITypesUtil::Unmarshalling(std::u16string &output, MessageParcel &data)
124 {
125 return data.ReadString16(output);
126 }
127
Marshalling(const std::vector<uint8_t> & input,MessageParcel & data)128 bool ITypesUtil::Marshalling(const std::vector<uint8_t> &input, MessageParcel &data)
129 {
130 return data.WriteUInt8Vector(input);
131 }
132
Unmarshalling(std::vector<uint8_t> & output,MessageParcel & data)133 bool ITypesUtil::Unmarshalling(std::vector<uint8_t> &output, MessageParcel &data)
134 {
135 return data.ReadUInt8Vector(&output);
136 }
137
Marshalling(const sptr<IRemoteObject> & input,MessageParcel & data)138 bool ITypesUtil::Marshalling(const sptr<IRemoteObject> &input, MessageParcel &data)
139 {
140 return data.WriteRemoteObject(input);
141 }
142
Unmarshalling(sptr<IRemoteObject> & output,MessageParcel & data)143 bool ITypesUtil::Unmarshalling(sptr<IRemoteObject> &output, MessageParcel &data)
144 {
145 output = data.ReadRemoteObject();
146 return true;
147 }
148
Marshalling(const Property & input,MessageParcel & data)149 bool ITypesUtil::Marshalling(const Property &input, MessageParcel &data)
150 {
151 if (!Marshal(data, input.name, input.id, input.label, input.labelId, input.icon, input.iconId)) {
152 IMSA_HILOGE("write Property to message parcel failed.");
153 return false;
154 }
155 return true;
156 }
157
Unmarshalling(Property & output,MessageParcel & data)158 bool ITypesUtil::Unmarshalling(Property &output, MessageParcel &data)
159 {
160 if (!Unmarshal(data, output.name, output.id, output.label, output.labelId, output.icon, output.iconId)) {
161 IMSA_HILOGE("read Property from message parcel failed.");
162 return false;
163 }
164 return true;
165 }
166
Marshalling(const SubProperty & input,MessageParcel & data)167 bool ITypesUtil::Marshalling(const SubProperty &input, MessageParcel &data)
168 {
169 if (!Marshal(data, input.label, input.labelId, input.name, input.id, input.mode, input.locale, input.language,
170 input.icon, input.iconId)) {
171 IMSA_HILOGE("write SubProperty to message parcel failed.");
172 return false;
173 }
174 return true;
175 }
176
Unmarshalling(SubProperty & output,MessageParcel & data)177 bool ITypesUtil::Unmarshalling(SubProperty &output, MessageParcel &data)
178 {
179 if (!Unmarshal(data, output.label, output.labelId, output.name, output.id, output.mode, output.locale,
180 output.language, output.icon, output.iconId)) {
181 IMSA_HILOGE("read SubProperty from message parcel failed.");
182 return false;
183 }
184 return true;
185 }
186
Marshalling(const InputAttribute & input,MessageParcel & data)187 bool ITypesUtil::Marshalling(const InputAttribute &input, MessageParcel &data)
188 {
189 if (!Marshal(data, input.inputPattern, input.enterKeyType, input.inputOption, input.isTextPreviewSupported,
190 input.bundleName, input.immersiveMode, input.windowId, input.callingDisplayId)) {
191 IMSA_HILOGE("write InputAttribute to message parcel failed.");
192 return false;
193 }
194 return true;
195 }
196
Unmarshalling(InputAttribute & output,MessageParcel & data)197 bool ITypesUtil::Unmarshalling(InputAttribute &output, MessageParcel &data)
198 {
199 if (!Unmarshal(data, output.inputPattern, output.enterKeyType, output.inputOption, output.isTextPreviewSupported,
200 output.bundleName, output.immersiveMode, output.windowId, output.callingDisplayId)) {
201 IMSA_HILOGE("read InputAttribute from message parcel failed.");
202 return false;
203 }
204 return true;
205 }
206
Marshalling(const TextTotalConfig & input,MessageParcel & data)207 bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data)
208 {
209 if (!Marshal(data, input.inputAttribute)) {
210 IMSA_HILOGE("write InputAttribute to message parcel failed.");
211 return false;
212 }
213 if (!Marshal(data, input.cursorInfo.left, input.cursorInfo.top, input.cursorInfo.height, input.cursorInfo.width)) {
214 IMSA_HILOGE("write CursorInfo to message parcel failed.");
215 return false;
216 }
217 if (!Marshal(data, input.textSelection.oldBegin, input.textSelection.oldEnd, input.textSelection.newBegin,
218 input.textSelection.newEnd)) {
219 IMSA_HILOGE("write TextSelection to message parcel failed.");
220 return false;
221 }
222 if (!Marshal(data, input.windowId)) {
223 IMSA_HILOGE("write windowId to message parcel failed.");
224 return false;
225 }
226 if (!Marshal(data, input.positionY)) {
227 IMSA_HILOGE("write positionY to message parcel failed.");
228 return false;
229 }
230 if (!Marshal(data, input.height)) {
231 IMSA_HILOGE("write height to message parcel failed.");
232 return false;
233 }
234 if (!Marshal(data, input.privateCommand)) {
235 IMSA_HILOGE("write privateCommand to message parcel failed.");
236 return false;
237 }
238 return true;
239 }
240
Unmarshalling(TextTotalConfig & output,MessageParcel & data)241 bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data)
242 {
243 if (!Unmarshalling(output.inputAttribute, data)) {
244 IMSA_HILOGE("read InputAttribute from message parcel failed.");
245 return false;
246 }
247 if (!Unmarshal(data, output.cursorInfo.left, output.cursorInfo.top, output.cursorInfo.height,
248 output.cursorInfo.width)) {
249 IMSA_HILOGE("read CursorInfo from message parcel failed.");
250 return false;
251 }
252 if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, output.textSelection.newBegin,
253 output.textSelection.newEnd)) {
254 IMSA_HILOGE("read TextSelection from message parcel failed.");
255 return false;
256 }
257 if (!Unmarshal(data, output.windowId)) {
258 IMSA_HILOGE("read windowId from message parcel failed.");
259 return false;
260 }
261 if (!Unmarshal(data, output.positionY)) {
262 IMSA_HILOGE("read positionY from message parcel failed.");
263 return false;
264 }
265 if (!Unmarshal(data, output.height)) {
266 IMSA_HILOGE("read height from message parcel failed.");
267 return false;
268 }
269 if (!Unmarshal(data, output.privateCommand)) {
270 IMSA_HILOGE("read privateCommand from message parcel failed.");
271 return false;
272 }
273 return true;
274 }
275
Marshalling(const InputClientInfo & input,MessageParcel & data)276 bool ITypesUtil::Marshalling(const InputClientInfo &input, MessageParcel &data)
277 {
278 if (!Marshal(data, input.pid, input.uid, input.userID, input.isShowKeyboard, input.eventFlag, input.config,
279 input.state, input.isNotifyInputStart, input.needHide, input.requestKeyboardReason)) {
280 IMSA_HILOGE("write InputClientInfo to message parcel failed.");
281 return false;
282 }
283 Marshal(data, input.type, input.name);
284 return true;
285 }
286
Unmarshalling(InputClientInfo & output,MessageParcel & data)287 bool ITypesUtil::Unmarshalling(InputClientInfo &output, MessageParcel &data)
288 {
289 if (!Unmarshal(data, output.pid, output.uid, output.userID, output.isShowKeyboard, output.eventFlag, output.config,
290 output.state, output.isNotifyInputStart, output.needHide, output.requestKeyboardReason)) {
291 IMSA_HILOGE("read InputClientInfo from message parcel failed.");
292 return false;
293 }
294 Unmarshal(data, output.type, output.name);
295 return true;
296 }
297
Marshalling(const ImeWindowInfo & input,MessageParcel & data)298 bool ITypesUtil::Marshalling(const ImeWindowInfo &input, MessageParcel &data)
299 {
300 if (!Marshal(data, static_cast<int32_t>(input.panelInfo.panelFlag),
301 static_cast<int32_t>(input.panelInfo.panelType), input.windowInfo.name, input.windowInfo.top,
302 input.windowInfo.left, input.windowInfo.width, input.windowInfo.height)) {
303 IMSA_HILOGE("write InputWindowInfo to message parcel failed.");
304 return false;
305 }
306 return true;
307 }
308
Unmarshalling(ImeWindowInfo & output,MessageParcel & data)309 bool ITypesUtil::Unmarshalling(ImeWindowInfo &output, MessageParcel &data)
310 {
311 int32_t panelFlag = 0;
312 int32_t panelType = 0;
313 InputWindowInfo windowInfo;
314 if (!Unmarshal(data, panelFlag, panelType, windowInfo.name, windowInfo.top, windowInfo.left, windowInfo.width,
315 windowInfo.height)) {
316 IMSA_HILOGE("read InputWindowInfo from message parcel failed.");
317 return false;
318 }
319 output.panelInfo = { static_cast<PanelType>(panelType), static_cast<PanelFlag>(panelFlag) };
320 output.windowInfo = windowInfo;
321 return true;
322 }
323
Marshalling(const PanelStatusInfo & input,MessageParcel & data)324 bool ITypesUtil::Marshalling(const PanelStatusInfo &input, MessageParcel &data)
325 {
326 return data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelType)) &&
327 data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelFlag)) && data.WriteBool(input.visible) &&
328 data.WriteInt32(static_cast<int32_t>(input.trigger));
329 }
330
Unmarshalling(PanelStatusInfo & output,MessageParcel & data)331 bool ITypesUtil::Unmarshalling(PanelStatusInfo &output, MessageParcel &data)
332 {
333 int32_t type = -1;
334 int32_t flag = -1;
335 bool visible = false;
336 int32_t trigger = -1;
337 if (!data.ReadInt32(type) || !data.ReadInt32(flag) || !data.ReadBool(visible) || !data.ReadInt32(trigger)) {
338 return false;
339 }
340 output = { { static_cast<PanelType>(type), static_cast<PanelFlag>(flag) }, visible, static_cast<Trigger>(trigger) };
341 return true;
342 }
343
Marshalling(const SysPanelStatus & input,MessageParcel & data)344 bool ITypesUtil::Marshalling(const SysPanelStatus &input, MessageParcel &data)
345 {
346 bool ret = data.WriteInt32(static_cast<int32_t>(input.inputType)) &&
347 data.WriteInt32(input.flag) &&
348 data.WriteUint32(input.width) &&
349 data.WriteUint32(input.height) &&
350 data.WriteBool(input.isMainDisplay);
351 return ret;
352 }
353
Unmarshalling(SysPanelStatus & output,MessageParcel & data)354 bool ITypesUtil::Unmarshalling(SysPanelStatus &output, MessageParcel &data)
355 {
356 int32_t inputType = 0;
357 if (!data.ReadInt32(inputType) || !data.ReadInt32(output.flag) || !data.ReadUint32(output.width) ||
358 !data.ReadUint32(output.height)) {
359 return false;
360 }
361 output.inputType = static_cast<InputType>(inputType);
362 data.ReadBool(output.isMainDisplay);
363 return true;
364 }
365
Marshalling(const OHOS::AppExecFwk::ElementName & input,MessageParcel & data)366 bool ITypesUtil::Marshalling(const OHOS::AppExecFwk::ElementName &input, MessageParcel &data)
367 {
368 return data.WriteString(input.GetBundleName().c_str()) && data.WriteString(input.GetModuleName().c_str()) &&
369 data.WriteString(input.GetAbilityName().c_str());
370 }
371
Unmarshalling(OHOS::AppExecFwk::ElementName & output,MessageParcel & data)372 bool ITypesUtil::Unmarshalling(OHOS::AppExecFwk::ElementName &output, MessageParcel &data)
373 {
374 std::string bundleName;
375 std::string moduleName;
376 std::string abilityName;
377 if (data.ReadString(bundleName) && data.ReadString(moduleName) && data.ReadString(abilityName)) {
378 output.SetBundleName(bundleName);
379 output.SetModuleName(moduleName);
380 output.SetAbilityName(abilityName);
381 return true;
382 }
383 IMSA_HILOGE("read ElementName from message parcel failed.");
384 return false;
385 }
386
Marshalling(InputType input,MessageParcel & data)387 bool ITypesUtil::Marshalling(InputType input, MessageParcel &data)
388 {
389 return data.WriteInt32(static_cast<int32_t>(input));
390 }
391
Unmarshalling(InputType & output,MessageParcel & data)392 bool ITypesUtil::Unmarshalling(InputType &output, MessageParcel &data)
393 {
394 int32_t ret = 0;
395 if (!data.ReadInt32(ret)) {
396 return false;
397 }
398 output = static_cast<InputType>(ret);
399 return true;
400 }
401
Marshalling(const PanelInfo & input,MessageParcel & data)402 bool ITypesUtil::Marshalling(const PanelInfo &input, MessageParcel &data)
403 {
404 return data.WriteInt32(static_cast<int32_t>(input.panelType)) &&
405 data.WriteInt32(static_cast<int32_t>(input.panelFlag));
406 }
407
Unmarshalling(PanelInfo & output,MessageParcel & data)408 bool ITypesUtil::Unmarshalling(PanelInfo &output, MessageParcel &data)
409 {
410 int32_t panelType = 0;
411 int32_t panelFlag = 0;
412 if (!data.ReadInt32(panelType) || !data.ReadInt32(panelFlag)) {
413 return false;
414 }
415 output.panelFlag = static_cast<PanelFlag>(panelFlag);
416 output.panelType = static_cast<PanelType>(panelType);
417 return true;
418 }
419
Marshalling(ClientState input,MessageParcel & data)420 bool ITypesUtil::Marshalling(ClientState input, MessageParcel &data)
421 {
422 return data.WriteUint32(static_cast<uint32_t>(input));
423 }
424
Unmarshalling(ClientState & output,MessageParcel & data)425 bool ITypesUtil::Unmarshalling(ClientState &output, MessageParcel &data)
426 {
427 uint32_t state = 0;
428 if (!data.ReadUint32(state)) {
429 IMSA_HILOGE("ClientState read failed.");
430 return false;
431 }
432 output = static_cast<ClientState>(state);
433 return true;
434 }
435
Marshalling(SwitchTrigger input,MessageParcel & data)436 bool ITypesUtil::Marshalling(SwitchTrigger input, MessageParcel &data)
437 {
438 return data.WriteUint32(static_cast<uint32_t>(input));
439 }
440
Unmarshalling(SwitchTrigger & output,MessageParcel & data)441 bool ITypesUtil::Unmarshalling(SwitchTrigger &output, MessageParcel &data)
442 {
443 uint32_t state = 0;
444 if (!data.ReadUint32(state)) {
445 IMSA_HILOGE("ClientState read failed.");
446 return false;
447 }
448 output = static_cast<SwitchTrigger>(state);
449 return true;
450 }
451
Marshalling(const PrivateDataValue & input,MessageParcel & data)452 bool ITypesUtil::Marshalling(const PrivateDataValue &input, MessageParcel &data)
453 {
454 size_t idx = input.index();
455 if (!data.WriteInt32(static_cast<int32_t>(idx))) {
456 IMSA_HILOGE("write index failed.");
457 return false;
458 }
459 if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
460 auto stringValue = std::get_if<std::string>(&input);
461 if (stringValue != nullptr) {
462 return data.WriteString(*stringValue);
463 }
464 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
465 auto boolValue = std::get_if<bool>(&input);
466 if (boolValue != nullptr) {
467 return data.WriteBool(*boolValue);
468 }
469 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
470 auto numberValue = std::get_if<int32_t>(&input);
471 if (numberValue != nullptr) {
472 return data.WriteInt32(*numberValue);
473 }
474 }
475 IMSA_HILOGE("write PrivateDataValue with wrong type.");
476 return false;
477 }
478
Unmarshalling(PrivateDataValue & output,MessageParcel & data)479 bool ITypesUtil::Unmarshalling(PrivateDataValue &output, MessageParcel &data)
480 {
481 int32_t valueType = data.ReadInt32();
482 bool res = false;
483 if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
484 std::string strValue;
485 res = data.ReadString(strValue);
486 output.emplace<std::string>(strValue);
487 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
488 bool boolValue = false;
489 res = data.ReadBool(boolValue);
490 output.emplace<bool>(boolValue);
491 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
492 int32_t intValue = 0;
493 res = data.ReadInt32(intValue);
494 output.emplace<int32_t>(intValue);
495 }
496 if (!res) {
497 IMSA_HILOGE("read PrivateDataValue from message parcel failed.");
498 }
499 return res;
500 }
501
Marshalling(const Range & input,MessageParcel & data)502 bool ITypesUtil::Marshalling(const Range &input, MessageParcel &data)
503 {
504 if (!Marshal(data, input.start, input.end)) {
505 IMSA_HILOGE("failed to write Range into message parcel.");
506 return false;
507 }
508 return true;
509 }
510
Unmarshalling(Range & output,MessageParcel & data)511 bool ITypesUtil::Unmarshalling(Range &output, MessageParcel &data)
512 {
513 if (!Unmarshal(data, output.start, output.end)) {
514 IMSA_HILOGE("failed to read Range from message parcel.");
515 return false;
516 }
517 return true;
518 }
519
Marshalling(const ArrayBuffer & input,MessageParcel & data)520 bool ITypesUtil::Marshalling(const ArrayBuffer &input, MessageParcel &data)
521 {
522 if (!Marshal(data, input.msgId, input.msgParam, input.jsArgc)) {
523 IMSA_HILOGE("failed to write ArrayBuffer into message parcel.");
524 return false;
525 }
526 return true;
527 }
528
Unmarshalling(ArrayBuffer & output,MessageParcel & data)529 bool ITypesUtil::Unmarshalling(ArrayBuffer &output, MessageParcel &data)
530 {
531 if (!Unmarshal(data, output.msgId, output.msgParam, output.jsArgc)) {
532 IMSA_HILOGE("failed to read ArrayBuffer from message parcel.");
533 return false;
534 }
535 return true;
536 }
537
Marshalling(RequestKeyboardReason input,MessageParcel & data)538 bool ITypesUtil::Marshalling(RequestKeyboardReason input, MessageParcel &data)
539 {
540 return data.WriteInt32(static_cast<int32_t>(input));
541 }
542
Unmarshalling(RequestKeyboardReason & output,MessageParcel & data)543 bool ITypesUtil::Unmarshalling(RequestKeyboardReason &output, MessageParcel &data)
544 {
545 int32_t ret = 0;
546 if (!data.ReadInt32(ret)) {
547 return false;
548 }
549 output = static_cast<RequestKeyboardReason>(ret);
550 return true;
551 }
552 } // namespace MiscServices
553 } // namespace OHOS