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