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