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 "bundle_priority_list.h"
17 #include "memmgr_log.h"
18 
19 namespace OHOS {
20 namespace Memory {
21 namespace {
22 const std::string TAG = "MemMgrClient";
23 constexpr int MAX_PARCEL_SIZE = 1000;
24 }
25 
GetCount() const26 int32_t BundlePriorityList::GetCount() const
27 {
28     return count_;
29 }
30 
SetCount(int32_t count)31 void BundlePriorityList::SetCount(int32_t count)
32 {
33     count_ = count;
34 }
35 
GetList()36 const std::vector<BundlePriority>& BundlePriorityList::GetList()
37 {
38     return list_;
39 }
40 
Size() const41 int32_t BundlePriorityList::Size() const
42 {
43     return list_.size();
44 }
45 
AddBundleInfo(BundlePriority & bundleInfo)46 void BundlePriorityList::AddBundleInfo(BundlePriority &bundleInfo)
47 {
48     list_.push_back(bundleInfo);
49 }
50 
Show() const51 void BundlePriorityList::Show() const
52 {
53     HILOGI("     uid                                            name   priority   accountId");
54     for (auto bi : list_) {
55         HILOGI("%{public}8d\t%{public}42s\t%{public}5d\t%{public}3d",
56             bi.uid_, bi.name_.c_str(), bi.priority_, bi.accountId_);
57     }
58     HILOGI("-------------------------------------------------------------------------------");
59 }
60 
Marshalling(Parcel & parcel) const61 bool BundlePriorityList::Marshalling(Parcel &parcel) const
62 {
63     if (!parcel.WriteInt32(count_)) {
64         HILOGE("Failed to write count_");
65         return false;
66     }
67     for (auto i = 0; i < count_; ++i) {
68         if (!parcel.WriteInt32(list_[i].uid_)) {
69             HILOGE("Failed to write uid_");
70             return false;
71         }
72         if (!parcel.WriteString(list_[i].name_)) {
73             HILOGE("Failed to write ability name");
74             return false;
75         }
76         if (!parcel.WriteInt32(list_[i].priority_)) {
77             HILOGE("Failed to write priority_");
78             return false;
79         }
80         if (!parcel.WriteInt32(list_[i].accountId_)) {
81             HILOGE("Failed to write accountId_");
82             return false;
83         }
84     }
85     return true;
86 }
87 
Unmarshalling(Parcel & parcel)88 BundlePriorityList* BundlePriorityList::Unmarshalling(Parcel &parcel)
89 {
90     auto object = new (std::nothrow) BundlePriorityList();
91     if ((object != nullptr) && !object->ReadFromParcel(parcel)) {
92         delete object;
93         object = nullptr;
94     }
95 
96     return object;
97 }
98 
ReadFromParcel(Parcel & parcel)99 bool BundlePriorityList::ReadFromParcel(Parcel &parcel)
100 {
101     count_ = parcel.ReadInt32();
102     if (count_ < 0 || count_ > MAX_PARCEL_SIZE) {
103         count_ = 0;
104         return false;
105     }
106     for (auto i = 0; i < count_; ++i) {
107         int32_t uid = parcel.ReadInt32();
108         std::string name;
109         if (!parcel.ReadString(name)) {
110             HILOGE("Failed to read creator ability name");
111             return false;
112         }
113         int32_t priority = parcel.ReadInt32();
114         int32_t accountId = parcel.ReadInt32();
115         list_.push_back(BundlePriority(uid, name, priority, accountId));
116     }
117     return true;
118 }
119 }
120 }
121 
122