1 /*
2  * Copyright (c) 2023-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 "service_info.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "parcel_macro.h"
20 #include "string_ex.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 constexpr int32_t CYCLE_LIMIT = 1000;
ReadFromParcel(Parcel & parcel)25 bool AppInfo::ReadFromParcel(Parcel &parcel)
26 {
27     bundleName = Str16ToStr8(parcel.ReadString16());
28     iconId = parcel.ReadInt32();
29     labelId = parcel.ReadInt32();
30     descriptionId = parcel.ReadInt32();
31     return true;
32 }
33 
Marshalling(Parcel & parcel) const34 bool AppInfo::Marshalling(Parcel &parcel) const
35 {
36     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
37     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, iconId);
38     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
39     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, descriptionId);
40     return true;
41 }
42 
Unmarshalling(Parcel & parcel)43 AppInfo *AppInfo::Unmarshalling(Parcel &parcel)
44 {
45     AppInfo *info = new (std::nothrow) AppInfo();
46     if (info && !info->ReadFromParcel(parcel)) {
47         TAG_LOGW(AAFwkTag::SER_ROUTER, "Read from parcel failed");
48         delete info;
49         info = nullptr;
50     }
51     return info;
52 }
53 
ReadFromParcel(Parcel & parcel)54 bool BusinessAbilityFilter::ReadFromParcel(Parcel &parcel)
55 {
56     businessType = static_cast<BusinessType>(parcel.ReadInt32());
57     mimeType = Str16ToStr8(parcel.ReadString16());
58     uri = Str16ToStr8(parcel.ReadString16());
59     return true;
60 }
61 
Marshalling(Parcel & parcel) const62 bool BusinessAbilityFilter::Marshalling(Parcel &parcel) const
63 {
64     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(businessType));
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(mimeType));
66     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(uri));
67     return true;
68 }
69 
Unmarshalling(Parcel & parcel)70 BusinessAbilityFilter *BusinessAbilityFilter::Unmarshalling(Parcel &parcel)
71 {
72     BusinessAbilityFilter *filter = new (std::nothrow) BusinessAbilityFilter();
73     if (filter && !filter->ReadFromParcel(parcel)) {
74         TAG_LOGW(AAFwkTag::SER_ROUTER, "Read from parcel failed");
75         delete filter;
76         filter = nullptr;
77     }
78     return filter;
79 }
80 
ReadFromParcel(Parcel & parcel)81 bool BusinessAbilityInfo::ReadFromParcel(Parcel &parcel)
82 {
83     std::unique_ptr<AppInfo> app(parcel.ReadParcelable<AppInfo>());
84     if (!app) {
85         TAG_LOGE(AAFwkTag::SER_ROUTER, "ReadParcelable<AppInfo> failed");
86         return false;
87     }
88     appInfo = *app;
89     bundleName = Str16ToStr8(parcel.ReadString16());
90     moduleName = Str16ToStr8(parcel.ReadString16());
91     abilityName = Str16ToStr8(parcel.ReadString16());
92     businessType = static_cast<BusinessType>(parcel.ReadInt32());
93     iconId = parcel.ReadInt32();
94     labelId = parcel.ReadInt32();
95     descriptionId = parcel.ReadInt32();
96 
97     int32_t size;
98     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, size);
99     CONTAINER_SECURITY_VERIFY(parcel, size, &permissions);
100     if (size > CYCLE_LIMIT) {
101         TAG_LOGE(AAFwkTag::SER_ROUTER, "Size too large");
102         return false;
103     }
104     for (int32_t i = 0; i < size; i++) {
105         permissions.emplace_back(Str16ToStr8(parcel.ReadString16()));
106     }
107 
108     return true;
109 }
110 
Marshalling(Parcel & parcel) const111 bool BusinessAbilityInfo::Marshalling(Parcel &parcel) const
112 {
113     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &appInfo);
114     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
115     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
116     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
117     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(businessType));
118     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, iconId);
119     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, labelId);
120     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, descriptionId);
121     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, permissions.size());
122     for (auto &permission : permissions) {
123         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(permission));
124     }
125     return true;
126 }
127 
Unmarshalling(Parcel & parcel)128 BusinessAbilityInfo *BusinessAbilityInfo::Unmarshalling(Parcel &parcel)
129 {
130     BusinessAbilityInfo *info = new (std::nothrow) BusinessAbilityInfo();
131     if (info && !info->ReadFromParcel(parcel)) {
132         TAG_LOGW(AAFwkTag::SER_ROUTER, "Read from parcel failed");
133         delete info;
134         info = nullptr;
135     }
136     return info;
137 }
138 
ReadFromParcel(Parcel & parcel)139 bool PurposeInfo::ReadFromParcel(Parcel &parcel)
140 {
141     std::unique_ptr<AppInfo> app(parcel.ReadParcelable<AppInfo>());
142     if (!app) {
143         TAG_LOGE(AAFwkTag::SER_ROUTER, "ReadParcelable<AppInfo> failed");
144         return false;
145     }
146     appInfo = *app;
147     purposeName = Str16ToStr8(parcel.ReadString16());
148     bundleName = Str16ToStr8(parcel.ReadString16());
149     moduleName = Str16ToStr8(parcel.ReadString16());
150     abilityName = Str16ToStr8(parcel.ReadString16());
151     cardName = Str16ToStr8(parcel.ReadString16());
152     int32_t supportDimensionSize;
153     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, supportDimensionSize);
154     CONTAINER_SECURITY_VERIFY(parcel, supportDimensionSize, &supportDimensions);
155     if (supportDimensionSize > CYCLE_LIMIT) {
156         TAG_LOGE(AAFwkTag::SER_ROUTER, "SupportDimensionSize too large");
157         return false;
158     }
159     for (int32_t i = 0; i < supportDimensionSize; i++) {
160         supportDimensions.emplace_back(parcel.ReadInt32());
161     }
162     componentType = static_cast<ComponentType>(parcel.ReadInt32());
163     return true;
164 }
165 
Marshalling(Parcel & parcel) const166 bool PurposeInfo::Marshalling(Parcel &parcel) const
167 {
168     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Parcelable, parcel, &appInfo);
169     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(purposeName));
170     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
171     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName));
172     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName));
173     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(cardName));
174     const auto supportDimensionSize = static_cast<int32_t>(supportDimensions.size());
175     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, supportDimensionSize);
176     for (auto i = 0; i < supportDimensionSize; i++) {
177         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, supportDimensions[i]);
178     }
179     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(componentType));
180     return true;
181 }
182 
Unmarshalling(Parcel & parcel)183 PurposeInfo *PurposeInfo::Unmarshalling(Parcel &parcel)
184 {
185     PurposeInfo *info = new (std::nothrow) PurposeInfo();
186     if (info && !info->ReadFromParcel(parcel)) {
187         TAG_LOGW(AAFwkTag::SER_ROUTER, "Read from parcel failed");
188         delete info;
189         info = nullptr;
190     }
191     return info;
192 }
193 } // namespace AbilityRuntime
194 } // namespace OHOS