1 /* 2 * Copyright (c) 2022-2024 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_FILEMGMT_BACKUP_SERVICE_H 17 #define OHOS_FILEMGMT_BACKUP_SERVICE_H 18 19 #include <cstdint> 20 #include <mutex> 21 22 #include "b_jsonutil/b_jsonutil.h" 23 #include "b_json/b_json_clear_data_config.h" 24 #include "b_json/b_json_entity_caps.h" 25 #include "b_json/b_json_service_disposal_config.h" 26 #include "i_service_reverse.h" 27 #include "iremote_stub.h" 28 #include "module_sched/sched_scheduler.h" 29 #include "service_stub.h" 30 #include "svc_session_manager.h" 31 #include "system_ability.h" 32 #include "thread_pool.h" 33 34 namespace OHOS::FileManagement::Backup { 35 struct ExtensionMutexInfo { 36 std::string bundleName; 37 std::mutex callbackMutex; ExtensionMutexInfoOHOS::FileManagement::Backup::ExtensionMutexInfo38 ExtensionMutexInfo(std::string bundleName_) : bundleName(bundleName_) {}; 39 }; 40 41 class Service : public SystemAbility, public ServiceStub, protected NoCopyable { 42 DECLARE_SYSTEM_ABILITY(Service); 43 44 // 以下都是IPC接口 45 public: 46 ErrCode InitRestoreSession(sptr<IServiceReverse> remote) override; 47 ErrCode InitBackupSession(sptr<IServiceReverse> remote) override; 48 ErrCode Start() override; 49 UniqueFd GetLocalCapabilities() override; 50 ErrCode PublishFile(const BFileInfo &fileInfo) override; 51 ErrCode AppFileReady(const std::string &fileName, UniqueFd fd, int32_t errCode) override; 52 ErrCode AppDone(ErrCode errCode) override; 53 ErrCode ServiceResultReport(const std::string restoreRetInfo, 54 BackupRestoreScenario sennario, ErrCode errCode) override; 55 ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName) override; 56 ErrCode AppendBundlesRestoreSession(UniqueFd fd, 57 const std::vector<BundleName> &bundleNames, 58 const std::vector<std::string> &bundleInfos, 59 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, 60 int32_t userId = DEFAULT_INVAL_VALUE) override; 61 ErrCode AppendBundlesRestoreSession(UniqueFd fd, 62 const std::vector<BundleName> &bundleNames, 63 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, 64 int32_t userId = DEFAULT_INVAL_VALUE) override; 65 ErrCode AppendBundlesBackupSession(const std::vector<BundleName> &bundleNames) override; 66 ErrCode AppendBundlesDetailsBackupSession(const std::vector<BundleName> &bundleNames, 67 const std::vector<std::string> &bundleInfos) override; 68 ErrCode Finish() override; 69 ErrCode Release() override; 70 71 UniqueFd GetLocalCapabilitiesIncremental(const std::vector<BIncrementalData> &bundleNames) override; 72 ErrCode GetAppLocalListAndDoIncrementalBackup() override; 73 ErrCode InitIncrementalBackupSession(sptr<IServiceReverse> remote) override; 74 ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup) override; 75 ErrCode AppendBundlesIncrementalBackupSession(const std::vector<BIncrementalData> &bundlesToBackup, 76 const std::vector<std::string> &infos) override; 77 78 ErrCode PublishIncrementalFile(const BFileInfo &fileInfo) override; 79 ErrCode PublishSAIncrementalFile(const BFileInfo &fileInfo, UniqueFd fd) override; 80 ErrCode AppIncrementalFileReady(const std::string &fileName, UniqueFd fd, UniqueFd manifestFd, 81 int32_t errCode) override; 82 ErrCode AppIncrementalDone(ErrCode errCode) override; 83 ErrCode GetIncrementalFileHandle(const std::string &bundleName, const std::string &fileName) override; 84 ErrCode GetBackupInfo(BundleName &bundleName, std::string &result) override; 85 ErrCode UpdateTimer(BundleName &bundleName, uint32_t timeout, bool &result) override; 86 ErrCode UpdateSendRate(std::string &bundleName, int32_t sendRate, bool &result) override; 87 ErrCode ReportAppProcessInfo(const std::string processInfo, const BackupRestoreScenario sennario) override; 88 ErrCode StartExtTimer(bool &isExtStart) override; 89 ErrCode StartFwkTimer(bool &isFwkStart) override; 90 91 ErrCode SAResultReport(const std::string bundleName, const std::string resultInfo, 92 const ErrCode errCode, const BackupRestoreScenario sennario); 93 void StartGetFdTask(std::string bundleName, wptr<Service> ptr); 94 95 // 以下都是非IPC接口 96 public: 97 void OnStart() override; 98 void OnStop() override; 99 void StopAll(const wptr<IRemoteObject> &obj, bool force = false); 100 int Dump(int fd, const std::vector<std::u16string> &args) override; 101 102 /** 103 * @brief 执行启动 backup extension 104 * 105 * @param bundleName 106 * @return ErrCode 107 */ 108 virtual ErrCode LaunchBackupExtension(const BundleName &bundleName); 109 110 /** 111 * @brief 执行启动 backup sa extension 112 * 113 * @param bundleName 114 * @return ErrCode 115 */ 116 ErrCode LaunchBackupSAExtension(const BundleName &bundleName); 117 118 /** 119 * @brief backup extension died 120 * 121 * @param bundleName 应用名称 122 */ 123 void OnBackupExtensionDied(const std::string &&bundleName, bool isSecondCalled = false); 124 125 /** 126 * @brief extension启动连接成功 127 * 128 * @param bundleName 应用名称 129 */ 130 void ExtConnectDone(std::string bundleName); 131 132 /** 133 * @brief extension启动连接失败 134 * 135 * @param bundleName 应用名称 136 */ 137 void ExtConnectFailed(const std::string &bundleName, ErrCode ret); 138 139 /** 140 * @brief 执行backup extension 备份恢复流程 141 * 142 * @param bundleName 应用名称 143 */ 144 virtual void ExtStart(const std::string &bundleName); 145 146 /** 147 * @brief 备份恢复开始,设置处置位 148 * 149 * @param bundleName 应用名称 150 * 151 */ 152 void SendStartAppGalleryNotify(const std::string &bundleName); 153 154 /** 155 * @brief 备份恢复结束,清理处置位 156 * 157 * @param bundleName 应用名称 158 * 159 */ 160 void SendEndAppGalleryNotify(const std::string &bundleName); 161 162 /** 163 * @brief 备份恢复异常结束,清理处置位 164 * 165 */ 166 void SendErrAppGalleryNotify(); 167 168 /** 169 * @brief SA开始时,清理配置文件中的处置位 170 * 171 */ 172 void ClearDisposalOnSaStart(); 173 174 /** 175 * @brief 备份恢复全部结束,删除配置文件 176 * 177 * 178 */ 179 void DeleteDisConfigFile(); 180 181 /** 182 * @brief 尝试清理处置 183 * 184 * @param bundleName 应用名称 185 * 186 */ 187 void TryToClearDispose(const BundleName &bundleName); 188 189 /** 190 * @brief 结束会话删除session,卸载服务 191 * 192 */ 193 void SessionDeactive(); 194 195 /** 196 * @brief 构造拉起应用所需的want 197 * 198 * @param bundleName 应用名称 199 * 200 */ 201 AAFwk::Want CreateConnectWant (BundleName &bundleName); 202 203 /** 204 * @brief SA backup回调 205 * 206 * @param bundleName 应用名称 207 * @param fd 备份数据 208 * @param result SA备份的结果(异常) 209 * @param errCode backup的错误 210 * 211 */ 212 void OnSABackup(const std::string &bundleName, const int &fd, const std::string &result, const ErrCode &errCode); 213 214 /** 215 * @brief SA restore回调 216 * 217 * @param bundleName 应用名称 218 * @param result SA恢复的结果(异常) 219 * @param errCode restore的错误 220 * 221 */ 222 void OnSARestore(const std::string &bundleName, const std::string &result, const ErrCode &errCode); 223 224 /** 225 * @brief GetBackupInfo extension成功回调 226 * 227 * @param obj 当前对象 228 * @param bundleName 应用名称 229 * 230 */ 231 std::function<void(const std::string &&)> GetBackupInfoConnectDone(wptr<Service> obj, std::string &bundleName); 232 233 /** 234 * @brief GetBackupInfo extension死亡回调 235 * 236 * @param obj 当前对象 237 * @param bundleName 应用名称 238 * 239 */ 240 std::function<void(const std::string &&, bool)> GetBackupInfoConnectDied( 241 wptr<Service> obj, std::string &bundleName); 242 243 /** 244 * @brief timeout callback 245 * 246 * @param ptr 当前对象 247 * @param bundleName 应用名称 248 */ 249 std::function<void()> TimeOutCallback(wptr<Service> ptr, std::string bundleName); 250 251 /** 252 * @brief do timeout 253 * 254 * @param ptr 当前对象 255 * @param bundleName 应用名称 256 */ 257 void DoTimeout(wptr<Service> ptr, std::string bundleName); 258 259 /** 260 * @brief 清理残留数据 261 * 262 * @param bundleName 应用名称 263 * 264 */ 265 void ClearResidualBundleData(const std::string &bundleName); 266 267 /** 268 * @brief 添加清理记录 269 * 270 * @param bundleName 应用名称 271 * 272 */ 273 void AddClearBundleRecord(const std::string &bundleName); 274 275 /** 276 * @brief 删除清理记录 277 * 278 * @param bundleName 应用名称 279 * 280 */ 281 void DelClearBundleRecord(const std::vector<std::string> &bundleNames); 282 283 /** 284 * @brief 获取extension锁 285 * 286 * @param bundleName 应用名称 287 * 288 */ 289 std::shared_ptr<ExtensionMutexInfo> GetExtensionMutex(const BundleName &bundleName); 290 291 /** 292 * @brief 清理extension锁 293 * 294 * @param bundleName 应用名称 295 * 296 */ 297 void RemoveExtensionMutex(const BundleName &bundleName); 298 public: Service(int32_t saID, bool runOnCreate = false)299 explicit Service(int32_t saID, bool runOnCreate = false) : SystemAbility(saID, runOnCreate) 300 { 301 threadPool_.Start(BConstants::EXTENSION_THREAD_POOL_COUNT); 302 session_ = sptr<SvcSessionManager>(new SvcSessionManager(wptr(this))); 303 disposal_ = make_shared<BJsonDisposalConfig>(); 304 clearRecorder_ = make_shared<BJsonClearDataConfig>(); 305 sched_ = sptr(new SchedScheduler(wptr(this), wptr(session_))); 306 }; 307 ~Service() override 308 { 309 threadPool_.Stop(); 310 }; 311 312 private: 313 /** 314 * @brief 验证调用者 315 * 316 */ 317 void VerifyCaller(); 318 319 /** 320 * @brief 验证调用者 321 * 322 * @param scenario Scenario状态 323 */ 324 void VerifyCaller(IServiceReverse::Scenario scenario); 325 326 /** 327 * @brief 验证调用者并返回名称 328 * 329 * @return std::string 330 */ 331 std::string VerifyCallerAndGetCallerName(); 332 333 /** 334 * @brief 清除Session Sched相关资源 335 * 336 * @param bundleName 应用名称 337 */ 338 void ClearSessionAndSchedInfo(const std::string &bundleName); 339 340 /** 341 * @brief 整个备份恢复流程结束 342 * 343 * @param errCode 错误码 344 */ 345 void OnAllBundlesFinished(ErrCode errCode); 346 347 /** 348 * @brief 执行调度器 349 * 350 */ 351 void OnStartSched(); 352 353 /** 354 * @brief 通知客户端程序扩展能力处理结果 355 * 356 * @param bundleName 应用名称 357 * 358 */ 359 void NoticeClientFinish(const std::string &bundleName, ErrCode errCode); 360 361 /** 362 * @brief 处理有依赖的应用恢复 363 * 364 * @param bundleName 应用名称 365 * 366 */ 367 void HandleRestoreDepsBundle(const std::string &bundleName); 368 369 /** 370 * @brief 增量备份恢复逻辑处理 371 * 372 * @param bundleName 373 * @return true 374 * @return false 375 */ 376 bool IncrementalBackup(const std::string &bundleName); 377 378 /** 379 * @brief extension连接断开 380 * 381 * @param bundleName 382 */ 383 void ExtConnectDied(const std::string &bundleName); 384 385 /** 386 * @brief 设置当前session的关键信息 387 * 388 * @param restoreBundleInfos 待恢复的应用 389 * @param restoreBundleNames 待恢复的应用包信息 390 * @param bundleNameDetailMap bundle和detail的对应关系 391 * @param isClearDataFlags 清理数据标志集合 392 * @param restoreType 任务类型 393 */ 394 void SetCurrentSessProperties(std::vector<BJsonEntityCaps::BundleInfo> &restoreBundleInfos, 395 std::vector<std::string> &restoreBundleNames, 396 std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap, 397 std::map<std::string, bool> &isClearDataFlags, RestoreTypeEnum restoreType); 398 399 /** 400 * @brief set session info 401 * 402 * @param restoreBundleInfos: bundles to be restored 403 * @param restoreBundleNames: bundles info to be restored 404 * @param restoreType: retore type 405 */ 406 void SetCurrentSessProperties(std::vector<BJsonEntityCaps::BundleInfo> &restoreBundleInfos, 407 std::vector<std::string> &restoreBundleNames, RestoreTypeEnum restoreType); 408 409 void SetCurrentSessProperties(BJsonEntityCaps::BundleInfo &info, std::map<std::string, bool> &isClearDataFlags, 410 const std::string &bundleNameIndexInfo); 411 412 /** 413 * @brief add useridinfo to current backup session 414 * 415 * @param bundleNames: bundleNames list 416 * @param userId: userId 417 * 418 */ 419 void SetCurrentBackupSessProperties(const std::vector<std::string> &bundleNames, int32_t userId); 420 421 /** 422 * @brief send userid to app 423 * 424 * @param bundleName: bundleName 425 * @param userId: userId 426 * 427 */ 428 void SendUserIdToApp(std::string &bundleName, int32_t userId); 429 430 /** 431 * @brief 通知权限模块 432 * 433 * @param bundleName 包名称 434 * 435 */ 436 void NotifyCloneBundleFinish(std::string bundleName, const BackupRestoreScenario sennario); 437 438 /** 439 * @brief SA 备份恢复结束 440 * 441 * @param bundleName SAID 442 * 443 * @return ErrCode OK if saDone, otherwise saDone failed. 444 */ 445 ErrCode SADone(const ErrCode errCode, std::string bundleName); 446 447 /** 448 * @brief SA备份恢复入口 449 * 450 * @param bundleName SAID 451 * 452 * @return ErrCode OK if backup sa, otherwise backup sa failed. 453 */ 454 ErrCode BackupSA(std::string bundleName); 455 456 /** 457 * @brief 执行通知调用方 458 * 459 * @param errCode 错误码 460 * @param callerName 业务调用方 461 * 462 */ 463 void NotifyCallerCurAppDone(ErrCode errCode, const std::string &callerName); 464 465 /** 466 * @brief 执行通知调用方 467 * 468 * @param errCode 错误码 469 * @param callerName 业务调用方 470 * 471 */ 472 void NotifyCallerCurAppIncrementDone(ErrCode errCode, const std::string &callerName); 473 474 void SetWant(AAFwk::Want &want, const BundleName &bundleName, const BConstants::ExtensionAction &action); 475 476 /** 477 * @brief GetBackupInfo 任务执行 478 * 479 * @param bundleName 应用名称 480 * @param result 业务结果出参 481 * 482 */ 483 ErrCode GetBackupInfoCmdHandle(BundleName &bundleName, std::string &result); 484 485 /** 486 * @brief 添加需要清理的Session 487 * 488 * @param bundleNames 需要清理的应用包信息 489 * 490 */ 491 ErrCode AppendBundlesClearSession(const std::vector<BundleName> &bundleNames); 492 493 /** 494 * @brief 设置是否自占用Session 495 * 496 * @param isOccupyingSession 框架是否自占用session 497 * 498 */ 499 void SetOccupySession(bool isOccupyingSession); 500 501 void ReportOnExtConnectFailed(const IServiceReverse::Scenario scenario, 502 const std::string &bundleName, const ErrCode ret); 503 504 void ReleaseOnException(); 505 506 vector<BIncrementalData> MakeDetailList(const vector<BundleName> &bundleNames); 507 508 vector<string> GetBundleNameByDetails(const std::vector<BIncrementalData> &bundlesToBackup); 509 510 void HandleCurGroupBackupInfos(vector<BJsonEntityCaps::BundleInfo> &bundleInfos, 511 std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap, 512 std::map<std::string, bool> &isClearDataFlags); 513 514 void HandleCurGroupIncBackupInfos(vector<BJsonEntityCaps::BundleInfo> &bundleInfos, 515 std::map<std::string, std::vector<BJsonUtil::BundleDetailInfo>> &bundleNameDetailMap, 516 std::map<std::string, bool> &isClearDataFlags); 517 518 void TimeoutRadarReport(IServiceReverse::Scenario scenario, std::string &bundleName); 519 private: 520 static sptr<Service> instance_; 521 static std::mutex instanceLock_; 522 std::mutex getBackupInfoProcLock_; 523 std::mutex getBackupInfoSyncLock_; 524 std::condition_variable getBackupInfoCondition_; 525 static inline std::atomic<uint32_t> seed {1}; 526 std::atomic<bool> isConnectDied_ {false}; 527 std::atomic<bool> isOccupyingSession_ {false}; 528 529 sptr<SvcSessionManager> session_; 530 sptr<SchedScheduler> sched_; 531 std::shared_ptr<BJsonDisposalConfig> disposal_; 532 std::shared_ptr<BJsonClearDataConfig> clearRecorder_; 533 std::atomic<bool> isInRelease_ {false}; 534 std::atomic<bool> isRmConfigFile_ {true}; 535 friend class ServiceTest; 536 537 OHOS::ThreadPool threadPool_; 538 std::mutex extensionMutexLock_; 539 public: 540 std::map<BundleName, std::shared_ptr<ExtensionMutexInfo>> backupExtMutexMap_; 541 }; 542 } // namespace OHOS::FileManagement::Backup 543 544 #endif // OHOS_FILEMGMT_BACKUP_SERVICE_H