1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "want_sender_info.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
19eace7efcSopenharmony_ci
20eace7efcSopenharmony_cinamespace OHOS {
21eace7efcSopenharmony_cinamespace AAFwk {
22eace7efcSopenharmony_ci
23eace7efcSopenharmony_ciconstexpr int32_t READ_PARCEL_MAX_WANT_INFO_SIZE = 1000;
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_cibool WantSenderInfo::ReadFromParcel(Parcel &parcel)
26eace7efcSopenharmony_ci{
27eace7efcSopenharmony_ci    type = parcel.ReadInt32();
28eace7efcSopenharmony_ci    bundleName = Str16ToStr8(parcel.ReadString16());
29eace7efcSopenharmony_ci    resultWho = Str16ToStr8(parcel.ReadString16());
30eace7efcSopenharmony_ci    requestCode = parcel.ReadInt32();
31eace7efcSopenharmony_ci    int32_t wantsInfoSize = parcel.ReadInt32();
32eace7efcSopenharmony_ci    if (wantsInfoSize > READ_PARCEL_MAX_WANT_INFO_SIZE) {
33eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::WANTAGENT, "ReadFromParcel wantsInfoSize Control");
34eace7efcSopenharmony_ci        return false;
35eace7efcSopenharmony_ci    }
36eace7efcSopenharmony_ci    for (int32_t i = 0; i < wantsInfoSize; i++) {
37eace7efcSopenharmony_ci        std::unique_ptr<WantsInfo> wantsInfo(parcel.ReadParcelable<WantsInfo>());
38eace7efcSopenharmony_ci        if (!wantsInfo) {
39eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::WANTAGENT, "null wantsInfo");
40eace7efcSopenharmony_ci            return false;
41eace7efcSopenharmony_ci        }
42eace7efcSopenharmony_ci        allWants.emplace_back(*wantsInfo);
43eace7efcSopenharmony_ci    }
44eace7efcSopenharmony_ci    flags = parcel.ReadUint32();
45eace7efcSopenharmony_ci    userId = parcel.ReadInt32();
46eace7efcSopenharmony_ci    return true;
47eace7efcSopenharmony_ci}
48eace7efcSopenharmony_ci
49eace7efcSopenharmony_ciWantSenderInfo *WantSenderInfo::Unmarshalling(Parcel &parcel)
50eace7efcSopenharmony_ci{
51eace7efcSopenharmony_ci    WantSenderInfo *info = new (std::nothrow) WantSenderInfo();
52eace7efcSopenharmony_ci    if (info == nullptr) {
53eace7efcSopenharmony_ci        return nullptr;
54eace7efcSopenharmony_ci    }
55eace7efcSopenharmony_ci
56eace7efcSopenharmony_ci    if (!info->ReadFromParcel(parcel)) {
57eace7efcSopenharmony_ci        delete info;
58eace7efcSopenharmony_ci        info = nullptr;
59eace7efcSopenharmony_ci    }
60eace7efcSopenharmony_ci    return info;
61eace7efcSopenharmony_ci}
62eace7efcSopenharmony_ci
63eace7efcSopenharmony_cibool WantSenderInfo::Marshalling(Parcel &parcel) const
64eace7efcSopenharmony_ci{
65eace7efcSopenharmony_ci    parcel.WriteInt32(type);
66eace7efcSopenharmony_ci    parcel.WriteString16(Str8ToStr16(bundleName));
67eace7efcSopenharmony_ci    parcel.WriteString16(Str8ToStr16(resultWho));
68eace7efcSopenharmony_ci    parcel.WriteInt32(requestCode);
69eace7efcSopenharmony_ci    size_t wantsInfoSize = allWants.size();
70eace7efcSopenharmony_ci    if (!parcel.WriteInt32(wantsInfoSize)) {
71eace7efcSopenharmony_ci        return false;
72eace7efcSopenharmony_ci    }
73eace7efcSopenharmony_ci    for (size_t i = 0; i < wantsInfoSize; i++) {
74eace7efcSopenharmony_ci        if (!parcel.WriteParcelable(&allWants[i])) {
75eace7efcSopenharmony_ci            return false;
76eace7efcSopenharmony_ci        }
77eace7efcSopenharmony_ci    }
78eace7efcSopenharmony_ci    parcel.WriteUint32(flags);
79eace7efcSopenharmony_ci    parcel.WriteInt32(userId);
80eace7efcSopenharmony_ci    return true;
81eace7efcSopenharmony_ci}
82eace7efcSopenharmony_ci}  // namespace AAFwk
83eace7efcSopenharmony_ci}  // namespace OHOS
84