1 /* 2 * Copyright (c) 2021-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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H 17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H 18 19 #include <list> 20 #include <unordered_set> 21 22 #include "bundle_constants.h" 23 #include "bundle_data_mgr.h" 24 #include "pre_scan_info.h" 25 26 namespace OHOS { 27 namespace AppExecFwk { 28 class BundleMgrService; 29 enum class ScanMode; 30 enum class ResultMode; 31 32 enum class ResultCode { 33 RECOVER_OK = 0, 34 REINSTALL_OK, 35 NO_INSTALLED_DATA, 36 SYSTEM_ERROR, 37 }; 38 39 enum class ScanResultCode { 40 SCAN_HAS_DATA_PARSE_SUCCESS, 41 SCAN_HAS_DATA_PARSE_FAILED, 42 SCAN_NO_DATA, 43 }; 44 45 class BMSEventHandler { 46 public: 47 BMSEventHandler(); 48 ~BMSEventHandler(); 49 /** 50 * @brief Get preInstall root dir list, 51 * which the catalog of production has higher priority. 52 * @param rootDirList Indicates the root dir list. 53 * @return 54 */ 55 static void GetPreInstallRootDirList(std::vector<std::string> &rootDirList); 56 /** 57 * @brief Load all preInstall infos from proFile. 58 * @return Returns true if get the preInstall list successfully; returns false otherwise. 59 */ 60 static bool LoadPreInstallProFile(); 61 /** 62 * @brief Clear all preInstall infos cache. 63 * @return 64 */ 65 static void ClearPreInstallCache(); 66 /** 67 * @brief Get the preInstall capability. 68 * @param preBundleConfigInfo Indicates the preBundleConfigInfo. 69 * @return Returns true if get the preInstall capability successfully; returns false otherwise. 70 */ 71 static bool GetPreInstallCapability(PreBundleConfigInfo &preBundleConfigInfo); 72 /** 73 * @brief Has preInstall profile or not. 74 * @return Returns result. 75 */ 76 static bool HasPreInstallProfile(); 77 /** 78 * @brief Bms start event. 79 * @return 80 */ 81 void BmsStartEvent(); 82 private: 83 /** 84 * @brief Before Bms start. 85 * @return 86 */ 87 void BeforeBmsStart(); 88 /** 89 * @brief On Bms starting. 90 * @return 91 */ 92 void OnBmsStarting(); 93 /** 94 * @brief After Bms start. 95 * @return 96 */ 97 void AfterBmsStart(); 98 /** 99 * @brief Load install infos from db. 100 * @return Returns true if load successfully; returns false otherwise. 101 */ 102 bool LoadInstallInfosFromDb(); 103 /** 104 * @brief Guard against install infos lossed strategy. 105 * @return Returns ResultCode for recover install infos. 106 */ 107 ResultCode GuardAgainstInstallInfosLossedStrategy(); 108 /** 109 * @brief Scan and analyze install infos. 110 * @param installInfos Indicates the install infos. 111 * @return 112 */ 113 void ScanAndAnalyzeInstallInfos( 114 std::map<std::string, std::vector<InnerBundleInfo>> &installInfos); 115 /** 116 * @brief Scan and analyze common install dir. 117 * @param installInfos Indicates the install infos. 118 * @return 119 */ 120 void ScanInstallDir( 121 std::map<std::string, std::vector<std::string>> &hapPathsMap); 122 /** 123 * @brief Get preInstall haps. 124 * @param bundleDirs Indicates preInstall hapPath. 125 * @return 126 */ 127 void GetPreInstallDir(std::vector<std::string> &bundleDirs); 128 /** 129 * @brief Analyze hap to InnerBundleInfo. 130 * @param isPreInstallApp Indicates is preInstallApp or not. 131 * @param hapPathsMap Indicates the hapPathsMap which will be analyzed. 132 * @param installInfos Indicates the install infos. 133 * @return 134 */ 135 void AnalyzeHaps( 136 bool isPreInstallApp, 137 const std::map<std::string, std::vector<std::string>> &hapPathsMap, 138 std::map<std::string, std::vector<InnerBundleInfo>> &installInfos); 139 /** 140 * @brief Analyze hap to InnerBundleInfo. 141 * @param isPreInstallApp Indicates is preInstallApp or not. 142 * @param bundleDirs Indicates the bundleDirs which will be analyzed. 143 * @param installInfos Indicates the install infos. 144 * @return 145 */ 146 void AnalyzeHaps( 147 bool isPreInstallApp, 148 const std::vector<std::string> &bundleDirs, 149 std::map<std::string, std::vector<InnerBundleInfo>> &installInfos); 150 /** 151 * @brief Get preBundle install dir. 152 * @param bundleDirs Indicates the bundleDirs. 153 * @return 154 */ 155 void GetPreBundleDir(std::list<std::string> &bundleDirs); 156 /** 157 * @brief Check scaned hapPath whether end with .hap. 158 * @param hapPaths Indicates the hapPaths. 159 * @return Returns the checked hapPaths. 160 */ 161 std::vector<std::string> CheckHapPaths(const std::vector<std::string> &hapPaths); 162 /** 163 * @brief Collect install infos from parse result. 164 * @param hapInfos Indicates the parse result. 165 * @param installInfos Indicates the saved installInfos. 166 * @return. 167 */ 168 void CollectInstallInfos( 169 const std::unordered_map<std::string, InnerBundleInfo> &hapInfos, 170 std::map<std::string, std::vector<InnerBundleInfo>> &installInfos); 171 /** 172 * @brief Scan and analyze userDatas. 173 * @param userMaps Indicates the userMaps to save userInfo. 174 * @return Returns ScanResultCode if Scan and analyze infos successfully; returns false otherwise. 175 */ 176 ScanResultCode ScanAndAnalyzeUserDatas( 177 std::map<std::string, std::vector<InnerBundleUserInfo>> &userMaps); 178 /** 179 * @brief Analyze userDatas. 180 * @param userId Indicates the userId. 181 * @param userDataDir Indicates the userDataDir. 182 * @param userDataBundleName Indicates the userDataBundleName. 183 * @param userMaps Indicates the userMaps to save userInfo. 184 * @return Returns true if analyze infos successfully; returns false otherwise. 185 */ 186 bool AnalyzeUserData( 187 int32_t userId, const std::string &userDataDir, const std::string &userDataBundleName, 188 std::map<std::string, std::vector<InnerBundleUserInfo>> &userMaps); 189 /** 190 * @brief ReInstall all Apps from installDir. 191 * @return Returns the ResultCode indicates the result of this action. 192 */ 193 ResultCode ReInstallAllInstallDirApps(); 194 /** 195 * @brief Combine install infos and userInfos. 196 * @param installInfos Indicates the installInfos. 197 * @param userInfoMaps Indicates the userInfoMaps. 198 * @return Returns true if combine infos successfully; returns false otherwise. 199 */ 200 bool CombineBundleInfoAndUserInfo( 201 const std::map<std::string, std::vector<InnerBundleInfo>> &installInfos, 202 const std::map<std::string, std::vector<InnerBundleUserInfo>> &userInfoMaps); 203 /** 204 * @brief Save recover info to cache. 205 * @param info Indicates the InnerBundleInfo. 206 * @return 207 */ 208 void SaveInstallInfoToCache(InnerBundleInfo &info); 209 /** 210 * @brief Scan dir by scanMode and resultMode, this function will perform 211 * scan through installd because installd has higher permissions. 212 * @param scanMode Indicates the scanMode, 213 * which maybe SUB_FILE_ALL SUB_FILE_DIR or SUB_FILE_FILE. 214 * @param resultMode Indicates the resultMode, 215 * which maybe ABSOLUTE_PATH or RELATIVE_PATH. 216 * @param resultList Indicates the scan resultList. 217 * @return Returns true if Scan successfully; returns false otherwise. 218 */ 219 bool ScanDir(const std::string& dir, ScanMode scanMode, 220 ResultMode resultMode, std::vector<std::string> &resultList); 221 /** 222 * @brief Bundle boot start event. 223 * @return 224 */ 225 void BundleBootStartEvent(); 226 /** 227 * @brief Bundle reboot start event. 228 * @return 229 */ 230 void BundleRebootStartEvent(); 231 /** 232 * @brief start boot scan. 233 * @param userId Indicates the userId. 234 * @return 235 */ 236 void OnBundleBootStart(int32_t userId = Constants::UNSPECIFIED_USERID); 237 /** 238 * @brief Process boot bundle install from scan. 239 * @param userId Indicates the userId. 240 * @return 241 */ 242 void ProcessBootBundleInstallFromScan(int32_t userId); 243 /** 244 * @brief Process bundle install by scanInfos. 245 * @param userId Indicates the userId. 246 * @return 247 */ 248 void InnerProcessBootPreBundleProFileInstall(int32_t userId); 249 /** 250 * @brief Install bundles by scanDir. 251 * @param scanDir Indicates the scanDir. 252 * @param appType Indicates the bundle type. 253 * @param userId Indicates userId. 254 * @return 255 */ 256 void ProcessSystemBundleInstall( 257 const std::string &scanDir, 258 Constants::AppType appType, 259 int32_t userId = Constants::UNSPECIFIED_USERID); 260 /** 261 * @brief Install bundles by preScanInfo. 262 * @param preScanInfo Indicates the preScanInfo. 263 * @param appType Indicates the bundle type. 264 * @param userId Indicates userId. 265 * @return 266 */ 267 void ProcessSystemBundleInstall( 268 const PreScanInfo &preScanInfo, 269 Constants::AppType appType, 270 int32_t userId = Constants::UNSPECIFIED_USERID); 271 /** 272 * @brief Install system shared bundle. 273 * @param sharedBundlePath Indicates the path of shared bundle. 274 * @param appType Indicates the bundle type. 275 * @return 276 */ 277 void ProcessSystemSharedBundleInstall(const std::string &sharedBundlePath, Constants::AppType appType); 278 /** 279 * @brief start reboot scan. 280 * @return 281 */ 282 void OnBundleRebootStart(); 283 /** 284 * @brief Process reboot bundle. 285 * @return 286 */ 287 void ProcessRebootBundle(); 288 /** 289 * @brief Obtains the PreInstallBundleInfo objects. 290 * @return Returns true if this function is successfully called; returns false otherwise. 291 */ 292 bool LoadAllPreInstallBundleInfos(); 293 /** 294 * @brief Process reboot bundle install. 295 * @return 296 */ 297 void ProcessRebootBundleInstall(); 298 /** 299 * @brief Process reboot bundle install by scanInfos. 300 * @return 301 */ 302 void ProcessReBootPreBundleProFileInstall(); 303 /** 304 * @brief Process reboot bundle install from scan. 305 * @return 306 */ 307 void ProcessRebootBundleInstallFromScan(); 308 /** 309 * @brief Process reboot install bundles by bundleList. 310 * @param bundleList Indicates store bundle list. 311 * @param appType Indicates the bundle type. 312 * @return 313 */ 314 void InnerProcessRebootBundleInstall( 315 const std::list<std::string> &bundleList, Constants::AppType appType); 316 /** 317 * @brief Process reboot install shared bundles by bundleList. 318 * @param bundleList Indicates store bundle list. 319 * @param appType Indicates the bundle type. 320 * @return 321 */ 322 void InnerProcessRebootSharedBundleInstall(const std::list<std::string> &bundleList, Constants::AppType appType); 323 /** 324 * @brief Reboot uninstall system and system vendor bundles. 325 * @return 326 */ 327 void ProcessRebootBundleUninstall(); 328 /** 329 * @brief Get bundle dir by scan. 330 * @param bundleDirs Indicates the return bundleDirs. 331 * @return 332 */ 333 void GetBundleDirFromScan(std::list<std::string> &bundleDirs); 334 /** 335 * @brief Process scan dir. 336 * @param dir Indicates the dir. 337 * @param bundleDirs Indicates the return bundleDirs. 338 * @return 339 */ 340 void ProcessScanDir(const std::string &dir, std::list<std::string> &bundleDirs); 341 /** 342 * @brief Process parse pre bundle profile. 343 * @param dir Indicates the dir. 344 * @return 345 */ 346 static void ParsePreBundleProFile(const std::string &dir); 347 /** 348 * @brief Set the flag indicates that all system and vendor applications installed. 349 * @return 350 */ 351 void SetAllInstallFlag() const; 352 /** 353 * @brief Check and parse hap. 354 * @param hapFilePath Indicates the absolute file path of the HAP. 355 * @param isPreInstallApp Indicates the hap is preInstallApp or not. 356 * @param infos Indicates the obtained BundleInfo object. 357 * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. 358 */ 359 bool CheckAndParseHapFiles(const std::string &hapFilePath, 360 bool isPreInstallApp, std::unordered_map<std::string, InnerBundleInfo> &infos); 361 /** 362 * @brief Parse hap. 363 * @param hapFilePath Indicates the absolute file path of the HAP. 364 * @param infos Indicates the obtained BundleInfo object. 365 * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. 366 */ 367 bool ParseHapFiles( 368 const std::string &hapFilePath, 369 std::unordered_map<std::string, InnerBundleInfo> &infos); 370 /** 371 * @brief OTA Install system app and system vendor bundles. 372 * @param filePaths Indicates the filePaths. 373 * @param appType Indicates the bundle type. 374 * @param removable Indicates whether it can be removed. 375 * @return Returns true if this function called successfully; returns false otherwise. 376 */ 377 bool OTAInstallSystemBundle( 378 const std::vector<std::string> &filePaths, 379 Constants::AppType appType, 380 bool removable); 381 /** 382 * @brief OTA Install system app and system vendor shared bundles. 383 * @param filePaths Indicates the filePaths. 384 * @param appType Indicates the bundle type. 385 * @param removable Indicates whether it can be removed. 386 * @return Returns true if this function called successfully; returns false otherwise. 387 */ 388 bool OTAInstallSystemSharedBundle( 389 const std::vector<std::string> &filePaths, 390 Constants::AppType appType, 391 bool removable); 392 /** 393 * @brief Used to determine whether the module has been installed. If the installation has 394 * been uninstalled, OTA install and upgrade will not be allowed. 395 * @param bundleName Indicates the bundleName. 396 * @param bundlePath Indicates the bundlePath. 397 * @return Returns true if this function called successfully; returns false otherwise. 398 */ 399 bool HasModuleSavedInPreInstalledDb( 400 const std::string &bundleName, const std::string &bundlePath); 401 /** 402 * @brief Delete preInstallInfo to Db. 403 * @param bundleName Indicates the bundleName. 404 * @param bundlePath Indicates the bundlePath. 405 */ 406 void DeletePreInfoInDb( 407 const std::string &bundleName, const std::string &bundlePath, bool bundleLevel); 408 /** 409 * @brief Add parseInfos to map. 410 * @param bundleName Indicates the bundleName. 411 * @param infos Indicates the infos. 412 */ 413 void AddParseInfosToMap(const std::string &bundleName, 414 const std::unordered_map<std::string, InnerBundleInfo> &infos); 415 /** 416 * @brief Clear cache. 417 */ 418 void ClearCache(); 419 /** 420 * @brief Judge whether the preInstall app can be removable. 421 * @param path Indicates the path. 422 * @return Returns true if the preInstall is removable; returns false otherwise. 423 */ 424 bool IsPreInstallRemovable(const std::string &path); 425 /** 426 * @brief Ota upgrade scenario, uninstall the hap application updated by the app hot patch and retain the data. 427 * @param bundleName Indicates the bundleName. 428 * @return Returns true if this function called successfully; returns false otherwise. 429 */ 430 bool HotPatchAppProcessing(const std::string &bundleName); 431 /** 432 * @brief Judge whether hot patch application. 433 * @param bundleName Indicates the bundleName. 434 * @return Returns true if called successfully; returns false code otherwise. 435 */ 436 bool IsHotPatchApp(const std::string &bundleName); 437 438 bool IsSystemUpgrade(); 439 bool IsTestSystemUpgrade(); 440 bool IsSystemFingerprintChanged(); 441 std::string GetCurSystemFingerprint(); 442 std::string GetOldSystemFingerprint(); 443 bool GetSystemParameter(const std::string &key, std::string &value); 444 void SaveSystemFingerprint(); 445 446 bool FetchInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &innerBundleInfo); 447 void GetPreInstallDirFromLoadProFile(std::vector<std::string> &bundleDirs); 448 void GetPreInstallDirFromScan(std::vector<std::string> &bundleDirs); 449 450 void AddStockAppProvisionInfoByOTA(const std::string &bundleName, const std::string &filePath); 451 void UpdateAppDataSelinuxLabel(const std::string &bundleName, const std::string &apl, 452 bool isPreInstall, bool debug); 453 #ifdef USE_PRE_BUNDLE_PROFILE 454 void UpdateRemovable(const std::string &bundleName, bool removable); 455 void UpdateAllPrivilegeCapability(); 456 void UpdatePrivilegeCapability(const PreBundleConfigInfo &preBundleConfigInfo); 457 bool MatchSignature(const PreBundleConfigInfo &configInfo, const std::string &signature); 458 void UpdateTrustedPrivilegeCapability(const PreBundleConfigInfo &preBundleConfigInfo); 459 #endif 460 void ListeningUserUnlocked() const; 461 void RemoveUnreservedSandbox() const; 462 void StartBmsExtensionService() const; 463 void HandleSceneBoard() const; 464 void InnerProcessStockBundleProvisionInfo(); 465 void ProcessBundleProvisionInfo(const std::unordered_set<std::string> &allBundleNames); 466 void ProcessSharedBundleProvisionInfo(const std::unordered_set<std::string> &allBundleNames); 467 bool UpdateModuleByHash(const BundleInfo &oldBundleInfo, const InnerBundleInfo &newInfo) const; 468 bool IsNeedToUpdateSharedAppByHash(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const; 469 // Used to save the information parsed by Hap in the scanned directory. 470 std::map<std::string, std::unordered_map<std::string, InnerBundleInfo>> hapParseInfoMap_; 471 // Used to save application information that already exists in the Db. 472 std::map<std::string, PreInstallBundleInfo> loadExistData_; 473 // Used to mark Whether trigger OTA check 474 bool needRebootOta_ = false; 475 // Used to notify bundle scan status 476 bool needNotifyBundleScanStatus_ = false; 477 478 bool hasLoadAllPreInstallBundleInfosFromDb_ = false; 479 }; 480 } // namespace AppExecFwk 481 } // namespace OHOS 482 #endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_MGR_SERVICE_EVENT_HANDLER_H 483