1 /*
2  * Copyright (c) 2022 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 "hap_token_info_for_sync_parcel.h"
17 #include "refbase.h"
18 #include "hap_token_info_parcel.h"
19 #include "parcel_utils.h"
20 #include "permission_state_full.h"
21 #include "permission_state_full_parcel.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
Marshalling(Parcel & out) const26 bool HapTokenInfoForSyncParcel::Marshalling(Parcel& out) const
27 {
28     HapTokenInfoParcel baseInfoParcel;
29     baseInfoParcel.hapTokenInfoParams = this->hapTokenInfoForSyncParams.baseInfo;
30     out.WriteParcelable(&baseInfoParcel);
31 
32     const std::vector<PermissionStateFull>& permStateList = this->hapTokenInfoForSyncParams.permStateList;
33     uint32_t permStateListSize = permStateList.size();
34     RETURN_IF_FALSE(permStateListSize <= MAX_PERMLIST_SIZE);
35     RETURN_IF_FALSE(out.WriteUint32(permStateListSize));
36     for (uint32_t i = 0; i < permStateListSize; i++) {
37         PermissionStateFullParcel permStateParcel;
38         permStateParcel.permStatFull = permStateList[i];
39         out.WriteParcelable(&permStateParcel);
40     }
41 
42     return true;
43 }
44 
Unmarshalling(Parcel & in)45 HapTokenInfoForSyncParcel* HapTokenInfoForSyncParcel::Unmarshalling(Parcel& in)
46 {
47     auto* hapTokenInfoForSyncParcel = new (std::nothrow) HapTokenInfoForSyncParcel();
48     if (hapTokenInfoForSyncParcel == nullptr) {
49         return nullptr;
50     }
51 
52     sptr<HapTokenInfoParcel> baseInfoParcel = in.ReadParcelable<HapTokenInfoParcel>();
53     RELEASE_IF_FALSE(baseInfoParcel != nullptr, hapTokenInfoForSyncParcel);
54     hapTokenInfoForSyncParcel->hapTokenInfoForSyncParams.baseInfo = baseInfoParcel->hapTokenInfoParams;
55 
56     uint32_t permStateListSize;
57     RELEASE_IF_FALSE(in.ReadUint32(permStateListSize), hapTokenInfoForSyncParcel);
58     RELEASE_IF_FALSE((permStateListSize <= MAX_PERMLIST_SIZE), hapTokenInfoForSyncParcel);
59     for (uint32_t i = 0; i < permStateListSize; i++) {
60         sptr<PermissionStateFullParcel> permissionStateParcel = in.ReadParcelable<PermissionStateFullParcel>();
61         RELEASE_IF_FALSE(permissionStateParcel != nullptr, hapTokenInfoForSyncParcel);
62         hapTokenInfoForSyncParcel->hapTokenInfoForSyncParams.permStateList.emplace_back(
63             permissionStateParcel->permStatFull);
64     }
65     return hapTokenInfoForSyncParcel;
66 }
67 } // namespace AccessToken
68 } // namespace Security
69 } // namespace OHOS
70