• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "input_client_proxy.h"
20 #include "input_data_channel_proxy.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
Marshal(MessageParcel & data)25 bool ITypesUtil::Marshal(MessageParcel &data)
26 {
27     return true;
28 }
29 
Unmarshal(MessageParcel & data)30 bool ITypesUtil::Unmarshal(MessageParcel &data)
31 {
32     return true;
33 }
34 
Marshalling(bool input,MessageParcel & data)35 bool ITypesUtil::Marshalling(bool input, MessageParcel &data)
36 {
37     return data.WriteBool(input);
38 }
39 
Unmarshalling(bool & output,MessageParcel & data)40 bool ITypesUtil::Unmarshalling(bool &output, MessageParcel &data)
41 {
42     return data.ReadBool(output);
43 }
44 
Marshalling(uint32_t input,MessageParcel & data)45 bool ITypesUtil::Marshalling(uint32_t input, MessageParcel &data)
46 {
47     return data.WriteUint32(input);
48 }
49 
Unmarshalling(uint32_t & output,MessageParcel & data)50 bool ITypesUtil::Unmarshalling(uint32_t &output, MessageParcel &data)
51 {
52     return data.ReadUint32(output);
53 }
54 
Marshalling(int32_t input,MessageParcel & data)55 bool ITypesUtil::Marshalling(int32_t input, MessageParcel &data)
56 {
57     return data.WriteInt32(input);
58 }
59 
Unmarshalling(int32_t & output,MessageParcel & data)60 bool ITypesUtil::Unmarshalling(int32_t &output, MessageParcel &data)
61 {
62     return data.ReadInt32(output);
63 }
64 
Marshalling(uint64_t input,MessageParcel & data)65 bool ITypesUtil::Marshalling(uint64_t input, MessageParcel &data)
66 {
67     return data.WriteUint64(input);
68 }
69 
Unmarshalling(uint64_t & output,MessageParcel & data)70 bool ITypesUtil::Unmarshalling(uint64_t &output, MessageParcel &data)
71 {
72     return data.ReadUint64(output);
73 }
74 
Marshalling(double input,MessageParcel & data)75 bool ITypesUtil::Marshalling(double input, MessageParcel &data)
76 {
77     return data.WriteDouble(input);
78 }
79 
Unmarshalling(double & output,MessageParcel & data)80 bool ITypesUtil::Unmarshalling(double &output, MessageParcel &data)
81 {
82     return data.ReadDouble(output);
83 }
84 
Marshalling(const std::string & input,MessageParcel & data)85 bool ITypesUtil::Marshalling(const std::string &input, MessageParcel &data)
86 {
87     return data.WriteString(input);
88 }
89 
Unmarshalling(std::string & output,MessageParcel & data)90 bool ITypesUtil::Unmarshalling(std::string &output, MessageParcel &data)
91 {
92     return data.ReadString(output);
93 }
94 
Marshalling(const std::u16string & input,MessageParcel & data)95 bool ITypesUtil::Marshalling(const std::u16string &input, MessageParcel &data)
96 {
97     return data.WriteString16(input);
98 }
99 
Unmarshalling(std::u16string & output,MessageParcel & data)100 bool ITypesUtil::Unmarshalling(std::u16string &output, MessageParcel &data)
101 {
102     return data.ReadString16(output);
103 }
104 
Marshalling(const std::vector<uint8_t> & input,MessageParcel & data)105 bool ITypesUtil::Marshalling(const std::vector<uint8_t> &input, MessageParcel &data)
106 {
107     return data.WriteUInt8Vector(input);
108 }
109 
Unmarshalling(std::vector<uint8_t> & output,MessageParcel & data)110 bool ITypesUtil::Unmarshalling(std::vector<uint8_t> &output, MessageParcel &data)
111 {
112     return data.ReadUInt8Vector(&output);
113 }
114 
Marshalling(const sptr<IRemoteObject> & input,MessageParcel & data)115 bool ITypesUtil::Marshalling(const sptr<IRemoteObject> &input, MessageParcel &data)
116 {
117     return data.WriteRemoteObject(input);
118 }
119 
Unmarshalling(sptr<IRemoteObject> & output,MessageParcel & data)120 bool ITypesUtil::Unmarshalling(sptr<IRemoteObject> &output, MessageParcel &data)
121 {
122     output = data.ReadRemoteObject();
123     return true;
124 }
Marshalling(const Property & input,MessageParcel & data)125 bool ITypesUtil::Marshalling(const Property &input, MessageParcel &data)
126 {
127     if (!Marshal(data, input.name, input.id, input.label, input.labelId, input.icon, input.iconId)) {
128         IMSA_HILOGE("ITypesUtil::write Property to message parcel failed");
129         return false;
130     }
131     return true;
132 }
133 
Unmarshalling(Property & output,MessageParcel & data)134 bool ITypesUtil::Unmarshalling(Property &output, MessageParcel &data)
135 {
136     if (!Unmarshal(data, output.name, output.id, output.label, output.labelId, output.icon, output.iconId)) {
137         IMSA_HILOGE("ITypesUtil::read Property from message parcel failed");
138         return false;
139     }
140     return true;
141 }
142 
Marshalling(const SubProperty & input,MessageParcel & data)143 bool ITypesUtil::Marshalling(const SubProperty &input, MessageParcel &data)
144 {
145     if (!Marshal(data, input.label, input.labelId, input.name, input.id, input.mode, input.locale, input.language,
146             input.icon, input.iconId)) {
147         IMSA_HILOGE("ITypesUtil::write SubProperty to message parcel failed");
148         return false;
149     }
150     return true;
151 }
152 
Unmarshalling(SubProperty & output,MessageParcel & data)153 bool ITypesUtil::Unmarshalling(SubProperty &output, MessageParcel &data)
154 {
155     if (!Unmarshal(data, output.label, output.labelId, output.name, output.id, output.mode, output.locale,
156         output.language, output.icon, output.iconId)) {
157         IMSA_HILOGE("ITypesUtil::read SubProperty from message parcel failed");
158         return false;
159     }
160     return true;
161 }
162 
Marshalling(const InputAttribute & input,MessageParcel & data)163 bool ITypesUtil::Marshalling(const InputAttribute &input, MessageParcel &data)
164 {
165     if (!Marshal(data, input.inputPattern, input.enterKeyType, input.inputOption)) {
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)) {
175         IMSA_HILOGE("read InputAttribute from message parcel failed");
176         return false;
177     }
178     return true;
179 }
180 
Marshalling(const TextTotalConfig & input,MessageParcel & data)181 bool ITypesUtil::Marshalling(const TextTotalConfig &input, MessageParcel &data)
182 {
183     if (!Marshal(data, input.inputAttribute.inputPattern, input.inputAttribute.enterKeyType,
184         input.inputAttribute.inputOption)) {
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     return true;
210 }
211 
Unmarshalling(TextTotalConfig & output,MessageParcel & data)212 bool ITypesUtil::Unmarshalling(TextTotalConfig &output, MessageParcel &data)
213 {
214     if (!Unmarshalling(output.inputAttribute, data)) {
215         IMSA_HILOGE("read InputAttribute from message parcel failed");
216         return false;
217     }
218     if (!Unmarshal(data, output.cursorInfo.left, output.cursorInfo.top,
219         output.cursorInfo.height, output.cursorInfo.width)) {
220         IMSA_HILOGE("read CursorInfo from message parcel failed");
221         return false;
222     }
223     if (!Unmarshal(data, output.textSelection.oldBegin, output.textSelection.oldEnd,
224         output.textSelection.newBegin, output.textSelection.newEnd)) {
225         IMSA_HILOGE("read TextSelection from message parcel failed");
226         return false;
227     }
228     if (!Unmarshal(data, output.windowId)) {
229         IMSA_HILOGE("read windowId from message parcel failed");
230         return false;
231     }
232     if (!Unmarshal(data, output.positionY)) {
233         IMSA_HILOGE("read positionY from message parcel failed");
234         return false;
235     }
236     if (!Unmarshal(data, output.height)) {
237         IMSA_HILOGE("read height from message parcel failed");
238         return false;
239     }
240     return true;
241 }
242 
Marshalling(const InputClientInfo & input,MessageParcel & data)243 bool ITypesUtil::Marshalling(const InputClientInfo &input, MessageParcel &data)
244 {
245     if (!Marshal(data, input.pid, input.uid, input.userID, input.isShowKeyboard, input.eventFlag, input.config,
246         input.state, input.isNotifyInputStart, input.client->AsObject(), input.channel->AsObject())) {
247         IMSA_HILOGE("write InputClientInfo to message parcel failed");
248         return false;
249     }
250     return true;
251 }
252 
Unmarshalling(InputClientInfo & output,MessageParcel & data)253 bool ITypesUtil::Unmarshalling(InputClientInfo &output, MessageParcel &data)
254 {
255     if (!Unmarshal(data, output.pid, output.uid, output.userID, output.isShowKeyboard, output.eventFlag, output.config,
256         output.state, output.isNotifyInputStart)) {
257         IMSA_HILOGE("read InputClientInfo from message parcel failed");
258         return false;
259     }
260     auto client = data.ReadRemoteObject();
261     auto channel = data.ReadRemoteObject();
262     if (client == nullptr || channel == nullptr) {
263         IMSA_HILOGE("read remote object failed");
264         return false;
265     }
266     output.client = iface_cast<IInputClient>(client);
267     output.channel = iface_cast<IInputDataChannel>(channel);
268     return true;
269 }
270 
Marshalling(const InputWindowInfo & input,MessageParcel & data)271 bool ITypesUtil::Marshalling(const InputWindowInfo &input, MessageParcel &data)
272 {
273     if (!Marshal(data, input.name, input.top, input.left, input.width, input.height)) {
274         IMSA_HILOGE("write InputWindowInfo to message parcel failed");
275         return false;
276     }
277     return true;
278 }
Unmarshalling(InputWindowInfo & output,MessageParcel & data)279 bool ITypesUtil::Unmarshalling(InputWindowInfo &output, MessageParcel &data)
280 {
281     if (!Unmarshal(data, output.name, output.top, output.left, output.width, output.height)) {
282         IMSA_HILOGE("read InputWindowInfo from message parcel failed");
283         return false;
284     }
285     return true;
286 }
287 
Marshalling(const PanelStatusInfo & input,MessageParcel & data)288 bool ITypesUtil::Marshalling(const PanelStatusInfo &input, MessageParcel &data)
289 {
290     return data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelType))
291            && data.WriteInt32(static_cast<int32_t>(input.panelInfo.panelFlag)) && data.WriteBool(input.visible)
292            && data.WriteInt32(static_cast<int32_t>(input.trigger));
293 }
294 
Unmarshalling(PanelStatusInfo & output,MessageParcel & data)295 bool ITypesUtil::Unmarshalling(PanelStatusInfo &output, MessageParcel &data)
296 {
297     int32_t type = -1;
298     int32_t flag = -1;
299     bool visible = false;
300     int32_t trigger = -1;
301     if (!data.ReadInt32(type) || !data.ReadInt32(flag) || !data.ReadBool(visible) || !data.ReadInt32(trigger)) {
302         return false;
303     }
304     output = { { static_cast<PanelType>(type), static_cast<PanelFlag>(flag) }, visible, static_cast<Trigger>(trigger) };
305     return true;
306 }
307 
Marshalling(EventType input,MessageParcel & data)308 bool ITypesUtil::Marshalling(EventType input, MessageParcel &data)
309 {
310     return data.WriteUint32(static_cast<uint32_t>(input));
311 }
312 
Unmarshalling(EventType & output,MessageParcel & data)313 bool ITypesUtil::Unmarshalling(EventType &output, MessageParcel &data)
314 {
315     auto ret = data.ReadUint32();
316     output = static_cast<EventType>(ret);
317     return true;
318 }
319 
Marshalling(const OHOS::AppExecFwk::ElementName & input,MessageParcel & data)320 bool ITypesUtil::Marshalling(const OHOS::AppExecFwk::ElementName &input, MessageParcel &data)
321 {
322     return data.WriteString(input.GetBundleName().c_str()) && data.WriteString(input.GetModuleName().c_str()) &&
323            data.WriteString(input.GetAbilityName().c_str());
324 }
325 
Unmarshalling(OHOS::AppExecFwk::ElementName & output,MessageParcel & data)326 bool ITypesUtil::Unmarshalling(OHOS::AppExecFwk::ElementName &output, MessageParcel &data)
327 {
328     std::string bundleName;
329     std::string moduleName;
330     std::string abilityName;
331     if (data.ReadString(bundleName) && data.ReadString(moduleName) && data.ReadString(abilityName)) {
332         output.SetBundleName(bundleName);
333         output.SetModuleName(moduleName);
334         output.SetAbilityName(abilityName);
335         return true;
336     }
337     IMSA_HILOGE("read ElementName from message parcel failed");
338     return false;
339 }
340 
Marshalling(InputType input,MessageParcel & data)341 bool ITypesUtil::Marshalling(InputType input, MessageParcel &data)
342 {
343     return data.WriteInt32(static_cast<int32_t>(input));
344 }
345 
Unmarshalling(InputType & output,MessageParcel & data)346 bool ITypesUtil::Unmarshalling(InputType &output, MessageParcel &data)
347 {
348     int32_t ret = 0;
349     if (!data.ReadInt32(ret)) {
350         return false;
351     }
352     output = static_cast<InputType>(ret);
353     return true;
354 }
355 
Marshalling(const PanelInfo & input,MessageParcel & data)356 bool ITypesUtil::Marshalling(const PanelInfo &input, MessageParcel &data)
357 {
358     return data.WriteInt32(static_cast<int32_t>(input.panelType))
359            && data.WriteInt32(static_cast<int32_t>(input.panelFlag));
360 }
361 
Unmarshalling(PanelInfo & output,MessageParcel & data)362 bool ITypesUtil::Unmarshalling(PanelInfo &output, MessageParcel &data)
363 {
364     int32_t panelType = 0;
365     int32_t panelFlag = 0;
366     if (!data.ReadInt32(panelType) || !data.ReadInt32(panelFlag)) {
367         return false;
368     }
369     output.panelFlag = static_cast<PanelFlag>(panelFlag);
370     output.panelType = static_cast<PanelType>(panelType);
371     return true;
372 }
373 
Marshalling(ClientState input,MessageParcel & data)374 bool ITypesUtil::Marshalling(ClientState input, MessageParcel &data)
375 {
376     return data.WriteUint32(static_cast<uint32_t>(input));
377 }
378 
Unmarshalling(ClientState & output,MessageParcel & data)379 bool ITypesUtil::Unmarshalling(ClientState &output, MessageParcel &data)
380 {
381     uint32_t state = 0;
382     if (!data.ReadUint32(state)) {
383         IMSA_HILOGE("ClientState read failed");
384         return false;
385     }
386     output = static_cast<ClientState>(state);
387     return true;
388 }
389 
Marshalling(SwitchTrigger input,MessageParcel & data)390 bool ITypesUtil::Marshalling(SwitchTrigger input, MessageParcel &data)
391 {
392     return data.WriteUint32(static_cast<uint32_t>(input));
393 }
394 
Unmarshalling(SwitchTrigger & output,MessageParcel & data)395 bool ITypesUtil::Unmarshalling(SwitchTrigger &output, MessageParcel &data)
396 {
397     uint32_t state = 0;
398     if (!data.ReadUint32(state)) {
399         IMSA_HILOGE("ClientState read failed");
400         return false;
401     }
402     output = static_cast<SwitchTrigger>(state);
403     return true;
404 }
405 } // namespace MiscServices
406 } // namespace OHOS