1 /* 2 * Copyright (c) 2022-2025 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 /* 17 * 说明: 18 * 备份服务同一时间只接受一个唯一的会话。在会话期间,服务只与一个备份应用通信 19 * 且只响应备份或恢复接口中的一种。 20 */ 21 22 #ifndef OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H 23 #define OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H 24 25 #include <map> 26 #include <memory> 27 #include <shared_mutex> 28 #include <vector> 29 30 #include <refbase.h> 31 32 #include "b_file_info.h" 33 #include "b_incremental_data.h" 34 #include "b_resources/b_constants.h" 35 #include "service_common.h" 36 #include "iservice.h" 37 #include "iservice_reverse.h" 38 #include "module_ipc/svc_backup_connection.h" 39 #include "module_ipc/sa_backup_connection.h" 40 #include "svc_death_recipient.h" 41 #include "timer.h" 42 43 namespace OHOS::FileManagement::Backup { 44 struct BackupExtInfo { 45 bool receExtManageJson {false}; 46 bool receExtAppDone {false}; 47 bool isBundleFinished {false}; 48 std::string backupExtName; 49 sptr<SvcBackupConnection> backUpConnection; 50 std::shared_ptr<SABackupConnection> saBackupConnection; 51 std::set<std::string> fileNameInfo; 52 BConstants::ServiceSchedAction schedAction {BConstants::ServiceSchedAction::WAIT}; 53 /* [RESTORE] Record whether data backup is required during the app exec restore proceess. */ 54 RestoreTypeEnum restoreType; 55 /* Clone App: old device app versionCode */ 56 int64_t versionCode; 57 /* Clone App: old device app versionCode */ 58 std::string versionName; 59 /* Ext Ability APP process time */ 60 uint32_t timerId; 61 /* Timer Status: true is start & false is stop */ 62 bool extTimerStatus {false}; 63 bool fwkTimerStatus {false}; 64 uint32_t timeout = BConstants::TIMEOUT_INVALID; 65 uint32_t startTime; 66 int64_t dataSize; 67 int64_t lastIncrementalTime; 68 int32_t manifestFd; 69 std::string backupParameters; 70 int32_t backupPriority; 71 std::string extInfo; 72 int32_t userId {BConstants::DEFAULT_USER_ID}; 73 int32_t appendNum {1}; 74 bool isClearData {true}; 75 bool isInPublishFile {false}; 76 bool isReadyLaunch {false}; 77 bool isRestoreEnd {false}; 78 }; 79 80 class Service; 81 class SvcSessionManager : public virtual RefBase { 82 public: 83 struct Impl { 84 uint32_t clientToken {0}; 85 IServiceReverseType::Scenario scenario {IServiceReverseType::Scenario::UNDEFINED}; 86 std::map<BundleName, BackupExtInfo> backupExtNameMap; 87 sptr<IServiceReverse> clientProxy; 88 bool isBackupStart {false}; 89 bool isAppendFinish {false}; 90 /* Note: Multi user scenario: <System Update Upgrade> 91 Caller must complete all processes before next user tigger.<Session> 92 [RESTORE] Support for multiple users, incoming during restore process. 93 */ 94 int32_t userId {100}; 95 RestoreTypeEnum restoreDataType {RESTORE_DATA_WAIT_SEND}; 96 bool isIncrementalBackup {false}; 97 std::string oldBackupVersion {""}; 98 std::string callerName {}; 99 std::string activeTime {}; 100 }; 101 102 public: 103 /** 104 * @brief 检验调用者是否是会话所有者,且当前命令是否与当前场景相匹配 105 * 106 * @param clientToken 调用者TOKEN 107 * @param scenario 给定场景 108 * 109 * @return ErrCode 错误码 110 */ 111 ErrCode VerifyCallerAndScenario(uint32_t clientToken, IServiceReverseType::Scenario scenario) const; 112 113 /** 114 * @brief 激活会话 115 * 116 * @param impl 客户端信息 117 * @param isOccupyingSession 框架是否自占用session 118 * 119 * @return ErrCode 错误码 120 */ 121 ErrCode Active(Impl newImpl, bool isOccupyingSession = false); 122 123 /** 124 * @brief 关闭会话 125 * 126 * @param remoteInAction 尝试关闭会话的客户端代理。只有激活会话的客户端代理有权关闭会话 127 * @param force 强制关闭 128 * 129 * @return ErrCode 错误码 130 */ 131 ErrCode Deactive(const wptr<IRemoteObject> &remoteInAction, bool force = false); 132 133 /** 134 * @brief 检验调用者给定的bundleName是否是有效的 135 * 136 * @param bundleName 调用者名称 137 * @return ErrCode 调用者不是会话所有者 138 */ 139 ErrCode VerifyBundleName(std::string &bundleName); 140 141 /** 142 * @brief 获取IServiceReverse 143 * 144 * @return sptr<IServiceReverse> 返回clientProxy 145 * @throw BError::Codes::SA_REFUSED_ACT 调用者不是会话所有者 146 */ 147 sptr<IServiceReverse> GetServiceReverseProxy(); 148 149 /** 150 * @brief 获取Scenario 151 * 152 * @return IServiceReverseType::Scenario 返回scenario 153 * @throw BError::Codes::SA_INVAL_ARG 获取异常 154 */ 155 IServiceReverseType::Scenario GetScenario(); 156 157 /** 158 * @brief 获取当前处理事务会话对应的userId 159 * 160 * @return int32_t 161 */ 162 int32_t GetSessionUserId(); 163 164 /** 165 * @brief 设置当前处理事务会话对应的userId 166 * 167 * @param userid 相关会话对应的userId 168 */ 169 void SetSessionUserId(int32_t userId); 170 171 /** 172 * @brief 获取当前处理事务会话对应的callerName 173 * 174 * @return string 175 */ 176 std::string GetSessionCallerName(); 177 178 /** 179 * @brief 获取当前处理事务会话对应的激活时间 180 * 181 * @return string 182 */ 183 std::string GetSessionActiveTime(); 184 185 /** 186 * @brief 更新backupExtNameMap并判断是否完成分发 187 * 188 * @param bundleName 客户端信息 189 * @param fileName 文件名称 190 * @throw BError::Codes::SA_INVAL_ARG 获取异常 191 * @return true 分发已完成 192 * @return false 分发未完成 193 */ 194 bool OnBundleFileReady(const std::string &bundleName, const std::string &fileName = ""); 195 196 /** 197 * @brief 设置backup manage.json 信息 198 * 199 * @param bundleName 客户端信息 200 * @param fd manage.json 文件描述符 201 * @return UniqueFd 返回manage.json 文件描述符 202 * @throw BError::Codes::SA_INVAL_ARG 获取异常 203 */ 204 UniqueFd OnBundleExtManageInfo(const std::string &bundleName, UniqueFd fd); 205 206 /** 207 * @brief Remove backup extension info 208 * 209 * @param bundleName 应用名称 210 */ 211 void RemoveExtInfo(const std::string &bundleName); 212 213 /** 214 * @brief get extension connection info 215 * 216 * @param bundleName 217 * @return wptr<SvcBackupConnection> 218 */ 219 wptr<SvcBackupConnection> GetExtConnection(const BundleName &bundleName); 220 221 void UpdateDfxInfo(const std::string &bundleName, uint64_t uniqId); 222 223 /** 224 * @brief get sa extension connection 225 * 226 * @param bundleName 227 * @return std::weak_ptr<SABackupConnection> 228 */ 229 std::weak_ptr<SABackupConnection> GetSAExtConnection(const BundleName &bundleName); 230 231 /** 232 * @brief HiDumper dump info 233 * 234 * @param fd 对端dump句柄 235 * @param args 服务参数 236 */ 237 void DumpInfo(const int fd, const std::vector<std::u16string> &args); 238 239 /** 240 * @brief 暂存restore流程真实文件请求 241 * 242 * @param bundleName 应用名称 243 * @param fileName 文件名称 244 */ 245 void SetExtFileNameRequest(const std::string &bundleName, const std::string &fileName); 246 247 /** 248 * @brief 获取restore流程真实文件请求 249 * 250 * @param bundleName 应用名称 251 * @return std::set<std::string> 返回真实文件vec 252 */ 253 std::set<std::string> GetExtFileNameRequest(const std::string &bundleName); 254 255 /** 256 * @brief 获取ServiceSchedAction状态 257 * 258 * @param bundleName 应用名称 259 * @return BConstants::ServiceSchedAction 260 */ 261 BConstants::ServiceSchedAction GetServiceSchedAction(const std::string &bundleName); 262 263 /** 264 * @brief 设置ServiceSchedAction状态 265 * 266 * @param bundleName 应用名称 267 * @param action 状态 268 */ 269 void SetServiceSchedAction(const std::string &bundleName, BConstants::ServiceSchedAction action); 270 271 /** 272 * @brief 获取所需要的调度信息 273 * 274 * @return std::string 返回bundleName 275 */ 276 bool GetSchedBundleName(std::string &bundleName); 277 278 /** 279 * @brief Set the Backup Ext Name object 280 * 281 * @param bundleName 应用名称 282 * @param backupExtName extension name 283 */ 284 void SetBackupExtName(const std::string &bundleName, const std::string &backupExtName); 285 286 /** 287 * @brief 获取backup extension name 288 * 289 * @param bundleName 应用名称 290 * @return std::string extension name 291 */ 292 std::string GetBackupExtName(const std::string &bundleName); 293 294 /** 295 * @brief 暂存ext info 296 * 297 * @param bundleName 应用名称 298 * @param extInfo ext info 299 */ 300 void SetBackupExtInfo(const std::string &bundleName, const std::string &extInfo); 301 302 /** 303 * @brief 获取ext info 304 * 305 * @param bundleName 应用名称 306 * @return std::string ext info 307 */ 308 std::string GetBackupExtInfo(const std::string &bundleName); 309 310 /** 311 * @brief 暂存应用用户id 312 * 313 * @param bundleName 应用名称 314 * @param userId 用户id 315 */ 316 void SetBundleUserId(const std::string &bundleName, const int32_t userId); 317 318 /** 319 * @brief 获取应用用户id 320 * 321 * @param bundleName 应用名称 322 * @return int32_t userId 323 */ 324 int32_t GetBundleUserId(const std::string &bundleName); 325 326 /** 327 * @brief 追加应用 328 * 329 * @param bundleNames 应用名称 330 * @param failedBundles 返回失败应用名称 331 */ 332 void AppendBundles(const std::vector<std::string> &bundleNames, std::vector<std::string> &failedBundles); 333 334 /** 335 * @brief 添加指定应用 336 * 337 * @param bundleName 应用名称 338 */ 339 sptr<SvcBackupConnection> CreateBackupConnection(const BundleName &bundleName); 340 341 /** 342 * @brief 开始备份 343 * 344 * @return ErrCode 345 */ 346 ErrCode Start(); 347 348 /** 349 * @brief 结束追加应用 350 * 351 * @return ErrCode 352 */ 353 ErrCode Finish(); 354 355 /** 356 * @brief 整个备份恢复流程是否结束 357 * 358 * @return true 备份恢复流程结束 359 * @return false 备份恢复流程未结束 360 */ 361 bool IsOnAllBundlesFinished(); 362 363 /** 364 * @brief 是否启动调度器 365 * 366 * @return true 启动调度器 367 * @return false 不启动调度器 368 */ 369 bool IsOnOnStartSched(); 370 371 /** 372 * @brief Whether to unload the service 373 * 374 * @return true 375 * @return false 376 */ 377 bool NeedToUnloadService(); 378 379 /** 380 * @brief Set the bundle restore type object 381 * 382 * @param bundleName 383 * @param restoreType 384 */ 385 void SetBundleRestoreType(const std::string &bundleName, RestoreTypeEnum restoreType); 386 387 /** 388 * @brief Get the bundle restore type object 389 * 390 * @param bundleName 391 * @return restoreType 392 */ 393 RestoreTypeEnum GetBundleRestoreType(const std::string &bundleName); 394 395 /** 396 * @brief Set the bundle version code object 397 * 398 * @param bundleName 399 * @param versionCode 400 */ 401 void SetBundleVersionCode(const std::string &bundleName, int64_t versionCode); 402 403 /** 404 * @brief Get the bundle version code object 405 * 406 * @param bundleName 407 * @return versionCode 408 */ 409 int64_t GetBundleVersionCode(const std::string &bundleName); 410 411 /** 412 * @brief Set the bundle version name object 413 * 414 * @param bundleName 415 * @param versionName 416 */ 417 void SetBundleVersionName(const std::string &bundleName, std::string versionName); 418 419 /** 420 * @brief Get the bundle version name object 421 * 422 * @param bundleName 423 * @return versionName 424 */ 425 std::string GetBundleVersionName(const std::string &bundleName); 426 427 /** 428 * @brief Set the bundle data size object 429 * 430 * @param bundleName 431 * @param dataSize 432 */ 433 void SetBundleDataSize(const std::string &bundleName, int64_t dataSize); 434 435 /** 436 * @brief 启动框架定时器 437 * 438 * @param bundleName 应用名称 439 * @param callback 超时回调 440 * @return bool 441 */ 442 bool StartFwkTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); 443 444 /** 445 * @brief 停止框架定时器 446 * 447 * @param bundleName 应用名称 448 * @param callback 超时回调 449 * @return bool 450 */ 451 bool StopFwkTimer(const std::string &bundleName); 452 453 /** 454 * @brief 启动extension定时器 455 * 456 * @param bundleName 应用名称 457 * @param callback 超时回调 458 * @return bool 459 */ 460 bool StartExtTimer(const std::string &bundleName, const Utils::Timer::TimerCallback &callback); 461 462 /** 463 * @brief 停止extension定时器 464 * 465 * @param bundleName 应用名称 466 * @return bool 467 */ 468 bool StopExtTimer(const std::string &bundleName); 469 470 /** 471 * @brief 重新设置定时器 472 * 473 * @param bundleName 应用名称 474 * @param timeout 超时时间 475 * @return true 476 * @return false 477 */ 478 bool UpdateTimer(const std::string &bundleName, uint32_t timeout, 479 const Utils::Timer::TimerCallback &callback); 480 481 /** 482 * @brief sessionCnt加计数 483 * 484 * @param sessionCnt 485 */ 486 void IncreaseSessionCnt(const std::string funcName); 487 488 /** 489 * @brief sessionCnt加计数 490 * 491 * @param sessionCnt 492 */ 493 void DecreaseSessionCnt(const std::string funcName); 494 495 /** 496 * @brief clear session data 497 * 498 */ 499 ErrCode ClearSessionData(); 500 501 /** 502 * @brief Get the Is Incremental Backup object 503 * 504 * @return true 505 * @return false 506 */ 507 bool GetIsIncrementalBackup(); 508 509 /** 510 * @brief Set the Incremental Data object 511 * 512 * @param incrementalData 513 */ 514 void SetIncrementalData(const BIncrementalData &incrementalData); 515 516 /** 517 * @brief Get the Manifest Fd object 518 * 519 * @param bundleName 应用名称 520 * @return int32_t 521 */ 522 int32_t GetIncrementalManifestFd(const std::string &bundleName); 523 524 /** 525 * @brief Get the Last Incremental Time object 526 * 527 * @param bundleName 528 * @return int64_t 529 */ 530 int64_t GetLastIncrementalTime(const std::string &bundleName); 531 532 /** 533 * @brief 获取备份前内存参数 534 * 535 */ 536 int32_t GetMemParaCurSize(); 537 538 /** 539 * @brief 记录备份前内存参数 540 * 541 * @param size 542 */ 543 void SetMemParaCurSize(int32_t size); 544 545 /** 546 * @brief 获取超时时间 547 * 548 * @param bundleName 应用名称 549 * @return uint32_t 550 */ 551 uint32_t GetTimeoutValue(const std::string &bundleName); 552 553 /** 554 * @brief Set the old device backup version object 555 * 556 * @param backupVersion 557 */ 558 void SetOldBackupVersion(const std::string &backupVersion); 559 560 /** 561 * @brief Get the old device backup version object 562 */ 563 std::string GetOldBackupVersion(); 564 565 bool ValidRestoreDataType(RestoreTypeEnum restoreType); 566 567 Impl GetImpl(); 568 int GetSessionCnt(); 569 570 void SetClearDataFlag(const std::string &bundleName, bool isClearData); 571 bool GetClearDataFlag(const std::string &bundleName); 572 573 bool CleanAndCheckIfNeedWait(ErrCode &ret, std::vector<std::string> &bundleNameList); 574 575 void SetPublishFlag(const std::string &bundleName); 576 577 void SetImplRestoreType(const RestoreTypeEnum restoreType); 578 579 void SetIsReadyLaunch(const std::string &bundleName); 580 581 void HandleOnRelease(sptr<IExtension> proxy); 582 583 void SetIsRestoreEnd(const std::string &bundleName); 584 585 bool GetIsRestoreEnd(const std::string &bundleName); 586 587 private: 588 /** 589 * @brief 获取backup extension ability 590 * 591 * @param bundleName 应用名称 592 * 593 * @return sptr<SvcBackupConnection> 594 */ 595 sptr<SvcBackupConnection> GetBackupAbilityExt(const std::string &bundleName); 596 597 /** 598 * @brief 获取backup sa extension 599 * 600 * @param bundleName 应用名称 601 * 602 * @return std::shared_ptr<SABackupConnection> 603 */ 604 std::shared_ptr<SABackupConnection> GetBackupSAExt(const std::string &bundleName); 605 606 /** 607 * @brief 初始化 clientProxy 608 * 609 * @param newImpl 610 */ 611 ErrCode InitClient(Impl &newImpl); 612 613 /** 614 * @brief 获取BackupExtNameMap iterator 615 * 616 * @param bundleName 应用名称 617 * @return std::map<BundleName, BackupExtInfo>::iterator 618 */ 619 std::tuple<bool, std::map<BundleName, BackupExtInfo>::iterator> GetBackupExtNameMap(const std::string &bundleName); 620 621 /** 622 * @brief 计算出应用程序处理数据可能使用的时间 623 * 624 * @param bundleName 应用名称 625 * @return 626 */ 627 uint32_t CalAppProcessTime(const std::string &bundleName); 628 629 public: 630 /** 631 * @brief Construct a new Svc Session object 632 * 633 * @param reversePtr 指向Service的反向指针,使用wptr避免循环引用 634 */ SvcSessionManager(wptr<Service> reversePtr)635 explicit SvcSessionManager(wptr<Service> reversePtr) : reversePtr_(reversePtr) 636 { 637 timer_.Setup(); 638 } ~SvcSessionManager()639 ~SvcSessionManager() override 640 { 641 timer_.Shutdown(); 642 } 643 644 private: 645 mutable std::shared_mutex lock_; 646 wptr<Service> reversePtr_; 647 sptr<SvcDeathRecipient> deathRecipient_; 648 Impl impl_; 649 uint32_t extConnectNum_ {0}; 650 Utils::Timer timer_ {"backupTimer"}; 651 std::atomic<int> sessionCnt_ {0}; 652 int32_t memoryParaCurSize_ {BConstants::DEFAULT_VFS_CACHE_PRESSURE}; 653 }; 654 655 class CounterHelper { 656 public: CounterHelper(sptr<SvcSessionManager> session,const std::string & funcName)657 CounterHelper(sptr<SvcSessionManager> session, const std::string& funcName) 658 : session_(session), funcName_(funcName) 659 { 660 session_->IncreaseSessionCnt(funcName_); 661 } ~CounterHelper()662 ~CounterHelper() 663 { 664 session_->DecreaseSessionCnt(funcName_); 665 } 666 private: 667 sptr<SvcSessionManager> session_; 668 std::string funcName_; 669 }; 670 } // namespace OHOS::FileManagement::Backup 671 672 #endif // OHOS_FILEMGMT_BACKUP_SVC_SESSION_MANAGER_H 673