1 /* 2 * Copyright (c) 2023-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 #ifndef OHOS_FILEMGMT_BACKUP_B_SESSION_RESTORE_ASYNC_H 17 #define OHOS_FILEMGMT_BACKUP_B_SESSION_RESTORE_ASYNC_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <queue> 23 #include <vector> 24 25 #include "b_file_info.h" 26 #include "errors.h" 27 #include "service_common.h" 28 #include "iservice.h" 29 #include "svc_death_recipient.h" 30 #include "unique_fd.h" 31 32 namespace OHOS::FileManagement::Backup { 33 class BSessionRestoreAsync : public std::enable_shared_from_this<BSessionRestoreAsync> { 34 public: 35 struct Callbacks { 36 std::function<void(const BFileInfo &, UniqueFd, ErrCode)> onFileReady; // 当备份服务有文件待发送时执行的回调 37 std::function<void(ErrCode, const BundleName)> onBundleStarted; // 当启动某个应用的恢复流程结束时执行的回调函数 38 std::function<void(ErrCode, const BundleName)> 39 onBundleFinished; // 当某个应用的恢复流程结束或意外中止时执行的回调函数 40 std::function<void(ErrCode)> onAllBundlesFinished; // 当整个恢复流程结束或意外中止时执行的回调函数 41 std::function<void(const std::string, const std::string)> onResultReport; // 某个应用恢复流程中自定义错误信息的上报的回调函数 42 std::function<void()> onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 43 std::function<void(const std::string, const std::string)> onProcess; // 上报备份恢复过程中的进度和异常 44 }; 45 46 struct AppendBundleInfo { 47 UniqueFd remoteCap; // 已打开的保存远端设备能力的Json文件 48 std::vector<BundleName> bundlesToRestore; // 需要恢复的应用名称列表 49 RestoreTypeEnum restoreType; // 待恢复类型(例如升级服务数据迁移就绪无需进行数据传输) 50 int32_t userId; // 用户ID 51 }; 52 53 public: 54 /** 55 * @brief 获取一个用于控制恢复流程的会话 56 * 57 * @param callbacks 注册的回调函数 58 * @return std::unique_ptr<BRestoreSession> 指向BRestoreSession的智能指针。失败时为空指针 59 */ 60 static std::shared_ptr<BSessionRestoreAsync> Init(Callbacks callbacks); 61 62 /** 63 * @brief 通知备份服务文件内容已就绪 64 * 65 * @param fileInfo 文件描述信息 66 * @return ErrCode 规范错误码 67 * @see GetFileHandle 68 */ 69 ErrCode PublishFile(BFileInfo fileInfo); 70 71 /** 72 * @brief 请求恢复流程所需的真实文件 73 * 74 * @param bundleName 应用名称 75 * @param fileName 文件名称 76 */ 77 ErrCode GetFileHandle(const std::string &bundleName, const std::string &fileName); 78 79 /** 80 * @brief 用于追加待恢复应用 81 * 82 * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 83 * @param bundlesToRestore 待恢复的应用清单 84 * @param detailInfos bundle对应的单双映射的json串 85 * @param userId 用户ID 86 * @return ErrCode 规范错误码 87 */ 88 ErrCode AppendBundles(UniqueFd remoteCap, 89 std::vector<BundleName> bundlesToRestore, 90 std::vector<std::string> detailInfos, 91 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, 92 int32_t userId = DEFAULT_INVAL_VALUE); 93 94 /** 95 * @brief 用于追加待恢复应用 96 * 97 * @param remoteCap 已打开的保存远端设备能力的Json文件。可使用GetLocalCapabilities方法获取 98 * @param bundlesToRestore 待恢复的应用清单 99 * @param userId 用户ID 100 * @return ErrCode 规范错误码 101 */ 102 ErrCode AppendBundles(UniqueFd remoteCap, 103 std::vector<BundleName> bundlesToRestore, 104 RestoreTypeEnum restoreType = RestoreTypeEnum::RESTORE_DATA_WAIT_SEND, 105 int32_t userId = DEFAULT_INVAL_VALUE); 106 107 /** 108 * @brief 用于结束服务 109 * 110 * @return ErrCode 规范错误码 111 */ 112 ErrCode Release(); 113 114 /** 115 * @brief 用于结束应用的备份恢复任务 116 * 117 * @param bundleName 要取消的应用包名 118 * @return ErrCode 规范错误码 119 */ 120 ErrCode Cancel(std::string bundleName); 121 122 public: BSessionRestoreAsync(Callbacks callbacks)123 explicit BSessionRestoreAsync(Callbacks callbacks) : callbacks_(callbacks) {}; 124 ~BSessionRestoreAsync(); 125 126 private: 127 /** @brief 注册备份服务意外死亡时执行的回调函数 */ 128 void OnBackupServiceDied(); 129 130 /** 131 * @brief 注册备份服务意外死亡时执行的回调函数 132 * 133 * @param functor 回调函数 134 */ 135 void RegisterBackupServiceDied(std::function<void()> functor); 136 137 private: 138 sptr<SvcDeathRecipient> deathRecipient_; 139 Callbacks callbacks_; 140 std::atomic<bool> isAppend_ {false}; 141 std::mutex mutex_; 142 std::queue<AppendBundleInfo> workList_; 143 }; 144 } // namespace OHOS::FileManagement::Backup 145 146 #endif // OHOS_FILEMGMT_BACKUP_B_SESSION_RESTORE_ASYNC_H