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 specified authority. 131 * 132 * @param authorityId Id of the specified authority. 133 */ 134 std::string GetAuthority(int index) const; 135 136 /** 137 * @brief Adds an authority to this Skills object. 138 * 139 * @param authority Indicates the authority to add. 140 */ 141 void AddAuthority(const std::string &authority); 142 143 /** 144 * @brief Checks whether the specified authority is exist. 145 * 146 * @param authority Name of the specified authority. 147 */ 148 bool HasAuthority(const std::string &authority); 149 150 /** 151 * @brief Remove the specified authority. 152 * 153 * @param authority Name of the specified authority. 154 */ 155 void RemoveAuthority(const std::string &authority); 156 157 /** 158 * @brief Obtains the count of authorities. 159 * 160 */ 161 int CountAuthorities() const; 162 163 /** 164 * @brief Obtains the specified path. 165 * 166 * @param pathId Id of the specified path. 167 */ 168 std::string GetPath(int index) const; 169 170 /** 171 * @brief Adds a path to this Skills object. 172 * 173 * @param path Indicates the path to add. 174 */ 175 void AddPath(const std::string &path); 176 177 /** 178 * @brief Adds a path to this Skills object. 179 * 180 * @param path Indicates the path to add. 181 */ 182 void AddPath(const PatternsMatcher &patternsMatcher); 183 184 /** 185 * @brief Adds a path to this Skills object. 186 * 187 * @param path Indicates the path to add. 188 * @param matchType the specified match type. 189 */ 190 void AddPath(const std::string &path, const MatchType &matchType); 191 192 /** 193 * @brief Checks whether the specified path is exist. 194 * 195 * @param path Name of the specified path. 196 */ 197 bool HasPath(const std::string &path); 198 199 /** 200 * @brief Remove the specified path. 201 * 202 * @param path Name of the specified path. 203 */ 204 void RemovePath(const std::string &path); 205 206 /** 207 * @brief Remove the specified path. 208 * 209 * @param path The path to be added. 210 */ 211 void RemovePath(const PatternsMatcher &patternsMatcher); 212 213 /** 214 * @brief Remove the specified path. 215 * 216 * @param path Name of the specified path. 217 * @param matchType the specified match type. 218 */ 219 void RemovePath(const std::string &path, const MatchType &matchType); 220 221 /** 222 * @brief Obtains the count of paths. 223 * 224 */ 225 int CountPaths() const; 226 227 /** 228 * @brief Obtains the specified scheme. 229 * 230 * @param schemeId Id of the specified scheme. 231 */ 232 std::string GetScheme(int index) const; 233 234 /** 235 * @brief Adds an scheme to this Skills object. 236 * 237 * @param scheme Indicates the scheme to add. 238 */ 239 void AddScheme(const std::string &scheme); 240 241 /** 242 * @brief Checks whether the specified scheme is exist. 243 * 244 * @param scheme Name of the specified scheme. 245 */ 246 bool HasScheme(const std::string &scheme); 247 248 /** 249 * @brief Remove the specified scheme. 250 * 251 * @param scheme Name of the specified scheme. 252 */ 253 void RemoveScheme(const std::string &scheme); 254 255 /** 256 * @brief Obtains the count of schemes. 257 * 258 */ 259 int CountSchemes() const; 260 261 /** 262 * @brief Obtains the specified scheme part. 263 * 264 * @param schemeId Id of the specified scheme part. 265 */ 266 std::string GetSchemeSpecificPart(int index) const; 267 268 /** 269 * @brief Adds an scheme to this Skills object. 270 * 271 * @param scheme Indicates the scheme to add. 272 */ 273 void AddSchemeSpecificPart(const std::string &schemeSpecificPart); 274 275 /** 276 * @brief Checks whether the specified scheme part is exist. 277 * 278 * @param scheme Name of the specified scheme part. 279 */ 280 bool HasSchemeSpecificPart(const std::string &schemeSpecificPart); 281 282 /** 283 * @brief Remove the specified scheme part. 284 * 285 * @param scheme Name of the specified scheme part. 286 */ 287 void RemoveSchemeSpecificPart(const std::string &schemeSpecificPart); 288 289 /** 290 * @brief Obtains the count of scheme parts. 291 * 292 */ 293 int CountSchemeSpecificParts() const; 294 295 /** 296 * @brief Obtains the specified type. 297 * 298 * @param typeId Id of the specified type. 299 */ 300 std::string GetType(int index) const; 301 302 /** 303 * @brief Adds a type to this Skills object. 304 * 305 * @param type Indicates the type to add. 306 */ 307 void AddType(const std::string &type); 308 309 /** 310 * @brief Adds a type to this Skills object. 311 * 312 * @param type Indicates the type to add. 313 */ 314 void AddType(const PatternsMatcher &patternsMatcher); 315 316 /** 317 * @brief Adds a type to this Skills object. 318 * 319 * @param type Indicates the type to add. 320 * @param matchType the specified match type. 321 */ 322 void AddType(const std::string &type, const MatchType &matchType); 323 324 /** 325 * @brief Checks whether the specified type is exist. 326 * 327 * @param type Name of the specified type. 328 */ 329 bool HasType(const std::string &type); 330 331 /** 332 * @brief Remove the specified type. 333 * 334 * @param type Name of the specified type. 335 */ 336 void RemoveType(const std::string &type); 337 338 /** 339 * @brief Remove the specified scheme type. 340 * 341 * @param type The type to be added. 342 */ 343 void RemoveType(const PatternsMatcher &patternsMatcher); 344 345 /** 346 * @brief Remove the specified scheme type. 347 * 348 * @param type Name of the specified type. 349 * @param matchType the specified match type. 350 */ 351 void RemoveType(const std::string &type, const MatchType &matchType); 352 353 /** 354 * @brief Obtains the count of types. 355 * 356 */ 357 int CountTypes() const; 358 359 /** 360 * @brief Match this skill against a Want's data. 361 * 362 * @param want The desired want data to match for. 363 */ 364 bool Match(const Want &want); 365 366 /** 367 * @brief Obtains the want params data. 368 * 369 * @return the WantParams object. 370 */ 371 const WantParams &GetWantParams() const; 372 373 /** 374 * @brief Sets a WantParams object in this MatchingSkills object. 375 * 376 * @param wantParams Indicates the WantParams object. 377 */ 378 void SetWantParams(const WantParams &wantParams); 379 380 /** 381 * @brief Marshals this Sequenceable object into a Parcel. 382 * 383 * @param outParcel Indicates the Parcel object to which the Sequenceable object will be marshaled. 384 */ 385 bool Marshalling(Parcel &parcel) const; 386 387 /** 388 * @brief Unmarshals this Sequenceable object from a Parcel. 389 * 390 * @param inParcel Indicates the Parcel object into which the Sequenceable object has been marshaled. 391 */ 392 static Skills *Unmarshalling(Parcel &parcel); 393 394 private: 395 static const int DISMATCH_TYPE = -101; 396 static const int DISMATCH_DATA = -102; 397 static const int DISMATCH_ACTION = -103; 398 static const int DISMATCH_ENTITIES = -104; 399 400 static const int RESULT_EMPTY = 0x10000; 401 static const int RESULT_SCHEME = 0x20000; 402 static const int RESULT_PATH = 0x50000; 403 static const int RESULT_SCHEME_SPECIFIC_PART = 0x58000; 404 static const int RESULT_TYPE = 0x60000; 405 static const int RESULT_NORMAL = 0x800; 406 407 std::vector<std::string> entities_; 408 std::vector<std::string> actions_; 409 std::vector<std::string> authorities_; 410 std::vector<std::string> schemes_; 411 412 std::vector<PatternsMatcher> paths_; 413 std::vector<PatternsMatcher> schemeSpecificParts_; 414 std::vector<PatternsMatcher> types_; 415 416 WantParams wantParams_; 417 bool hasPartialTypes_ = false; 418 419 // no object in parcel 420 static constexpr int VALUE_NULL = -1; 421 // object exist in parcel 422 static constexpr int VALUE_OBJECT = 1; 423 424 private: 425 bool ReadFromParcel(Parcel &parcel); 426 427 /** 428 * @brief Match this skills against a Want's action. If the skills does not 429 * specify any actions, the match will always fail. 430 * 431 * @param action The desired action to look for. 432 * 433 * @return True if the action is listed in the skills. 434 */ 435 bool MatchAction(const std::string &action); 436 437 /** 438 * @brief Match this skills against a Want's data (type, scheme and path). 439 * 440 * @param type The desired data type to look for. 441 * @param scheme The desired data scheme to look for. 442 * @param data The full data string to match against. 443 * 444 * @return Returns either a valid match constant. 445 */ 446 int MatchData(const std::string &type, const std::string &scheme, Uri data); 447 448 bool FindMimeType(const std::string &type); 449 450 bool RegionMatches(const std::string &type, int toffset, const std::string &other, int ooffset, int len); 451 452 /** 453 * @brief Match this skills against a Want's entities. Each entity in 454 * the Want must be specified by the skills; if any are not in the 455 * skills, the match fails. 456 * 457 * @param entities The entities included in the want, as returned by 458 * Want.getEntities(). 459 * 460 * @return If all entities match (success), null; else the name of the 461 * first entity that didn't match. 462 */ 463 std::string MatchEntities(const std::vector<std::string> &entities); 464 }; 465 } // namespace AAFwk 466 } // namespace OHOS 467 468 #endif // OHOS_ABILITY_BASE_SKILLS_H 469