1 /*
2 * Copyright (c) 2025 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 "service_response_data.h"
17
18 namespace OHOS {
19 namespace MiscServices {
20 const std::unordered_map<int32_t, UnmarshalFunc> ServiceResponseDataInner::UNMARSHAL_FUNCTION_MAP = {
__anon245d2c5e0102() 21 { static_cast<int32_t>(TYPE_MONOSTATE), [](Parcel &in, ServiceResponseData &out) { out = std::monostate{}; } },
__anon245d2c5e0202() 22 { static_cast<int32_t>(TYPE_BOOL), [](Parcel &in, ServiceResponseData &out) { out = in.ReadBool(); } },
__anon245d2c5e0302() 23 { static_cast<int32_t>(TYPE_INT32), [](Parcel &in, ServiceResponseData &out) { out = in.ReadInt32(); } },
__anon245d2c5e0402() 24 { static_cast<int32_t>(TYPE_UINT32), [](Parcel &in, ServiceResponseData &out) { out = in.ReadUint32(); } },
__anon245d2c5e0502() 25 { static_cast<int32_t>(TYPE_INT64), [](Parcel &in, ServiceResponseData &out) { out = in.ReadInt64(); } },
__anon245d2c5e0602() 26 { static_cast<int32_t>(TYPE_UINT64), [](Parcel &in, ServiceResponseData &out) { out = in.ReadUint64(); } },
27 { static_cast<int32_t>(TYPE_REMOTE_OBJECT),
__anon245d2c5e0702() 28 [](Parcel &in, ServiceResponseData &out) { out = static_cast<MessageParcel *>(&in)->ReadRemoteObject(); } },
29 { static_cast<int32_t>(TYPE_PROPERTY),
__anon245d2c5e0802() 30 [](Parcel &in, ServiceResponseData &out) {
31 Property value;
32 value.ReadFromParcel(in);
33 out = value;
34 } },
35 { static_cast<int32_t>(TYPE_PROPERTIES),
__anon245d2c5e0902() 36 [](Parcel &in, ServiceResponseData &out) {
37 std::vector<Property> value;
38 ResponseDataUtil::Unmarshall(in, value);
39 out = value;
40 } },
41 { static_cast<int32_t>(TYPE_SUB_PROPERTY),
__anon245d2c5e0a02() 42 [](Parcel &in, ServiceResponseData &out) {
43 SubProperty value;
44 value.ReadFromParcel(in);
45 out = value;
46 } },
47 { static_cast<int32_t>(TYPE_SUB_PROPERTIES),
__anon245d2c5e0b02() 48 [](Parcel &in, ServiceResponseData &out) {
49 std::vector<SubProperty> value;
50 ResponseDataUtil::Unmarshall(in, value);
51 out = value;
52 } },
53 { static_cast<int32_t>(TYPE_START_INPUT_RESPONSE),
__anon245d2c5e0c02() 54 [](Parcel &in, ServiceResponseData &out) {
55 StartInputResponse value;
56 value.ReadFromParcel(in);
57 out = value;
58 } },
59 { static_cast<int32_t>(TYPE_INPUT_START_INFO),
__anon245d2c5e0d02() 60 [](Parcel &in, ServiceResponseData &out) {
61 InputStartInfo value;
62 value.ReadFromParcel(in);
63 out = value;
64 } },
65 };
ReadFromParcel(Parcel & in)66 bool ServiceResponseDataInner::ReadFromParcel(Parcel &in)
67 {
68 int32_t valueType = 0;
69 if (!in.ReadInt32(valueType)) {
70 return false;
71 }
72 if (valueType < static_cast<int32_t>(ServiceDataType::TYPE_MONOSTATE) ||
73 valueType >= static_cast<int32_t>(ServiceDataType::TYPE_END)) {
74 IMSA_HILOGE("invalid value type");
75 return false;
76 }
77 auto iter = UNMARSHAL_FUNCTION_MAP.find(valueType);
78 if (iter == UNMARSHAL_FUNCTION_MAP.end()) {
79 return false;
80 }
81 auto handler = iter->second;
82 if (handler == nullptr) {
83 return false;
84 }
85 handler(in, data);
86 return true;
87 }
88
Marshalling(Parcel & out) const89 bool ServiceResponseDataInner::Marshalling(Parcel &out) const
90 {
91 int32_t valueType = static_cast<int32_t>(data.index());
92 if (valueType < static_cast<int32_t>(ServiceDataType::TYPE_MONOSTATE) ||
93 valueType >= static_cast<int32_t>(ServiceDataType::TYPE_END)) {
94 IMSA_HILOGE("invalid value type");
95 return false;
96 }
97 if (!out.WriteInt32(valueType)) {
98 IMSA_HILOGE("failed to write valueType");
99 return false;
100 }
101 if (valueType == static_cast<int32_t>(ServiceDataType::TYPE_MONOSTATE)) {
102 return true;
103 }
104 ServiceResponseWriter writer{ out };
105 std::visit(writer, data);
106 if (!writer.result) {
107 IMSA_HILOGE("failed to write response data");
108 return false;
109 }
110 return true;
111 }
112
Unmarshalling(Parcel & in)113 ServiceResponseDataInner *ServiceResponseDataInner::Unmarshalling(Parcel &in)
114 {
115 auto data = new (std::nothrow) ServiceResponseDataInner();
116 if (data == nullptr) {
117 return nullptr;
118 }
119 if (!data->ReadFromParcel(in)) {
120 delete data;
121 data = nullptr;
122 }
123 return data;
124 }
125
Set(sptr<IRemoteObject> imeAgent,int64_t imePid,const std::string & imeBundleName)126 void StartInputResponse::Set(sptr<IRemoteObject> imeAgent, int64_t imePid, const std::string &imeBundleName)
127 {
128 agent = imeAgent;
129 pid = imePid;
130 bundleName = imeBundleName;
131 }
132
ReadFromParcel(Parcel & in)133 bool StartInputResponse::ReadFromParcel(Parcel &in)
134 {
135 agent = (static_cast<MessageParcel *>(&in))->ReadRemoteObject();
136 if (agent == nullptr) {
137 return false;
138 }
139 if (!in.ReadInt64(pid)) {
140 return false;
141 }
142 if (!in.ReadString(bundleName)) {
143 return false;
144 }
145 return true;
146 }
147
Marshalling(Parcel & out) const148 bool StartInputResponse::Marshalling(Parcel &out) const
149 {
150 if (!(static_cast<MessageParcel *>(&out))->WriteRemoteObject(agent)) {
151 return false;
152 }
153 if (!out.WriteInt64(pid)) {
154 return false;
155 }
156 if (!out.WriteString(bundleName)) {
157 return false;
158 }
159 return true;
160 }
161
Unmarshalling(Parcel & in)162 StartInputResponse *StartInputResponse::Unmarshalling(Parcel &in)
163 {
164 auto data = new (std::nothrow) StartInputResponse();
165 if (data == nullptr) {
166 return nullptr;
167 }
168 if (!data->ReadFromParcel(in)) {
169 delete data;
170 return nullptr;
171 }
172 return data;
173 }
174
Set(bool inputStart,uint32_t id,int32_t reason)175 void InputStartInfo::Set(bool inputStart, uint32_t id, int32_t reason)
176 {
177 isInputStart = inputStart;
178 callingWindowId = id;
179 requestKeyboardReason = reason;
180 }
181
ReadFromParcel(Parcel & in)182 bool InputStartInfo::ReadFromParcel(Parcel &in)
183 {
184 if (!in.ReadBool(isInputStart)) {
185 return false;
186 }
187 if (!in.ReadUint32(callingWindowId)) {
188 return false;
189 }
190 if (!in.ReadInt32(requestKeyboardReason)) {
191 return false;
192 }
193 return true;
194 }
195
Marshalling(Parcel & out) const196 bool InputStartInfo::Marshalling(Parcel &out) const
197 {
198 if (!out.WriteBool(isInputStart)) {
199 return false;
200 }
201 if (!out.WriteUint32(callingWindowId)) {
202 return false;
203 }
204 if (!out.WriteInt32(requestKeyboardReason)) {
205 return false;
206 }
207 return true;
208 }
209
Unmarshalling(Parcel & in)210 InputStartInfo *InputStartInfo::Unmarshalling(Parcel &in)
211 {
212 auto data = new (std::nothrow) InputStartInfo();
213 if (data == nullptr) {
214 return nullptr;
215 }
216 if (!data->ReadFromParcel(in)) {
217 delete data;
218 return nullptr;
219 }
220 return data;
221 }
222 } // namespace MiscServices
223 } // namespace OHOS