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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_DATA_MGR_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_DATA_MGR_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <set> 23 #include <shared_mutex> 24 #include <string> 25 26 #include "ohos/aafwk/content/want.h" 27 28 #include "ability_info.h" 29 #include "application_info.h" 30 #include "bundle_data_storage_interface.h" 31 #include "bundle_event_callback_interface.h" 32 #include "bundle_promise.h" 33 #include "bundle_status_callback_interface.h" 34 #include "common_event_data.h" 35 #include "common_event_manager.h" 36 #include "distributed_data_storage.h" 37 #include "inner_bundle_info.h" 38 #include "inner_bundle_user_info.h" 39 #include "module_usage_data_storage.h" 40 #include "module_usage_record.h" 41 #include "on_permission_changed_callback_interface.h" 42 #ifdef SUPPORT_GRAPHICS 43 #include "pixel_map.h" 44 #endif 45 #include "preinstall_data_storage.h" 46 #include "resource_manager.h" 47 48 namespace OHOS { 49 namespace AppExecFwk { 50 enum class NotifyType { INSTALL, UPDATE, UNINSTALL_BUNDLE, UNINSTALL_MODULE, ABILITY_ENABLE, APPLICATION_ENABLE }; 51 52 enum class InstallState { 53 INSTALL_START = 1, 54 INSTALL_SUCCESS, 55 INSTALL_FAIL, 56 UNINSTALL_START, 57 UNINSTALL_SUCCESS, 58 UNINSTALL_FAIL, 59 UPDATING_START, 60 UPDATING_SUCCESS, 61 UPDATING_FAIL, 62 ROLL_BACK, 63 USER_CHANGE, 64 }; 65 66 class BundleDataMgr { 67 public: 68 using Want = OHOS::AAFwk::Want; 69 70 // init state transfer map data. 71 BundleDataMgr(); 72 ~BundleDataMgr(); 73 74 /** 75 * @brief Boot query persistent storage. 76 * @return Returns true if this function is successfully called; returns false otherwise. 77 */ 78 bool LoadDataFromPersistentStorage(); 79 /** 80 * @brief Update internal state for whole bundle. 81 * @param bundleName Indicates the bundle name. 82 * @param state Indicates the install state to be set. 83 * @return Returns true if this function is successfully called; returns false otherwise. 84 */ 85 bool UpdateBundleInstallState(const std::string &bundleName, const InstallState state); 86 /** 87 * @brief Add new InnerBundleInfo. 88 * @param bundleName Indicates the bundle name. 89 * @param info Indicates the InnerBundleInfo object to be save. 90 * @return Returns true if this function is successfully called; returns false otherwise. 91 */ 92 bool AddInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &info); 93 /** 94 * @brief Add new module info to an exist InnerBundleInfo. 95 * @param bundleName Indicates the bundle name. 96 * @param newInfo Indicates the new InnerBundleInfo object. 97 * @param oldInfo Indicates the old InnerBundleInfo object. 98 * @return Returns true if this function is successfully called; returns false otherwise. 99 */ 100 bool AddNewModuleInfo(const std::string &bundleName, const InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo); 101 /** 102 * @brief Remove module info from an exist InnerBundleInfo. 103 * @param bundleName Indicates the bundle name. 104 * @param modulePackage Indicates the module Package. 105 * @param oldInfo Indicates the old InnerBundleInfo object. 106 * @return Returns true if this function is successfully called; returns false otherwise. 107 */ 108 bool RemoveModuleInfo(const std::string &bundleName, const std::string &modulePackage, InnerBundleInfo &oldInfo); 109 /** 110 * @brief Update module info of an exist module. 111 * @param bundleName Indicates the bundle name. 112 * @param newInfo Indicates the new InnerBundleInfo object. 113 * @param oldInfo Indicates the old InnerBundleInfo object. 114 * @return Returns true if this function is successfully called; returns false otherwise. 115 */ 116 bool UpdateInnerBundleInfo(const std::string &bundleName, const InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo); 117 /** 118 * @brief Get an InnerBundleInfo if exist (will change the status to DISABLED). 119 * @param bundleName Indicates the bundle name. 120 * @param deviceId Indicates this device Id corresponding to the bundle name. 121 * @param info Indicates the obtained InnerBundleInfo object. 122 * @return Returns true if this function is successfully called; returns false otherwise. 123 */ 124 bool GetInnerBundleInfo(const std::string &bundleName, const std::string &deviceId, InnerBundleInfo &info); 125 /** 126 * @brief Generate UID and GID for a bundle. 127 * @param innerBundleUserInfo Indicates the InnerBundleUserInfo object. 128 * @return Returns true if this function is successfully called; returns false otherwise. 129 */ 130 bool GenerateUidAndGid(InnerBundleUserInfo &innerBundleUserInfo); 131 /** 132 * @brief Generate cloned uid by cloned bundle. 133 * @param info Indicates the InnerBundleInfo object. 134 * @return Returns true if this function is successfully called; returns false otherwise. 135 */ 136 bool GenerateCloneUid(InnerBundleInfo &info); 137 /** 138 * @brief save the new cloned inner bundle info by cloned bundle. 139 * @param info Indicates the InnerBundleInfo object. 140 * @return Returns true if this function is successfully called; returns false otherwise. 141 */ 142 bool SaveNewInfoToDB(const std::string &bundleName, InnerBundleInfo &info); 143 /** 144 * @brief Query the AbilityInfo by the given Want. 145 * @param want Indicates the information of the ability. 146 * @param flags Indicates the information contained in the AbilityInfo object to be returned. 147 * @param userId Indicates the user ID. 148 * @param abilityInfo Indicates the obtained AbilityInfo object. 149 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 150 */ 151 bool QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo) const; 152 /** 153 * @brief Query a AbilityInfo of list by the given Want. 154 * @param want Indicates the information of the ability. 155 * @param flags Indicates the information contained in the AbilityInfo object to be returned. 156 * @param userId Indicates the user ID. 157 * @param abilityInfos Indicates the obtained AbilityInfo of list. 158 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 159 */ 160 bool QueryAbilityInfos( 161 const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const; 162 /** 163 * @brief Query a AbilityInfo of list for clone by the given Want. 164 * @param want Indicates the information of the ability. 165 * @param abilityInfo Indicates the obtained AbilityInfo of list. 166 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 167 */ 168 bool QueryAbilityInfosForClone(const Want &want, std::vector<AbilityInfo> &abilityInfo); 169 /** 170 * @brief Query all match launcher ability infos by given wants. 171 * @param want Indicates the match infomation for abilities. 172 * @param info Indicates the bundleInfo. 173 * @param abilityInfo Indicates the obtained AbilityInfo of list. 174 * @param userId Indicates the user ID. 175 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 176 */ 177 void GetMatchLauncherAbilityInfos(const Want& want, const InnerBundleInfo& info, 178 std::vector<AbilityInfo>& abilityInfos, int32_t userId = Constants::UNSPECIFIED_USERID) const; 179 /** 180 * @brief Query a AbilityInfo of list by the given userId. 181 * @param want Indicates the match infomation for abilities. 182 * @param flags Indicates weather to get application Info. 183 * @param userId Indicates the id of the user. 184 * @param abilityInfo Indicates the obtained AbilityInfo of list. 185 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 186 */ 187 bool QueryLauncherAbilityInfos( 188 const Want& want, uint32_t userId, std::vector<AbilityInfo>& abilityInfos) const; 189 /** 190 * @brief Query the AbilityInfo by ability.uri in config.json. 191 * @param abilityUri Indicates the uri of the ability. 192 * @param abilityInfos Indicates the obtained AbilityInfos object. 193 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 194 */ 195 bool QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos); 196 /** 197 * @brief Query the AbilityInfo by ability.uri in config.json. 198 * @param abilityUri Indicates the uri of the ability. 199 * @param userId Indicates the user ID. 200 * @param abilityInfo Indicates the obtained AbilityInfo object. 201 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 202 */ 203 bool QueryAbilityInfoByUri( 204 const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo) const; 205 /** 206 * @brief Obtains the ApplicationInfo based on a given bundle name. 207 * @param appName Indicates the application bundle name to be queried. 208 * @param flags Indicates the flag used to specify information contained 209 * in the ApplicationInfo object that will be returned. 210 * @param userId Indicates the user ID. 211 * @param appInfo Indicates the obtained ApplicationInfo object. 212 * @return Returns true if the application is successfully obtained; returns false otherwise. 213 */ 214 bool GetApplicationInfo( 215 const std::string &appName, int32_t flags, const int userId, ApplicationInfo &appInfo) const; 216 /** 217 * @brief Obtains information about all installed applications of a specified user. 218 * @param flags Indicates the flag used to specify information contained 219 * in the ApplicationInfo objects that will be returned. 220 * @param userId Indicates the user ID. 221 * @param appInfos Indicates all of the obtained ApplicationInfo objects. 222 * @return Returns true if the application is successfully obtained; returns false otherwise. 223 */ 224 bool GetApplicationInfos( 225 int32_t flags, const int userId, std::vector<ApplicationInfo> &appInfos) const; 226 /** 227 * @brief Obtains BundleInfo of all bundles available in the system. 228 * @param flags Indicates the flag used to specify information contained in the BundleInfo that will be returned. 229 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 230 * @param userId Indicates the user ID. 231 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 232 */ 233 bool GetBundleInfos(int32_t flags, 234 std::vector<BundleInfo> &bundleInfos, int32_t userId = Constants::UNSPECIFIED_USERID) const; 235 /** 236 * @brief Obtains the BundleInfo based on a given bundle name. 237 * @param bundleName Indicates the application bundle name to be queried. 238 * @param flags Indicates the information contained in the BundleInfo object to be returned. 239 * @param bundleInfo Indicates the obtained BundleInfo object. 240 * @param userId Indicates the user ID. 241 * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. 242 */ 243 bool GetBundleInfo(const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, 244 int32_t userId = Constants::UNSPECIFIED_USERID) const; 245 /** 246 * @brief Obtains the BundleInfo of application bundles based on the specified metaData. 247 * @param metaData Indicates the metadata to get in the bundle. 248 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 249 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 250 */ 251 bool GetBundleInfosByMetaData(const std::string &metaData, std::vector<BundleInfo> &bundleInfos) const; 252 /** 253 * @brief Obtains the bundle name of a specified application based on the given UID. 254 * @param uid Indicates the uid. 255 * @param bundleName Indicates the obtained bundle name. 256 * @return Returns true if the bundle name is successfully obtained; returns false otherwise. 257 */ 258 bool GetBundleNameForUid(const int uid, std::string &bundleName) const; 259 /** 260 * @brief Obtains all bundle names of a specified application based on the given application UID. 261 * @param uid Indicates the uid. 262 * @param bundleNames Indicates the obtained bundle names. 263 * @return Returns true if the bundle names is successfully obtained; returns false otherwise. 264 */ 265 bool GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames) const; 266 /** 267 * @brief Obtains the formal name associated with the given UID. 268 * @param uid Indicates the uid. 269 * @param name Indicates the obtained formal name. 270 * @return Returns true if the formal name is successfully obtained; returns false otherwise. 271 */ 272 bool GetNameForUid(const int uid, std::string &name) const; 273 /** 274 * @brief Obtains an array of all group IDs associated with a specified bundle. 275 * @param bundleName Indicates the bundle name. 276 * @param gids Indicates the group IDs associated with the specified bundle. 277 * @return Returns true if the gids is successfully obtained; returns false otherwise. 278 */ 279 bool GetBundleGids(const std::string &bundleName, std::vector<int> &gids) const; 280 /** 281 * @brief Obtains an array of all group IDs associated with the given bundle name and UID. 282 * @param bundleName Indicates the bundle name. 283 * @param uid Indicates the uid. 284 * @param gids Indicates the group IDs associated with the specified bundle. 285 * @return Returns true if the gids is successfully obtained; returns false otherwise. 286 */ 287 bool GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids) const; 288 /** 289 * @brief Obtains the BundleInfo of all keep-alive applications in the system. 290 * @param bundleInfos Indicates all of the obtained BundleInfo objects. 291 * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. 292 */ 293 bool QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos) const; 294 /** 295 * @brief Obtains the label of a specified ability. 296 * @param bundleName Indicates the bundle name. 297 * @param className Indicates the ability class name. 298 * @return Returns the label of the ability if exist; returns empty string otherwise. 299 */ 300 std::string GetAbilityLabel(const std::string &bundleName, const std::string &className) const; 301 /** 302 * @brief Obtains the Want for starting the main ability of an application based on the given bundle name. 303 * @param bundleName Indicates the bundle name. 304 * @param want Indicates the obtained launch Want object. 305 * @return Returns true if the launch Want object is successfully obtained; returns false otherwise. 306 */ 307 bool GetLaunchWantForBundle(const std::string &bundleName, Want &want) const; 308 /** 309 * @brief Obtain the HAP module info of a specific ability. 310 * @param abilityInfo Indicates the ability. 311 * @param hapModuleInfo Indicates the obtained HapModuleInfo object. 312 * @return Returns true if the HapModuleInfo is successfully obtained; returns false otherwise. 313 */ 314 bool GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo) const; 315 /** 316 * @brief Check whether the app is system app by it's UID. 317 * @param uid Indicates the uid. 318 * @return Returns true if the bundle is a system application; returns false otherwise. 319 */ 320 bool CheckIsSystemAppByUid(const int uid) const; 321 /** 322 * @brief Obtains all bundle names installed. 323 * @param bundleNames Indicates the bundle Names. 324 * @param userId Indicates the user ID. 325 * @return Returns true if have bundle installed; returns false otherwise. 326 */ 327 bool GetBundleList( 328 std::vector<std::string> &bundleNames, int32_t userId = Constants::UNSPECIFIED_USERID) const; 329 /** 330 * @brief Set the bundle status disable. 331 * @param bundleName Indicates the bundle name. 332 * @return Returns true if the bundle status successfully set; returns false otherwise. 333 */ 334 bool DisableBundle(const std::string &bundleName); 335 /** 336 * @brief Set the bundle status enable. 337 * @param bundleName Indicates the bundle name. 338 * @return Returns true if the bundle status successfully set; returns false otherwise. 339 */ 340 bool EnableBundle(const std::string &bundleName); 341 /** 342 * @brief Get whether the application status is enabled. 343 * @param bundleName Indicates the bundle name. 344 * @return Returns true if the bundle status is enabled; returns false otherwise. 345 */ 346 bool IsApplicationEnabled(const std::string &bundleName) const; 347 /** 348 * @brief Set the application status. 349 * @param bundleName Indicates the bundle name. 350 * @param isEnable Indicates the status to set. 351 * @param userId Indicates the user id. 352 * @return Returns true if the bundle status successfully set; returns false otherwise. 353 */ 354 bool SetApplicationEnabled(const std::string &bundleName, bool isEnable, 355 int32_t userId = Constants::UNSPECIFIED_USERID); 356 /** 357 * @brief Sets whether to enable a specified ability through the proxy object. 358 * @param abilityInfo Indicates information about the ability to check. 359 * @return Returns true if the ability is enabled; returns false otherwise. 360 */ 361 bool IsAbilityEnabled(const AbilityInfo &abilityInfo) const; 362 /** 363 * @brief Sets whether to enable a specified ability through the proxy object. 364 * @param abilityInfo Indicates information about the ability. 365 * @param isEnabled Specifies whether to enable the ability. 366 * The value true means to enable it, and the value false means to disable it. 367 * @param userId Indicates the user id. 368 * @return Returns true if the ability is enabled; returns false otherwise. 369 */ 370 bool SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, 371 int32_t userId = Constants::UNSPECIFIED_USERID); 372 /** 373 * @brief Obtains the icon of a specified ability through the proxy object. 374 * @param bundleName Indicates the bundle name. 375 * @param className Indicates the ability class name. 376 * @return Returns the icon resource string of the ability if exist; returns empty string otherwise. 377 */ 378 std::string GetAbilityIcon(const std::string &bundleName, const std::string &className) const; 379 /** 380 * @brief Register the bundle status callback function. 381 * @param bundleStatusCallback Indicates the callback object that using for notifing the bundle status. 382 * @return Returns true if this function is successfully called; returns false otherwise. 383 */ 384 bool RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback); 385 386 bool RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback); 387 388 void NotifyBundleEventCallback(const EventFwk::CommonEventData &eventData) const; 389 390 bool UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback); 391 /** 392 * @brief Clear the specific bundle status callback. 393 * @param bundleStatusCallback Indicates the callback to be cleared. 394 * @return Returns true if this function is successfully called; returns false otherwise. 395 */ 396 bool ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback); 397 /** 398 * @brief Unregister all the callbacks of status changed. 399 * @return Returns true if this function is successfully called; returns false otherwise. 400 */ 401 bool UnregisterBundleStatusCallback(); 402 /** 403 * @brief Notify when the installation, update, or uninstall state of an application changes. 404 * @param bundleName Indicates the name of the bundle whose state has changed. 405 * @param modulePackage Indicates the modulePackage name of the bundle whose state has changed. 406 * @param resultCode Indicates the status code returned for the application installation, update, or uninstall 407 * result. 408 * @param type Indicates the NotifyType object. 409 * @param uid Indicates the uid of the application. 410 * @return Returns true if this function is successfully called; returns false otherwise. 411 */ 412 bool NotifyBundleStatus(const std::string &bundleName, const std::string &modulePackage, 413 const std::string &mainAbility, const ErrCode resultCode, const NotifyType type, const int32_t &uid); 414 /** 415 * @brief Get a mutex for locking by bundle name. 416 * @param bundleName Indicates the bundle name. 417 * @return Returns a reference of mutex that for locing by bundle name. 418 */ 419 std::mutex &GetBundleMutex(const std::string &bundleName); 420 /** 421 * @brief Obtains the provision Id based on a given bundle name. 422 * @param bundleName Indicates the application bundle name to be queried. 423 * @param provisionId Indicates the provision Id to be returned. 424 * @return Returns true if the provision Id is successfully obtained; returns false otherwise. 425 */ 426 bool GetProvisionId(const std::string &bundleName, std::string &provisionId) const; 427 /** 428 * @brief Obtains the app feature based on a given bundle name. 429 * @param bundleName Indicates the application bundle name to be queried. 430 * @param provisionId Indicates the app feature to be returned. 431 * @return Returns true if the app feature is successfully obtained; returns false otherwise. 432 */ 433 bool GetAppFeature(const std::string &bundleName, std::string &appFeature) const; 434 /** 435 * @brief Set the flag that indicates whether initial user create successfully. 436 * @param flag Indicates the flag to be set. 437 * @return 438 */ 439 void SetInitialUserFlag(bool flag); 440 /** 441 * @brief Checks whether the publickeys of two bundles are the same. 442 * @param firstBundleName Indicates the first bundle name. 443 * @param secondBundleName Indicates the second bundle name. 444 * @return Returns SIGNATURE_UNKNOWN_BUNDLE if at least one of the given bundles is not found; 445 * returns SIGNATURE_NOT_MATCHED if their publickeys are different; 446 * returns SIGNATURE_MATCHED if their publickeys are the same. 447 */ 448 int CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName) const; 449 /** 450 * @brief Get a shared pointer to the IBundleDataStorage object. 451 * @return Returns the pointer of IBundleDataStorage object. 452 */ 453 std::shared_ptr<IBundleDataStorage> GetDataStorage() const; 454 /** 455 * @brief Obtains the FormInfo objects provided by all applications on the device. 456 * @param formInfos List of FormInfo objects if obtained; 457 * @return Returns true if this function is successfully called; returns false otherwise. 458 */ 459 bool GetAllFormsInfo(std::vector<FormInfo> &formInfos) const; 460 /** 461 * @brief Obtains the FormInfo objects provided by a specified application on the device. 462 * @param bundleName Indicates the bundle name of the application. 463 * @param formInfos List of FormInfo objects if obtained; 464 * @return Returns true if this function is successfully called; returns false otherwise. 465 */ 466 bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos) const; 467 /** 468 * @brief Obtains the FormInfo objects provided by a specified module name. 469 * @param formInfos List of FormInfo objects if obtained; 470 * @param moduleName Indicates the module name of the application. 471 * @param bundleName Indicates the bundle name of the application. 472 * @return Returns true if this function is successfully called; returns false otherwise. 473 */ 474 bool GetFormsInfoByModule( 475 const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos) const; 476 /** 477 * @brief Obtains the ShortcutInfo objects provided by a specified application on the device. 478 * @param bundleName Indicates the bundle name of the application. 479 * @param userId Indicates the user ID. 480 * @param shortcutInfos List of ShortcutInfo objects if obtained. 481 * @return Returns true if this function is successfully called; returns false otherwise. 482 */ 483 bool GetShortcutInfos( 484 const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos) const; 485 /** 486 * @brief Obtains the CommonEventInfo objects provided by an event key on the device. 487 * @param eventKey Indicates the event of the subscribe. 488 * @param commonEventInfos List of CommonEventInfo objects if obtained. 489 * @return Returns true if this function is successfully called; returns false otherwise. 490 */ 491 bool GetAllCommonEventInfo(const std::string &eventKey, std::vector<CommonEventInfo> &commonEventInfos) const; 492 /** 493 * @brief Notify a specified ability for ability. 494 * @param bundleName Indicates the bundle name of the ability to ability. 495 * @param abilityName Indicates the name of the ability to ability. 496 * @param launchTime Indicates the ability launchTime. 497 * @param uid Indicates the uid. 498 * @return Returns true if this function is successfully called; returns false otherwise. 499 */ 500 bool NotifyAbilityLifeStatus( 501 const std::string &bundleName, const std::string &abilityName, const int64_t launchTime, const int uid) const; 502 /** 503 * @brief Query ModuleUsageRecord objects ordered by lastLaunchTime desc 504 * @param maxNum Indicates the max number ShortcutInfo objects to get. 505 * @param records List of ModuleUsageRecord objects if obtained. 506 * @return Returns true if this function is successfully called; returns false otherwise. 507 */ 508 bool GetUsageRecords(int32_t maxNum, std::vector<ModuleUsageRecord> &records); 509 /** 510 * @brief Registers a callback for listening for permission changes of all UIDs. 511 * @param callback Indicates the callback method to register. 512 * @return Returns true if this function is successfully called; returns false otherwise. 513 */ 514 bool RegisterAllPermissionsChanged(const sptr<OnPermissionChangedCallback> &callback); 515 /** 516 * @brief Registers a callback for listening for permission changes of specified UIDs. 517 * @param uids Indicates the list of UIDs whose permission changes will be monitored. 518 * @param callback Indicates the callback method to register. 519 * @return Returns true if this function is successfully called; returns false otherwise. 520 */ 521 bool RegisterPermissionsChanged(const std::vector<int> &uids, const sptr<OnPermissionChangedCallback> &callback); 522 /** 523 * @brief Add death recipient for specified callback registerd. 524 * @param callback Indicates the callback for death recipient. 525 * @return Returns true if this function is successfully called; returns false otherwise. 526 */ 527 bool AddDeathRecipient(const sptr<OnPermissionChangedCallback> &callback); 528 /** 529 * @brief Unregisters a specified callback for listening for permission changes. 530 * @param callback Indicates the callback method to unregister. 531 * @return Returns true if this function is successfully called; returns false otherwise. 532 */ 533 bool UnregisterPermissionsChanged(const sptr<OnPermissionChangedCallback> &callback); 534 /** 535 * @brief Call callback for listening the uid permission changes. 536 * @param uid Indicates the bundle uid whose permission changes. 537 * @return Returns true if this function is successfully called; returns false otherwise. 538 */ 539 bool NotifyPermissionsChanged(int32_t uid); 540 /** 541 * @brief Update bundle usage record on bundle removed. 542 * @param keepUsage Indicates the flag record is remove on bundle removed. 543 * @param userId Indicates the user Id of the application. 544 * @param bundleName Indicates the bundle name of the application. 545 * @return Returns true if this function is successfully called; returns false otherwise. 546 */ 547 bool UpdateUsageRecordOnBundleRemoved(bool keepUsage, const int userId, const std::string &bundleName) const; 548 /** 549 * @brief Update bundle usage record on module removed. 550 * @param keepUsage Indicates the flag record is remove on module removed. 551 * @param userId Indicates the user Id of the application. 552 * @param bundleName Indicates the bundle name of the application. 553 * @param moduleName Indicates the module name of the application. 554 * @return Returns true if this function is successfully called; returns false otherwise. 555 */ 556 bool UpdateUsageRecordOnModuleRemoved( 557 bool keepUsage, const int userId, const std::string &bundleName, const std::string &moduleName) const; 558 /** 559 * @brief Remove cloned bundle info. 560 * @param bundleName Indicates the bundle name of remove cloned bundle. 561 * @return Returns true if this function is successfully called; returns false otherwise. 562 */ 563 bool RemoveClonedBundleInfo(const std::string &bundleName); 564 /** 565 * @brief Get cloned bundle's name. 566 * @param bundleName Indicates the bundle name of the ontology bundle name. 567 * @param newName Indicates the cloned name to be return. 568 * @return Returns true if this function is successfully called; returns false otherwise. 569 */ 570 bool GetClonedBundleName(const std::string &bundleName, std::string &newName); 571 /** 572 * @brief Obtains the PreInstallBundleInfo objects provided by bundleName. 573 * @param bundleName Indicates the bundle name of the application. 574 * @param preInstallBundleInfo Indicates information about the PreInstallBundleInfo. 575 * @return Returns true if this function is successfully called; returns false otherwise. 576 */ 577 bool GetPreInstallBundleInfo(const std::string &bundleName, PreInstallBundleInfo &preInstallBundleInfo); 578 /** 579 * @brief Save new PreInstallBundleInfo. 580 * @param bundleName Indicates the bundle name. 581 * @param preInstallBundleInfo Indicates the PreInstallBundleInfo object to be save. 582 * @return Returns true if this function is successfully called; returns false otherwise. 583 */ 584 bool SavePreInstallBundleInfo(const std::string &bundleName, const PreInstallBundleInfo &preInstallBundleInfo); 585 /** 586 * @brief Obtains the PreInstallBundleInfo objects provided by bundleName. 587 * @param bundleName Indicates the bundle name of the application. 588 * @param preInstallBundleInfo Indicates information about the PreInstallBundleInfo. 589 * @return Returns true if this function is successfully called; returns false otherwise. 590 */ 591 bool LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> &preInstallBundleInfos); 592 /** 593 * @brief Save new PreInstallBundleInfo. 594 * @param bundleName Indicates the bundle name. 595 * @param preInstallBundleInfo Indicates the PreInstallBundleInfo object to be save. 596 * @return Returns true if this function is successfully called; returns false otherwise. 597 */ 598 bool DeletePreInstallBundleInfo( 599 const std::string &bundleName, const PreInstallBundleInfo &preInstallBundleInfo); 600 /** 601 * @brief Save installation mark to datebase storage. 602 * @param info Indicates the innerBundleInfo of the bundle which needs to save installation mark. 603 * @param isAppExisted Indicates whether the application exists in the database storage or not. 604 * @return Returns true if this function is successfully called; returns false otherwise. 605 */ 606 bool SaveInstallMark(const InnerBundleInfo &info, bool isAppExisted) const; 607 /** 608 * @brief GetInnerBundleUserInfoByUserId. 609 * @param bundleName Indicates the application bundle name to be queried. 610 * @param userId Indicates the user ID. 611 * @param innerBundleUserInfo Indicates the obtained InnerBundleUserInfo object. 612 * @return Returns true if the application is successfully obtained; returns false otherwise. 613 */ 614 bool GetInnerBundleUserInfoByUserId( 615 const std::string &bundleName, int32_t userId, InnerBundleUserInfo &innerBundleUserInfo) const; 616 /** 617 * @brief save all created users. 618 * @param userId Indicates the user ID. 619 */ 620 void AddUserId(int32_t userId); 621 /** 622 * @brief remove userId. 623 * @param userId Indicates the user ID. 624 */ 625 void RemoveUserId(int32_t userId); 626 /** 627 * @brief query users. 628 * @param userId Indicates the user ID. 629 * @return Returns true when query user success; returns false otherwise. 630 */ 631 bool HasUserId(int32_t userId) const; 632 /** 633 * @brief Get userId by calling uid. 634 * @return Returns userId. 635 */ 636 int32_t GetUserIdByCallingUid() const; 637 /** 638 * @brief Get all user. 639 * @return Returns all userId. 640 */ 641 std::set<int32_t> GetAllUser() const; 642 /** 643 * @brief Obtains the DistributedBundleInfo based on a given bundle name and networkId. 644 * @param networkId Indicates the networkId of remote device. 645 * @param userId Indicates the user id. 646 * @param bundleName Indicates the application bundle name to be queried. 647 * @param distributedBundleInfo Indicates the obtained DistributedBundleInfo object. 648 * @return Returns true if the DistributedBundleInfo is successfully obtained; returns false otherwise. 649 */ 650 bool GetDistributedBundleInfo( 651 const std::string &networkId, int32_t userId, const std::string &bundleName, 652 DistributedBundleInfo &distributedBundleInfo); 653 /** 654 * @brief Has initial user created. 655 * @return Returns initial user flag. 656 */ HasInitialUserCreated()657 bool HasInitialUserCreated() const 658 { 659 return initialUserFlag_; 660 } 661 /** 662 * @brief Set bundlePromise. 663 * @param bundlePromise Indicates the bundlePromise. 664 */ SetBundlePromise(const std::shared_ptr<BundlePromise> & bundlePromise)665 void SetBundlePromise(const std::shared_ptr<BundlePromise>& bundlePromise) 666 { 667 bundlePromise_ = bundlePromise; 668 } 669 /** 670 * @brief Get bundleUserInfos by bundleName. 671 * @param bundleName Indicates the application bundle name to be queried. 672 * @param innerBundleUserInfo Indicates the obtained InnerBundleUserInfo object. 673 * @return Returns true if the application is successfully obtained; returns false otherwise. 674 */ 675 bool GetInnerBundleUserInfos( 676 const std::string &bundleName, std::vector<InnerBundleUserInfo> &innerBundleUserInfos) const; 677 /** 678 * @brief Get app privilege level. 679 * @param bundleName Indicates the bundle name of the app privilege level. 680 * @param userId Indicates the user id. 681 * @return Returns app privilege level. 682 */ 683 std::string GetAppPrivilegeLevel( 684 const std::string &bundleName, int32_t userId = Constants::UNSPECIFIED_USERID); 685 /** 686 * @brief Query a ExtensionAbilityInfo of list by the given Want. 687 * @param want Indicates the information of the ability. 688 * @param flags Indicates the information contained in the AbilityInfo object to be returned. 689 * @param userId Indicates the user ID. 690 * @param extensionInfos Indicates the obtained ExtensionAbilityInfo of list. 691 * @return Returns true if the ExtensionAbilityInfo is successfully obtained; returns false otherwise. 692 */ 693 bool QueryExtensionAbilityInfos(const Want &want, int32_t flags, int32_t userId, 694 std::vector<ExtensionAbilityInfo> &extensionInfos) const; 695 696 bool QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId, 697 std::vector<ExtensionAbilityInfo> &extensionInfos) const; 698 699 std::vector<std::string> GetAccessibleAppCodePaths(int32_t userId) const; 700 701 bool QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId, 702 ExtensionAbilityInfo &extensionAbilityInfo) const; 703 704 void GetAllUriPrefix(std::vector<std::string> &uriPrefixList, int32_t userId, 705 const std::string &excludeModule = "") const; 706 707 bool AddInnerBundleUserInfo(const std::string &bundleName, const InnerBundleUserInfo& newUserInfo); 708 709 bool RemoveInnerBundleUserInfo(const std::string &bundleName, int32_t userId); 710 711 bool ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId, 712 AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo); 713 #ifdef SUPPORT_GRAPHICS 714 std::shared_ptr<Media::PixelMap> GetAbilityPixelMapIcon(const std::string &bundleName, 715 const std::string &abilityName) const; 716 #endif 717 private: 718 /** 719 * @brief Init transferStates. 720 * @return 721 */ 722 void InitStateTransferMap(); 723 /** 724 * @brief Determine whether to delete the data status. 725 * @param state Indicates the InstallState object. 726 * @return Returns true if state is INSTALL_FAIL,UNINSTALL_FAIL,UNINSTALL_SUCCESS,or UPDATING_FAIL; returns false 727 * otherwise. 728 */ 729 bool IsDeleteDataState(const InstallState state) const; 730 /** 731 * @brief Determine whether it is disable. 732 * @param state Indicates the InstallState object. 733 * @return Returns true if install state is UPDATING_START or UNINSTALL_START; returns false otherwise. 734 */ 735 bool IsDisableState(const InstallState state) const; 736 /** 737 * @brief Delete bundle info if InstallState is not INSTALL_FAIL. 738 * @param bundleName Indicates the bundle Names. 739 * @param state Indicates the InstallState object. 740 * @return Returns true if install state is UPDATING_START or UNINSTALL_START; returns false otherwise. 741 */ 742 void DeleteBundleInfo(const std::string &bundleName, const InstallState state); 743 /** 744 * @brief Determine whether app is installed. 745 * @param bundleName Indicates the bundle Names. 746 * @return Returns true if install state is INSTALL_SUCCESS; returns false otherwise. 747 */ 748 bool IsAppOrAbilityInstalled(const std::string &bundleName) const; 749 /** 750 * @brief Restore uid and gid . 751 * @return Returns true if this function is successfully called; returns false otherwise. 752 */ 753 bool RestoreUidAndGid(); 754 /** 755 * @brief Recycle uid and gid . 756 * @param info Indicates the InnerBundleInfo object. 757 */ 758 void RecycleUidAndGid(const InnerBundleInfo &info); 759 /** 760 * @brief Implicit query abilityInfos by the given Want. 761 * @param want Indicates the information of the ability. 762 * @param flags Indicates the information contained in the AbilityInfo object to be returned. 763 * @param userId Indicates the user ID. 764 * @param abilityInfos Indicates the obtained AbilityInfo of list. 765 * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. 766 */ 767 bool ImplicitQueryAbilityInfos( 768 const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const; 769 void GetMatchAbilityInfos(const Want &want, int32_t flags, 770 const InnerBundleInfo &info, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const; 771 bool ExplicitQueryAbilityInfo(const std::string &bundleName, const std::string &abilityName, 772 int32_t flags, int32_t userId, AbilityInfo &abilityInfo) const; 773 bool GetInnerBundleInfoWithFlags(const std::string &bundleName, const int32_t flags, 774 const std::string &deviceId, InnerBundleInfo &info, int32_t userId = Constants::UNSPECIFIED_USERID) const; 775 int32_t GetUserId(int32_t userId = Constants::UNSPECIFIED_USERID) const; 776 bool GenerateBundleId(const std::string &bundleName, int32_t &bundleId); 777 int32_t GetUserIdByUid(int32_t uid) const; 778 bool GetInnerBundleInfoByUid(const int uid, InnerBundleInfo &innerBundleInfo) const; 779 bool GetAllBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos) const; 780 bool ExplicitQueryExtensionInfo(const std::string &bundleName, const std::string &extensionName, int32_t flags, 781 int32_t userId, ExtensionAbilityInfo &extensionInfo) const; 782 bool ImplicitQueryExtensionInfos(const Want &want, int32_t flags, int32_t userId, 783 std::vector<ExtensionAbilityInfo> &extensionInfos) const; 784 void GetMatchExtensionInfos(const Want &want, int32_t flags, const int32_t &userId, const InnerBundleInfo &info, 785 std::vector<ExtensionAbilityInfo> &einfos) const; 786 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager( 787 const AppExecFwk::BundleInfo &bundleInfo) const; 788 #ifdef SUPPORT_GRAPHICS 789 std::shared_ptr<Media::PixelMap> LoadImageFile(const std::string &path) const; 790 #endif 791 792 private: 793 mutable std::mutex bundleInfoMutex_; 794 mutable std::mutex stateMutex_; 795 mutable std::mutex bundleIdMapMutex_; 796 mutable std::mutex callbackMutex_; 797 mutable std::mutex eventCallbackMutex_; 798 mutable std::shared_mutex bundleMutex_; 799 mutable std::mutex allPermissionsChangedLock_; 800 mutable std::mutex permissionsChangedLock_; 801 mutable std::mutex multiUserIdSetMutex_; 802 mutable std::mutex preInstallInfoMutex_; 803 bool initialUserFlag_ = false; 804 // using for locking by bundleName 805 std::unordered_map<std::string, std::mutex> bundleMutexMap_; 806 // using for generating bundleId 807 // key:bundleId 808 // value:bundleName 809 std::map<int32_t, std::string> bundleIdMap_; 810 // save all created users. 811 std::set<int32_t> multiUserIdsSet_; 812 // use vector because these functions using for IPC, the bundleName may duplicate 813 std::vector<sptr<IBundleStatusCallback>> callbackList_; 814 // common event callback 815 std::vector<sptr<IBundleEventCallback>> eventCallbackList_; 816 // all installed bundles 817 // key:bundleName 818 // value:deviceId-innerbundleinfo pair 819 std::map<std::string, std::map<std::string, InnerBundleInfo>> bundleInfos_; 820 // key:bundle name 821 std::map<std::string, InstallState> installStates_; 822 // current-status:previous-statue pair 823 std::multimap<InstallState, InstallState> transferStates_; 824 std::shared_ptr<IBundleDataStorage> dataStorage_; 825 std::shared_ptr<ModuleUsageRecordStorage> usageRecordStorage_; 826 std::shared_ptr<PreInstallDataStorage> preInstallDataStorage_; 827 std::shared_ptr<DistributedDataStorage> distributedDataStorage_; 828 std::set<sptr<OnPermissionChangedCallback>> allPermissionsCallbacks_; 829 std::vector<PreInstallBundleInfo> preInstallBundleInfos_; 830 // map<uid, callback>. 831 std::map<int32_t, std::set<sptr<OnPermissionChangedCallback>>> permissionsCallbacks_; 832 std::shared_ptr<BundlePromise> bundlePromise_ = nullptr; 833 }; 834 } // namespace AppExecFwk 835 } // namespace OHOS 836 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_DATA_MGR_H 837