1 /*
2 * Copyright (c) 2021-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 "running_lock_info.h"
17
18 #include "new"
19 #include "power_log.h"
20 #include "power_common.h"
21 #include "string_ex.h"
22
23 namespace OHOS {
24 namespace PowerMgr {
ReadFromParcelWorkTriggerList(Parcel & parcel,WorkTriggerList & list)25 bool RunningLockInfo::ReadFromParcelWorkTriggerList(Parcel& parcel, WorkTriggerList& list)
26 {
27 uint32_t listSize = 0;
28 if (!parcel.ReadUint32(listSize)) {
29 POWER_HILOGE(FEATURE_RUNNING_LOCK, "WriteUint32 is failed");
30 return false;
31 }
32 POWER_HILOGD(FEATURE_RUNNING_LOCK, "WorkTigger size: %{public}u", listSize);
33 while (listSize > 0) {
34 std::shared_ptr<WorkTrigger> workTrigger(parcel.ReadParcelable<WorkTrigger>());
35 if (workTrigger == nullptr) {
36 return false;
37 }
38 list.emplace_back(workTrigger);
39 listSize--;
40 }
41 return true;
42 }
43
ReadFromParcel(Parcel & parcel)44 bool RunningLockInfo::ReadFromParcel(Parcel& parcel)
45 {
46 uint32_t readType;
47 std::u16string u16Name;
48 READ_PARCEL_WITH_RET(parcel, String16, u16Name, false);
49 name = Str16ToStr8(u16Name);
50 READ_PARCEL_WITH_RET(parcel, Uint32, readType, false);
51 type = static_cast<RunningLockType>(readType);
52 return ReadFromParcelWorkTriggerList(parcel, workTriggerlist);
53 }
54
Unmarshalling(Parcel & parcel)55 RunningLockInfo* RunningLockInfo::Unmarshalling(Parcel& parcel)
56 {
57 RunningLockInfo* info = new RunningLockInfo();
58 if (info == nullptr) {
59 return nullptr;
60 }
61 if (!info->ReadFromParcel(parcel)) {
62 delete info;
63 return nullptr;
64 }
65 return info;
66 }
67
MarshallingWorkTriggerList(Parcel & parcel,const WorkTriggerList & list)68 bool RunningLockInfo::MarshallingWorkTriggerList(Parcel& parcel, const WorkTriggerList& list)
69 {
70 uint32_t listSize = static_cast<uint32_t>(list.size());
71 POWER_HILOGD(FEATURE_RUNNING_LOCK, "WorkTigger size: %{public}u", listSize);
72 if (!parcel.WriteUint32(listSize)) {
73 POWER_HILOGE(FEATURE_RUNNING_LOCK, "WriteUint32 is failed");
74 return false;
75 }
76
77 for (const auto& templateVal : list) {
78 if (templateVal == nullptr) {
79 continue;
80 }
81 if (!parcel.WriteParcelable(templateVal.get())) {
82 POWER_HILOGE(FEATURE_RUNNING_LOCK, "templateVal Marshalling failed");
83 return false;
84 }
85 }
86 return true;
87 }
88
Marshalling(Parcel & parcel) const89 bool RunningLockInfo::Marshalling(Parcel& parcel) const
90 {
91 WRITE_PARCEL_WITH_RET(parcel, String16, Str8ToStr16(name), false);
92 WRITE_PARCEL_WITH_RET(parcel, Uint32, static_cast<uint32_t>(type), false);
93
94 return MarshallingWorkTriggerList(parcel, workTriggerlist);
95 }
96
ReadFromParcel(Parcel & parcel)97 bool WorkTrigger::ReadFromParcel(Parcel& parcel)
98 {
99 READ_PARCEL_WITH_RET(parcel, String, name_, false);
100 READ_PARCEL_WITH_RET(parcel, Int32, uid_, false);
101 READ_PARCEL_WITH_RET(parcel, Int32, pid_, false);
102 READ_PARCEL_WITH_RET(parcel, Int32, abilityId_, false);
103
104 POWER_HILOGD(FEATURE_RUNNING_LOCK, "name_: %{public}s, uid_: %{public}d, \
105 pid_: %{public}d, abilityId_: %{public}d", name_.c_str(), uid_, pid_, abilityId_);
106 return true;
107 }
108
Unmarshalling(Parcel & parcel)109 WorkTrigger* WorkTrigger::Unmarshalling(Parcel& parcel)
110 {
111 WorkTrigger* workTrigger = new WorkTrigger();
112 if (workTrigger == nullptr) {
113 return nullptr;
114 }
115 if (!workTrigger->ReadFromParcel(parcel)) {
116 delete workTrigger;
117 return nullptr;
118 }
119 return workTrigger;
120 }
121
Marshalling(Parcel & parcel) const122 bool WorkTrigger::Marshalling(Parcel& parcel) const
123 {
124 WRITE_PARCEL_WITH_RET(parcel, String, name_, false);
125 WRITE_PARCEL_WITH_RET(parcel, Int32, uid_, false);
126 WRITE_PARCEL_WITH_RET(parcel, Int32, pid_, false);
127 WRITE_PARCEL_WITH_RET(parcel, Int32, abilityId_, false);
128
129 POWER_HILOGD(FEATURE_RUNNING_LOCK, "name_: %{public}s, uid_: %{public}d, \
130 pid_: %{public}d, abilityId_: %{public}d", name_.c_str(), uid_, pid_, abilityId_);
131 return true;
132 }
133 } // namespace PowerMgr
134 } // namespace OHOS
135