1 /*
2  * Copyright (C) 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 #include "ce_payment_services_parcelable.h"
16 #include "loghelper.h"
17 
18 namespace OHOS {
19 namespace NFC {
20 namespace KITS {
21 const uint32_t MAX_APP_LIST_NUM = 100;
CePaymentServicesParcelable()22 CePaymentServicesParcelable::CePaymentServicesParcelable()
23 {
24 }
~CePaymentServicesParcelable()25 CePaymentServicesParcelable::~CePaymentServicesParcelable()
26 {
27     paymentAbilityInfos.clear();
28 }
Marshalling(Parcel & parcel) const29 bool CePaymentServicesParcelable::Marshalling(Parcel &parcel) const
30 {
31     if (!parcel.WriteUint32(paymentAbilityInfos.size())) {
32         ErrorLog("write size failed");
33         return false;
34     }
35     for (unsigned int i = 0; i < paymentAbilityInfos.size(); i++) {
36         if (!paymentAbilityInfos[i].Marshalling(parcel)) {
37             ErrorLog("write ability failed");
38             return false;
39         }
40     }
41     return true;
42 }
Unmarshalling(Parcel & parcel)43 CePaymentServicesParcelable *CePaymentServicesParcelable::Unmarshalling(Parcel &parcel)
44 {
45     uint32_t extraLen = 0;
46     parcel.ReadUint32(extraLen);
47     if (extraLen >= MAX_APP_LIST_NUM) {
48         ErrorLog("invalid length");
49         return nullptr;
50     }
51     std::vector<AbilityInfo> abilityInfos;
52     for (uint32_t i = 0; i < extraLen; i++) {
53         AbilityInfo *ability = AbilityInfo::Unmarshalling(parcel);
54         if (ability == nullptr) {
55             ErrorLog("Unmarshalling ability failed");
56             return nullptr;
57         }
58         abilityInfos.push_back(*(ability));
59         // push back copy the ability so it can be deleted
60         delete ability;
61     }
62     CePaymentServicesParcelable *paymentService = new (std::nothrow) CePaymentServicesParcelable();
63     paymentService->paymentAbilityInfos = std::move(abilityInfos);
64     return paymentService;
65 }
66 } // namespace KITS
67 } // namespace NFC
68 } // namespace OHOS