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