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 "system_ability_on_demand_event.h"
17
18 #include "sam_log.h"
19
20 namespace OHOS {
WriteOnDemandEventsToParcel(const std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents,MessageParcel & data)21 bool OnDemandEventToParcel::WriteOnDemandEventsToParcel(
22 const std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents, MessageParcel& data)
23 {
24 size_t size = abilityOnDemandEvents.size();
25 if (!data.WriteInt32(size)) {
26 HILOGW("WriteOnDemandEvents write list size failed!");
27 return false;
28 }
29 for (auto& event : abilityOnDemandEvents) {
30 if (!WriteOnDemandEventToParcel(event, data)) {
31 HILOGW("WriteOnDemandEvents write event failed!");
32 return false;
33 }
34 if (!data.WriteBool(event.persistence)) {
35 HILOGW("WriteOnDemandEvents write persistence failed!");
36 return false;
37 }
38 if (!data.WriteInt32(event.conditions.size())) {
39 HILOGW("WriteOnDemandEvents write conditions size failed!");
40 return false;
41 }
42 for (auto& condition : event.conditions) {
43 if (!WriteOnDemandConditionToParcel(condition, data)) {
44 HILOGW("WriteOnDemandEvents write condition failed!");
45 return false;
46 }
47 }
48 if (!data.WriteBool(event.enableOnce)) {
49 HILOGW("WriteOnDemandEvents write enableOnce failed!");
50 return false;
51 }
52 }
53 return true;
54 }
55
WriteOnDemandEventToParcel(const SystemAbilityOnDemandEvent & event,MessageParcel & data)56 bool OnDemandEventToParcel::WriteOnDemandEventToParcel(const SystemAbilityOnDemandEvent& event, MessageParcel& data)
57 {
58 if (!data.WriteInt32(static_cast<int32_t>(event.eventId))) {
59 HILOGW("WriteOnDemandEvent write eventId failed!");
60 return false;
61 }
62 if (!data.WriteString(event.name)) {
63 HILOGW("WriteOnDemandEvent write name failed!");
64 return false;
65 }
66 if (!data.WriteString(event.value)) {
67 HILOGW("WriteOnDemandEvent write value failed!");
68 return false;
69 }
70 return true;
71 }
72
WriteOnDemandConditionToParcel(const SystemAbilityOnDemandCondition & condition,MessageParcel & data)73 bool OnDemandEventToParcel::WriteOnDemandConditionToParcel(const SystemAbilityOnDemandCondition& condition,
74 MessageParcel& data)
75 {
76 if (!data.WriteInt32(static_cast<int32_t>(condition.eventId))) {
77 HILOGW("WriteOnDemandCondition write eventId failed!");
78 return false;
79 }
80 if (!data.WriteString(condition.name)) {
81 HILOGW("WriteOnDemandCondition write name failed!");
82 return false;
83 }
84 if (!data.WriteString(condition.value)) {
85 HILOGW("WriteOnDemandCondition write value failed!");
86 return false;
87 }
88 return true;
89 }
90
ReadOnDemandEventsFromParcel(std::vector<SystemAbilityOnDemandEvent> & abilityOnDemandEvents,MessageParcel & reply)91 bool OnDemandEventToParcel::ReadOnDemandEventsFromParcel(
92 std::vector<SystemAbilityOnDemandEvent>& abilityOnDemandEvents, MessageParcel& reply)
93 {
94 int32_t size = 0;
95 if (!reply.ReadInt32(size)) {
96 HILOGW("ReadOnDemandEvents read list size failed!");
97 return false;
98 }
99 if (static_cast<size_t>(size) > reply.GetReadableBytes() || size < 0) {
100 HILOGW("invalid list size: %{public}d", size);
101 return false;
102 }
103 for (int32_t i = 0; i < size; i++) {
104 SystemAbilityOnDemandEvent event;
105 if (!ReadOnDemandEventFromParcel(event, reply)) {
106 HILOGW("ReadOnDemandEvents read event failed!");
107 return false;
108 }
109 if (!reply.ReadBool(event.persistence)) {
110 HILOGW("ReadOnDemandEvents read persistence failed!");
111 return false;
112 }
113 int32_t conditionsSize = 0;
114 if (!reply.ReadInt32(conditionsSize)) {
115 HILOGW("ReadOnDemandEvents read conditions size failed!");
116 return false;
117 }
118 if (static_cast<size_t>(conditionsSize) > reply.GetReadableBytes() || conditionsSize < 0) {
119 HILOGW("invalid condition list size: %{public}d", conditionsSize);
120 return false;
121 }
122 for (int32_t j = 0; j < conditionsSize; j++) {
123 SystemAbilityOnDemandCondition condition;
124 if (!ReadOnDemandConditionFromParcel(condition, reply)) {
125 HILOGW("ReadOnDemandEvents read condition failed!");
126 return false;
127 }
128 event.conditions.push_back(condition);
129 }
130 if (!reply.ReadBool(event.enableOnce)) {
131 HILOGW("ReadOnDemandEvents read enableOnce failed!");
132 return false;
133 }
134 abilityOnDemandEvents.push_back(event);
135 }
136 return true;
137 }
138
ReadOnDemandEventFromParcel(SystemAbilityOnDemandEvent & event,MessageParcel & reply)139 bool OnDemandEventToParcel::ReadOnDemandEventFromParcel(SystemAbilityOnDemandEvent& event, MessageParcel& reply)
140 {
141 int32_t eventId = 0;
142 if (!reply.ReadInt32(eventId)) {
143 HILOGW("ReadOnDemandEvent read eventId failed!");
144 return false;
145 }
146 event.eventId = static_cast<OnDemandEventId>(eventId);
147 if (!reply.ReadString(event.name)) {
148 HILOGW("ReadOnDemandEvent read name failed!");
149 return false;
150 }
151 if (!reply.ReadString(event.value)) {
152 HILOGW("ReadOnDemandEvent read value failed!");
153 return false;
154 }
155 return true;
156 }
157
ReadOnDemandConditionFromParcel(SystemAbilityOnDemandCondition & condition,MessageParcel & reply)158 bool OnDemandEventToParcel::ReadOnDemandConditionFromParcel(SystemAbilityOnDemandCondition& condition,
159 MessageParcel& reply)
160 {
161 int32_t eventId = 0;
162 if (!reply.ReadInt32(eventId)) {
163 HILOGW("ReadOnDemandCondition read eventId failed!");
164 return false;
165 }
166 condition.eventId = static_cast<OnDemandEventId>(eventId);
167 if (!reply.ReadString(condition.name)) {
168 HILOGW("ReadOnDemandCondition read name failed!");
169 return false;
170 }
171 if (!reply.ReadString(condition.value)) {
172 HILOGW("ReadOnDemandCondition read value failed!");
173 return false;
174 }
175 return true;
176 }
177 }