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
32 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, label);
33 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, icon);
34
35 int32_t foregroundSize;
36 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, foregroundSize);
37 CONTAINER_SECURITY_VERIFY(parcel, foregroundSize, &foreground);
38 uint8_t foregroundVal = 0;
39 for (auto i = 0; i < foregroundSize; i++) {
40 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8Unaligned, parcel, foregroundVal);
41 foreground.emplace_back(foregroundVal);
42 }
43
44 int32_t backgroundSize;
45 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, backgroundSize);
46 CONTAINER_SECURITY_VERIFY(parcel, backgroundSize, &background);
47 uint8_t backgroundVal = 0;
48 for (auto i = 0; i < backgroundSize; i++) {
49 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8Unaligned, parcel, backgroundVal);
50 background.emplace_back(backgroundVal);
51 }
52 READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
53 return true;
54 }
55
Marshalling(Parcel & parcel) const56 bool BundleResourceInfo::Marshalling(Parcel &parcel) const
57 {
58 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName));
59 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, label);
60 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String, parcel, icon);
61
62 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, foreground.size());
63 for (const auto &data : foreground) {
64 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8Unaligned, parcel, data);
65 }
66 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, background.size());
67 for (const auto &data : background) {
68 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Uint8Unaligned, parcel, data);
69 }
70 WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex);
71 return true;
72 }
73
Unmarshalling(Parcel & parcel)74 BundleResourceInfo *BundleResourceInfo::Unmarshalling(Parcel &parcel)
75 {
76 BundleResourceInfo *info = new (std::nothrow) BundleResourceInfo();
77 if ((info != nullptr) && !info->ReadFromParcel(parcel)) {
78 APP_LOGW("read from parcel failed");
79 delete info;
80 info = nullptr;
81 }
82 return info;
83 }
84 } // AppExecFwk
85 } // OHOS
86