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