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