1 /* 2 * Copyright (c) 2021-2023 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 "sender_info.h" 17 18 #include "hilog_wrapper.h" 19 #include "nlohmann/json.hpp" 20 #include "string_ex.h" 21 22 #include "ipc_types.h" 23 #include "iremote_object.h" 24 25 namespace OHOS { 26 namespace AAFwk { ReadFromParcel(Parcel & parcel)27bool SenderInfo::ReadFromParcel(Parcel &parcel) 28 { 29 HILOG_INFO("call"); 30 31 code = parcel.ReadInt32(); 32 std::unique_ptr<Want> wantResquest(parcel.ReadParcelable<Want>()); 33 if (wantResquest == nullptr) { 34 HILOG_ERROR("wantResquest is nullptr."); 35 return false; 36 } 37 want = *wantResquest; 38 resolvedType = Str16ToStr8(parcel.ReadString16()); 39 40 if (parcel.ReadBool()) { 41 sptr<IRemoteObject> finishedReceiverResquest = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject(); 42 if (finishedReceiverResquest == nullptr) { 43 HILOG_ERROR("remote object is nullptr."); 44 return false; 45 } 46 finishedReceiver = iface_cast<IWantReceiver>(finishedReceiverResquest); 47 if (!finishedReceiver) { 48 HILOG_ERROR("receiver is nullptr."); 49 return false; 50 } 51 } 52 requiredPermission = Str16ToStr8(parcel.ReadString16()); 53 return true; 54 } 55 Unmarshalling(Parcel & parcel)56SenderInfo *SenderInfo::Unmarshalling(Parcel &parcel) 57 { 58 HILOG_INFO("call"); 59 60 SenderInfo *info = new (std::nothrow) SenderInfo(); 61 if (info == nullptr) { 62 HILOG_ERROR("senderInfo is nullptr."); 63 return nullptr; 64 } 65 66 if (!info->ReadFromParcel(parcel)) { 67 HILOG_ERROR("ReadFromParcel failed."); 68 delete info; 69 info = nullptr; 70 } 71 return info; 72 } 73 Marshalling(Parcel & parcel) const74bool SenderInfo::Marshalling(Parcel &parcel) const 75 { 76 HILOG_INFO("call"); 77 78 if (!parcel.WriteInt32(code)) { 79 HILOG_ERROR("Failed to write code"); 80 return false; 81 } 82 if (!parcel.WriteParcelable(&want)) { 83 HILOG_ERROR("Failed to write want"); 84 return false; 85 } 86 if (!parcel.WriteString16(Str8ToStr16(resolvedType))) { 87 HILOG_ERROR("Failed to write resolvedType"); 88 return false; 89 } 90 if (!parcel.WriteBool(finishedReceiver != nullptr)) { 91 HILOG_ERROR("Failed to write the flag which indicate whether receiver is null"); 92 return false; 93 } 94 if (finishedReceiver) { 95 if (finishedReceiver->AsObject() == nullptr) { 96 HILOG_ERROR("finishedReceiver->AsObject is null"); 97 return false; 98 } 99 if (!(static_cast<MessageParcel*>(&parcel))->WriteRemoteObject(finishedReceiver->AsObject())) { 100 HILOG_ERROR("Failed to write receiver"); 101 return false; 102 } 103 } 104 if (!parcel.WriteString16(Str8ToStr16(requiredPermission))) { 105 HILOG_ERROR("Failed to write requiredPermission"); 106 return false; 107 } 108 return true; 109 } 110 } // namespace AAFwk 111 } // namespace OHOS 112