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 "want_sender_info.h" 17 18 #include "hilog_wrapper.h" 19 #include "nlohmann/json.hpp" 20 #include "string_ex.h" 21 22 namespace OHOS { 23 namespace AAFwk { 24 25 constexpr int32_t READ_PARCEL_MAX_WANT_INFO_SIZE = 1000; 26 ReadFromParcel(Parcel & parcel)27bool WantSenderInfo::ReadFromParcel(Parcel &parcel) 28 { 29 type = parcel.ReadInt32(); 30 bundleName = Str16ToStr8(parcel.ReadString16()); 31 resultWho = Str16ToStr8(parcel.ReadString16()); 32 requestCode = parcel.ReadInt32(); 33 int32_t wantsInfoSize = parcel.ReadInt32(); 34 if (wantsInfoSize > READ_PARCEL_MAX_WANT_INFO_SIZE) { 35 HILOG_ERROR("ReadFromParcel wantsInfoSize Control"); 36 return false; 37 } 38 for (int32_t i = 0; i < wantsInfoSize; i++) { 39 std::unique_ptr<WantsInfo> wantsInfo(parcel.ReadParcelable<WantsInfo>()); 40 if (!wantsInfo) { 41 HILOG_ERROR("ReadParcelable<WantsInfo> failed"); 42 return false; 43 } 44 allWants.emplace_back(*wantsInfo); 45 } 46 flags = parcel.ReadInt32(); 47 userId = parcel.ReadInt32(); 48 return true; 49 } 50 Unmarshalling(Parcel & parcel)51WantSenderInfo *WantSenderInfo::Unmarshalling(Parcel &parcel) 52 { 53 WantSenderInfo *info = new (std::nothrow) WantSenderInfo(); 54 if (info == nullptr) { 55 return nullptr; 56 } 57 58 if (!info->ReadFromParcel(parcel)) { 59 delete info; 60 info = nullptr; 61 } 62 return info; 63 } 64 Marshalling(Parcel & parcel) const65bool WantSenderInfo::Marshalling(Parcel &parcel) const 66 { 67 parcel.WriteInt32(type); 68 parcel.WriteString16(Str8ToStr16(bundleName)); 69 parcel.WriteString16(Str8ToStr16(resultWho)); 70 parcel.WriteInt32(requestCode); 71 size_t wantsInfoSize = allWants.size(); 72 if (!parcel.WriteInt32(wantsInfoSize)) { 73 return false; 74 } 75 for (size_t i = 0; i < wantsInfoSize; i++) { 76 if (!parcel.WriteParcelable(&allWants[i])) { 77 return false; 78 } 79 } 80 parcel.WriteInt32(flags); 81 parcel.WriteInt32(userId); 82 return true; 83 } 84 } // namespace AAFwk 85 } // namespace OHOS