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(double input,MessageParcel & data)73 bool ITypesUtil::Marshalling(double input, MessageParcel &data)
74 {
75 return data.WriteDouble(input);
76 }
77
Unmarshalling(double & output,MessageParcel & data)78 bool ITypesUtil::Unmarshalling(double &output, MessageParcel &data)
79 {
80 return data.ReadDouble(output);
81 }
82
Marshalling(const std::string & input,MessageParcel & data)83 bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data)
84 {
85 return data.WriteString(input);
86 }
87
Unmarshalling(std::string & output,MessageParcel & data)88 bool ITypesUtil::Unmarshalling(std::string &output, MessageParcel &data)
89 {
90 return data.ReadString(output);
91 }
92
Marshalling(const std::u16string & input,MessageParcel & data)93 bool ITypesUtil::Marshalling(const std::u16string &input, MessageParcel &data)
94 {
95 return data.WriteString16(input);
96 }
97
Unmarshalling(std::u16string & output,MessageParcel & data)98 bool ITypesUtil::Unmarshalling(std::u16string &output, MessageParcel &data)
99 {
100 return data.ReadString16(output);
101 }
102
Marshalling(const std::vector<uint8_t> & input,MessageParcel & data)103 bool ITypesUtil::Marshalling(const std::vector<uint8_t> &input, MessageParcel &data)
104 {
105 return data.WriteUInt8Vector(input);
106 }
107
Unmarshalling(std::vector<uint8_t> & output,MessageParcel & data)108 bool ITypesUtil::Unmarshalling(std::vector<uint8_t> &output, MessageParcel &data)
109 {
110 return data.ReadUInt8Vector(&output);
111 }
112
Marshalling(const sptr<IRemoteObject> & input,MessageParcel & data)113 bool ITypesUtil::Marshalling(const sptr<IRemoteObject> &input, MessageParcel &data)
114 {
115 return data.WriteRemoteObject(input);
116 }
117
Unmarshalling(sptr<IRemoteObject> & output,MessageParcel & data)118 bool ITypesUtil::Unmarshalling(sptr<IRemoteObject> &output, MessageParcel &data)
119 {
120 output = data.ReadRemoteObject();
121 return true;
122 }
123
Marshalling(const Property & input,MessageParcel & data)124 bool ITypesUtil::Marshalling(const Property &input, MessageParcel &data)
125 {
126 if (!Marshal(data, input.name, input.id, input.label, input.labelId, input.icon, input.iconId)) {
127 IMSA_HILOGE("write Property to message parcel failed.");
128 return false;
129 }
130 return true;
131 }
132
Unmarshalling(Property & output,MessageParcel & data)133 bool ITypesUtil::Unmarshalling(Property &output, MessageParcel &data)
134 {
135 if (!Unmarshal(data, output.name, output.id, output.label, output.labelId, output.icon, output.iconId)) {
136 IMSA_HILOGE("read Property from message parcel failed.");
137 return false;
138 }
139 return true;
140 }
141
Marshalling(const SubProperty & input,MessageParcel & data)142 bool ITypesUtil::Marshalling(const SubProperty &input, MessageParcel &data)
143 {
144 if (!Marshal(data, input.label, input.labelId, input.name, input.id, input.mode, input.locale, input.language,
145 input.icon, input.iconId)) {
146 IMSA_HILOGE("write SubProperty to message parcel failed.");
147 return false;
148 }
149 return true;
150 }
151
Unmarshalling(SubProperty & output,MessageParcel & data)152 bool ITypesUtil::Unmarshalling(SubProperty &output, MessageParcel &data)
153 {
154 if (!Unmarshal(data, output.label, output.labelId, output.name, output.id, output.mode, output.locale,
155 output.language, output.icon, output.iconId)) {
156 IMSA_HILOGE("read SubProperty from message parcel failed.");
157 return false;
158 }
159 return true;
160 }
161
Marshalling(const InputAttribute & input,MessageParcel & data)162 bool ITypesUtil::Marshalling(const InputAttribute &input, MessageParcel &data)
163 {
164 if (!Marshal(data, input.inputPattern, input.enterKeyType, input.inputOption, input.isTextPreviewSupported,
165 input.bundleName)) {
166 IMSA_HILOGE("write InputAttribute to message parcel failed.");
167 return false;
168 }
169 return true;
170 }
171
Unmarshalling(InputAttribute & output,MessageParcel & data)172 bool ITypesUtil::Unmarshalling(InputAttribute &output, MessageParcel &data)
173 {
174 if (!Unmarshal(data, output.inputPattern, output.enterKeyType, output.inputOption, output.isTextPreviewSupported,
175 output.bundleName)) {
176 IMSA_HILOGE("read InputAttribute from message parcel failed.");
177 return false;
178 }
179 return true;
180 }
181
Marshalling(const TextTotalConfig & input,MessageParcel & data)182 bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data)
183 {
184 if (!Marshal(data, input.inputAttribute)) {
185 IMSA_HILOGE("write InputAttribute to message parcel failed.");
186 return false;
187 }
188 if (!Marshal(data, input.cursorInfo.left, input.cursorInfo.top, input.cursorInfo.height, input.cursorInfo.width)) {
189 IMSA_HILOGE("write CursorInfo to message parcel failed.");
190 return false;
191 }
192 if (!Marshal(data, input.textSelection.oldBegin, input.textSelection.oldEnd, input.textSelection.newBegin,
193 input.textSelection.newEnd)) {
194 IMSA_HILOGE("write TextSelection to message parcel failed.");
195 return false;
196 }
197 if (!Marshal(data, input.windowId)) {
198 IMSA_HILOGE("write windowId to message parcel failed.");
199 return false;
200 }
201 if (!Marshal(data, input.positionY)) {
202 IMSA_HILOGE("write positionY to message parcel failed.");
203 return false;
204 }
205 if (!Marshal(data, input.height)) {
206 IMSA_HILOGE("write height to message parcel failed.");
207 return false;
208 }
209 if (!Marshal(data, input.privateCommand)) {
210 IMSA_HILOGE("write privateCommand to message parcel failed.");
211 return false;
212 }
213 return true;
214 }
215
Unmarshalling(TextTotalConfig & output,MessageParcel & data)216 bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data)
217 {
218 if (!Unmarshalling(output.inputAttribute, data)) {
219 IMSA_HILOGE("read InputAttribute from message parcel failed.");
220 return false;
221 }
222 if (!Unmarshal(data, output.cursorInfo.left, output.cursorInfo.top, output.cursorInfo.height,
223 output.cursorInfo.width)) {
224 IMSA_HILOGE("read CursorInfo from message parcel failed.");
225 return false;
226 }
227 if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, output.textSelection.newBegin,
228 output.textSelection.newEnd)) {
229 IMSA_HILOGE("read TextSelection from message parcel failed.");
230 return false;
231 }
232 if (!Unmarshal(data, output.windowId)) {
233 IMSA_HILOGE("read windowId from message parcel failed.");
234 return false;
235 }
236 if (!Unmarshal(data, output.positionY)) {
237 IMSA_HILOGE("read positionY from message parcel failed.");
238 return false;
239 }
240 if (!Unmarshal(data, output.height)) {
241 IMSA_HILOGE("read height from message parcel failed.");
242 return false;
243 }
244 if (!Unmarshal(data, output.privateCommand)) {
245 IMSA_HILOGE("read privateCommand from message parcel failed.");
246 return false;
247 }
248 return true;
249 }
250
Marshalling(const InputClientInfo & input,MessageParcel & data)251 bool ITypesUtil::Marshalling(const InputClientInfo &input, MessageParcel &data)
252 {
253 if (!Marshal(data, input.pid, input.uid, input.userID, input.isShowKeyboard, input.eventFlag, input.config,
254 input.state, input.isNotifyInputStart, input.needHide)) {
255 IMSA_HILOGE("write InputClientInfo to message parcel failed.");
256 return false;
257 }
258 return true;
259 }
260
Unmarshalling(InputClientInfo & output,MessageParcel & data)261 bool ITypesUtil::Unmarshalling(InputClientInfo &output, MessageParcel &data)
262 {
263 if (!Unmarshal(data, output.pid, output.uid, output.userID, output.isShowKeyboard, output.eventFlag, output.config,
264 output.state, output.isNotifyInputStart, output.needHide)) {
265 IMSA_HILOGE("read InputClientInfo from message parcel failed.");
266 return false;
267 }
268 return true;
269 }
270
Marshalling(const ImeWindowInfo & input,MessageParcel & data)271 bool ITypesUtil::Marshalling(const ImeWindowInfo &input, MessageParcel &data)
272 {
273 if (!Marshal(data, static_cast<int32_t>(input.panelInfo.panelFlag),
274 static_cast<int32_t>(input.panelInfo.panelType), input.windowInfo.name, input.windowInfo.top,
275 input.windowInfo.left, input.windowInfo.width, input.windowInfo.height)) {
276 IMSA_HILOGE("write InputWindowInfo to message parcel failed.");
277 return false;
278 }
279 return true;
280 }
281
Unmarshalling(ImeWindowInfo & output,MessageParcel & data)282 bool ITypesUtil::Unmarshalling(ImeWindowInfo &output, MessageParcel &data)
283 {
284 int32_t panelFlag = 0;
285 int32_t panelType = 0;
286 InputWindowInfo windowInfo;
287 if (!Unmarshal(data, panelFlag, panelType, windowInfo.name, windowInfo.top, windowInfo.left, windowInfo.width,
288 windowInfo.height)) {
289 IMSA_HILOGE("read InputWindowInfo from message parcel failed.");
290 return false;
291 }
292 output.panelInfo = { static_cast<PanelType>(panelType), static_cast<PanelFlag>(panelFlag) };
293 output.windowInfo = windowInfo;
294 return true;
295 }
296
Marshalling(const PanelStatusInfo & input,MessageParcel & data)297 bool ITypesUtil::Marshalling(const PanelStatusInfo &input, MessageParcel &data)
298 {
299 return data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelType))
300 && data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelFlag)) && data.WriteBool(input.visible)
301 && data.WriteInt32(static_cast<int32_t>(input.trigger));
302 }
303
Unmarshalling(PanelStatusInfo & output,MessageParcel & data)304 bool ITypesUtil::Unmarshalling(PanelStatusInfo &output, MessageParcel &data)
305 {
306 int32_t type = -1;
307 int32_t flag = -1;
308 bool visible = false;
309 int32_t trigger = -1;
310 if (!data.ReadInt32(type) || !data.ReadInt32(flag) || !data.ReadBool(visible) || !data.ReadInt32(trigger)) {
311 return false;
312 }
313 output = { { static_cast<PanelType>(type), static_cast<PanelFlag>(flag) }, visible, static_cast<Trigger>(trigger) };
314 return true;
315 }
316
Marshalling(const SysPanelStatus & input,MessageParcel & data)317 bool ITypesUtil::Marshalling(const SysPanelStatus &input, MessageParcel &data)
318 {
319 return data.WriteBool(input.isSecurity) && data.WriteInt32(input.flag) && data.WriteUint32(input.width) &&
320 data.WriteUint32(input.height);
321 }
322
Unmarshalling(SysPanelStatus & output,MessageParcel & data)323 bool ITypesUtil::Unmarshalling(SysPanelStatus &output, MessageParcel &data)
324 {
325 if (!data.ReadBool(output.isSecurity) || !data.ReadInt32(output.flag) || !data.ReadUint32(output.width) ||
326 !data.ReadUint32(output.height)) {
327 return false;
328 }
329 return true;
330 }
331
Marshalling(const OHOS::AppExecFwk::ElementName & input,MessageParcel & data)332 bool ITypesUtil::Marshalling(const OHOS::AppExecFwk::ElementName &input, MessageParcel &data)
333 {
334 return data.WriteString(input.GetBundleName().c_str()) && data.WriteString(input.GetModuleName().c_str()) &&
335 data.WriteString(input.GetAbilityName().c_str());
336 }
337
Unmarshalling(OHOS::AppExecFwk::ElementName & output,MessageParcel & data)338 bool ITypesUtil::Unmarshalling(OHOS::AppExecFwk::ElementName &output, MessageParcel &data)
339 {
340 std::string bundleName;
341 std::string moduleName;
342 std::string abilityName;
343 if (data.ReadString(bundleName) && data.ReadString(moduleName) && data.ReadString(abilityName)) {
344 output.SetBundleName(bundleName);
345 output.SetModuleName(moduleName);
346 output.SetAbilityName(abilityName);
347 return true;
348 }
349 IMSA_HILOGE("read ElementName from message parcel failed.");
350 return false;
351 }
352
Marshalling(InputType input,MessageParcel & data)353 bool ITypesUtil::Marshalling(InputType input, MessageParcel &data)
354 {
355 return data.WriteInt32(static_cast<int32_t>(input));
356 }
357
Unmarshalling(InputType & output,MessageParcel & data)358 bool ITypesUtil::Unmarshalling(InputType &output, MessageParcel &data)
359 {
360 int32_t ret = 0;
361 if (!data.ReadInt32(ret)) {
362 return false;
363 }
364 output = static_cast<InputType>(ret);
365 return true;
366 }
367
Marshalling(const PanelInfo & input,MessageParcel & data)368 bool ITypesUtil::Marshalling(const PanelInfo &input, MessageParcel &data)
369 {
370 return data.WriteInt32(static_cast<int32_t>(input.panelType))
371 && data.WriteInt32(static_cast<int32_t>(input.panelFlag));
372 }
373
Unmarshalling(PanelInfo & output,MessageParcel & data)374 bool ITypesUtil::Unmarshalling(PanelInfo &output, MessageParcel &data)
375 {
376 int32_t panelType = 0;
377 int32_t panelFlag = 0;
378 if (!data.ReadInt32(panelType) || !data.ReadInt32(panelFlag)) {
379 return false;
380 }
381 output.panelFlag = static_cast<PanelFlag>(panelFlag);
382 output.panelType = static_cast<PanelType>(panelType);
383 return true;
384 }
385
Marshalling(ClientState input,MessageParcel & data)386 bool ITypesUtil::Marshalling(ClientState input, MessageParcel &data)
387 {
388 return data.WriteUint32(static_cast<uint32_t>(input));
389 }
390
Unmarshalling(ClientState & output,MessageParcel & data)391 bool ITypesUtil::Unmarshalling(ClientState &output, MessageParcel &data)
392 {
393 uint32_t state = 0;
394 if (!data.ReadUint32(state)) {
395 IMSA_HILOGE("ClientState read failed.");
396 return false;
397 }
398 output = static_cast<ClientState>(state);
399 return true;
400 }
401
Marshalling(SwitchTrigger input,MessageParcel & data)402 bool ITypesUtil::Marshalling(SwitchTrigger input, MessageParcel &data)
403 {
404 return data.WriteUint32(static_cast<uint32_t>(input));
405 }
406
Unmarshalling(SwitchTrigger & output,MessageParcel & data)407 bool ITypesUtil::Unmarshalling(SwitchTrigger &output, MessageParcel &data)
408 {
409 uint32_t state = 0;
410 if (!data.ReadUint32(state)) {
411 IMSA_HILOGE("ClientState read failed.");
412 return false;
413 }
414 output = static_cast<SwitchTrigger>(state);
415 return true;
416 }
417
Marshalling(const PrivateDataValue & input,MessageParcel & data)418 bool ITypesUtil::Marshalling(const PrivateDataValue &input, MessageParcel &data)
419 {
420 size_t idx = input.index();
421 if (!data.WriteInt32(static_cast<int32_t>(idx))) {
422 IMSA_HILOGE("write index failed.");
423 return false;
424 }
425 if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
426 auto stringValue = std::get_if<std::string>(&input);
427 if (stringValue != nullptr) {
428 return data.WriteString(*stringValue);
429 }
430 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
431 auto boolValue = std::get_if<bool>(&input);
432 if (boolValue != nullptr) {
433 return data.WriteBool(*boolValue);
434 }
435 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
436 auto numberValue = std::get_if<int32_t>(&input);
437 if (numberValue != nullptr) {
438 return data.WriteInt32(*numberValue);
439 }
440 }
441 IMSA_HILOGE("write PrivateDataValue with wrong type.");
442 return false;
443 }
444
Unmarshalling(PrivateDataValue & output,MessageParcel & data)445 bool ITypesUtil::Unmarshalling(PrivateDataValue &output, MessageParcel &data)
446 {
447 int32_t valueType = data.ReadInt32();
448 bool res = false;
449 if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
450 std::string strValue;
451 res = data.ReadString(strValue);
452 output.emplace<std::string>(strValue);
453 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
454 bool boolValue = false;
455 res = data.ReadBool(boolValue);
456 output.emplace<bool>(boolValue);
457 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
458 int32_t intValue = 0;
459 res = data.ReadInt32(intValue);
460 output.emplace<int32_t>(intValue);
461 }
462 if (!res) {
463 IMSA_HILOGE("read PrivateDataValue from message parcel failed.");
464 }
465 return res;
466 }
467
Marshalling(const Range & input,MessageParcel & data)468 bool ITypesUtil::Marshalling(const Range &input, MessageParcel &data)
469 {
470 if (!Marshal(data, input.start, input.end)) {
471 IMSA_HILOGE("failed to write Range into message parcel.");
472 return false;
473 }
474 return true;
475 }
476
Unmarshalling(Range & output,MessageParcel & data)477 bool ITypesUtil::Unmarshalling(Range &output, MessageParcel &data)
478 {
479 if (!Unmarshal(data, output.start, output.end)) {
480 IMSA_HILOGE("failed to read Range from message parcel.");
481 return false;
482 }
483 return true;
484 }
485 } // namespace MiscServices
486 } // namespace OHOS