1 /* 2 * Copyright (c) 2022-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_BACKUP_H 17 #define OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H 18 19 #include <functional> 20 #include <memory> 21 #include <vector> 22 23 #include "b_file_info.h" 24 #include "errors.h" 25 #include "svc_death_recipient.h" 26 #include "unique_fd.h" 27 28 namespace OHOS::FileManagement::Backup { 29 class BSessionBackup { 30 public: 31 struct Callbacks { 32 std::function<void(const BFileInfo &, UniqueFd)> onFileReady; // 当备份服务有文件待发送时执行的回调 33 std::function<void(ErrCode, const BundleName)> onBundleStarted; // 当启动某个应用的备份流程结束时执行的回调函数 34 std::function<void(ErrCode, const BundleName)> onBundleFinished; // 当某个应用的备份流程结束或意外中止时执行的回调函数 35 std::function<void(ErrCode)> onAllBundlesFinished; // 当整个备份流程结束或意外中止时执行的回调函数 36 std::function<void()> onBackupServiceDied; // 当备份服务意外死亡时执行的回调函数 37 }; 38 39 public: 40 /** 41 * @brief 获取一个用于控制备份流程的会话 42 * 43 * @param callbacks 注册回调 44 * @return std::unique_ptr<BSessionBackup> 指向会话的智能指针。失败时为空指针 45 */ 46 static std::unique_ptr<BSessionBackup> Init(Callbacks callbacks); 47 48 /** 49 * @brief 用于追加应用,现阶段仅支持在Start之前调用 50 * 51 * @param bundlesToBackup 待备份的应用清单 52 * @return ErrCode 规范错误码 53 */ 54 ErrCode AppendBundles(std::vector<BundleName> bundlesToBackup); 55 56 /** 57 * @brief 用于结束追加应用,结束后不可在调用AppendBundles 58 * 59 * @return ErrCode 规范错误码 60 */ 61 ErrCode Finish(); 62 63 /** 64 * @brief 用于启动备份流程 65 * 66 * @return ErrCode 规范错误码 67 */ 68 ErrCode Start(); 69 70 /** 71 * @brief 注册备份服务意外死亡时执行的回调函数 72 * 73 * @param functor 回调函数 74 */ 75 void RegisterBackupServiceDied(std::function<void()> functor); 76 77 public: 78 ~BSessionBackup(); 79 80 private: 81 sptr<SvcDeathRecipient> deathRecipient_; 82 }; 83 } // namespace OHOS::FileManagement::Backup 84 85 #endif // OHOS_FILEMGMT_BACKUP_B_SESSION_BACKUP_H