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 IMSA_HILOGE("write InputAttribute to message parcel failed.");
166 return false;
167 }
168 return true;
169 }
170
Unmarshalling(InputAttribute & output,MessageParcel & data)171 bool ITypesUtil::Unmarshalling(InputAttribute &output, MessageParcel &data)
172 {
173 if (!Unmarshal(data, output.inputPattern, output.enterKeyType, output.inputOption, output.isTextPreviewSupported)) {
174 IMSA_HILOGE("read InputAttribute from message parcel failed.");
175 return false;
176 }
177 return true;
178 }
179
Marshalling(const TextTotalConfig & input,MessageParcel & data)180 bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data)
181 {
182 if (!Marshal(data, input.inputAttribute)) {
183 IMSA_HILOGE("write InputAttribute to message parcel failed.");
184 return false;
185 }
186 if (!Marshal(data, input.cursorInfo.left, input.cursorInfo.top, input.cursorInfo.height, input.cursorInfo.width)) {
187 IMSA_HILOGE("write CursorInfo to message parcel failed.");
188 return false;
189 }
190 if (!Marshal(data, input.textSelection.oldBegin, input.textSelection.oldEnd, input.textSelection.newBegin,
191 input.textSelection.newEnd)) {
192 IMSA_HILOGE("write TextSelection to message parcel failed.");
193 return false;
194 }
195 if (!Marshal(data, input.windowId)) {
196 IMSA_HILOGE("write windowId to message parcel failed.");
197 return false;
198 }
199 if (!Marshal(data, input.positionY)) {
200 IMSA_HILOGE("write positionY to message parcel failed.");
201 return false;
202 }
203 if (!Marshal(data, input.height)) {
204 IMSA_HILOGE("write height to message parcel failed.");
205 return false;
206 }
207 if (!Marshal(data, input.privateCommand)) {
208 IMSA_HILOGE("write privateCommand to message parcel failed.");
209 return false;
210 }
211 return true;
212 }
213
Unmarshalling(TextTotalConfig & output,MessageParcel & data)214 bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data)
215 {
216 if (!Unmarshalling(output.inputAttribute, data)) {
217 IMSA_HILOGE("read InputAttribute from message parcel failed.");
218 return false;
219 }
220 if (!Unmarshal(data, output.cursorInfo.left, output.cursorInfo.top, output.cursorInfo.height,
221 output.cursorInfo.width)) {
222 IMSA_HILOGE("read CursorInfo from message parcel failed.");
223 return false;
224 }
225 if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd, output.textSelection.newBegin,
226 output.textSelection.newEnd)) {
227 IMSA_HILOGE("read TextSelection from message parcel failed.");
228 return false;
229 }
230 if (!Unmarshal(data, output.windowId)) {
231 IMSA_HILOGE("read windowId from message parcel failed.");
232 return false;
233 }
234 if (!Unmarshal(data, output.positionY)) {
235 IMSA_HILOGE("read positionY from message parcel failed.");
236 return false;
237 }
238 if (!Unmarshal(data, output.height)) {
239 IMSA_HILOGE("read height from message parcel failed.");
240 return false;
241 }
242 if (!Unmarshal(data, output.privateCommand)) {
243 IMSA_HILOGE("read privateCommand from message parcel failed.");
244 return false;
245 }
246 return true;
247 }
248
Marshalling(const InputClientInfo & input,MessageParcel & data)249 bool ITypesUtil::Marshalling(const InputClientInfo &input, MessageParcel &data)
250 {
251 if (!Marshal(data, input.pid, input.uid, input.userID, input.isShowKeyboard, input.eventFlag, input.config,
252 input.state, input.isNotifyInputStart, input.needHide)) {
253 IMSA_HILOGE("write InputClientInfo to message parcel failed.");
254 return false;
255 }
256 return true;
257 }
258
Unmarshalling(InputClientInfo & output,MessageParcel & data)259 bool ITypesUtil::Unmarshalling(InputClientInfo &output, MessageParcel &data)
260 {
261 if (!Unmarshal(data, output.pid, output.uid, output.userID, output.isShowKeyboard, output.eventFlag, output.config,
262 output.state, output.isNotifyInputStart, output.needHide)) {
263 IMSA_HILOGE("read InputClientInfo from message parcel failed.");
264 return false;
265 }
266 return true;
267 }
268
Marshalling(const ImeWindowInfo & input,MessageParcel & data)269 bool ITypesUtil::Marshalling(const ImeWindowInfo &input, MessageParcel &data)
270 {
271 if (!Marshal(data, static_cast<int32_t>(input.panelInfo.panelFlag),
272 static_cast<int32_t>(input.panelInfo.panelType), input.windowInfo.name, input.windowInfo.top,
273 input.windowInfo.left, input.windowInfo.width, input.windowInfo.height)) {
274 IMSA_HILOGE("write InputWindowInfo to message parcel failed.");
275 return false;
276 }
277 return true;
278 }
279
Unmarshalling(ImeWindowInfo & output,MessageParcel & data)280 bool ITypesUtil::Unmarshalling(ImeWindowInfo &output, MessageParcel &data)
281 {
282 int32_t panelFlag = 0;
283 int32_t panelType = 0;
284 InputWindowInfo windowInfo;
285 if (!Unmarshal(data, panelFlag, panelType, windowInfo.name, windowInfo.top, windowInfo.left, windowInfo.width,
286 windowInfo.height)) {
287 IMSA_HILOGE("read InputWindowInfo from message parcel failed.");
288 return false;
289 }
290 output.panelInfo = { static_cast<PanelType>(panelType), static_cast<PanelFlag>(panelFlag) };
291 output.windowInfo = windowInfo;
292 return true;
293 }
294
Marshalling(const PanelStatusInfo & input,MessageParcel & data)295 bool ITypesUtil::Marshalling(const PanelStatusInfo &input, MessageParcel &data)
296 {
297 return data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelType))
298 && data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelFlag)) && data.WriteBool(input.visible)
299 && data.WriteInt32(static_cast<int32_t>(input.trigger));
300 }
301
Unmarshalling(PanelStatusInfo & output,MessageParcel & data)302 bool ITypesUtil::Unmarshalling(PanelStatusInfo &output, MessageParcel &data)
303 {
304 int32_t type = -1;
305 int32_t flag = -1;
306 bool visible = false;
307 int32_t trigger = -1;
308 if (!data.ReadInt32(type) || !data.ReadInt32(flag) || !data.ReadBool(visible) || !data.ReadInt32(trigger)) {
309 return false;
310 }
311 output = { { static_cast<PanelType>(type), static_cast<PanelFlag>(flag) }, visible, static_cast<Trigger>(trigger) };
312 return true;
313 }
314
Marshalling(const SysPanelStatus & input,MessageParcel & data)315 bool ITypesUtil::Marshalling(const SysPanelStatus &input, MessageParcel &data)
316 {
317 return data.WriteBool(input.isSecurity) && data.WriteInt32(input.flag) && data.WriteUint32(input.width) &&
318 data.WriteUint32(input.height);
319 }
320
Unmarshalling(SysPanelStatus & output,MessageParcel & data)321 bool ITypesUtil::Unmarshalling(SysPanelStatus &output, MessageParcel &data)
322 {
323 if (!data.ReadBool(output.isSecurity) || !data.ReadInt32(output.flag) || !data.ReadUint32(output.width) ||
324 !data.ReadUint32(output.height)) {
325 return false;
326 }
327 return true;
328 }
329
Marshalling(const OHOS::AppExecFwk::ElementName & input,MessageParcel & data)330 bool ITypesUtil::Marshalling(const OHOS::AppExecFwk::ElementName &input, MessageParcel &data)
331 {
332 return data.WriteString(input.GetBundleName().c_str()) && data.WriteString(input.GetModuleName().c_str()) &&
333 data.WriteString(input.GetAbilityName().c_str());
334 }
335
Unmarshalling(OHOS::AppExecFwk::ElementName & output,MessageParcel & data)336 bool ITypesUtil::Unmarshalling(OHOS::AppExecFwk::ElementName &output, MessageParcel &data)
337 {
338 std::string bundleName;
339 std::string moduleName;
340 std::string abilityName;
341 if (data.ReadString(bundleName) && data.ReadString(moduleName) && data.ReadString(abilityName)) {
342 output.SetBundleName(bundleName);
343 output.SetModuleName(moduleName);
344 output.SetAbilityName(abilityName);
345 return true;
346 }
347 IMSA_HILOGE("read ElementName from message parcel failed.");
348 return false;
349 }
350
Marshalling(InputType input,MessageParcel & data)351 bool ITypesUtil::Marshalling(InputType input, MessageParcel &data)
352 {
353 return data.WriteInt32(static_cast<int32_t>(input));
354 }
355
Unmarshalling(InputType & output,MessageParcel & data)356 bool ITypesUtil::Unmarshalling(InputType &output, MessageParcel &data)
357 {
358 int32_t ret = 0;
359 if (!data.ReadInt32(ret)) {
360 return false;
361 }
362 output = static_cast<InputType>(ret);
363 return true;
364 }
365
Marshalling(const PanelInfo & input,MessageParcel & data)366 bool ITypesUtil::Marshalling(const PanelInfo &input, MessageParcel &data)
367 {
368 return data.WriteInt32(static_cast<int32_t>(input.panelType))
369 && data.WriteInt32(static_cast<int32_t>(input.panelFlag));
370 }
371
Unmarshalling(PanelInfo & output,MessageParcel & data)372 bool ITypesUtil::Unmarshalling(PanelInfo &output, MessageParcel &data)
373 {
374 int32_t panelType = 0;
375 int32_t panelFlag = 0;
376 if (!data.ReadInt32(panelType) || !data.ReadInt32(panelFlag)) {
377 return false;
378 }
379 output.panelFlag = static_cast<PanelFlag>(panelFlag);
380 output.panelType = static_cast<PanelType>(panelType);
381 return true;
382 }
383
Marshalling(ClientState input,MessageParcel & data)384 bool ITypesUtil::Marshalling(ClientState input, MessageParcel &data)
385 {
386 return data.WriteUint32(static_cast<uint32_t>(input));
387 }
388
Unmarshalling(ClientState & output,MessageParcel & data)389 bool ITypesUtil::Unmarshalling(ClientState &output, MessageParcel &data)
390 {
391 uint32_t state = 0;
392 if (!data.ReadUint32(state)) {
393 IMSA_HILOGE("ClientState read failed.");
394 return false;
395 }
396 output = static_cast<ClientState>(state);
397 return true;
398 }
399
Marshalling(SwitchTrigger input,MessageParcel & data)400 bool ITypesUtil::Marshalling(SwitchTrigger input, MessageParcel &data)
401 {
402 return data.WriteUint32(static_cast<uint32_t>(input));
403 }
404
Unmarshalling(SwitchTrigger & output,MessageParcel & data)405 bool ITypesUtil::Unmarshalling(SwitchTrigger &output, MessageParcel &data)
406 {
407 uint32_t state = 0;
408 if (!data.ReadUint32(state)) {
409 IMSA_HILOGE("ClientState read failed.");
410 return false;
411 }
412 output = static_cast<SwitchTrigger>(state);
413 return true;
414 }
415
Marshalling(const PrivateDataValue & input,MessageParcel & data)416 bool ITypesUtil::Marshalling(const PrivateDataValue &input, MessageParcel &data)
417 {
418 size_t idx = input.index();
419 if (!data.WriteInt32(static_cast<int32_t>(idx))) {
420 IMSA_HILOGE("write index failed.");
421 return false;
422 }
423 if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
424 auto stringValue = std::get_if<std::string>(&input);
425 if (stringValue != nullptr) {
426 return data.WriteString(*stringValue);
427 }
428 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
429 auto boolValue = std::get_if<bool>(&input);
430 if (boolValue != nullptr) {
431 return data.WriteBool(*boolValue);
432 }
433 } else if (idx == static_cast<size_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
434 auto numberValue = std::get_if<int32_t>(&input);
435 if (numberValue != nullptr) {
436 return data.WriteInt32(*numberValue);
437 }
438 }
439 IMSA_HILOGE("write PrivateDataValue with wrong type.");
440 return false;
441 }
442
Unmarshalling(PrivateDataValue & output,MessageParcel & data)443 bool ITypesUtil::Unmarshalling(PrivateDataValue &output, MessageParcel &data)
444 {
445 int32_t valueType = data.ReadInt32();
446 bool res = false;
447 if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_STRING)) {
448 std::string strValue;
449 res = data.ReadString(strValue);
450 output.emplace<std::string>(strValue);
451 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) {
452 bool boolValue = false;
453 res = data.ReadBool(boolValue);
454 output.emplace<bool>(boolValue);
455 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) {
456 int32_t intValue = 0;
457 res = data.ReadInt32(intValue);
458 output.emplace<int32_t>(intValue);
459 }
460 if (!res) {
461 IMSA_HILOGE("read PrivateDataValue from message parcel failed.");
462 }
463 return res;
464 }
465
Marshalling(const Range & input,MessageParcel & data)466 bool ITypesUtil::Marshalling(const Range &input, MessageParcel &data)
467 {
468 if (!Marshal(data, input.start, input.end)) {
469 IMSA_HILOGE("failed to write Range into message parcel.");
470 return false;
471 }
472 return true;
473 }
474
Unmarshalling(Range & output,MessageParcel & data)475 bool ITypesUtil::Unmarshalling(Range &output, MessageParcel &data)
476 {
477 if (!Unmarshal(data, output.start, output.end)) {
478 IMSA_HILOGE("failed to read Range from message parcel.");
479 return false;
480 }
481 return true;
482 }
483 } // namespace MiscServices
484 } // namespace OHOS