1 /* 2 * Copyright (C) 2021 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 <cinttypes> 17 #include "input_method_utils.h" 18 #include "input_method_tools.h" 19 namespace OHOS { 20 namespace MiscServices { 21 ReadFromParcel(Parcel & in)22bool PanelStatusInfoInner::ReadFromParcel(Parcel &in) 23 { 24 std::unique_ptr<PanelInfo> panelDataInfo(in.ReadParcelable<PanelInfo>()); 25 if (panelDataInfo == nullptr) { 26 return false; 27 } 28 panelInfo = *panelDataInfo; 29 30 visible = in.ReadBool(); 31 int32_t triggerData = in.ReadInt32(); 32 trigger = static_cast<Trigger>(triggerData); 33 sessionId = in.ReadUint32(); 34 return true; 35 } 36 ReadFromParcel(Parcel & in)37bool TextSelectionInner::ReadFromParcel(Parcel &in) 38 { 39 oldBegin = in.ReadInt32(); 40 oldEnd = in.ReadInt32(); 41 newBegin = in.ReadInt32(); 42 newEnd = in.ReadInt32(); 43 return true; 44 } 45 ReadFromParcel(Parcel & in)46bool Value::ReadFromParcel(Parcel &in) 47 { 48 uint32_t size = 0; 49 size = in.ReadUint32(); 50 51 valueMap.clear(); 52 if (size == 0) { 53 IMSA_HILOGD("size is zero!"); 54 return true; 55 } 56 57 if (size > MAX_VALUE_MAP_COUNT) { 58 IMSA_HILOGD("size is invalid!"); 59 return false; 60 } 61 62 for (uint32_t index = 0; index < size; index++) { 63 std::string key = in.ReadString(); 64 int32_t valueType = in.ReadInt32(); 65 if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_STRING)) { 66 std::string strValue = in.ReadString(); 67 valueMap.insert(std::make_pair(key, strValue)); 68 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) { 69 bool boolValue = false; 70 boolValue = in.ReadBool(); 71 valueMap.insert(std::make_pair(key, boolValue)); 72 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) { 73 int32_t intValue = 0; 74 intValue = in.ReadInt32(); 75 valueMap.insert(std::make_pair(key, intValue)); 76 } 77 } 78 return true; 79 } 80 ReadFromParcel(Parcel & in)81bool TextTotalConfigInner::ReadFromParcel(Parcel &in) 82 { 83 std::unique_ptr<InputAttributeInner> inputAttributeInfo(in.ReadParcelable<InputAttributeInner>()); 84 if (inputAttributeInfo == nullptr) { 85 return false; 86 } 87 inputAttribute = *inputAttributeInfo; 88 89 std::unique_ptr<CursorInfoInner> cInfo(in.ReadParcelable<CursorInfoInner>()); 90 if (cInfo == nullptr) { 91 return false; 92 } 93 cursorInfo = *cInfo; 94 95 std::unique_ptr<TextSelectionInner> textSelectionInfo(in.ReadParcelable<TextSelectionInner>()); 96 if (textSelectionInfo == nullptr) { 97 return false; 98 } 99 textSelection = *textSelectionInfo; 100 101 windowId = in.ReadUint32(); 102 positionY = in.ReadDouble(); 103 height = in.ReadDouble(); 104 105 std::unique_ptr<Value> commandValueInfo(in.ReadParcelable<Value>()); 106 if (commandValueInfo == nullptr) { 107 return false; 108 } 109 commandValue = *commandValueInfo; 110 int32_t requestKeyboardReasonData = in.ReadInt32(); 111 requestKeyboardReason = static_cast<RequestKeyboardReason>(requestKeyboardReasonData); 112 abilityToken = (static_cast<MessageParcel*>(&in))->ReadRemoteObject(); 113 isSimpleKeyboardEnabled = in.ReadBool(); 114 return true; 115 } 116 ReadFromParcel(Parcel & in)117bool ArrayBuffer::ReadFromParcel(Parcel &in) 118 { 119 uint64_t jsArgcData = in.ReadUint64(); 120 jsArgc = static_cast<size_t>(jsArgcData); 121 msgId = in.ReadString(); 122 123 if (!in.ReadUInt8Vector(&msgParam)) { 124 return false; 125 } 126 return true; 127 } 128 Unmarshalling(Parcel & in)129PanelStatusInfoInner *PanelStatusInfoInner::Unmarshalling(Parcel &in) 130 { 131 PanelStatusInfoInner *data = new (std::nothrow) PanelStatusInfoInner(); 132 if (data && !data->ReadFromParcel(in)) { 133 delete data; 134 data = nullptr; 135 } 136 return data; 137 } 138 Unmarshalling(Parcel & in)139TextSelectionInner *TextSelectionInner::Unmarshalling(Parcel &in) 140 { 141 TextSelectionInner *data = new (std::nothrow) TextSelectionInner(); 142 if (data && !data->ReadFromParcel(in)) { 143 delete data; 144 data = nullptr; 145 } 146 return data; 147 } 148 Unmarshalling(Parcel & in)149Value *Value::Unmarshalling(Parcel &in) 150 { 151 Value *data = new (std::nothrow) Value(); 152 if (data && !data->ReadFromParcel(in)) { 153 delete data; 154 data = nullptr; 155 } 156 return data; 157 } 158 Unmarshalling(Parcel & in)159KeyEventValue *KeyEventValue::Unmarshalling(Parcel &in) 160 { 161 KeyEventValue *data = new (std::nothrow) KeyEventValue; 162 if (data == nullptr) { 163 return data; 164 } 165 data->event = MMI::KeyEvent::Create(); 166 if (data->event == nullptr) { 167 delete data; 168 return nullptr; 169 } 170 if (!data->event->ReadFromParcel(in)) { 171 delete data; 172 data = nullptr; 173 } 174 return data; 175 } 176 Unmarshalling(Parcel & in)177TextTotalConfigInner *TextTotalConfigInner::Unmarshalling(Parcel &in) 178 { 179 TextTotalConfigInner *data = new (std::nothrow) TextTotalConfigInner(); 180 if (data && !data->ReadFromParcel(in)) { 181 delete data; 182 data = nullptr; 183 } 184 return data; 185 } 186 Unmarshalling(Parcel & in)187ArrayBuffer *ArrayBuffer::Unmarshalling(Parcel &in) 188 { 189 ArrayBuffer *data = new (std::nothrow) ArrayBuffer(); 190 if (data && !data->ReadFromParcel(in)) { 191 delete data; 192 data = nullptr; 193 } 194 return data; 195 } 196 Marshalling(Parcel & out) const197bool PanelStatusInfoInner::Marshalling(Parcel &out) const 198 { 199 if (!out.WriteParcelable(&panelInfo)) { 200 return false; 201 } 202 203 if (!out.WriteBool(visible)) { 204 return false; 205 } 206 207 if (!out.WriteInt32(static_cast<int32_t>(trigger))) { 208 return false; 209 } 210 211 if (!out.WriteUint32(sessionId)) { 212 return false; 213 } 214 return true; 215 } 216 Marshalling(Parcel & out) const217bool TextSelectionInner::Marshalling(Parcel &out) const 218 { 219 if (!out.WriteInt32(oldBegin)) { 220 return false; 221 } 222 223 if (!out.WriteInt32(oldEnd)) { 224 return false; 225 } 226 227 if (!out.WriteInt32(newBegin)) { 228 return false; 229 } 230 231 if (!out.WriteInt32(newEnd)) { 232 return false; 233 } 234 return true; 235 } 236 Marshalling(Parcel & out) const237bool Value::Marshalling(Parcel &out) const 238 { 239 if (!out.WriteUint32(valueMap.size())) { 240 return false; 241 } 242 if (valueMap.size() == 0) { 243 IMSA_HILOGE("valueMap size is zero!"); 244 return true; 245 } 246 for (auto& it : valueMap) { 247 std::string key = it.first; 248 if (!out.WriteString(key)) { 249 return false; 250 } 251 auto value = it.second; 252 bool ret = false; 253 int32_t valueType = static_cast<int32_t>(value.index()); 254 if (!out.WriteInt32(valueType)) { 255 return false; 256 } 257 if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_STRING)) { 258 auto stringValue = std::get_if<std::string>(&value); 259 if (stringValue != nullptr) { 260 ret = out.WriteString(*stringValue); 261 } 262 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_BOOL)) { 263 auto boolValue = std::get_if<bool>(&value); 264 if (boolValue != nullptr) { 265 ret = out.WriteBool(*boolValue); 266 } 267 } else if (valueType == static_cast<int32_t>(PrivateDataValueType::VALUE_TYPE_NUMBER)) { 268 auto numberValue = std::get_if<int32_t>(&value); 269 if (numberValue != nullptr) { 270 ret = out.WriteInt32(*numberValue); 271 } 272 } 273 if (ret == false) { 274 return ret; 275 } 276 } 277 return true; 278 } 279 Marshalling(Parcel & out) const280bool KeyEventValue::Marshalling(Parcel &out) const 281 { 282 return event->WriteToParcel(out); 283 } 284 Marshalling(Parcel & out) const285bool TextTotalConfigInner::Marshalling(Parcel &out) const 286 { 287 if (!out.WriteParcelable(&inputAttribute)) { 288 return false; 289 } 290 291 if (!out.WriteParcelable(&cursorInfo)) { 292 return false; 293 } 294 295 if (!out.WriteParcelable(&textSelection)) { 296 return false; 297 } 298 299 if (!out.WriteUint32(windowId)) { 300 return false; 301 } 302 303 if (!out.WriteDouble(positionY)) { 304 return false; 305 } 306 307 if (!out.WriteDouble(height)) { 308 return false; 309 } 310 311 if (!out.WriteParcelable(&commandValue)) { 312 return false; 313 } 314 315 if (!out.WriteInt32(static_cast<int32_t>(requestKeyboardReason))) { 316 return false; 317 } 318 319 if (abilityToken != nullptr && !out.WriteRemoteObject(abilityToken)) { 320 return false; 321 } 322 if (!out.WriteBool(isSimpleKeyboardEnabled)) { 323 return false; 324 } 325 return true; 326 } 327 Marshalling(Parcel & out) const328bool ArrayBuffer::Marshalling(Parcel &out) const 329 { 330 if (!out.WriteUint64(static_cast<uint64_t>(jsArgc))) { 331 return false; 332 } 333 if (!out.WriteString(msgId)) { 334 return false; 335 } 336 if (!out.WriteUInt8Vector(msgParam)) { 337 return false; 338 } 339 return true; 340 } 341 ReadFromParcel(Parcel & in)342bool ResponseDataInner::ReadFromParcel(Parcel &in) 343 { 344 uint64_t index = in.ReadUint64(); 345 switch (index) { 346 case static_cast<uint64_t>(ResponseDataType::NONE_TYPE): { 347 rspData = std::monostate{}; 348 break; 349 } 350 case static_cast<uint64_t>(ResponseDataType::STRING_TYPE): { 351 rspData = in.ReadString(); 352 break; 353 } 354 case static_cast<uint64_t>(ResponseDataType::INT32_TYPE): { 355 rspData = in.ReadInt32(); 356 break; 357 } 358 default: { 359 IMSA_HILOGE("bad parameter index: %{public}" PRIu64 "", index); 360 return false; 361 } 362 } 363 return true; 364 } 365 Marshalling(Parcel & out) const366bool ResponseDataInner::Marshalling(Parcel &out) const 367 { 368 uint64_t index = static_cast<uint64_t>(rspData.index()); 369 if (!out.WriteUint64(index)) { 370 return false; 371 } 372 switch (index) { 373 case static_cast<uint64_t>(ResponseDataType::NONE_TYPE): { 374 return true; 375 } 376 case static_cast<uint64_t>(ResponseDataType::STRING_TYPE): { 377 if (!std::holds_alternative<std::string>(rspData)) { 378 return false; 379 } 380 return out.WriteString(std::get<std::string>(rspData)); 381 } 382 case static_cast<uint64_t>(ResponseDataType::INT32_TYPE): { 383 if (!std::holds_alternative<int32_t>(rspData)) { 384 return false; 385 } 386 return out.WriteInt32(std::get<int32_t>(rspData)); 387 } 388 default: { 389 return false; 390 } 391 } 392 } 393 } // namespace MiscServices 394 } // namespace OHOS 395