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 #ifndef OHOS_ABILITY_BASE_SKILLS_H
17 #define OHOS_ABILITY_BASE_SKILLS_H
18 
19 #include <vector>
20 #include <string>
21 #include "want.h"
22 #include "want_params.h"
23 #include "parcel.h"
24 #include "parcel_macro_base.h"
25 #include "match_type.h"
26 #include "patterns_matcher.h"
27 #include "uri.h"
28 
29 namespace OHOS {
30 namespace AAFwk {
31 class Skills final : public Parcelable {
32 public:
33     /**
34      * @brief Default constructor used to create a Skills instance.
35      *
36      */
37     Skills();
38 
39     /**
40      * @brief A parameterized constructor used to create a Skills instance.
41      *
42      * @param skills Indicates skills used to create a Skills instance.
43      */
44     Skills(const Skills &skills);
45     ~Skills();
46 
47     /**
48      * @brief Obtains the list of entities.
49      *
50      * @return vector of Entities.
51      */
52     std::vector<std::string> GetEntities() const;
53 
54     /**
55      * @brief Obtains the specified entity.
56      *
57      * @param entity Id of the specified entity.
58      */
59     std::string GetEntity(int index) const;
60 
61     /**
62      * @brief Adds an entity to this Skills object.
63      *
64      * @param entity Indicates the entity to add.
65      */
66     void AddEntity(const std::string &entity);
67 
68     /**
69      * @brief Checks whether the specified entity is exist.
70      *
71      * @param entity Name of the specified entity.
72      */
73     bool HasEntity(const std::string &entity);
74 
75     /**
76      * @brief Remove the specified entity.
77      *
78      * @param entity Name of the specified entity.
79      */
80     void RemoveEntity(const std::string &entity);
81 
82     /**
83      * @brief Obtains the count of entities.
84      *
85      */
86     int CountEntities() const;
87 
88     /**
89      * @brief Obtains the specified action.
90      *
91      * @param actionId Id of the specified action.
92      */
93     std::string GetAction(int index) const;
94 
95     /**
96      * @brief Adds an action to this Skills object.
97      *
98      * @param action Indicates the action to add.
99      */
100     void AddAction(const std::string &action);
101 
102     /**
103      * @brief Checks whether the specified action is exist.
104      *
105      * @param action Name of the specified action.
106      */
107     bool HasAction(const std::string &action);
108 
109     /**
110      * @brief Remove the specified action.
111      *
112      * @param action Name of the specified action.
113      */
114     void RemoveAction(const std::string &action);
115 
116     /**
117      * @brief Obtains the count of actions.
118      *
119      */
120     int CountActions() const;
121 
122     /**
123      * @brief Obtains the iterator of Actions.
124      *
125      * @return iterator of Actions.
126      */
127     std::vector<std::string>::iterator ActionsIterator();
128 
129     /**
130      * @brief Obtains the iterator of Authorities.
131      *
132      * @return iterator of Authorities.
133      */
134     std::vector<std::string>::iterator AuthoritiesIterator();
135 
136     /**
137      * @brief Obtains the iterator of Entities.
138      *
139      * @return iterator of Entities.
140      */
141     std::vector<std::string>::iterator EntitiesIterator();
142 
143     /**
144      * @brief Obtains the iterator of Paths.
145      *
146      * @return iterator of Paths.
147      */
148     std::vector<std::string>::iterator PathsIterator();
149 
150     /**
151      * @brief Obtains the iterator of Schemes parts.
152      *
153      * @return iterator of Schemes parts.
154      */
155     std::vector<std::string>::iterator SchemeSpecificPartsIterator();
156 
157     /**
158      * @brief Obtains the iterator of Schemes.
159      *
160      * @return iterator of Schemes.
161      */
162     std::vector<std::string>::iterator SchemesIterator();
163 
164     /**
165      * @brief Obtains the iterator of Types.
166      *
167      * @return iterator of Types.
168      */
169     std::vector<std::string>::iterator TypesIterator();
170 
171     /**
172      * @brief Obtains the specified authority.
173      *
174      * @param authorityId Id of the specified authority.
175      */
176     std::string GetAuthority(int index) const;
177 
178     /**
179      * @brief Adds an authority to this Skills object.
180      *
181      * @param authority Indicates the authority to add.
182      */
183     void AddAuthority(const std::string &authority);
184 
185     /**
186      * @brief Checks whether the specified authority is exist.
187      *
188      * @param authority Name of the specified authority.
189      */
190     bool HasAuthority(const std::string &authority);
191 
192     /**
193      * @brief Remove the specified authority.
194      *
195      * @param authority Name of the specified authority.
196      */
197     void RemoveAuthority(const std::string &authority);
198 
199     /**
200      * @brief Obtains the count of authorities.
201      *
202      */
203     int CountAuthorities() const;
204 
205     /**
206      * @brief Obtains the specified path.
207      *
208      * @param pathId Id of the specified path.
209      */
210     std::string GetPath(int index) const;
211 
212     /**
213      * @brief Adds a path to this Skills object.
214      *
215      * @param path Indicates the path to add.
216      */
217     void AddPath(const std::string &path);
218 
219     /**
220      * @brief Adds a path to this Skills object.
221      *
222      * @param path Indicates the path to add.
223      */
224     void AddPath(const PatternsMatcher &patternsMatcher);
225 
226     /**
227      * @brief Adds a path to this Skills object.
228      *
229      * @param path Indicates the path to add.
230      * @param matchType the specified match type.
231      */
232     void AddPath(const std::string &path, const MatchType &matchType);
233 
234     /**
235      * @brief Checks whether the specified path is exist.
236      *
237      * @param path Name of the specified path.
238      */
239     bool HasPath(const std::string &path);
240 
241     /**
242      * @brief Remove the specified path.
243      *
244      * @param path Name of the specified path.
245      */
246     void RemovePath(const std::string &path);
247 
248     /**
249      * @brief Remove the specified path.
250      *
251      * @param path The path to be added.
252      */
253     void RemovePath(const PatternsMatcher &patternsMatcher);
254 
255     /**
256      * @brief Remove the specified path.
257      *
258      * @param path Name of the specified path.
259      * @param matchType the specified match type.
260      */
261     void RemovePath(const std::string &path, const MatchType &matchType);
262 
263     /**
264      * @brief Obtains the count of paths.
265      *
266      */
267     int CountPaths() const;
268 
269     /**
270      * @brief Obtains the specified scheme.
271      *
272      * @param schemeId Id of the specified scheme.
273      */
274     std::string GetScheme(int index) const;
275 
276     /**
277      * @brief Adds an scheme to this Skills object.
278      *
279      * @param scheme Indicates the scheme to add.
280      */
281     void AddScheme(const std::string &scheme);
282 
283     /**
284      * @brief Checks whether the specified scheme is exist.
285      *
286      * @param scheme Name of the specified scheme.
287      */
288     bool HasScheme(const std::string &scheme);
289 
290     /**
291      * @brief Remove the specified scheme.
292      *
293      * @param scheme Name of the specified scheme.
294      */
295     void RemoveScheme(const std::string &scheme);
296 
297     /**
298      * @brief Obtains the count of schemes.
299      *
300      */
301     int CountSchemes() const;
302 
303     /**
304      * @brief Obtains the specified scheme part.
305      *
306      * @param schemeId Id of the specified scheme part.
307      */
308     std::string GetSchemeSpecificPart(int index) const;
309 
310     /**
311      * @brief Adds an scheme to this Skills object.
312      *
313      * @param scheme Indicates the scheme to add.
314      */
315     void AddSchemeSpecificPart(const std::string &schemeSpecificPart);
316 
317     /**
318      * @brief Checks whether the specified scheme part is exist.
319      *
320      * @param scheme Name of the specified scheme part.
321      */
322     bool HasSchemeSpecificPart(const std::string &schemeSpecificPart);
323 
324     /**
325      * @brief Remove the specified scheme part.
326      *
327      * @param scheme Name of the specified scheme part.
328      */
329     void RemoveSchemeSpecificPart(const std::string &schemeSpecificPart);
330 
331     /**
332      * @brief Obtains the count of scheme parts.
333      *
334      */
335     int CountSchemeSpecificParts() const;
336 
337     /**
338      * @brief Obtains the specified type.
339      *
340      * @param typeId Id of the specified type.
341      */
342     std::string GetType(int index) const;
343 
344     /**
345      * @brief Adds a type to this Skills object.
346      *
347      * @param type Indicates the type to add.
348      */
349     void AddType(const std::string &type);
350 
351     /**
352      * @brief Adds a type to this Skills object.
353      *
354      * @param type Indicates the type to add.
355      */
356     void AddType(const PatternsMatcher &patternsMatcher);
357 
358     /**
359      * @brief Adds a type to this Skills object.
360      *
361      * @param type Indicates the type to add.
362      * @param matchType the specified match type.
363      */
364     void AddType(const std::string &type, const MatchType &matchType);
365 
366     /**
367      * @brief Checks whether the specified type is exist.
368      *
369      * @param type Name of the specified type.
370      */
371     bool HasType(const std::string &type);
372 
373     /**
374      * @brief Remove the specified type.
375      *
376      * @param type Name of the specified type.
377      */
378     void RemoveType(const std::string &type);
379 
380     /**
381      * @brief Remove the specified scheme type.
382      *
383      * @param type The type to be added.
384      */
385     void RemoveType(const PatternsMatcher &patternsMatcher);
386 
387     /**
388      * @brief Remove the specified scheme type.
389      *
390      * @param type Name of the specified type.
391      * @param matchType the specified match type.
392      */
393     void RemoveType(const std::string &type, const MatchType &matchType);
394 
395     /**
396      * @brief Obtains the count of types.
397      *
398      */
399     int CountTypes() const;
400 
401     /**
402      * @brief Match this skill against a Want's data.
403      *
404      * @param want The desired want data to match for.
405      */
406     bool Match(const Want &want);
407 
408     /**
409      * @brief Obtains the want params data.
410      *
411      * @return the WantParams object.
412      */
413     const WantParams &GetWantParams() const;
414 
415     /**
416      * @brief Sets a WantParams object in this MatchingSkills object.
417      *
418      * @param wantParams Indicates the WantParams object.
419      */
420     void SetWantParams(const WantParams &wantParams);
421 
422     /**
423      * @brief Marshals this Sequenceable object into a Parcel.
424      *
425      * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled.
426      */
427     bool Marshalling(Parcel &parcel) const;
428 
429     /**
430      * @brief Unmarshals this Sequenceable object from a Parcel.
431      *
432      * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled.
433      */
434     static Skills *Unmarshalling(Parcel &parcel);
435 
436 private:
437     static const int DISMATCH_TYPE = -101;
438     static const int DISMATCH_DATA = -102;
439     static const int DISMATCH_ACTION = -103;
440     static const int DISMATCH_ENTITIES = -104;
441 
442     static const int RESULT_EMPTY = 0x10000;
443     static const int RESULT_SCHEME = 0x20000;
444     static const int RESULT_PATH = 0x50000;
445     static const int RESULT_SCHEME_SPECIFIC_PART = 0x58000;
446     static const int RESULT_TYPE = 0x60000;
447     static const int RESULT_NORMAL = 0x800;
448 
449     std::vector<std::string> entities_;
450     std::vector<std::string> actions_;
451     std::vector<std::string> authorities_;
452     std::vector<std::string> schemes_;
453 
454     std::vector<PatternsMatcher> paths_;
455     std::vector<PatternsMatcher> schemeSpecificParts_;
456     std::vector<PatternsMatcher> types_;
457 
458     WantParams wantParams_;
459     bool hasPartialTypes_ = false;
460 
461     // no object in parcel
462     static constexpr int VALUE_NULL = -1;
463     // object exist in parcel
464     static constexpr int VALUE_OBJECT = 1;
465 
466 private:
467     bool ReadFromParcel(Parcel &parcel);
468 
469     /**
470      * @brief Match this skills against a Want's action.  If the skills does not
471      * specify any actions, the match will always fail.
472      *
473      * @param action The desired action to look for.
474      *
475      * @return True if the action is listed in the skills.
476      */
477     bool MatchAction(const std::string &action);
478 
479     /**
480      * @brief Match this skills against a Want's data (type, scheme and path).
481      *
482      * @param type The desired data type to look for.
483      * @param scheme The desired data scheme to look for.
484      * @param data The full data string to match against.
485      *
486      * @return Returns either a valid match constant.
487      */
488     int MatchData(const std::string &type, const std::string &scheme, Uri data);
489 
490     bool FindMimeType(const std::string &type);
491 
492     bool RegionMatches(const std::string &type, int toffset, const std::string &other, int ooffset, int len);
493 
494     /**
495      * @brief Match this skills against a Want's entities.  Each entity in
496      * the Want must be specified by the skills; if any are not in the
497      * skills, the match fails.
498      *
499      * @param entities The entities included in the want, as returned by
500      *                   Want.getEntities().
501      *
502      * @return If all entities match (success), null; else the name of the
503      *         first entity that didn't match.
504      */
505     std::string MatchEntities(const std::vector<std::string> &entities);
506 };
507 }  // namespace AAFwk
508 }  // namespace OHOS
509 
510 #endif  // OHOS_ABILITY_BASE_SKILLS_H
511