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 
16 #include "bundle_resource_info.h"
17 
18 #include "app_log_wrapper.h"
19 #include "nlohmann/json.hpp"
20 #include "parcel_macro.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 
ReadFromParcel(Parcel & parcel)26 bool BundleResourceInfo::ReadFromParcel(Parcel &parcel)
27 {
28     std::u16string bundleNameVal;
29     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, bundleNameVal);
30     bundleName = Str16ToStr8(bundleNameVal);
31     std::u16string labelVal;
32     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, labelVal);
33     label = Str16ToStr8(labelVal);
34     std::u16string iconVal;
35     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, iconVal);
36     icon = Str16ToStr8(iconVal);
37 
38     int32_t foregroundSize;
39     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, foregroundSize);
40     CONTAINER_SECURITY_VERIFY(parcel, foregroundSize, &foreground);
41     for (auto i = 0; i < foregroundSize; i++) {
42         foreground.emplace_back(parcel.ReadUint8());
43     }
44 
45     int32_t backgroundSize;
46     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, backgroundSize);
47     CONTAINER_SECURITY_VERIFY(parcel, backgroundSize, &background);
48     for (auto i = 0; i < backgroundSize; i++) {
49         background.emplace_back(parcel.ReadUint8());
50     }
51     READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
52     return true;
53 }
54 
Marshalling(Parcel & parcel) const55 bool BundleResourceInfo::Marshalling(Parcel &parcel) const
56 {
57     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
58     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(label));
59     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(icon));
60 
61     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, foreground.size());
62     for (const auto &data : foreground) {
63         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8, parcel, data);
64     }
65     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, background.size());
66     for (const auto &data : background) {
67         WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8, parcel, data);
68     }
69     WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
70     return true;
71 }
72 
Unmarshalling(Parcel & parcel)73 BundleResourceInfo *BundleResourceInfo::Unmarshalling(Parcel &parcel)
74 {
75     BundleResourceInfo *info = new (std::nothrow) BundleResourceInfo();
76     if ((info != nullptr) && !info->ReadFromParcel(parcel)) {
77         APP_LOGW("read from parcel failed");
78         delete info;
79         info = nullptr;
80     }
81     return info;
82 }
83 } // AppExecFwk
84 } // OHOS
85