• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "matching_skills.h"
17 #include "event_log_wrapper.h"
18 #include "string_ex.h"
19 
20 namespace OHOS {
21 namespace EventFwk {
MatchingSkills()22 MatchingSkills::MatchingSkills()
23 {}
24 
MatchingSkills(const MatchingSkills & matchingSkills)25 MatchingSkills::MatchingSkills(const MatchingSkills &matchingSkills)
26 {
27     entities_ = matchingSkills.entities_;
28     events_ = matchingSkills.events_;
29     schemes_ = matchingSkills.schemes_;
30 }
31 
~MatchingSkills()32 MatchingSkills::~MatchingSkills()
33 {}
34 
GetEntity(size_t index) const35 std::string MatchingSkills::GetEntity(size_t index) const
36 {
37     std::string entity;
38     if ((index >= 0) && (index < entities_.size())) {
39         entity = entities_[index];
40     }
41     return entity;
42 }
43 
AddEntity(const std::string & entity)44 void MatchingSkills::AddEntity(const std::string &entity)
45 {
46     auto it = std::find(entities_.begin(), entities_.end(), entity);
47     if (it == entities_.end()) {
48         entities_.emplace_back(entity);
49     }
50 }
51 
HasEntity(const std::string & entity) const52 bool MatchingSkills::HasEntity(const std::string &entity) const
53 {
54     return std::find(entities_.cbegin(), entities_.cend(), entity) != entities_.cend();
55 }
56 
RemoveEntity(const std::string & entity)57 void MatchingSkills::RemoveEntity(const std::string &entity)
58 {
59     auto it = std::find(entities_.cbegin(), entities_.cend(), entity);
60     if (it != entities_.cend()) {
61         entities_.erase(it);
62     }
63 }
64 
CountEntities() const65 size_t MatchingSkills::CountEntities() const
66 {
67     return entities_.size();
68 }
69 
AddEvent(const std::string & event)70 void MatchingSkills::AddEvent(const std::string &event)
71 {
72     auto it = std::find(events_.cbegin(), events_.cend(), event);
73     if (it == events_.cend()) {
74         events_.emplace_back(event);
75     }
76 }
77 
CountEvent() const78 size_t MatchingSkills::CountEvent() const
79 {
80     return events_.size();
81 }
82 
GetEvent(size_t index) const83 std::string MatchingSkills::GetEvent(size_t index) const
84 {
85     std::string event;
86     if (index >= 0 && index < events_.size()) {
87         event = events_[index];
88     }
89     return event;
90 }
91 
GetEvents() const92 std::vector<std::string> MatchingSkills::GetEvents() const
93 {
94     return events_;
95 }
96 
RemoveEvent(const std::string & event)97 void MatchingSkills::RemoveEvent(const std::string &event)
98 {
99     auto it = std::find(events_.cbegin(), events_.cend(), event);
100     if (it != events_.cend()) {
101         events_.erase(it);
102     }
103 }
104 
HasEvent(const std::string & event) const105 bool MatchingSkills::HasEvent(const std::string &event) const
106 {
107     return std::find(events_.cbegin(), events_.cend(), event) != events_.cend();
108 }
109 
GetScheme(size_t index) const110 std::string MatchingSkills::GetScheme(size_t index) const
111 {
112     std::string schemes;
113     if ((index >= 0) && (index < schemes_.size())) {
114         schemes = schemes_[index];
115     }
116     return schemes;
117 }
118 
AddScheme(const std::string & scheme)119 void MatchingSkills::AddScheme(const std::string &scheme)
120 {
121     auto it = std::find(schemes_.begin(), schemes_.end(), scheme);
122     if (it == schemes_.end()) {
123         schemes_.emplace_back(scheme);
124     }
125 }
126 
HasScheme(const std::string & scheme) const127 bool MatchingSkills::HasScheme(const std::string &scheme) const
128 {
129     return std::find(schemes_.begin(), schemes_.end(), scheme) != schemes_.end();
130 }
131 
RemoveScheme(const std::string & scheme)132 void MatchingSkills::RemoveScheme(const std::string &scheme)
133 {
134     auto it = std::find(schemes_.cbegin(), schemes_.cend(), scheme);
135     if (it != schemes_.cend()) {
136         schemes_.erase(it);
137     }
138 }
139 
CountSchemes() const140 size_t MatchingSkills::CountSchemes() const
141 {
142     return schemes_.size();
143 }
144 
WriteVectorInfo(Parcel & parcel,std::vector<std::u16string> vectorInfo) const145 bool MatchingSkills::WriteVectorInfo(Parcel &parcel, std::vector<std::u16string>vectorInfo) const
146 {
147     if (vectorInfo.empty()) {
148         if (!parcel.WriteInt32(VALUE_NULL)) {
149             return false;
150         }
151     } else {
152         if (!parcel.WriteInt32(VALUE_OBJECT)) {
153             return false;
154         }
155         if (!parcel.WriteString16Vector(vectorInfo)) {
156             return false;
157         }
158     }
159     return true;
160 }
161 
Marshalling(Parcel & parcel) const162 bool MatchingSkills::Marshalling(Parcel &parcel) const
163 {
164     // write entity
165     std::vector<std::u16string> actionU16Entity;
166     for (std::vector<std::string>::size_type i = 0; i < entities_.size(); i++) {
167         actionU16Entity.emplace_back(Str8ToStr16(entities_[i]));
168     }
169 
170     if (!WriteVectorInfo(parcel, actionU16Entity)) {
171         EVENT_LOGE("matching skills write entity error");
172         return false;
173     }
174 
175     // write event
176     std::vector<std::u16string> actionU16Event;
177     for (std::vector<std::string>::size_type i = 0; i < events_.size(); i++) {
178         actionU16Event.emplace_back(Str8ToStr16(events_[i]));
179     }
180 
181     if (!WriteVectorInfo(parcel, actionU16Event)) {
182         EVENT_LOGE("matching skills write event error");
183         return false;
184     }
185 
186     // write scheme
187     std::vector<std::u16string> actionU16Scheme;
188     for (std::vector<std::string>::size_type i = 0; i < schemes_.size(); i++) {
189         actionU16Scheme.emplace_back(Str8ToStr16(schemes_[i]));
190     }
191 
192     if (!WriteVectorInfo(parcel, actionU16Scheme)) {
193         EVENT_LOGE("matching skills write scheme error");
194         return false;
195     }
196 
197     return true;
198 }
199 
ReadFromParcel(Parcel & parcel)200 bool MatchingSkills::ReadFromParcel(Parcel &parcel)
201 {
202     // read entities
203     std::vector<std::u16string> actionU16Entity;
204     int32_t empty = VALUE_NULL;
205     if (!parcel.ReadInt32(empty)) {
206         return false;
207     }
208     if (empty == VALUE_OBJECT) {
209         if (!parcel.ReadString16Vector(&actionU16Entity)) {
210             return false;
211         }
212     }
213     entities_.clear();
214     for (std::vector<std::u16string>::size_type i = 0; i < actionU16Entity.size(); i++) {
215         entities_.emplace_back(Str16ToStr8(actionU16Entity[i]));
216     }
217 
218     // read event
219     std::vector<std::u16string> actionU16Event;
220     empty = VALUE_NULL;
221     if (!parcel.ReadInt32(empty)) {
222         return false;
223     }
224     if (empty == VALUE_OBJECT) {
225         if (!parcel.ReadString16Vector(&actionU16Event)) {
226             return false;
227         }
228     }
229     events_.clear();
230     for (std::vector<std::u16string>::size_type i = 0; i < actionU16Event.size(); i++) {
231         events_.emplace_back(Str16ToStr8(actionU16Event[i]));
232     }
233 
234     // read event
235     std::vector<std::u16string> actionU16Scheme;
236     empty = VALUE_NULL;
237     if (!parcel.ReadInt32(empty)) {
238         return false;
239     }
240     if (empty == VALUE_OBJECT) {
241         if (!parcel.ReadString16Vector(&actionU16Scheme)) {
242             return false;
243         }
244     }
245     schemes_.clear();
246     for (std::vector<std::u16string>::size_type i = 0; i < actionU16Scheme.size(); i++) {
247         schemes_.emplace_back(Str16ToStr8(actionU16Scheme[i]));
248     }
249     return true;
250 }
251 
Unmarshalling(Parcel & parcel)252 MatchingSkills *MatchingSkills::Unmarshalling(Parcel &parcel)
253 {
254     MatchingSkills *matchingSkills = new (std::nothrow) MatchingSkills();
255 
256     if (matchingSkills == nullptr) {
257         EVENT_LOGE("failed to create obj");
258         return nullptr;
259     }
260 
261     if (!matchingSkills->ReadFromParcel(parcel)) {
262         EVENT_LOGE("failed to ReadFromParcel");
263         delete matchingSkills;
264         matchingSkills = nullptr;
265     }
266 
267     return matchingSkills;
268 }
269 
MatchEvent(const std::string & event) const270 bool MatchingSkills::MatchEvent(const std::string &event) const
271 {
272     if (event == std::string()) {
273         EVENT_LOGD("event is null");
274         return false;
275     }
276 
277     return HasEvent(event);
278 }
279 
MatchEntity(const std::vector<std::string> & entities) const280 bool MatchingSkills::MatchEntity(const std::vector<std::string> &entities) const
281 {
282     if (entities.empty()) {
283         EVENT_LOGD("match empty entity");
284         return true;
285     }
286 
287     for (auto vec : entities) {
288         auto it = std::find(entities_.cbegin(), entities_.cend(), vec);
289         if (it == entities_.cend()) {
290             return false;
291         }
292     }
293 
294     return true;
295 }
296 
MatchScheme(const std::string & scheme) const297 bool MatchingSkills::MatchScheme(const std::string &scheme) const
298 {
299     if (schemes_.size()) {
300         return HasScheme(scheme);
301     }
302 
303     if (scheme == std::string()) {
304         EVENT_LOGD("scheme is null");
305         return true;
306     }
307 
308     return false;
309 }
310 
Match(const Want & want) const311 bool MatchingSkills::Match(const Want &want) const
312 {
313     return MatchEvent(want.GetAction()) && MatchEntity(want.GetEntities()) && MatchScheme(want.GetScheme());
314 }
315 }  // namespace EventFwk
316 }  // namespace OHOS